Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The best way to inflate view programmatically

Tags:

android

I found a simple SwipeSample that I changed to allow me to create new xml layouts and inflate the main layout to display them. What I wanted to do was also be able to programmatically add layouts for the swipe process.

I have the main.xml layout and a red.xml and yellow.xml which are a simple linearlayout with a textview set to a solid color.

The code below works but I don't think that it's correct or the best way to do what I'm trying to get. If anyone can suggest a better way that would be greatly appreciated.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //Create a layout with a solid blue background programmatically
    TextView tv1 = new TextView(this);
    tv1.setText("Blue");
    tv1.setBackgroundColor(Color.BLUE);
    tv1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    ll.addView(tv1);
    //Create a layout with a solid green background programmatically
    TextView tv2 = new TextView(this);
    tv2.setText("Green");
    tv2.setBackgroundColor(Color.GREEN);
    tv2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

    LinearLayout ll2 = new LinearLayout(this);
    ll2.setOrientation(LinearLayout.VERTICAL);
    ll2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    ll2.addView(tv2);
    //inflate the flipper view and add the yellow and red xml layouts and also the 2 programmatically created layouts
    fSpace = (ViewFlipper)findViewById(R.id.flipper);
    inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.yellow, fSpace);
    inflater.inflate(R.layout.red, fSpace);
    fSpace.addView(ll);
    fSpace.addView(ll2);  

}
like image 461
John Avatar asked Oct 28 '11 01:10

John


2 Answers

If you have a complex layout that you want to create programmatically, it might be easiest to have the layout premade in xml and then just inflate it and add it at runtime.

Create view in xml

Here is a sample premade xml layout that is in the layout folder. Yours could be anything, a single view or a whole complex layout.

layout/my_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/TextView1"
        android:text="This is a TV"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/TextView2"
        android:text="How are you today?"/>
</LinearLayout>

Make a container for your view

Have some place it put your view in your activity layout. You could have something like this.

<FrameLayout
    android:id="@+id/flContainer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

</FrameLayout>

Inflate the view

Use get a reference to the container, inflate your view from xml, and then add it to the container.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FrameLayout container = (FrameLayout) findViewById(R.id.flContainer);
    View inflatedLayout= getLayoutInflater().inflate(R.layout.my_view, null, false);
    container.addView(inflatedLayout);

}

Doing it like this keeps your code a lot cleaner.

See also:

  • How to inflate one view with a layout
  • Add a View to a wrapper multiple times with inflate
like image 105
Suragch Avatar answered Nov 16 '22 15:11

Suragch


The way you inflate R.layout.yellow and R.layout.red is indeed the right way to do so. You may be able to simplify your code by moving a lot of it over to xml. I assume tv1 is just a sample? if not, it could go into main.xml. You may even find a way to create yellow and red with a single inflation... depending on what you're doing.

Programmatically creating views is just, for the most part, slightly tedious.

like image 3
aleph_null Avatar answered Nov 16 '22 14:11

aleph_null