Android lollipop Menu button to Back button rendering - android

If you see the Google Play app , when swiping the menu the menu button convert itself to back button with the finger gradually and nicely, it seems not an animation. can any one help me to achieve this thing ?
Feel free to move/close the post but please be nice to redirect me in any helpful direction.

Icon transitions can be implemented using AnimatedStateListDrawable (There’s also support for animated vector icons.)
e.g.
public void setOrAnimatePlusCheckIcon(final ImageView imageView, boolean isCheck,
boolean allowAnimate) {
if (!hasL()) {
compatSetOrAnimatePlusCheckIcon(imageView, isCheck, allowAnimate);
return;
}
Drawable drawable = imageView.getDrawable();
if (!(drawable instanceof AnimatedStateListDrawable)) {
drawable = mActivity.getResources().getDrawable(R.drawable.add_schedule_fab_icon_anim);
imageView.setImageDrawable(drawable);
}
imageView.setColorFilter(isCheck ?
mActivity.getResources().getColor(R.color.theme_accent_1) : Color.WHITE);
if (allowAnimate) {
imageView.setImageState(isCheck ? STATE_UNCHECKED : STATE_CHECKED, false);
drawable.jumpToCurrentState();
imageView.setImageState(isCheck ? STATE_CHECKED : STATE_UNCHECKED, false);
} else {
imageView.setImageState(isCheck ? STATE_CHECKED : STATE_UNCHECKED, false);
drawable.jumpToCurrentState();
}
}
code from https://github.com/google/iosched

Related

Fiiling an image with color in android

how to fill an image with a color in android? Like the image given below
You can set color to the icon by using the below code
ImageView imageView = (ImageView) findViewById(R.id.your_imageview);
imageView.setColorFilter(Color.RED);
Option 1: use backgroundTint property in xml file of ImageView
Option 2:
ImageView imageView = ...;
Drawable drawable = imageView.getDrawable();
ColorFilter colorFilter = ColorFilterGenerator.from(drawable).to(Color.RED);
imageView.setColorFilter(colorFilter);
Since,Android Drawable Tinting is supported in Android 5.0+ (API 21+) only. (not 100% sure ).
u can provide two different images in drawable folder and set it problematically when user clicks on it which will work in every android version.
public boolean enable = false;
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(enable)
{
imageView.setImageResource(R.drawable.ic_home_enable);
}
else
{
imageView.setImageResource(R.drawable.ic_home_disable);
}
}
});

Android animate toolbar icon while changing its image

in my app i need to change the image of a toolbar button programmatically, and for so I´m using:
public boolean onCreateOptionsMenu( Menu menu ) {
getMenuInflater().inflate(R.menu.main_activity_menu, menu);
toolbarButton = menu.findItem(R.id.action_button);
return true;
}
toolbarButton.setIcon(R.drawable.ic_settings);
And it works perfectly, the problem is that it happens too fast, so i´d need to add an animation to the transition, but I don´t know how to add an animation to a toolbar button.
Could somebody help me please?
Thanks a lot!
Okay, so finally thanks to Victorldavila i managed to get it working, this is my final code:
View button = toolbar.findViewById(R.id.action_button);
//Remove icon animation
if (button != null) {
Animation animation = AnimationUtils.loadAnimation(getBaseContext(), R.anim.fragment_fade_out);
animation.setStartOffset(0);
button.startAnimation(animation);
}
//Add icon animation
if (button != null) {
Animation animation = AnimationUtils.loadAnimation(getBaseContext(), R.anim.fragment_slide_in_up);
animation.setStartOffset(0);
button.startAnimation(animation);
}
Maybe these answers can help you:
https://stackoverflow.com/a/39182291/2759449
https://stackoverflow.com/a/30787535/2759449

Android L, ripple effect not working properly in Gridview

I'm using sdk 21 and added buttons to a gridview. However, somehow, only the first button is showing ripple effect when clicked and others not showing...
Edit: I found if I didn't add onClick listener to the buttons, the ripple effect is showing, but after I add the listener, only the first one is showing ripple effect...
Edit: Well, after further investigation, I found the effect is gone after I call setCompoundDrawablesWithIntrinsicBounds()
I got the resourceId by following:
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.hashtag_button);
mSelectedResourceId = a.getResourceId(R.styleable.hashtag_button_selected_image, R.drawable.star_selected);
mUnselectedResourceId = a.getResourceId(R.styleable.hashtag_button_unselected_image, R.drawable.star_unselected);
a.recycle();
I change image by following method:
public void setFavorite(boolean isFavorite) {
mIsFavorite = isFavorite;
if (mIsFavorite) { //Favorite
this.setCompoundDrawablesWithIntrinsicBounds(0, 0, mSelectedResourceId, 0);
} else { //Not Favorite
this.setCompoundDrawablesWithIntrinsicBounds(0, 0, mUnselectedResourceId, 0);
}
}
So this caused only first button has ripple effect... Not sure how to solve this...
Anyone has same issue?
Thanks

Android View.getBackgroundColor #XXXXXXXX

The background color of my View is #FFFFFFFF. I want to get this through code. I do not want to just put #FFFFFFFF into the method because I will be changing the background through code, so this value will change all the time.
public void toggleEraser() {
ImageView btnEraser = (ImageView) this.findViewById(R.id.imgEraser);
ImageView btnBrush = (ImageView) this.findViewById(R.id.imgBrush);
if (erase) {
btnEraser.setImageResource(R.drawable.greyeraser);
btnBrush.setImageResource(R.drawable.brush);
} else {
btnEraser.setImageResource(R.drawable.eraser);
btnBrush.setImageResource(R.drawable.greybrush);
}
erase = !erase;
if (erase){
//Here is the problem
drawView.setColor(//drawView.getBackgroundColor());
}
else
drawView.setColor(brushColor);
drawView.setErase(erase);
}
store the color you want 0xFFFFFFFF in somevariable and then
change drawView.setColor( to drawView.setBackgroundColor(somevariable);
Check out this answer: Get the background color of a button in android. it is a little different but can lead you on the right path.

how to get white background theme for android options menu?

In my android application I would like my options menu to have a white background so that my icons show up better, I have seen many apps that had this but I am unable to figure out how to get this done.
The answers int his link will probably help you. This site gives you answer in many ways, for example using android:state_pressed=true and other options too(selected, focused) in your menu item will show your background of your item in white color.
How to change the background color of the options menu?
I'm not a fan of the standard options menu at all that's why I ALWAYS create a customized menu with ViewStubs. Just create a new layout where you design your menu bar, integrate it with ViewStub in your layout files and let the menu slide in.
Sample java code:
public boolean onKeyDown( int keyCode, KeyEvent event ) {
switch (keyCode) {
case KeyEvent.KEYCODE_MENU:
mMenuPanel = ( ( ViewStub ) findViewById( R.id.stub_onoption ) ).inflate();
// initialize buttons of your menu layout and define setOnClickListener()
if( !menuVisible ) {
constants.showPanel( this, mMenuPanel, true );
menuVisible = true;
} else {
constants.hidePanel( this, mMenuPanel, true );
menuVisible = false;
}
return true;
default:
break;
}
}
public static void hidePanel( Context context, View panel, boolean slideDown ) {
panel.startAnimation( AnimationUtils.loadAnimation( context, slideDown ? R.anim.slide_out : R.anim.slide_in_top ) );
panel.setVisibility( View.GONE );
}
public static void showPanel( Context context, View panel, boolean slideUp ) {
panel.startAnimation( AnimationUtils.loadAnimation( context, slideUp ? R.anim.slide_in : R.anim.slide_out_top ) );
panel.setVisibility( View.VISIBLE );
}
This way you'll be able to fully customize your menu bar (background buttons etc.)
Edit: This is just the rough idea how to do it. And if you're doing it for the first time it might be a little bit overweight just for changing the background color but You'll be able to use this concept later on in various occasions like different slide in effects, adjusting the menu design according to your application design, change location, size and many more things.
Furthermore this kind of concept can also be used for optional search bars, or in-app notifications (if you don't want to use a dialog). So it's definitely worth looking into it.

Categories

Resources