Question

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?

Was it helpful?

Solution

I hope I understood you right: You are using setContentView() in your BaseActivity to set up the drawer and setContentView() in your MainActivity to do the rest of your layout setup? If thats the case: it won't work that way. The second time you do setContentView() 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 in setContentView() in your MainActivity. Also, son't use setContentView() twice, the first time is useless.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top