Skip to content

Instantly share code, notes, and snippets.

@Morgazmo
Created May 16, 2018 03:06
Show Gist options
  • Save Morgazmo/00e8bc3299ca892255617db855367190 to your computer and use it in GitHub Desktop.
Save Morgazmo/00e8bc3299ca892255617db855367190 to your computer and use it in GitHub Desktop.
BaseActivity.xml
package xxx.xxxx.xxxxx;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.widget.FrameLayout;
public abstract class BaseActivity extends AppCompatActivity {
private static final String TAG = "BaseActivity";
private DrawerLayout drawerLayout;
private ActionBar supportActionBar;
private ActionBarDrawerToggle actionBarDrawerToggle;
private Toolbar toolbar;
@Override
public void setContentView(int layoutResID) {
drawerLayout = (DrawerLayout) getLayoutInflater().inflate(R.layout.base_activity, null);
NavigationView navigationView = drawerLayout.findViewById(R.id.navigation_view);
FrameLayout activityContainer = drawerLayout.findViewById(R.id.contentFrame);
getLayoutInflater().inflate(layoutResID, activityContainer, true);
super.setContentView(drawerLayout);
setupToolbar();
setupNavigationDrawer();
setupDrawerContent(navigationView);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d(TAG, "onOptionsItemSelected " + item.toString());
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case android.R.id.home: // Open the navigation drawer when the home icon is selected from the toolbar.
drawerLayout.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
private void setupToolbar() {
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
supportActionBar = getSupportActionBar();
if (supportActionBar != null) {
supportActionBar.setTitle(R.string.action_load);
supportActionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
supportActionBar.setDisplayHomeAsUpEnabled(true);
Log.w(TAG, "setupToolBar: Toolbar");
} else {
Log.w(TAG, "setupToolBar: No Toolbar!");
}
}
private void setupNavigationDrawer() {
drawerLayout = findViewById(R.id.load_drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close){
};
drawerLayout.addDrawerListener(actionBarDrawerToggle);
if (navigationView != null) {
setupDrawerContent(navigationView);
}
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
if (getSupportActionBar() != null) {
switch (menuItem.getItemId()) {
case R.id.navigation_item1:
getSupportActionBar().setTitle("1");
break;
default:
break;
}
}
// Close the navigation drawer when an item is selected.
drawerLayout.closeDrawers();
return true;
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment