40

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.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
LuminiousAndroid
  • 1,557
  • 4
  • 18
  • 28

6 Answers6

53

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);
        }
    }
Nalin
  • 135
  • 7
user
  • 86,916
  • 18
  • 197
  • 190
  • Can you tell why we it was giving nothing by extending View. – LuminiousAndroid Jun 14 '12 at 08:32
  • 1
    @Kabir121 The `View` class doesn't support adding other views to it, the `View` class represents a single individual `View`(like, if you want to build a rounded `Button`) and not a group(like in your case when you inflate that layout file). – user Jun 14 '12 at 08:37
  • 1
    This solution should work, but it also mean that there will be viewGroups that always have a single view, and this will always result in more work for the layout phase. I could say a builder paradigm would be better, but then you miss the whole point of extending the view. – android developer Jun 05 '13 at 14:04
  • You can inflate a View, you just need to pass the ViewGroup in the constructor. Be sure to have a ViewGroup which you can pass; some Layout. – Malba May 29 '22 at 02:30
12

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

smac89
  • 39,374
  • 15
  • 132
  • 179
Aracem
  • 7,227
  • 4
  • 35
  • 72
4

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/

mbo
  • 4,611
  • 2
  • 34
  • 54
3

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);
Pang
  • 9,564
  • 146
  • 81
  • 122
Bharat Sharma
  • 3,926
  • 2
  • 17
  • 29
1

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

d.k.
  • 415
  • 4
  • 16
0

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

chris
  • 194
  • 2
  • 10