I have set up a toolbar and added a home navigation button as follows;
Toolbar toolbar;
toolbar = (Toolbar) findViewById(R.id.toolbar_home);
//Setup toolbar
toolbar.setTitle("Home");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
toolbar.setTitleTextColor(getResources().getColor(R.color.icons,null));
}else{
toolbar.setTitleTextColor(getResources().getColor(R.color.icons));
}
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
} else {
Log.w("Home", "toolbar null");
}
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Navigate backwards as android back button
}
});
I want when i press the toolbar backbutton, it navigates backwards following the backstack as the the android backbutton.enter image description here
add this method in activity:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if(item.getItemId() == android.R.id.home){
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
I am following this from long time and not found any issue yet .
HomeFragment : First fragment which getting loaded first time
getFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
HomeFragment currentHomeFragment = null;
currentHomeFragment = (HomeFragment) getFragmentManager().findFragmentByTag("HOME_FRAGMENT");
if (currentHomeFragment != null && currentHomeFragment.isVisible()) {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
toggle.syncState();
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
drawer.openDrawer(GravityCompat.START);
}
});
} else {
if (getFragmentManager().getBackStackEntryCount() > 1) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true); // show back button
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getFragmentManager().popBackStackImmediate();
}
});
} else {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
toggle.syncState();
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
drawer.openDrawer(GravityCompat.START);
}
});
}
}
}
});
If you are using android.support.v7.widget.Toolbar add this code to your AppCompatActivity:
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
in your onClick write onBackPressed(); or in manifest
<activity android:name=".XYZActivity"
android:parentActivityName=".MainActivity" />
Setup toolbar in MainActivity onCreate() method.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_home);
toolbar.setTitle("Activity Name");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
Setup back navigation by implementing below method in MainActivity Level.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Works well..
#Rajesh
Related
I want my Home Icon in my Toolbar in my MainActivity to execute to go next page, but apparently, it is not working. Despite clicking the Home Icon in the Toolbar, it did not execute.
What is the correct way to correct the codes below
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setSupportActionBar(toolbar);
buttonTiaoLiWan = (ImageButton) findViewById(R.id.BtnTLW);
buttonTiaoLiWan.setOnClickListener(new View.OnClickListener() {
public void onClick (View v){
openTLWpage();
}
});
buttonZhenZhuWan = (ImageButton) findViewById(R.id.BtnZZW);
buttonZhenZhuWan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openZZWpage();
}
});
buttonBuYaoJing = (ImageButton) findViewById(R.id.BtnBYJ);
buttonBuYaoJing.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) { openBYJpage();}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_item,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch(item.getItemId()){
case R.id.action_home:
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I'm trying to add a BottomNavigationView in my project but don't show up when i run the project.
I'd like to put this on several activities, aswell as Toolbar so i created a class that initialize both of them and extends AppCompatActivity so that my activies using the Toolbar or the BottomNavigationView just have to extends this activity and call the method that initialize it (i don't know if this is the right method to use, if no please tell me). So this work with my Toolbar, but my BottomNavigationView isn't showing up.
This is my NavigationActivity i talked about :
public abstract class NavigationActivity extends AppCompatActivity {
private Toolbar mToolbar;
private BottomNavigationView mNavigationView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void initNavigation(int navigationId) {
mNavigationView = (BottomNavigationView) findViewById(navigationId);
mNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.navigation_entrainements:
startActivity(new Intent(getBaseContext(),MenuEntrainementsActivity.class));
break;
case R.id.navigation_nutrition:
startActivity(new Intent(getBaseContext(),NutritionActivity.class));
break;
case R.id.navigation_statistiques:
startActivity(new Intent(getBaseContext(),StatistiquesActivity.class));
break;
}
return true;
}
});
}
public void initToolbar(int toolbarId) {
mToolbar = (Toolbar) findViewById(toolbarId);
mToolbar.setNavigationIcon(R.drawable.baseline_arrow_back_black_18dp);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
mToolbar.setNavigationOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.btnHome :
startActivity(new Intent(this,MainActivity.class));
return true;
case R.id.btnProfil :
startActivity(new Intent(this,ProfilActivity.class));
return true;
default :
return super.onOptionsItemSelected(item);
}
}
}
and an exemple of how i'm using it in others activities :
initNavigation(R.id.navigation);
initToolbar(R.id.toolbar);
this is how i implement the BottomNavigationView in my XML files :
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:menu="#menu/navigation" />
if you need anything else just ask,
thanks for your help :)
(sorry if i made mistakes i'm not great in english)
I Use this from outside of onCreate mothod.
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment;
switch (item.getItemId()) {
case R.id.btnHome :
startActivity(new Intent(this,MainActivity.class));
return true;
case R.id.btnProfil :
startActivity(new Intent(this,ProfilActivity.class));
return true;
default :
startActivity(new Intent(this,MainActivity.class));
return true;
}
}
};
I am trying to debug an app built with fragments. Since I don't know nearly anything about fragments, I have some problems with them.
I want the action button to be transformed in back button when I open a fragment from MainActivity or another fragment.
I kinda made that work, but when I press back from a fragment and it resumes the other the back button transforms back to home button.
I want to know how I can make that home button behave like the back button on Android when it transforms to the back arrow. It does nothing currently.
Here is some code I think it's relevant, I will provide more if you need:
MainActivity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_trash_all:
DataHolder.cart = null;
isTrashButtonPresent = false;
supportInvalidateOptionsMenu();
inflateFragment(CartFragment.newInstance(null, null));
resolveCircularText(DataHolder.getOverallCartQuantity());
return true;
case R.id.btnMyMenu:
if (drawer.getDrawerLockMode(GravityCompat.START)
==DrawerLayout.LOCK_MODE_LOCKED_CLOSED) {
onBackPressed();
} else {
drawerToggle();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void drawerToggle() {
if (drawer.isDrawerVisible(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
drawer.openDrawer(GravityCompat.START);
}
}
private void setupNavigationDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(
this, drawer, toolbar,
R.string.navigation_drawer_open,R.string.navigation_drawer_close);
mDrawerToggle.setHomeAsUpIndicator(R.drawable.drawer_icon);
drawer.addDrawerListener(mDrawerToggle);
navigationView.setNavigationItemSelectedListener(this);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
setDrawerState(true);
}
public void setDrawerState(boolean isEnabled) {
if (isEnabled) {
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED,
GravityCompat.START);
mDrawerToggle.syncState();
} else {
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,
GravityCompat.START);
//Drawable backButton = ContextCompat.getDrawable(this,
R.mipmap.ic_back_button);
getSupportActionBar().setHomeAsUpIndicator(null);
}
}
Fragments:
OnCreate:
if (getActivity() != null) {
((MainActivity) getActivity()).setDrawerState(false);
}
#Override
public void onDestroy() {
super.onDestroy();
if (getActivity() != null) {
((MainActivity) getActivity()).setDrawerState(true);
}
}
Thank you in advance!
Try using Interface class:
public interface DrawerViewInterface {
void lockDrawer();
void unlockDrawer();
void showBackIcon();
void showHamburgerIcon();
}
Now, Implement this Interface class in your MainActivity:
public class MainActivity extends AppCompactActivity implements DrawerViewInterface {
#Override
public void lockDrawer() {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
#Override
public void unlockDrawer() {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}
#Override
public void showBackIcon() {
mActionBarDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public void showHamburgerIcon() {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
mActionBarDrawerToggle.setDrawerIndicatorEnabled(true);
}
}
Now, In your Fragment, call whatever you want by:
((DrawerViewInterface) getActivity()).showHamburgerIcon();
or
((DrawerViewInter) getActivity()).showBackIcon();
I've got a problem. I've got a hamburger icon and back arrow too. Hamburger is working perfectly, but not the back arrow. It's working like a hamburger, opening and closing drawer menu.
Here is a code:
#Override
public void onDrawerClosed(View drawerView) {
hideKeyboard();
super.onDrawerClosed(drawerView);
}
#Override
public void onDrawerOpened(View drawerView) {
hideKeyboard();
super.onDrawerOpened(drawerView);
}};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
pager.setVisibility(View.VISIBLE);
tabLayout.setVisibility(View.VISIBLE);
mDrawerToggle.setDrawerIndicatorEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle.syncState();
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbarInner);
toolbar.setNavigationOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
openAndCloseDrawer();
}
});
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
openAndCloseDrawer();
return true;
}
return false;
}
});
public void customizeActionBarWithBack(String string)
{
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbarInner);
toolbar.setTitleTextColor(getResources().getColor(R.color.md_white_1000));
toolbar.setTitle(string);
setDisplayHomeAsUpEnabled(true, R.drawable.abc_ic_ab_back_material);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
toolbar.setNavigationOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
customizeActionBar();
}
});
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch(item.getItemId()) {
case android.R.id.home:
recreate();
customizeActionBar();
return true;
}
return false;
}
});
}
Where is a problem? Please help me guys!
Override onBackPressed and call finish.
#Override
public void onBackPressed()
{
finish();
}
Try this
#Override
public void onBackPressed()
{
mDrawerLayout.closeDrawers();
}
Dawid, you can also do it like as you are implementing navigation drawer in fragment:
#Override
public void onBackPressed() {
int count = getFragmentManager().getBackStackEntryCount();
if (count == 0) {
super.onBackPressed();
//additional code
} else {
getFragmentManager().popBackStack();
}
}
and other option to communicate with activity using interface, implement interface in activity and define it in fragment then u can handle it by callback funtion
Inside my SettingsActivity extends AppCompatActivity I have SettingsFragment extends PreferenceFragment. When I try press home-buttion in toolbar it is not working. Here is my code
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/settingsToolbar"
android:layout_width="match_parent"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_height="?android:actionBarSize"
android:background="#color/colorPrimary" />
SettingsActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupToolBar();
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
private void setupToolBar() {
getLayoutInflater().inflate(R.layout.toolbar, (ViewGroup) findViewById(android.R.id.content));
Toolbar toolbar = (Toolbar) findViewById(R.id.settingsToolbar);
setSupportActionBar(toolbar);
setTitle(getResources().getString(R.string.settings));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setFocusable(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
I tried to solve it in such way: remove setNavigationOnClickListener and add this:
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return (super.onOptionsItemSelected(menuItem));
}
It does not help. How can I fix this?
add this line to config your getSupportActionBar:
getSupportActionBar().setHomeButtonEnabled(true);
use both code in your activity like below:
in your setupToolbar method:
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Log.d("cek", "home selected");
finish();
}
});
and in your onOptionsItemSelected method:
if (item.getItemId() == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);