Search code examples
androidandroid-activityexpandablelistviewnavigation-drawermain-activity

Android: Navigation drawer menu on any activities


I made a left menu, using ExpandableListView, this one works very well on my MainActivity. Now I want to use it on my other activities, how can I do this simply?

Should I just create a class for the menu or can I re-use my MainActivity?

Here is the code of my MainActivity:

public class MainActivity extends BaseMenuActivity {

public static final String SAVE_VALUES = "SaveFile";
static String saveDirectory = String.valueOf(Environment.getExternalStorageDirectory());
static String backgroundPath;
static ImageView layout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);

    layout = (ImageView) findViewById(R.id.background);
    SharedPreferences setting = getSharedPreferences(SAVE_VALUES,0);
    String backgrounImage = setting.getString("backgroundImage","bc_lip");
    backgroundPath = saveDirectory + "/galaxyv2Img/" + backgrounImage + ".jpg";
    Drawable background = Drawable.createFromPath(backgroundPath);
    layout.setBackgroundDrawable(background);

    UpdateDatabaseCsv tt = new UpdateDatabaseCsv(MainActivity.this);
    tt.open();
    tt.close();

}

@Override
public int getContentView() {
    return R.layout.activity_main;
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        Intent i = new Intent(this, SettingActivity .class);
        startActivity(i);
    }

    return super.onOptionsItemSelected(item);
}

My update activity :

public class UpdateActivity extends BaseMenuActivity {
public static final String SAVE_VALUES = "SaveFile";
public final String downloadDirectory = Environment.getExternalStorageDirectory() + "/galaxyv2/download/";
ProgressDialog barProgressDialog;
Button btnStartProgress;
FtpGetter ftp;
int progressBarStatus = 0;
String ftpLink, ftpPassword, ftpHost, ftpUser;
FilesManager filesManager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_update);
    filesManager = new FilesManager(this);
    Intent intent = getIntent();
    String value = intent.getStringExtra("todo");
    ftp = new FtpGetter();
}

@Override
public int getContentView() {
    return R.layout.activity_update;
}

MainActivity xml :

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/background"
    android:scaleType="fitXY"
    />



<!-- ExpandableListview to display slider menu -->
<ExpandableListView
    android:id="@+id/list_slidermenu"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"        
    android:listSelector="@drawable/list_selector"
    android:background="@color/list_background">
</ExpandableListView>

UpdateActivity xml :

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/update_start_update"
        android:id="@+id/button_update_start"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    </RelativeLayout>


<!-- ExpandableListview to display slider menu -->
<ExpandableListView
    android:id="@+id/list_slidermenu"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector"
    android:background="@color/list_background">
</ExpandableListView>

the problem is that I can not use my old UpdateActivity xml and RelativeLayout


Solution

  • You should create a activity with the login concerning the lateral menu and call BaseMenuActivity.class (for example). All the activitys that you want to have this menu, should extend from de BaseMenuActivity and the contentview should have same components.

    Maybe you should implement in parent activity a abstract function like this:

    public abstract int getContentView();
    

    and replace your:

    setContentView(R.layout.main_layout)

    for:

    setContentView(getContentView());

    In your child activity implement this function

     @Override
        public int getContentView() {
            return R.layout.layout;
        }
    

    This code allows you assign layout xml from the child's, and the parent can access this. If you not implement this, you cannot access to views from the parent activity for configure navigation drawer.