0

I'm attempting to create a custom widget which I can use in my activity's layout. I've created a class for the widget which extends View.

import android.app.Service;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import be.robinj.ubuntu.R;

public class AppLauncher extends View
{
    private String name;
    private String description;
    private boolean special;
    private AppIcon icon;
    private View view;

    public AppLauncher (Context context, AttributeSet attrs)
    {
        super (context, attrs);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService (Service.LAYOUT_INFLATER_SERVICE);
        this.view = inflater.inflate (R.layout.widget_applauncher, null, false);

        TypedArray styleAttrs = context.getTheme ().obtainStyledAttributes (attrs, R.styleable.AppLauncher, 0, 0);
        this.name = styleAttrs.getString (R.styleable.AppLauncher_label);
        this.description = styleAttrs.getString (R.styleable.AppLauncher_description);
        this.special = styleAttrs.getBoolean (R.styleable.AppLauncher_special, false);
        this.icon = new AppIcon (styleAttrs.getDrawable (R.styleable.AppLauncher_icon));
    }

    ...
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="64dp"
              android:layout_height="64dp">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ffff9243"
        android:layout_margin="6dp">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/launcher_icon_bg">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/imgIcon"
                android:src="@drawable/ic_launcher" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="AppLauncher">
        <attr name="label" format="string" />
        <attr name="description" format="string" />
        <attr name="icon" format="integer" />
        <attr name="special" format="boolean" />
    </declare-styleable>
</resources>

That's all fine, but the Java code is only going to contain the logic part, preferably. What I can't figure out is how I can specify which layout XML file should be used for my custom view/widget. Inflating the layout presumably loads it. The inflate () methods require a second and third parameter, though. Every example I can find shows this being passed as the second parameter, but the second parameter should be a ViewGroup, while this extends View. Where do I magically get this required ViewGroup object from? Passing null and false does not throw an error, but neither does it do anything else (nothing shows up when I run the app).

3 Answers 3

1

Use the following code to inflate xml in a custom view

    public AppLauncher (Context context, AttributeSet attrs)
    {
             super (context, attrs);
             LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Service.LAYOUT_INFLATER_SERVICE);
             View parent = inflater.inflate(R.layout.home_activity, this);
    }
Sign up to request clarification or add additional context in comments.

4 Comments

ctrl+shift+o import your R file.
I thought Import Class would result in importing android.R and that somehow my own R.java file had faield to compile. I should have checked. Sorry, and thanks :)
This answer is incorrect. this is not a valid parameter, since this is a View while a ViewGroup is expected. So where am I supposed to get this ViewGroup from?
extend ViewGroup class not View as you already figured out.
1

It seems from your source code that you want to create a ViewGroup therefore you need to extend the appropriate class LinearLayout for example. Next you can create an xml file as usual and inflate it in your custom ViewGroup constructor:

 LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.ur_view, this);

You may then inject your custom view within any layout in the following way:

<package.def.AppLauncher .../>

or:

<View 
      class="package.defAppLauncher"
/View>

4 Comments

I'm a bit unclear as to what the second parameter is, since this is not working (because it refers to my AppLauncher class). According to the docs it should be a ViewGroup, which doesn't really tell me a whole lot.
When I add the view/widget to my layout it's just empty/invisible, so something's not right.
This answer is incorrect. Passing null as a parameter will not add the view to its parent, causing it to not show up.
if you extend a ViewGroup like LinearLayout that would solve ur problem
0

Thanks to Pr38y and eldjon for LayoutInflater.
In the end the big problem was that I needed to extend LinearLayout rather than View.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.