empty-yo
empty-yo

Reputation: 143

android: inflate views into a layout

I add a view which is inflated by LayoutInflater into a layout, then I change the background color of this view,for example, red to blue .After all that, I add a new view , inflated with the same xml file, into the same layout,but i got a view with the background color blue, not the original color red. Here is the test code:

public class InflateActivity extends Activity{


    private LinearLayout mContainer;
    private View view;

    @Override
    protected void onCreate(Bundle saveInstanceState) {
        super.onCreate(saveInstanceState);
        setContentView(R.layout.inflate_test);
        mContainer = (LinearLayout)findViewById(R.id.inflate_container);
        findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                view = LayoutInflater.from(InflateActivity.this).inflate(R.layout.expanded_record,mContainer,false);
                mContainer.addView(view,2);
            }
        });
        findViewById(R.id.change_color).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                view.setBackgroundColor(Color.BLUE);

            }
        });
    }
} 

And I got different id by view.toString(). I can not figure out why. I wonder why I make some changes to view A,and the view B which is inflate with the same xml file that not be changed will be affected by these changes. Actually,I've made many changes to the view A, but only setBackgroundColor() will affect view B. The root view of the xml file is a FrameLayout.Other layout like LinearLayout,RelativeLayout will not come up the problem

Upvotes: 3

Views: 2958

Answers (3)

kerry
kerry

Reputation: 2582

I checked your code by creating a project. I am using Android Studio. I did not find any of that problem that you mentioned. Just have a look at my code and try to change yours accordingly.

MainActivity:

public class MainActivity extends Activity {


private LinearLayout mContainer;
private View view;


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


    mContainer =  (LinearLayout)findViewById(R.id.inflate_container);
    findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                view = LayoutInflater.from(MainActivity.this).inflate(R.layout.expanded_record,mContainer,false);
                mContainer.addView(view,mContainer.getChildCount());
            }
        });
        findViewById(R.id.change_color).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                view.setBackgroundColor(Color.BLUE);

            }
        });

}

Main XML:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"
android:layout_height="match_parent"   android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"   tools:context=".MainActivity">

<LinearLayout
    android:id="@+id/inflate_container"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">



</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_margin="20dp">


    <TextView

        android:text="add" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/add"
        android:clickable="true"/>

    <TextView
        android:id="@+id/change_color"
        android:clickable="true"
        android:text="change_color" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

</RelativeLayout>

and the XML that I am inflating

<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="10dp"
android:background="#000000"
android:layout_gravity="center">


</View>

This is the result I got

See that I can add black views and change them to blue and then again black

http://postimg.org/image/ggeotcz91/

Upvotes: 3

Narendra
Narendra

Reputation: 609

From the code snippets that you have provided above, as you are using the same inflated view while adding so when you are adding it 2nd time view background is blue. If you want to add view with all the default values as that of layout "expanded_record.xml", which you are using then you have to inflate it again while adding 2nd time. In short you should have to inflate "expanded_record.xml" times you want to add with defaults conditions, you can make changes on each instance as per your requirements.

 view1 = LayoutInflater.from(InflateActivity.this).inflate(R.layout.expanded_record,mContainer,false);
 mContainer.addView(view1,2);

 view2 = LayoutInflater.from(InflateActivity.this).inflate(R.layout.expanded_record,mContainer,false);
 mContainer.addView(view2,3);

Upvotes: 0

Karthika PB
Karthika PB

Reputation: 1373

after changing view.setBackgroundColor(Color.BLUE); the background will be blue.If you want to change it to red when you add a new view you need to CHANGE

findViewById(R.id.change_color).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            v.setBackgroundColor(Color.BLUE);//changes this view which clicked to blue

        }
    });

Upvotes: 0

Related Questions