0

I have two xml files in the layout folder main.xml and hidden.xml. in my activity, the content view is set to main.xml using setcontentview. what I want to do is, to inflate the main.xml with hidden.xml when a certain button is pressed. To accomplish this, I wrote the below posted code.

But the problem is, while checking if the LinearLayout is null or not, it returns null as the toast displays a respective message.

My question is, what can cause a LinearLayout to be null since it is available in the hidden.xml ?

JavaCode:

public void onClick(View v) {
        // TODO Auto-generated method stub

        switch (v.getId()) {
        case R.id.addBtn00:
            LinearLayout hiddenLinearLayout = (LinearLayout) findViewById(R.id.hiddenLayout00);
            if (hiddenLinearLayout == null) {
                Toast.makeText(getBaseContext(), "hidden layout is null", Toast.LENGTH_SHORT).show();
            }else {
                LinearLayout myLinearLayout = (LinearLayout) findViewById(R.id.linearLayout01);
                View hiddenInfo = getLayoutInflater().inflate(R.id.hiddenLayout00, myLinearLayout,false);
                myLinearLayout.addView(hiddenInfo);
            }
            //TextView mTv = (TextView) findViewById(R.id.textView00);
            //mTv.setText("This is not the original Text");
            break;

        case R.id.removeBtn00:
            break;

        default:
            break;
        }

hidden.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:id="@+id/hiddenLayout00">
    <TextView 
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="This is a Text View" 
        android:layout_height="wrap_content"
        android:id="@+id/textView00" 
        android:layout_width="wrap_content" />
  • You have to inflate layout first – user3455363 May 15 '14 at 15:09
  • @user3455363 ok, i followed what "nikis" suggested,but still the LinearLayout hiddenLinearLayout = (LinearLayout) findViewById(R.id.hiddenLayout00); returns null? –  May 15 '14 at 15:15
  • For parts of the xml that you only conditonally want to inflate the best practice is actually to use ViewStubs http://developer.android.com/reference/android/view/ViewStub.html – Patrick May 15 '14 at 15:17

1 Answers1

0

You are inflating in a wrong way, you should point to the layout at the first parameter, not to the id inside the layout. Replace this:

getLayoutInflater().inflate(R.id.hiddenLayout00, myLinearLayout,false);

with this:

getLayoutInflater().inflate(R.layout.hidden, myLinearLayout,false);
nikis
  • 11,166
  • 2
  • 35
  • 45
  • ok I corrected it, but it is one part of the problem because, "LinearLayout hiddenLinearLayout = (LinearLayout) findViewById(R.id.hiddenLayout00);" returns null –  May 15 '14 at 15:16
  • @Elpharaoh it should return `null` only the first time – nikis May 15 '14 at 15:21
  • @Elpharaoh also is `myLinearLayout` a part of `main.xml`? – nikis May 15 '14 at 15:28
  • one more inquiry if you do not mind. do you think checking if the hiddenLinearLayout is null or not useless? –  May 15 '14 at 15:34
  • @Elpharaoh `findViewById` call is expensive in case of big layouts, so I think using some boolean flag indicating you've already added hidden layout or not will be more appropriate – nikis May 15 '14 at 15:38