I know this question has been asked before (2 other i found similar) but they didnt address this side of the scenario!
SO here's the scenario, I created a class as this:
public class BaseActivity extends ActionBarActivity {
// Implemeted the Actionbar and navigation drawer here!
// and it containts onCreate method and setContentView
}
BaseActivity XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/base_nav_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
<ListView
android:id="@+id/base_left_drawer"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#dddddd"/>
<ListView
android:id="@+id/base_right_drawer"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#dddddd"/>
</android.support.v4.widget.DrawerLayout>
then extended my main class/activity from it:
public class Main extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
and here's Main activity XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/stream_new_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hi dude"/>
</LinearLayout>
so now i can see the Actionbar and Navigation drawer in my Main activity, however, if i try to use setContectView(R.layout.main)
on my Main activity to use the xml layout i created specific to this activity, the nav drawer will no show up anymore; i guess because onCreate
and setContectView
on Main activity, overrides the same statements from BaseActivity which i extended from. any solution for this?
I hope I understood you right: You are using
setContentView()
in yourBaseActivity
to set up the drawer andsetContentView()
in yourMainActivity
to do the rest of your layout setup? If thats the case: it won't work that way. The second time you dosetContentView()
it will replace the old layout and then there's no more drawer. You got to have a drawer in the layout that you use insetContentView()
in yourMainActivity
. Also, son't usesetContentView()
twice, the first time is useless.