LuminiousAndroid
LuminiousAndroid

Reputation: 1577

Inflating a xml layout in a custom View class

I have a class View1 that extends View. I want to inflate R.layout.test2.xml in this class View1. I have put a following code in this class

public class View1 extends View {

    View view;
    String[] countries = new String[] {"India", "USA", "Canada"};

    public View1( Context context) {
        super(context);
        LayoutInflater  mInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view=mInflater.inflate(R.layout.test2, null, false);
    }
}

From another class Home I want this inflated view to be there for some circumstances , In the Home class I wrote the following code:

public class Home extends Activity{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        CreateView();   
    }

    public void CreateView() {
        LinearLayout lv=(LinearLayout)findViewById(R.id.linearlayout);
        View1 view = new View1(Home.this);
        lv.addView(view);
    }
}

But as I run my project the activity doesn't show me anything.

Upvotes: 40

Views: 50893

Answers (6)

user
user

Reputation: 87064

You can't add views to the View class instead you should use ViewGroup or one of its subclasses(like Linearlayout, RelativeLayout etc). Then your code will be like this:

    public class View1 extends LinearLayout {

        View view;
        String[] countries = new String[] {"India", "USA", "Canada"};

        public View1( Context context) {
            super(context);
            inflate(context, R.layout.test2, this);
        }
    }

Upvotes: 53

Pranay
Pranay

Reputation: 304

In kotlin
LayoutInflater.from(this).inflate(R.layout.custom_toolbar, null, false)

Upvotes: 0

Bharat Sharma
Bharat Sharma

Reputation: 3966

Use the code below to inflate your layout, then you can use that view for any purpose. This will give you the most parent layout of your XML file. Type cast and use it accordingly.

View headerView = View.inflate(this, R.layout.layout_name, null);

Upvotes: 3

mbo
mbo

Reputation: 4691

You have to use a ViewGroup like FrameLayout and do:

public class View1 extends FrameLayout {

    public View1(Context context) {
        super(context);
        inflate(context, R.layout.view1, this);
    }
}

In the layout XML, use the <merge tag to not just add your view1 layout to the root layout which means we have an empty FrameLayout and your defined view beside each other.

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="content" />

</merge>

See http://trickyandroid.com/protip-inflating-layout-for-your-custom-view/

Upvotes: 4

Aracem
Aracem

Reputation: 7207

Use this

    LayoutInflater li = (LayoutInflater)getContext().getSystemService(infService);
    li.inflate(R.layout.test2, **this**, true);

You must to use this, not null, and change the false parameter (boolean AttachToRoot ) to true

Upvotes: 12

d.k.
d.k.

Reputation: 415

You are adding in home activity blank view. Because in View1 class you have only inflate view but not add anywhere.

Upvotes: 1

Related Questions