isep
isep

Reputation: 713

Getting Views from XMLs

In my app I have a navigation menu that is constant between activities. To make my code a bit easier to modify I made it into its own xml and have been importing it my layouts for each of my screens.

To setup the buttons I made a class which I pass my current Activity. The new class has no problem finding the views for the buttons but cannot find the encapsulating layout for the menu and returns null when I try to findViewById(). Since the buttons and layout are in the same XML

public static void setup(Activity a){
    myActivity = a;

    //OFFENDING BIT OF CODE
    View myLayout = (View) myActivity.findViewById(R.layout.navigation_bar); 

    Log.d(TAG, "layout: "+myLayout);

    set_btn = (ImageButton) myActivity.findViewById(R.id.a_settings);
    set_btn.setPressed(false);
    set_btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //TODO:
        }
    });

    inbox_btn = (ImageButton) myActivity.findViewById(R.id.a_offers);
    inbox_btn.setPressed(false);
    inbox_btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //TODO:
        }
    });

}

Main Screen XML

<RelativeLayout android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:id="@+id/navigationBar"
    android:layout_alignParentBottom="true">
    <include android:layout_height="wrap_content"
        android:id="@+id/include1"
        layout="@layout/navigation_bar"
        android:layout_width="wrap_content"></include>
</RelativeLayout>

Menu XML

<RelativeLayout mlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/NavigationLayout">

    <RelativeLayout android:id="@+id/buttonLayout" 
        android:layout_height="75dip" 
        android:layout_width="fill_parent"  
        android:layout_alignParentBottom="true">

        <ImageButton android:id="@+id/a_settings" 
            android:layout_width="100dip"
            android:background="@drawable/settings_button" 
            android:layout_centerHorizontal="true" 
            android:layout_centerVertical="true"
            android:layout_height="fill_parent"
            android:scaleType="fitXY">
        </ImageButton>

        <ImageButton android:id="@+id/a_wallet" 
            android:layout_width="100dip" 
            android:background="@drawable/wallet_button" 
            android:layout_toLeftOf="@+id/a_settings" 
            android:layout_alignBottom="@+id/a_settings"
            android:layout_height="fill_parent"
            android:scaleType="fitXY">
        </ImageButton>

        <ImageButton android:id="@+id/a_offers" 
            android:layout_width="100dip" 
            android:background="@drawable/offers_button" 
            android:layout_toRightOf="@+id/a_settings" 
            android:layout_alignBottom="@+id/a_settings" 
            android:layout_height="fill_parent"
            android:scaleType="fitXY">
        </ImageButton>

    </RelativeLayout>

</RelativeLayout>

Upvotes: 1

Views: 164

Answers (2)

antonyt
antonyt

Reputation: 21883

Even though your menu layout is indeed called navigation_bar, you gave your included menu layout the id of include1 -> android:id="@+id/include1". Try findViewById(R.id.include1) if you want to resolve it in your activity (or just try to find the ImageButtons, they are also visible).

Upvotes: 2

Maximus
Maximus

Reputation: 8431

I'm not 100% but I will go out on a limb and say that you probably need to be calling findViewById() from your myLayout View (which may have a casting issue as well).

So this instead:

public static void setup(Activity a){
    myActivity = a;

    //OFFENDING BIT OF CODE
    myLayout = (View) myActivity.findViewById(R.layout.navigation_bar); 

    Log.d(TAG, "layout: "+myLayout);

    set_btn = (ImageButton) myLayout.findViewById(R.id.a_settings);
    set_btn.setPressed(false);
    set_btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //TODO:
        }
    });

    inbox_btn = (ImageButton) myLayout.findViewById(R.id.a_offers);
    inbox_btn.setPressed(false);
    inbox_btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //TODO:
        }
    });

}

Upvotes: 1

Related Questions