1

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

ced
  • 31
  • 5

3 Answers3

0

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.

encastellano
  • 435
  • 3
  • 5
  • As you told me I created a "BaseMenuActivity extends Activity" and now I have: MainActivity extends BaseMenuActivity my function and variable for the menu are in BaseMenuActivity but 'have an error when I use Intent, eg new Intent (CurrentActivity.class, NewActivity.class) – ced Apr 16 '15 at 09:05
  • You should change CurrentActivity.class for only "this" or getApplicationContext(); – encastellano Apr 16 '15 at 09:09
  • it works well on my MainActivity but when I want to change the activity setUpDrawer function create errors for the first ten online I do not understand why does it work for the Main and not for others – ced Apr 16 '15 at 09:46
  • If you want change setUpDrawer function or other function in BaseActivity, you should override in the child Activity. If you want change your expandablelistview data (for example) make and abstract function like getContentView named configureMenuData and implement in child the code about configure expandablelist, like create adapter etc. You must be clear that code will be common to all activities and that code will be customizable. Play with this and try again – encastellano Apr 16 '15 at 09:54
  • If you dont see easy to make, you can use a library that handle that. If you want, update your post with your new class and logic and i try to help you – encastellano Apr 16 '15 at 09:58
  • ok I'm almost there, the menu appears well on my MainActivity and I can change the activity but the menu does not appear on this new activity, yet it extends BaseMenuActivity – ced Apr 16 '15 at 11:04
  • Remember the child layout should have the same structure that the activity_layout.xml Should contain drawer_layout and list_slidermenu. If you update the code in the main post, maybe i see something wrong – encastellano Apr 16 '15 at 11:19
0

Consider browsing Google I/O 2014 Source Code - they have implemented NavigationDrawer with BaseActivity class.

Also you can try using some libraries which can help you implement NavigationDrawer easily, check Android Arsenal Sliding Panels section.

localhost
  • 5,568
  • 1
  • 33
  • 53
0

If you want customize your contain layout for each activity i think you should use something like this:

//update_activity.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <!-- As the main content view, the view below consumes the entire
             space available using match_parent in both dimensions. -->


            <RelativeLayout
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                >
                //change this with your views for each activity
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/customImageView"
                    />
            </RelativeLayout>



        <!-- android:layout_gravity="start" tells DrawerLayout to treat
             this as a sliding drawer on the left side for left-to-right
             languages and on the right side for right-to-left languages.
             If you're not building against API 17 or higher, use
             android:layout_gravity="left" instead. -->
        <!-- The drawer is given a fixed width in dp and extends the full height of
             the container. -->
       <!-- 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">

    </android.support.v4.widget.DrawerLayout>
</RelativeLayout>

//UpdateActivity.class

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;
ImageView customImageView,
@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();
    customImageView = (ImageView) findViewById(R.id.customImageView);
 }

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

Inside RelativeLayout with "@+id/container" name, you can add whatever you want and customize for each activity and instantiate in the child activity.

encastellano
  • 435
  • 3
  • 5