I would like to help me customize my actionbar, I am using a navigation drawer and would like to have the option when you open the action bar is dark.
I also would like to know how to customize a imageview to take a photo and move the name to the right and playstore.
Thank you for your help.
EDIT
I solved
actionBar = getActionBar();
toggle = new ActionBarDrawerToggle(this, drawerMenu, R.drawable.ic_drawer, R.string.texto_menu_opciones, R.string.texto_lineas_transporte){
#Override
public void onDrawerClosed(View drawerView) {
getActionBar().setTitle(listaOpciones.get(position).getNombre());
getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.ab_solid_cvbus));
invalidateOptionsMenu();
}
#Override
public void onDrawerOpened(View drawerView) {
//getActionBar().setTitle("Opciones");
getActionBar().setTitle(listaOpciones.get(position).getNombre());
getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.ab_transparent_cvbus));
invalidateOptionsMenu();
}
#Override
public void onDrawerSlide(View drawerView, float slideOffset)
{
super.onDrawerSlide(drawerView, slideOffset);
}
};
you can put this code for changing the actionbar color
ActionBar ac= getActionBar();
ac.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#000000"))); // #000000 - for black
put this code in the drawer opening method..
Related
I have an Actionbar from android.support.v7.widget.Toolbar. It has hamburger image with animation to back arrow, I want to change back arrow from <- to ->. how can I do this in Android Studio?
I read somewhere to change it with setHomeAsUpIndicator() method, but it change hamburger button and it has no animation to back arrow.
There are at least half a dozen ways to do this, but probably the simplest and shortest is to use reflection to grab the ActionBarDrawerToggle's Drawable, and flip its direction.
This example wraps that functionality in a subclass, and should work no matter your ActionBar/Activity setup (provided the original class worked there in the first place).
public class FlippedDrawerToggle extends ActionBarDrawerToggle {
public FlippedDrawerToggle(Activity activity, DrawerLayout drawerLayout,
int openDrawerContentDescRes, int closeDrawerContentDescRes) {
this(activity, drawerLayout, null,
openDrawerContentDescRes, closeDrawerContentDescRes);
}
public FlippedDrawerToggle(Activity activity, DrawerLayout drawerLayout,
Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes) {
super(activity, drawerLayout, toolbar,
openDrawerContentDescRes, closeDrawerContentDescRes);
try {
Field sliderField = ActionBarDrawerToggle.class.getDeclaredField("mSlider");
sliderField.setAccessible(true);
DrawerArrowDrawable arrow = (DrawerArrowDrawable) sliderField.get(this);
arrow.setDirection(DrawerArrowDrawable.ARROW_DIRECTION_RIGHT);
}
catch (NoSuchFieldException | IllegalAccessException e) {
// Fail silently
}
}
}
This will only change the direction of the toggle's image. If you actually want the whole ActionBar/Toolbar to be flipped, you should instead change the layout's direction accordingly.
Try this:
//Find the action bar for customizations
final ActionBar ab = getSupportActionBar();
assert ab != null;
//Make the action bar display an up arrow and set its drawable and color
ab.setDisplayHomeAsUpEnabled(true);
final Drawable upArrow = ResourcesCompat.getDrawable(
getResources(),
R.drawable.abc_ic_ab_back_mtrl_am_alpha, //this is the <- arrow from android resources. Change this to the thing you want.
null);
assert upArrow != null;
upArrow.setColorFilter(
ContextCompat.getColor(
getApplicationContext(),
R.color.white // change this to the color you want (or remove the setColorFilter call)
),
PorterDuff.Mode.SRC_ATOP);
ab.setHomeAsUpIndicator(upArrow);
Add this condition between mDrawerToggle = new ActionBarDrawerToggle... and mDrawerToggle.syncState(); like this :
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (getResources().getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL)
{
toolbar.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}
mDrawerToggle.syncState();
I've been looking a solution for this quite some time. So this is what I'm trying to achieve. I have a NavigationDrawer implemented with the following menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_dashboard"
android:icon="#drawable/ic_dashboard"
android:title="Dashboard" />
<item
android:id="#+id/nav_logbook"
android:icon="#drawable/ic_logbook"
android:title="Logbook" />
<item
android:id="#+id/nav_context"
android:icon="#drawable/ic_context"
android:title="Context" />
</group>
</menu>
When there's new context to be added, I want to show this to the user in the navigation drawer by adding the amount of new updates between brackets. Like this for example: Context (2) when there are two new updates.
I want to check this every time when the Drawer opens, but preferably only once, because there's a pretty heavy SQL query behind it. I know I can use the onDrawerOpened() and onDrawerClosed(), but I don't like this solution because of the fact the number will be updated suddenly only when the drawer is completely open. So it will stay one number, until it's completely open, by which it then will change. This just looks silly. So that's why I would want to change it once when the navigation drawer starts opening. I found no decent states to check this with the listener, since there is only STATE_IDLE, STATE_DRAGGING or STATE_SETTLING and nothing specifing for when it's opening and closing, just dragging and settling.
Additionally I also would like to keep the title of the fragment Context to stay "Context" and not also change to "Context (2)". Which happens in my current code:
mToggle = new ActionBarDrawerToggle(
this,
mDrawer,
mToolbar,
R.string.drawer_open,
R.string.drawer_close
){
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
if (slideOffset != 0) {
Log.i(TAG, "onDrawerSlide() in update amount");
int amount = db.getAmountNoContext();
String sourcestring = "Context <b>(" + amount + ")</b>";
mNavigationView.getMenu().getItem(2).setTitle(Html.fromHtml(sourcestring));
}
}
};
This is working for changing the menu title in the Navigation drawer, but it also gets called many times in a row, because the offset keeps changing. And this is because of the SQL query not ideal. I also tried to set it to a specific float value like 0.01, but it's not getting triggered. Also I would like to remind that I would like to change the menu title back when the navigation drawer is closed, so the fragment title in the ActionBar just says "Context". I've done my research, but it seems like I just have a really specific problem? Maybe one of you guys have an idea. Thanks in advance.
I "solved" my problem for now by simply using a boolean to keep track if the SQL query is already exectued. If so the boolean is set to true and the condition will not be entered again until the drawer is completely closed by using the onDrawerClosed() function. I'll do it like this until maybe someone else comes up with a better solution. My solution looks now like this:
mToggle = new ActionBarDrawerToggle(
this,
mDrawer,
mToolbar,
R.string.drawer_open,
R.string.drawer_close
){
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
if (slideOffset != 0 && !contextUpdated) {
int amount = db.getAmountNoContext();
String sourcestring = "Context (" + amount + ")";
NavigationView.getMenu().getItem(2).setTitle(Html.fromHtml(sourcestring));
contextUpdated = true;
}
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
contextUpdated = false;
}
};
For changing the title of the action bar back to the regular title without brackets. I just manually set the title in the onCreateView() of the fragment by doing this:
MainActivity:
public void setActionBarTitle(String title) {
getSupportActionBar().setTitle(title);
}
Fragment:
((MainActivity) getActivity()).setActionBarTitle("Context");
i am trying to set status bar transparent on my content frame inside navigation drawer. I create the app using the template contain on android studio. I need make status bar transparent when navigation drawer is not open so my toolbar back ground is draw behind it.
This one:
This One What I mean
Not this one, because it already transparent:
Not this one
I actually had an issue related to this due to some custom rendering with parallax effects... Anyway, you can just set the status bar background color in onDrawerClosed(View drawerView) and then set it back to transparent when opening the drawer:
public class WindowHelper {
public static void setColoredStatusBar(Activity activity){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
Window window = activity.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(activity.getResources().getColor(R.color.secondary_color));
}
}
public static void setTranslucentStatusBar(Activity activity){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
Window window = activity.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
}
So for you it'd something like:
#Override
public void onDrawerClosed(View drawerView) {
WindowHelper.setColoredStatusBar(activity);
}
I've created a drawer using a DrawerLayout which contains a RecyclerView with the items. I've also attached a layoutAnimation to the RecyclerView to have the items come in from the side when opening the drawer. This works peachy the first time but when opening the drawer the second time everything is already in place. I would like the layoutAnimation to run every time the drawer is opened.
What I've tried so far is to have a custom ActionBarDrawerToggle (I need that one anyway), and add the following:
#Override
public void onDrawerOpened(final View drawerView) {
super.onDrawerOpened(drawerView);
final RecyclerView recyclerView =
(RecyclerView) drawerView.findViewById(R.id.drawer_content);
if (recyclerView != null) {
recyclerView.startLayoutAnimation();
}
}
It works sort of, because it re-runs the animation, however all the items are there when opening the drawer, then they disappear and then the animations starts.
Anyone have a solution how to "reset" the drawer item views every time the drawer is closed?
Not sure these are needed but I'll include them anyway
<--! layout_animation.xml -->
<layoutAnimation
xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="#anim/slide_from_right"
android:delay="15%"
android:animationOrder="normal"
/>
<--! slide_from_right.xml -->
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="100%p"
android:interpolator="#android:anim/decelerate_interpolator"
android:toXDelta="0"
/>
I found the solution after some more testing, perhaps not the prettiest solution but it works. By hiding the content when the drawer is closed and then making it visible again just before starting the animation solves the issue I was having:
private boolean mFirstDrawerOpen = true;
private boolean mAnimationScheduled;
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
// The framework handles the first animation
if (mIsFirstDrawerOpen) {
mIsFirstDrawerOpen = false;
return;
}
final RecyclerView recyclerView =
(RecyclerView) drawerView.findViewById(R.id.drawer_content);
if (mAnimationScheduled && recyclerView != null) {
recyclerView.setVisibility(View.VISIBLE);
recyclerView.startLayoutAnimation();
mAnimationScheduled = false;
} else if (slideOffset == 0f) {
// Handles the case when the drawer is not completly opened and then closed,
// which does not trigger onDrawerClosed()
mAnimationScheduled = true;
}
}
#Override
public void onDrawerOpened(final View drawerView) {
super.onDrawerOpened(drawerView);
mAnimationScheduled = false;
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
mAnimationScheduled = true;
final RecyclerView recyclerView =
(RecyclerView) drawerView.findViewById(R.id.drawer_content);
if (recyclerView != null) {
recyclerView.setVisibility(View.INVISIBLE);
}
}
Update:
The previous answer did not handle the case where the drawer is dragged halfway open and then closed, since onDrawerClosed is not called if the drawer haven't been fully opened. To solve that, I moved most of the code from onDrawerOpen to onDrawerSlide() and modify it a bit.
I was having the same problem as #patrick-iv and was wondering how other people solved it. I came up with adding the below code to the onDrawerStateChanged listener.
boolean drawerOpen = drawer.isDrawerOpen(GravityCompat.START);
_drawerTopMenu.setVisibility(drawerOpen ? View.VISIBLE : View.INVISIBLE);
if (newState == DrawerLayout.STATE_SETTLING && !drawerOpen)
_drawerTopMenu.startLayoutAnimation();
For my app I'm using a menu that is a NavigationDrawer and it's working perfectly. At this moment I have created my custom layout of action bar, just with the propose to have an icon centered in actionbar.
The problem is that apparently in some devices the NavigationDrawer's icon isn't showing with my custom ActionBar layout, but it's showing if I don't apply my custom ActionBar layout.
On my android 4.4.2 it's working fine. I've tried on 4.1.2 and on 4.3 and the icon not showing.
Here is my ActionBar layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ActionBarWrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/actionbar_centered_icon"
android:layout_width="150dp"
android:layout_height="wrap_content" />
</RelativeLayout>
Here you have the lines on my Activity that set my custom layout for the action bar and also the code that implements the navigationDrawer:
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getActionBar().setCustomView(R.layout.actionbar_centered_icon);
ImageView actionBarCenteredIcon = (ImageView) findViewById(R.id.actionbar_centered_icon);
actionBarCenteredIcon.setImageResource(R.drawable.vidytape_text);
// Hide App icon
// getActionBar().setDisplayHomeAsUpEnabled(false);
getActionBar().setDisplayHomeAsUpEnabled(true);
// Enable user to open menu from the button on action bar
getActionBar().setHomeButtonEnabled(true);
// Remove app title from action bar
getActionBar().setDisplayShowTitleEnabled(false);
// Set menu ic drawer as icon
/*
* if (lang.equalsIgnoreCase("deutsch")){
* getActionBar().setIcon(R.drawable.label_home_de); } else {
*/
getActionBar().setIcon(R.drawable.vidytape_text);
// }
ColorDrawable actionBarColor = new ColorDrawable(
Color.parseColor("#3498db"));
getActionBar().setBackgroundDrawable(actionBarColor);
// Menu behavior -- Toggle
mDrawerToggle = new ActionBarDrawerToggle((Activity) this,
mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
//Close keyboard
viewsSearchField = (EditText) findViewById(R.id.views_search);
nicesSearchField = (EditText) findViewById(R.id.nices_search);
newestSearchField = (EditText) findViewById(R.id.newest_search);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(viewsSearchField.getWindowToken(), 0);
imm.hideSoftInputFromWindow(nicesSearchField.getWindowToken(), 0);
imm.hideSoftInputFromWindow(newestSearchField.getWindowToken(), 0);
}
}; // End of menu behavior
mDrawerLayout.setDrawerListener(mDrawerToggle);
Anyone know what can be the problem here?
I FOUND THE SOLUTION
To make it work on the SDKs < 19 and on 19 too, I added the following two lines of code:
getActionBar().setDisplayShowHomeEnabled(true);
ColorDrawable actionBarIconColor = new ColorDrawable(Color.TRANSPARENT);
getActionBar().setIcon(actionBarIconColor);