Why menu items in android studio don't work on click? - android

I'm developing a project on android and completely new to this. I had made a drawer menu with some of menu items. Now when i want to put another activity on click from menu items, the drawer closes automatically, it doesn't work. Here
private DrawerLayout mdrawerLayout;
private ActionBarDrawerToggle mdrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mdrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
mdrawerToggle = new ActionBarDrawerToggle(this, mdrawerLayout, R.string.open, R.string.close);
mdrawerLayout.addDrawerListener(mdrawerToggle);
mdrawerToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mdrawerToggle.onOptionsItemSelected(item)) {
return true;
}
int id = item.getItemId();
if(id == R.id.setup){
Intent csetup = new Intent(MainActivity.this, Subactivity.class);
startActivity(csetup);
return false;
}
return super.onOptionsItemSelected(item);
}
}
`

Try this:
Add below code in your onCreate method:
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Implement lister in your mainactivity class:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener
And after that in your onNavigationItemSelected method look like this:
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.setup) {
//call new activity
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Hope this may help you

This is because you are overriding the wrong method. You have to override onNavigationItemSelected() for drawer, instead of onOptionsItemSelected() which is for top-right settings menu. See the following code for reference:
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.setup) {
Intent csetup = new Intent(MainActivity.this, Subactivity.class);
startActivity(csetup);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

First find the view for you NavigationView using findViewById method like below-NavigationView navView=(NavigationView)findViewById(R.id.navView)
then use this.
navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.setup) {
Intent csetup = new Intent(MainActivity.this, Subactivity.class);
startActivity(csetup);
}
return false;
}
});
then it will work.. onOptionsItemSelected(MenuItem item) is used to get the access to the MenuItems we have inflated on toolbar or on other layouts using onCreateOptionsMenu() method.

Related

Problems in NavigationDrawer (Swap Activities)

Forgive my English :(
I have a problem with "NavigationDrawer". I have the Fragments "Home " that have no special activities, "Import, Gallery and SlideShow", all with activities running perfectly.
However, if I click on Import (HOME> IMPORT) the activity IMPORT opens, but if I click on (IMPORT> GALLERY / SLIDESHOW / HOME) the Import activity remains open.
I have to press the back button to go to HOME, to click on another activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
//here is the main place where we need to work on.
int id=item.getItemId();
switch (id){
case R.id.nav_home:
Intent h= new Intent(Home.this,Home.class);
startActivity(h);
break;
case R.id.nav_import:
Intent i= new Intent(Home.this,Import.class);
startActivity(i);
break;
case R.id.nav_gallery:
Intent g= new Intent(Home.this,Gallery.class);
startActivity(g);
break;
case R.id.nav_slideshow:
Intent s= new Intent(Home.this,Slideshow.class);
startActivity(s);
break;
// oh nightmare
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
IMPORT ACTIVITY
public class Import extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawer;
NavigationView navigationView;
Toolbar toolbar=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_import);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button Button9 = (Button) findViewById(R.id.Button9);
Button Button11 = (Button) findViewById(R.id.Button11);
//We dont need this.
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Button9.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent it = new Intent(Import.this, PHP5.class);
startActivity(it);
}
});
Button11.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent it = new Intent(Import.this, PHP7.class);
startActivity(it);
}
});
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, 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);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
//here is the main place where we need to work on.
int id=item.getItemId();
switch (id){
case R.id.nav_home:
Intent h= new Intent(Import.this,Home.class);
startActivity(h);
break;
case R.id.nav_import:
Intent i= new Intent(Import.this,Import.class);
startActivity(i);
break;
case R.id.nav_gallery:
Intent g= new Intent(Import.this,Gallery.class);
startActivity(g);
break;
case R.id.nav_slideshow:
Intent s= new Intent(Import.this,Slideshow.class);
startActivity(s);
break;
// after this lets start copying the above.
// FOLLOW MEEEEE>>>
//copy this now.
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Maybe you forgot to call finish() in the IMPORT Activity? If you call it after your Intent to another Activity, the current Activity will close, and your Home Activity will be shown again.
You need to remove all previous Activities from stack, it can be done trough a flag in the intent
Intent it = new Intent(Import.this, OtherAct.class);
it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(it);
Else a combination of the keywors may work for you:
it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
Check the doc about how it works:
https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK
https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TASK

Android DrawerLayout does not close untill fragment is replaced

I got a problem that DrawerLayout does not close smoothly and it stops in the middle for a while, it totally closes after finish replacing fragment. In coding I close DrawerLayout before replacing fragment (productList class). In productList class, it calls asynctask to get all the product list from server meanwhile running progress bar. Ideally when user clicks menu, DrawerLayout will close at once and productList fragment will be loading all the product list. It seems to me DrawerLayout close event waits for productList's asynctask. Any suggestion greatly appreciated.
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {
public String tag = "MainActivity";
Context mContext;
//User Object
User user;
//Control
TextView txtName, txtPosition, txtLogout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
user = Constants.getUser();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
//NavigationView Header
View headerView = navigationView.getHeaderView(0);
txtName = (TextView) headerView.findViewById(R.id.nav_header_main_name) ;
txtName.setText(user.getName().toString());
txtLogout = (TextView) headerView.findViewById(R.id.nav_header_main_logout);
txtLogout.setOnClickListener(this);
navigationView.setNavigationItemSelectedListener(this);
//Add Menu in navigationView
Menu myMenu = navigationView.getMenu();
myMenu.clear();
myMenu.add(0,Integer.parseInt("01"),0,"Product List");
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#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) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
//close drawer
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
// replace fragment
int id = item.getItemId();
FragmentManager fragmentManager = getSupportFragmentManager();
if(Constants.DEBUG) {
Toast.makeText(mContext, " Menu Id " + String.valueOf(id), Toast.LENGTH_LONG).show();
}
fragmentManager.beginTransaction()
.replace(R.id.container,new productList())
.commit();
return true;
}
#Override
public void onClick(View v) {
//Log out onclick
if(v.getId() == R.id.nav_header_main_logout){
this.finish();
}
}
}
This probably happens beacuse the Navigation Drawer's animation and the creation of the Fragment happens at the same time.
You can solve it by starting your Fragment with a small delay-
handler.postDelayed(new Runnable() {
#Override
public void run() {
// replace fragment
int id = item.getItemId();
FragmentManager fragmentManager = getSupportFragmentManager();
if(Constants.DEBUG) {
Toast.makeText(mContext, " Menu Id " + String.valueOf(id), Toast.LENGTH_LONG).show();
}
fragmentManager.beginTransaction()
.replace(R.id.container,new productList())
.commit();
}
}, 300);

How to add menu and submenu in NavigationView dynamic in android

I want to add custom menu in slide navigation with menu icon as well as sub menu dynamically.Below code add menu successfully but sub menu is showing when slide navigation appear as well as unable to add icon to the menu so please help me to hide sub menu and show only menu with icon and when I click on menu then only its sub menu should be appear.
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private Toolbar toolbar;
private DrawerLayout drawer;
private NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getId();
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
// Add menu dynamically
Menu m = navigationView.getMenu();
SubMenu topChannelMenu = m.addSubMenu("Top Channels");
// Add submenu.
topChannelMenu.add("Foo");
topChannelMenu.add("Bar");
topChannelMenu.add("Baz");
topChannelMenu = m.addSubMenu("Top Channels2");
topChannelMenu.add("Foo2");
topChannelMenu.add("Bar2");
topChannelMenu.add("Baz2");
MenuItem mi = m.getItem(m.size()-1);
mi.setTitle(mi.getTitle());
}
private void getId()
{
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.nav_view);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
}
#Override
public void onBackPressed()
{
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START))
{
drawer.closeDrawer(GravityCompat.START);
} else
{
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item)
{
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera)
{
// Handle the camera action
} else if (id == R.id.nav_gallery)
{
} else if (id == R.id.nav_slideshow)
{
} else if (id == R.id.nav_manage)
{
} else if (id == R.id.nav_share)
{
} else if (id == R.id.nav_send)
{
}
drawer.closeDrawer(GravityCompat.START);
return true;
}

How to change selected Item in the navigation drawer depending on the activity/view?

I have a Navigation Drawer Activity and different activities in which I get. I want the Items in the nav drawer being selected depending on the activity or view whatever.
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), MainAddMedActivity.class);
/*EditText editText = (EditText) findViewById(R.id.search_medicament);
String medicament_search = editText.getText().toString();*/
/*intent.putExtra(EXTRA_MESSAGE, medicament_search);*/
startActivity(intent);
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.getMenu().getItem(0).setChecked(true);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
/*navigationView.getMenu().getItem(0).setChecked(true);*/
}
#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) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_healthdiary) {
} else if (id == R.id.nav_appointment) {
} else if (id == R.id.nav_physician) {
} else if (id == R.id.nav_protocol) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
For example, I get into the MainAddMedActivity and Press back. Then I want some code to check in which view or activity I am and select the item in the navigation drawer.
Seems like you have not yet implemented the switching of the content area. I suggest you use fragments for this.
So, if you use fragments, override onAttachFragment of your activity like:
#Override
public void onAttachFragment(Fragment fragment) {
super.onAttachFragment(fragment);
int id;
if(fragment instanceof HealthDiaryFragment) id = R.id.nav_healthdiary;
else
if(fragment instanceof AppointmentFragment) id = R.id.nav_appointment;
...
else return;
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setCheckedItem(id);
}
Also, modify your onBackPressed:
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
if(getFragmentManager().getBackStackEntryCount()>1) {
getFragmentManager().popBackStack();
}
else {
super.onBackPressed();
}
}
/*navigationView.getMenu().getItem(0).setChecked(true);*/
}
This is assuming that in your handling of drawer selection you replace fragments with pushing them on the back stack.

How to use Android Studio default Navigation Drawer in all Activities [duplicate]

This question already has answers here:
Same Navigation Drawer in different Activities
(13 answers)
Closed 7 years ago.
How to use the Android default Navigation in other Activities?
I don't want to use the back button.
MainActivity:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
TextView tvhead,tv6,tv7,tv8,tv9,tvbottom;
Typeface schriftart_courier_prime;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new MainFragment()).commit();
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#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_about) {
Intent getNameScreenIntent2 = new Intent (this, AboutActivity.class);
final int result = 1;
startActivity(getNameScreenIntent2);
return true;
//Actionbar element für später
}else if (id == R.id.action_phone){
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
FragmentManager fm = getFragmentManager();
int id = item.getItemId();
if (id == R.id.nav_home) {
fm.beginTransaction().replace(R.id.content_frame, new MainFragment()).commit();
} else if (id == R.id.nav_experiments) {
Intent getNameScreenIntent = new Intent(this, ExperimentsList.class);
final int result = 1;
startActivity(getNameScreenIntent);
} else if (id == R.id.nav_website) {
Intent getNameScreenIntent = new Intent(this, Web.class);
final int result = 1;
startActivity(getNameScreenIntent);
} else if (id == R.id.nav_share) {
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, getString(R.string.share_text));
startActivity(Intent.createChooser(share, getString(R.string.share_title)));
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Have all the activities that need that drawer inherit from a custom abstract Activity class, that adds the drawer in its onCreate method.
make a class something like:
public abstract class MyNavigationActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
}
then:
public class MyActivity extends MyNavigationActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new MainFragment()).commit();
}
}

Categories

Resources