1

I read some question regarding this but all the answers are about fragments and there is question similar to this one but the answer is incomplete, I want to reuse a set of layout or codes into multiple activities, i created a baseActivity that extends into Activity with the code below.

I also read that you need to put the code in the onCreateOptionMenu but it is still not working. (the code in baseacitivty xml is working, and homepage xml is working but does not show the navigation_layout)

public class BaseActivity extends Activity {
    private ImageButton ibButtonHome;
    private ImageButton ibButtonFavorite;
    private ImageButton ibButtonRandomize;
    private ImageButton ibButtonHistory;
    private ImageButton ibButtonLogOut;

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

    }

    View.OnClickListener Navigation = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent();
            if (v.equals(ibButtonHome)) {
                i.setClass(getBaseContext(), HomePage.class);
            } else if (v.equals(ibButtonFavorite)) {
                i.setClass(getBaseContext(), Favorite.class);
            } else if (v.equals(ibButtonHome)) {
                i.setClass(getBaseContext(), HomePage.class);
            } else if (v.equals(ibButtonRandomize)) {
                i.setClass(getBaseContext(), Randomize.class);
            } else if (v.equals(ibButtonHistory)) {
                i.setClass(getBaseContext(), History.class);
            } else if (v.equals(ibButtonLogOut)) {
                //TODO: something code here to not crash on activity exit??
                i.setClass(getBaseContext(), MainActivity.class);
            }

            startActivity(i);

        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        super.onCreateOptionsMenu(menu);
        ibButtonHome = (ImageButton) findViewById(R.id.button_Home);
        ibButtonFavorite = (ImageButton) findViewById(R.id.button_favorites);
        ibButtonRandomize = (ImageButton) findViewById(R.id.button_randomize);
        ibButtonHistory = (ImageButton) findViewById(R.id.button_history);
        ibButtonLogOut = (ImageButton) findViewById(R.id.button_logout);

        ibButtonFavorite.setOnClickListener(Navigation);
        ibButtonRandomize.setOnClickListener(Navigation);
        ibButtonHome.setOnClickListener(Navigation);
        ibButtonHistory.setOnClickListener(Navigation);
        ibButtonLogOut.setOnClickListener(Navigation);

        getMenuInflater().inflate(R.menu.menu_filter_menus, 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) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

Homepage activity

    public class HomePage extends BaseActivity {
    private CustomAdpaterFoodFeed ExpAdapter;
    private ArrayList<FoodFeed> foodFeeds;
    private ExpandableListView ExpandList;
    //Onclick listener for the Navigation Bar

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

        ExpandList = (ExpandableListView) findViewById(R.id.evFoodFeed);



        //runs the function and returns the data to foodFeeds
        foodFeeds = SetStandardGroups();

        //Adapter for ExapadableListView
        ExpAdapter = new CustomAdpaterFoodFeed(HomePage.this, foodFeeds);
        ExpandList.setAdapter(ExpAdapter);



    }

    // Dummy data method for pictures and comments
    public ArrayList<FoodFeed> SetStandardGroups() {

        String names[] = {"Geraldine", "Marielle", "Gina", "Bryan",
                "Pat", "Eugene", "Shermaine", "Kook"};

        String comments[] = {"TasteGood", "Nah", "DONT EAT HERE", "Cameroon",
                "Nice place", "chill", "woah Spain", "lalala"};

        int Images[] = {R.mipmap.ic_launcher, R.mipmap.ic_launcher,
                R.mipmap.ic_launcher, R.mipmap.ic_launcher,
                R.mipmap.ic_launcher, R.mipmap.ic_launcher,
                R.mipmap.ic_launcher, R.mipmap.ic_launcher
        };

        ArrayList<FoodFeed> list = new ArrayList<FoodFeed>();

        ArrayList<Comments> comments_list;


        for (int images : Images) {
            FoodFeed gru = new FoodFeed();
            gru.setIcon(images);

            comments_list = new ArrayList<Comments>();
            for (int j = 0; j < 4; j++) {
                Comments comments1 = new Comments();
                comments1.setName(names[j]);
                comments1.setComments(comments[j]);

                comments_list.add(comments1);
            }
            gru.setComments(comments_list);
            list.add(gru);


        }

        return list;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_home_page, 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) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
SCS
  • 1,162
  • 3
  • 17
  • 31
  • only one activity's layout can be visible at a time but you can use base activity to extend the navigation drawer in other activities – Awadesh Oct 09 '15 at 18:17
  • you can extend the layout of the activity in the fragments because they are the part of the activity – Awadesh Oct 09 '15 at 18:19
  • I already extended the base activity to my other activities but it is still not working, i think i am lacking something @Awadesh – SCS Oct 09 '15 at 18:23
  • I said in my earlier comment that two activities can not be visible simultaneously as each activity has its own layout so it can not be possible – Awadesh Oct 09 '15 at 18:25
  • http://stackoverflow.com/questions/8255614/extending-class-for-activity, i am trying to do this suggestion – SCS Oct 09 '15 at 18:27
  • 1
    yes I also said that it can be done for navigation drawer and action bar – Awadesh Oct 09 '15 at 18:32
  • I am trying to do something like that – SCS Oct 09 '15 at 18:36
  • 1
    I have used base activity to extend the navigation drawer in other activities if you want i can post the whole code – Awadesh Oct 09 '15 at 18:36
  • please :)) Thank you @Awadesh – SCS Oct 09 '15 at 18:37
  • I have posted the answer if you have any doubt please ask :) :) – Awadesh Oct 09 '15 at 19:06

1 Answers1

0

Base activity to extend the navigation drawer in other activities you can follow this link , described well. it is well tested i followed the same :)

http://androiddeveloperdemo.blogspot.in/2014/08/android-navigation-drawer-with-multiple.html

You can get the same action bar in other activities by declaring in AndroidManifest.xml like this

 <activity
        android:name=".SettingsActivity"
        android:label="@string/activity_title"
        android:theme="@style/AppTheme" />

For different menu options define a xml file under menu folder in android studio and inflate that file in onCreateOptionsMenu(Menu) overridden method of your activty

Awadesh
  • 3,530
  • 2
  • 20
  • 32
  • the problem with this is I dont know how to remove the animation in the side bar, I only want the navbar to be like the instagram navbar, I am just new to android :( – SCS Oct 10 '15 at 02:48