I have made a customised navigational drawer with 3 buttons and I want to change text on the buttons according to the activity they are associated with. So when I made a java object of the layout of navigational drawer using LayoutInflater and got handle of each button using findViewById on that layout object, I set Text of each button but when i am running the app the text does not appear(in XML I have not set any text for the buttons). Rest all is working fine.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sahil.childcare.NavigationalDrawerFragment">
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<include
layout="@layout/nav_profile"
android:id="@+id/nav_profile"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/nav_profile"
android:layout_marginLeft="-5dip"
android:layout_marginRight="-5dip"
android:layout_marginTop="-5dip"
android:layout_marginBottom="-5dip"
android:id="@+id/top_button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/top_button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginLeft="-5dip"
android:layout_marginRight="-5dip"
android:layout_marginTop="-7dip"
android:layout_marginBottom="-5dip"
android:id="@+id/middle_button" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/middle_button"
android:id="@+id/bottom_button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginLeft="-5dip"
android:layout_marginRight="-5dip"
android:layout_marginTop="-7dip"
android:layout_marginBottom="-5dip"/>
</RelativeLayout>
</FrameLayout>
The part of the code in main activity(inside onCreate() method) involving this drawer is here
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.fragment_navigational_drawer,null);
Button topButton = (Button) view.findViewById(R.id.top_button);
Button middleButton = (Button) view.findViewById(R.id.middle_button);
Button bottomButton = (Button) view.findViewById(R.id.bottom_button);
topButton.setText("School");
topButton.setTextColor(getResources().getColor(R.color.red));
middleButton.setText("Teacher");
middleButton.setTextColor(getResources().getColor(R.color.red));
bottomButton.setText("Child");
bottomButton.setTextColor(getResources().getColor(R.color.red));
First thing I'd check if the buttons are even visible on screen. To do that you can set them some color background, e.g.:
android:background="#ff0000"
and also use hierarchy-viewer to make sure your buttons appear where you expect them to appear.
Next thing to check is if the newly inflated fragment_navigational_drawer
is actually being used for the Navigation Drawer, or maybe it's a different instance of this layout, to do that you can add some logging to the inflation code, printing the specific instance you put on screen, and verifying in hierarchy-viewer you see the right instance on screen.
UPDATE:
For completeness of the answer, per comments below, if you already added a layout via xml (e.g. using <include>
), instead of inflating it in code:
View view = inflater.inflate(R.layout.fragment_navigational_drawer,null);
you need to get a handle of the already inflated layout, via something like:
View view = findViewById(R.id.my_navigational_drawer);