I'm trying to make the "sign out" item take me back to the Login window. I did a lot of searching and watching videos but nothing worked for me.
Here's my built-in navigation drawer code.
The code I typed starts from line 46 to line 56, most codes are just built-in.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityHomepageBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setSupportActionBar(binding.appBarHomepage.toolbar);
binding.appBarHomepage.fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = binding.drawerLayout;
NavigationView navigationView = binding.navView;
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
int id=menuItem.getItemId();
if (id == R.id.nav_Sign_out){
Intent intent = new Intent(Homepage.this, Login.class);
startActivity(intent);
}
return true;
}
});
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(R.id.nav_dashboard, R.id.nav_profile, R.id.nav_orders, R.id.nav_recent_orders,
R.id.nav_pending_deliveries, R.id.nav_cancelled_orders, R.id.nav_settings)
.setOpenableLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_homepage);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.homepage, menu);
return true;
}
#Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_homepage);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
Login.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
textview = (TextView) findViewById(R.id.textViewSignUp);
textview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Login.this,Register.class);
startActivity(intent);
}
});
button = (Button) findViewById(R.id.btnLogin);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Login.this,Homepage.class);
startActivity(intent);
}
});
}
}
It doesn't have an error but it doesn't work.
Here's the activity_main_drawer.xml where I put the items.
<group
android:id="#+id/menu_top"
android:checkableBehavior="none">
<item
android:id="#+id/nav_dashboard"
android:icon="#drawable/ic_baseline_dashboard_24"
android:title="#string/dashboard">
</item>
<item
android:id="#+id/nav_profile"
android:icon="#drawable/ic_baseline_person_24"
android:title="#string/profile" >
</item>
</group>
<item
android:id="#+id/nav_orders"
android:title="#string/orders" >
<menu>
<item
android:id="#+id/nav_menu_orders"
android:title="#string/Orders"
android:icon="#drawable/ic_orders" />
<item
android:id="#+id/nav_recent_orders"
android:title="#string/recent_orders"
android:icon="#drawable/ic_recent" />
</menu>
</item>
<item
android:id="#+id/nav_deliveries"
android:title="#string/deliveries" >
<menu>
<item
android:id="#+id/nav_pending_deliveries"
android:title="#string/pending_deliveries"
android:icon="#drawable/ic_pending_deliveries"/>
<item
android:id="#+id/nav_recent_deliveries"
android:title="#string/recent_deliveries"
android:icon="#drawable/ic_recent" />
</menu>
</item>
<item
android:id="#+id/nav_cancelled_orders"
android:icon="#drawable/ic_baseline_cancel_24"
android:title="#string/cancelled_orders" >
</item>
<item
android:id="#+id/nav_settings"
android:icon="#drawable/ic_baseline_settings_24"
android:title="#string/settings" >
</item>
<group
android:id="#+id/nav_menu_bottom"
android:checkableBehavior="none">
<item
android:id="#+id/nav_Sign_out"
android:icon="#drawable/ic_sign_out"
android:title="#string/sign_out">
</item>
</group>
Try this -
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
binding = ActivityHomepageBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setSupportActionBar(binding.appBarHomepage.toolbar);
binding.appBarHomepage.fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = binding.drawerLayout;
NavigationView navigationView = binding.navView;
navigationView.setNavigationItemSelectedListener(this);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(R.id.nav_dashboard, R.id.nav_profile, R.id.nav_orders, R.id.nav_recent_orders,
R.id.nav_pending_deliveries, R.id.nav_cancelled_orders, R.id.nav_settings)
.setOpenableLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_homepage);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.homepage, menu);
return true;
}
#Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_homepage);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
int id=menuItem.getItemId();
if (id == R.id.nav_Sign_out){
Intent intent = new Intent(Homepage.this, Login.class);
startActivity(intent);
}
return true;
}
}
Related
I want to make my Searchview to be visible only when I open product option from the drawer, to do that I am trying using onOptionsItemSelected method to listen which options is being used.
The problem is I am always get a NullPointerException everytime I tried to get my SearchView id from onOptionsItemSelected.
I knew how to hide the SearchView in onCreateOptionsMenu using :
menu.findItem(R.id.action_search).setVisible(false);
This is the source code for my onCreateOptionsMenu :
#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);
View lay = findViewById(R.id.nav_header);
//id in this line is found
menu.findItem(R.id.action_search).setVisible(false);
sharedpreferences = getSharedPreferences(Login.my_shared_preferences, Context.MODE_PRIVATE);
session = sharedpreferences.getBoolean(session_status, false);
id = sharedpreferences.getString(TAG_ID, null);
username = sharedpreferences.getString(TAG_USERNAME, null);
name = sharedpreferences.getString(TAG_NAME, null);
txt_id = lay.findViewById(R.id.txt_id_admin);
txt_username = lay.findViewById(R.id.txt_textView);
txt_name = lay.findViewById(R.id.txt_name_admin);
imb = lay.findViewById(R.id.btn_exit);
txt_id.setText("ID : " + id);
txt_name.setText("Nama : " + name);
txt_username.setText("Status : " + username);
imb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.clear();
editor.commit();
intent = new Intent(getApplicationContext(), Login.class);
finish();
startActivity(intent);
}
});
return true;
But I can't do the same thing in onOptionsItemSelected method, because the argument it used was MenuItem instead of Menu.
This is my latest try for the onOptionsItemSelected method :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
NavigationView navigationView = findViewById(R.id.nav_view);
Menu menu = navigationView.getMenu();
//id in this line is null
MenuItem item_search = menu.findItem(R.id.action_search);
Log.d(TAG, "Response : " + item_search);
SearchView searchView = (SearchView) item_search.getActionView();
if (item.getItemId() == R.id.nav_produk) {
searchView.setVisibility(View.VISIBLE);
return true;
} else {
Log.d(TAG, "Response : " + searchView);
searchView.setVisibility(View.INVISIBLE);
return super.onOptionsItemSelected(item);
}
}
And this is my res/menu/main.xml file :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:visible="false"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
<item
android:id="#+id/action_search"
android:visible="true"
android:orderInCategory="100"
android:icon="#android:drawable/ic_menu_search"
android:title="#string/search"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView"/>
I laready found answer! Apparently I don't need onOptionsItemSelected at all.
I am using NavController on my DrawerLayout, so the easier way to make a listener is using onDestinationChanged method inside onCreateOptionsMenu
This is the result :
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
View lay = findViewById(R.id.nav_header);
menu.findItem(R.id.action_search).setVisible(false);
sharedpreferences = getSharedPreferences(Login.my_shared_preferences, Context.MODE_PRIVATE);
session = sharedpreferences.getBoolean(session_status, false);
id = sharedpreferences.getString(TAG_ID, null);
username = sharedpreferences.getString(TAG_USERNAME, null);
name = sharedpreferences.getString(TAG_NAME, null);
txt_id = lay.findViewById(R.id.txt_id_admin);
txt_username = lay.findViewById(R.id.txt_textView);
txt_name = lay.findViewById(R.id.txt_name_admin);
imb = lay.findViewById(R.id.btn_exit);
txt_id.setText("ID : " + id);
txt_name.setText("Nama : " + name);
txt_username.setText("Status : " + username);
imb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.clear();
editor.commit();
intent = new Intent(getApplicationContext(), Login.class);
finish();
startActivity(intent);
}
});
//Only show SearchView when produk is open
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
#Override
public void onDestinationChanged(#NonNull NavController controller, #NonNull NavDestination destination, #Nullable Bundle arguments) {
if (destination.getId() == R.id.nav_produk) {
menu.findItem(R.id.action_search).setVisible(true);
} else {
menu.findItem(R.id.action_search).setVisible(false);
}
}
});
return true;
}
My MainActivity uses onNavigationItemSelected(MenuItem item) that includes an activity. But somehow it won't open the other fragments that I have on the drawer. I can open the fragments if i delete navigationView.setNavigationItemSelectedListener(this). But then I can't open BookingActivity.
My MainActivity:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
//region Properties
private AppBarConfiguration mAppBarConfiguration;
ImageButton settings_btn;
//endregion
#Override
protected void onStart() {
super.onStart();
ProductDatabase productDb = new ProductDatabase();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
settings_btn = findViewById(R.id.btn_settings);
settings_btn.setOnClickListener(v ->
startActivity(new Intent(MainActivity.this, SettingsActivity.class)));
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(view -> Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show());
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_login, R.id.nav_treatments, R.id.nav_product, R.id.nav_about, R.id.nav_contactUs)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
navigationView.setNavigationItemSelectedListener(this); // Fragments work if I remove this line, but not BookingActivity
}
#Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
#Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_booking) {
startActivity(new Intent(this, BookingActivity.class));
} else if (id == R.id.nav_home) {
} else if (id == R.id.nav_product) {
// What to write here to open ProductFragment?
} else if (id == R.id.nav_treatments) {
} else if (id == R.id.nav_about) {
} else if (id == R.id.nav_contactUs) {
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
please try this link How to go to fragment from activity
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_booking) {
startActivity(new Intent(this, BookingActivity.class));
} else if (id == R.id.nav_home) {
} else if (id == R.id.nav_product) {
// here you will set intent
} else if (id == R.id.nav_treatments) {
} else if (id == R.id.nav_about) {
} else if (id == R.id.nav_contactUs) {
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Hi i'm trying to put an event Item click in my Navigation View from drawer it works fine, but the problem is the other items in my drawer stop working, it seems that putting an item click event on my navigation view affect my NavigationController
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bundy_clock);
dBhelper = new DBhelper(this);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
R.id.nav_tools, R.id.nav_share, R.id.nav_send)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if(id == R.id.nav_logout){
AlertDialog.Builder conDialog = new AlertDialog.Builder(BundyClock.this);
conDialog.setTitle("Confirm");
conDialog.setMessage("Are you sure you want to log out?");
conDialog.setCancelable(false);
conDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(BundyClock.this, MainActivity.class));
Toast.makeText(getApplicationContext(),"Successfully Logout",Toast.LENGTH_SHORT).show();
finish();
}
});
conDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
final AlertDialog sdialog = conDialog.create();
sdialog.show();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
});
}
You have to use NavigationUI.onNavDestinationSelected to handle this. Check below:
boolean handled = NavigationUI.onNavDestinationSelected(menuItem, navController);
if (!handled) {
if(id == R.id.nav_logout){
AlertDialog.Builder conDialog = new AlertDialog.Builder(BundyClock.this);
conDialog.setTitle("Confirm");
conDialog.setMessage("Are you sure you want to log out?");
conDialog.setCancelable(false);
conDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(BundyClock.this, MainActivity.class));
Toast.makeText(getApplicationContext(),"Successfully Logout",Toast.LENGTH_SHORT).show();
finish();
}
});
conDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
final AlertDialog sdialog = conDialog.create();
sdialog.show();
}
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return handled;
I keep getting an IllegalArgumentException in my MainActivity code, please help me! The error is being shown on the line
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
I am trying to add in the QR code reader into the Main Activity, but when I did so, this error popped up
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
private SharedPreferences appData;
private Button scan_button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_scan);
scan_button = (Button) findViewById(R.id.btnScan);
final Activity activity = this;
scan_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
integrator.setPrompt("Scan");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
});
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
/*FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
*/
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_transaction, R.id.nav_qrcode,
R.id.nav_login, R.id.nav_logout, R.id.nav_signup, R.id.nav_rewards, R.id.nav_scan)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
appData = getSharedPreferences("appData", this.MODE_PRIVATE);
SharedPreferences.Editor editor = appData.edit();
editor.putFloat("Point", 0);
//editor.putBoolean("SAVE_LOGIN_DATA", checkBox.isChecked());
editor.putString("ID", "");
editor.putString("PWD", "");
editor.apply();
//Navigation.findNavController(navigationView).navigate(R.id.nav_login);
}
#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 onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
public boolean onChangeMenuItem() {
NavigationView navigationView = findViewById(R.id.nav_view);
Menu menu = navigationView.getMenu();
MenuItem nav_dashboard = menu.findItem(R.id.nav_login);
nav_dashboard.setVisible(false);
return true;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() == null) {
Toast.makeText(this, "You Cancelled the Scan", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, result.getContents(), Toast.LENGTH_LONG).show();
String scanResult = data.getStringExtra("SCAN_RESULT");
int pointsAdded = Integer.parseInt(scanResult);
}
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
I'm working on an app and I would like to have a toolbar going into each and every activity. I set up the toolbar into the primary activity, but when I click the item in the toolbar that sends me to the next activity, the toolbar isn't shown. I have included the toolbar in both .xml files. When I run the second activity by itself, the toolbar is shown. Any help please?
HomeActivty Java:
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
toolbar = (Toolbar) findViewById(R.id.tToolbar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_home,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
int res_id = item.getItemId();
if(res_id == R.id.action_profile)
{
Intent profileActivity = new Intent(HomeActivity.this,
ProfileActivity.class);
startActivity(profileActivity);
finish();
overridePendingTransition(R.anim.push_left_in,
R.anim.push_left_out);
}
else if(res_id == R.id.action_home)
{
}
else if(res_id == R.id.action_msg)
{
Intent cnectActivity = new Intent(HomeActivity.this,
ConnectionsActivity.class);
startActivity(cnectActivity);
finish();
overridePendingTransition(R.anim.push_left_in,
R.anim.push_left_out);
}
return true;
}
}
ProfileActivity java:
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
toolbar = (Toolbar) findViewById(R.id.tToolbar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_home,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
int res_id = item.getItemId();
if(res_id == R.id.action_profile)
{
}
else if(res_id == R.id.action_home)
{
Intent profileActivity = new Intent(ProfileActivity.this,
HomeActivity.class);
startActivity(profileActivity);
finish();
overridePendingTransition(R.anim.push_left_in,
R.anim.push_left_out);
}
else if(res_id == R.id.action_msg)
{
Intent cnectActivity = new Intent(ProfileActivity.this,
ConnectionsActivity.class);
startActivity(cnectActivity);
finish();
overridePendingTransition(R.anim.push_left_in,
R.anim.push_left_out);
}
return true;
}
}
toolbar xml:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/colorPrimary"
app:theme="#style/AppThemePenis"
android:elevation="4dp"
>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="P"/>
</android.support.v7.widget.Toolbar>
I'm using Android Studio.