How to use back button on toolbar with sliding navigation drawer - android

I have a Activity in which I have created NavigationDrawer functionality and rest I have fragments using this tutorial
My problem is I want that if I navigate using navigation drawer to all fragments except home like photos, notification, etc. It must show a back button instead of hamburger icon and also includes sliding navigation. I am not able to implement this.
Also want to change toolbar back button color. Please help.

to change back button color try this.
<style name="toolbar_theme" parent="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="colorControlNormal">#color/arrow_color</item>
</style>
or
<style name="CustomTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorControlNormal">#color/arrow_color</item>
</style>

You can do actionBarDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_new_icon); where toggle is your ActionBarDrawerToggle instance

To change Drawer icon to Back arrow use:
MenuFragment fragment = new MenuFragment();
//disable the toggle menu and show up carat
theDrawerToggle.setDrawerIndicatorEnabled(false); // add this
getSupportFragmentManager().beginTransaction().replace(R.id.frag_layout,fragment).addToBackStack(null).commit();
Add this code in your Drawer Item's Fragment Transaction without the Home Fragment
To revert back the change:
overwrite onBackPressed() in your Activity
#Override
public void onBackPressed() {
super.onBackPressed();
// turn on the Navigation Drawer image;
setDrawerIndicatorEnabled(true)
}

Related

Changing navigation back button icon dynamically doesn't work

I need to set navigation back button dynamically
when I do this `
viewBinding.toolbar.navigationIcon?.setColorFilter(Color.parseColor(theme.primaryTextColor), PorterDuff.Mode.MULTIPLY)
it doesn't effect changing color or if I do
viewBinding.toolbar.setNavigationIcon(R.drawable.ic_back) in MainActivity
where ic_back is white back button it doesn't work it shows back button
or if I add this app:navigationIcon="#drawable/ic_back" in xml it doesn't work
but when I set app:theme="#style/ToolbarColoredBackArrow"
<style name="ToolbarColoredBackArrow" parent="AppTheme">
<item name="android:textColorSecondary">#FFFFFF</item>
</style>
it works why? I should do this without using theme
If the toolbar is your support action bar, then this definitely works :
toolbar.setNavigationIcon(R.drawable.ic_back);
Or whatever you want to set the icon to, just that it has to be a drawable file.

Is it possible remove home button from actionbar

On my main activity, where there is no way to go back, I'd like to remove the app "home" button in the actionbar.
I think it is confusing for the user, who still can quit the app with the OS back button.
Browsing stackoverflow, I saw a whole lot of people asking this, and not a single answer worked for me. Here is the list :
Removing left arrow from the actionbar in android?
To remove the back arrow button from Action Bar
Remove default home/up button in action mode
how to hide up button in actionbar
How to remove v7 toolbar back button
Setting HomeAsUpEnabled(true) but hide back button actionbar
The answers were usually redondant and could be summarized to following :
ActionBar supportActionBar = getSupportActionBar(); //I target some low API
if(supportActionBar != null) {
supportActionBar.setDisplayShowHomeEnabled(false);
supportActionBar.setDisplayHomeAsUpEnabled(false);
supportActionBar.setHomeButtonEnabled(false);
supportActionBar.setHomeAsUpIndicator(null);
}
I tried every combination of those and nothing worked, whether I try with the default ActionBar or with an XML-declared compat-v7 ToolBar with setSupportActionBar().
I also couldn't see any question adapted for the now recommanded App Bar, and an answer with it would be even greater.
Here is my activity manifest :
<activity
android:name=".activity.WelcomeActivity"
android:logo="#drawable/ic_launcher"
android:label="#string/long_app_name"
android:launchMode="singleTop"/>
So, is it even possible to remove this annoying useless arrow nowadays ?
Create a new style in styles.xml:
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Set this style to your activity in Manifest file. To add the Toolbar, add this to your layout file:
<android.support.v7.widget.Toolbar
android:background="#color/colorPrimary"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.Toolbar>
and then in your Java class:
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
This will remove the top left arrow.
simply put getSupportActionBar().setDisplayHomeAsUpEnabled(false)in onCreate of the activity.

How to change the up navigation icon in android on searchview expand

i'm using actionbarsherlock for my actionbar. I have a sliding menu and searchview. when the application get launches, it will automatically pick the up navigation icon from style.xml file.
When the search menu gets expanded, the up navigation icon will not get changed, it will be still looking like 3 line slider menu. How to change the up navigation icon when searchview is expanded?. I have represented the flow in below image.
Create custom back button theme with parent of your custom theme.
<style name="MyThemeForSearch" parent="#style/MyCustomTheme">
<item name="android:homeAsUpIndicator">#drawable/my_back_indicator_for_search</item>
</style>
Set theme in runtime when Search is expanded., i.e onClick
setTheme(R.style.MyThemeForSearch);
check your layout drawable's whether you have set an image like 3 liner slide menu
when you create navigation drawer it automatically sets default image which will be loaded representing 3 liner slide like you mentioned change your ic_drawer.png image and replace image with what icon you wish.
also you might go check changing your theme
> <style name="Theme.MyFancyTheme" parent="android:Theme.Holo">
> <item name="android:homeAsUpIndicator">#drawable/my_fancy_up_indicator</item>
> </style>

Strange behavior of ActionBar home icon

I have 2 types of fragments between which I want to switch. Each time I create new instance of fragment I need.
In the first fragment I need to have clickable home icon so I set the appropriate (setDisplayShowHomeAsUpEnabled) display option to true and home icon starts look like an up. In the second one I don't need it so I set the appropriate display option to false and it stops looking as up. I'm listening to clicks on the action bar icon in parent activity.
The issue is that after second fragment is shown once the next time I show first fragment home icon remains clickable while it doesn't look like an up. The question is why is it clickable if currently visible fragment set it to not look like an up button?
I'm using appcompat-v7 library.
Please set the home buttom eanabled to false as well.
getSupportActionBar().setHomeButtonEnabled(false);
Lemme know if this works. :)
EDIT - One more way, but I am not sure on this.
You can set styles for your ActionBar title and over there you can set it as non-clickable as.
<item name="titleTextStyle">#style/TitleTextStyle</item>
and here is TitleTextStyle,
<!-- action bar title text -->
<style name="TitleTextStyle" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#color/actionbar_text</item>
<item name="android:clickable">false</item>
</style>
Remember to do this for support library as well. Try and see if it does the trick.

Android SlidingMenu not showing with SlidingFragmentActivity

Problem: My sliding menu only show empty view(with white background)
I use sliding menu which is widely used for facebook-like menu. (https://github.com/jfeinstein10/SlidingMenu)
But in my project, sliding menu is not showing any layout but only shows white background.
I've used sliding menu in my other projects before without any problem.
The big difference is I use actionbarsherlock in my new Project (And pager adapter).
Below is class definition of my Main activity:
public class MainActivity extends SlidingFragmentActivity {
And modify sliding menu libarary like below to support actionbar
public class SlidingFragmentActivity extends SherlockFragmentActivity implements SlidingActivityBase {
And setup sliding Menu as below in my main activity:
private void setupSlidingMenu(){
setBehindContentView(myMenu);
menu = getSlidingMenu();
menu.setMode(SlidingMenu.LEFT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
menu.setShadowWidthRes(R.dimen.shadow_width);
menu.setShadowDrawable(R.drawable.shadow);
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menu.setFadeDegree(0.35f);
}
I also test both very simple layout with one textview and simple Fragment but both trial shows same result.
Both my project and sliding menu library have dependency on ActionbarSherlock library.
Thanks for help in advance.
I solve my problem by myself.
Problem is that I use below theme in my main activity.
<style name="WhiteTheme.VPI" parent="Theme.Sherlock.Light">
<item name="vpiTabPageIndicatorStyle">#style/CustomTabPageIndicator</item>
<item name="actionBarStyle">#style/WhiteTheme.ActionBarStyle</item>
<item name="android:actionBarStyle">#style/WhiteTheme.ActionBarStyle</item>
<item name="android:background">#ffffff</item>
</style>
And <item name="android:background">#ffffff</item> make whole my sliding menu white and hiding every layout. Don't actually understand why this attribute hide everything.

Categories

Resources