I have created a button with a Gradient Drawable.
If you look at this button, it has those Extra Grey lines as pointed out by my red arrows. Those don't appear if I create a shape using XML but when i use
Gradient Drawable code seen below it shows these lines. How do I get rid of them??
GradientDrawable gd = new GradientDrawable();
gd.setColor(Color.parseColor("#FFFFFFFF"));
gd.setCornerRadius(20);
gd.setStroke(30, Color.parseColor("#0077CC"));
Button.setBackground(gd);
These become more apparent if i increase the setCorner Radius
Just add style to your button.
style="?android:attr/borderlessButtonStyle"
Related
Currently, I have this back arrow that I'm going to use in my toolbar
What I want is to fill the middle part which a specific color which is the middle part is a transparent part.
currently what i have is this :
Drawable backArrow = getResources().getDrawable(R.drawable.ic_back);
backArrow.setColorFilter(getResources().getColor(R.color.aub_red), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(backArrow);
it only changes the color of the white color part into the red color which is wrong. what I want to fill the transparent part a color not the non - transparent part.
Used this
Drawable backArrow = getResources().getDrawable(R.drawable.ic_back);
backArrow.setColorFilter(getResources().getColor(R.color.colorAccent), PorterDuff.Mode.LIGHTEN);
getSupportActionBar().setHomeAsUpIndicator(backArrow);
for more information click hear
you can use :
DrawableCompat.setTint(yourdrawable,getResources().getColor(R.color.primary));
in java code, or in xml tag :
android:backgroundTint="#color/primary"
I managed to have rounded edge for my button.
I also managed to have a dynamic background color (taken from a webservice).
The problem is when doing this :
btn.setBackgroundResource(R.drawable.radio_button_selector);
btn.setBackgroundColor(Color.parseColor(currentQuestion.backgroundColorButton));
One overrides the other, therefore I cannot have rounded edges AND dynamic background color.
I cannot use a dynamic color in the selector (as it's a static XML).
I cannot set the rounded edges programmatically (the method doesn't exists as far as I know).
How do I do ?
Use this
String backgroundColor= "#fc0000"; // set dynamic color here
btn.setBackgroundColor(Color.parseColor(backgroundColor));
another Examples:
GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.RECTANGLE);
drawable.setStroke(5, Color.MAGENTA);
drawable.setColor(Color.BLACK);
btnBlackColor.setBackgroundDrawable(drawable);
use this :-
final int color = Color.parseColor(homeCatPOJOS.get(position).getColor());
then implement it in background color :-
btn.setBackgroundResource(R.drawable.radio_button_selector);
btn.setBackgroundColor(color));
I think you are getting color code in String, first convert into int then implement it.
And for round edges make XML file for it and implement it statically.
I want to make FAB Button in triangular shape, But usually FAB is in circle shape. How will I achieve it.
Try like this:
take triangular shape Drawable and add it
app:backgroundTint should be "null"
I haven't try this yet, but this is my idea. What if you:
make transparent to background color of button using property
background or background-tint
add triangle image using property src
However, we are not suggested to change the shape of the button. Look this: Float Action Button
In my application I am using lot of buttons. And all buttons must be in same shape but different in color. I wrote drawable for each button with same features but different solid android:color. This makes more
than 20 drawable xml in my app.
How can I use the same drawable for all buttons, but should be able to
set different background color.
I tried as below, but did not work.
1. In attrs.xml
In drawable folder created buttonshape.xml for rectangle shape
In styles.xml
#drawable/buttonshape
#color/mycolor
In Colors.xml
#000000
And finally in my activity xml, for the button I set the theme as
<Button android:id="#+id/btnTest"
android:theme="#style/TestTheme" android:text="Test" />
This did not work for me. Could anyone of you please help me to sort this out? I am a new to android development. I am not able to figure out where I am going wrong. Any help would be really appreciated.
Thanks,
Roger
use this: GradientDrawable bgShape = (GradientDrawable)view.getBackground().getCurrent(); bgShape.setColor(Color.BLACK);
where ever you want to set different color of any view, just use this code.
.getCurrent gives the selected drawable layerlist
use this it will not throw java.lang.ClassCastException: android.graphics.drawable.GradientDrawable cannot be cast to android.graphics.drawable.ShapeDrawable exception.
it works perfectly for me.
I have an ImageButton and I'd like to remove the ugly (IMHO) background that surrounds the Image. I could just add an ImageView, but they're very hard to get set perfectly in a Layout like the grey one pictured. [gravity "Center" doesn't make it go to the middle, just centers it Vertically.)
So any way to remove that?
Just use android:background="#0000"
(#0000 same with #00000000)
or
ImageButton imageButton = new ImageButton(this);
imageButton.setBackgroundDrawable(null);
The default background is not transparent.
So, just add the transparent color "#00000000" as you background, then you could solve it.
p.s. #00000000 is the transparent color
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"
android:background="#00000000"
/>
You can keep the square box and just change its color like this:
android:backgroundTint="#color/colorPrimary"
works beautifully
I'm having the same problem but now it has been solved. If you are using "android:background="#null" it will resulted of no background at all and no button pressed animation will be appeared to the user.
Just adding this line of code to make an image button become flashing upon clicked. This is the proper way to alert the user an image button has been clicked.
"android:background="?android:selectableItemBackground"
just useandroid:background="#null" in your xml
if you set the
android:background
instead of
android:src
it should overwrite the gray background
Yeah. If you drag ImageButton that will usually ask for ImageSource. But the ImageSource(android:src) doesn't remove the gray background. It is an image that should be on top of background:
Instead of that try android:background in layout XML.
please use the Draw 9-patch format for the image b'use it is provided by SDK
1. From a terminal, launch the draw9patch application from your SDK /tools directory.
2. Drag your PNG image into the Draw 9-patch window (or File > Open 9-patch... to locate the file). Your workspace will now open.
The left pane is your drawing area, in which you can edit the lines for the stretchable patches and content area. The right pane is the preview area, where you can preview your graphic when stretched.
3. Click within the 1-pixel perimeter to draw the lines that define the stretchable patches and (optional) content area. Right-click (or hold Shift and click, on Mac) to erase previously drawn lines.
4. When done, select File > Save 9-patch...
Your image will be saved with the .9.png file name.
Use like this
android:background="#android:color/transparent"
android:src="#drawable/icon"
All proposed solutions remove the ripple animation (on tap)
Settings background tint mode to 'add' will remove the background and keep ripple animation:
android:backgroundTintMode="add"
If using Xamarin Forms it can be done like this in the xaml view. Set background to transparent.
<ImageButton BackgroundColor="Transparent"