user1349812
user1349812

Reputation: 73

How to get access to Views from inflated layouts?

I have a simple Activity with its main.xml attached. main.xml has a custom layout (customLinearLayout) and a simple TextView. The custom layout also have its own layout-file (linearlayout.xml) with a simple Button. The layout is inflated by the class properly.

The Button in linearlayout.xml shall change the text of the textView lying in main.xml, but I can't get access to that textView. What am I doing wrong? I can't inflate the main.xml either.

Here's the customLinearLayout:

public class CustomLinearLayout extends LinearLayout {

LayoutInflater mInflater;
View ContainerView;

Button button1;
TextView tv;

public CustomLinearLayout(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    init();
}

public CustomLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
    init();
}

private void init() {
    mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ContainerView = mInflater.inflate(R.layout.linearlayout, this, true);

    button1 = (Button) ContainerView.findViewById(R.id.button1);

    // trying to get the TextView from main.xml
    tv = (TextView) ContainerView.findViewById(R.id.textview1); // doesn't work

    // these tries doesn't work either
    //tv = (TextView) ContainerView.getRootView().findViewById(R.id.textview);  
    //tv = (TextView) this.getRootView().findViewById(R.id.textview);
    //tv = (TextView) this.findViewById(R.id.textview);

    button1.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(tv != null)
                tv.setText("works");
            else
                Log.d("CustomLinearLayout", "TextView not found");
        }
    });
}
}

the layout-file (linearlayout.xml):

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

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="press" />

</LinearLayout>

The Activity:

public class TestLayoutViewsv2Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
}

The Activity's main.xml:

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

<test.layoutviewsv2.CustomLinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/textview1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

Upvotes: 4

Views: 10070

Answers (2)

JRaymond
JRaymond

Reputation: 11782

The method findViewById is scoped - your custom view is looking inside itself for a text field that doesn't exist. But that's ok! your views should be their own little world - They shouldn't need to know anything about the world outside itself. Sadly you'll have to change your design to accomodate this (like with a callback to the activity, perhaps) to make this do what you want to.

Upvotes: 2

Mahdi Hijazi
Mahdi Hijazi

Reputation: 4454

You are using the inflate method in a wrong way..there is no point of inflating a layout if you are extending a view. The inflate method is used to init a new view without the need to create a new instance yourself. I am not getting what your are trying to do but you could use the include to reuse some of your layouts. Check this for more info http://developer.android.com/resources/articles/layout-tricks-merge.html

Upvotes: 0

Related Questions