I'm trying to make a button in Android that has a border but with a different background tint when it is pressed, and the ability to change the color of the button background. I know adding a border is assigning it a shape and that tap states are through a selector with different items, but the problem is that the button background color is meant to be user configured.
Without tap states, I am able to allow the user to change the background color of my shape by just doing:
GradientDrawable bgShape = (GradientDrawable) btn.getBackground();
//color value is obtained from shared preferences
if (sharedPref.contains(pref_color)) {
String color = sharedPref.getString(pref_color, "");
bgShape.setColor(Color.parseColor(color));
}
But I can't do the first line if my button is going to be assigned a selector. I don't know how I would get the reference to the drawable shape.
For reference, my button border shape is:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="5px" android:color="#ffffff" />
</shape>
The selector would look like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_press" android:state_pressed="true"/>
<item android:drawable="#drawable/button_border" />
</selector>
Is there a way to accomplish this?
you can define an id for drawable layer and then change their properties in runtime
LayerDrawable drawSettings = (LayerDrawable)
getResources().getDrawable(R.drawable.sample);
GradientDrawable backSettings = (GradientDrawable)
drawSettings.findDrawableByLayerId(R.id.backtemp);
and after change color reset it to button background
view.setBackground(drawable);
this is a sample drawable sample.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/backtemp">
<shape
android:shape="oval" >
<solid android:color="#color/default_color"/>
</shape>
</item>
<item
android:drawable="#drawable/w_pref">
</item>
</layer-list>
Related
How can I change the color of the card view elevation?
Or make it softer
It gets dark when I increase the app:cardElevation value
Add these 2 properties to your CardView
android:outlineAmbientShadowColor="#color/yourShadowColor"
android:outlineSpotShadowColor="#color/yourShadowColor"
here you can change color first you can make a drawable file and paste this code in it. then in your layout inside cardview, you can assign as background file.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background">
<shape>
<corners android:bottomLeftRadius="8dp"
android:topLeftRadius="5dp"
android:bottomRightRadius="8dp"
android:topRightRadius="5dp"/>
<solid android:color="#ddd"/>
</shape>
</item>
I have defined a custom background to RadioButton in my project so that I can change it in the java code. The background is a Layer-list that includes 2 items: XML drawable and image.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#color/colorAccent"/>
<size android:width="20dp" android:height="20dp"/>
<corners android:radius="3dp"/>
</shape>
</item>
<item
android:id="#+id/correct"
android:drawable="#drawable/correct_image"
android:height="15dp"
android:width="15dp"
android:gravity="center"/>
</layer-list>
The design will break because the height and width are not supported prior to API 23. Is there any way that I can set the size of the image programatically using dimension value?
I don't have any prior coding experience or knowledge, so any explanation provided with possible solution will be very helpful.
Thank you
Currently I have a action bar menu with few items inside.
I am trying to have a menu item with shape which has a drawable
The code i get so far:
ShapeDrawable circle = new ShapeDrawable(new OvalShape());
circle.getPaint().setColor(Color.GREEN);
circle.setIntrinsicHeight(120);
circle.setIntrinsicWidth(120);
circle.setBounds(0, 0, 120, 120);
menu.findItem(R.id.menu_item_1).setIcon(circle);
However currently only looks like a green circle, I want inside to have a drawable icon.
for example
just create icon.xml in drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid
android:color="#color/colorPrimaryDark"/>
<size
android:width="70dp"
android:height="70dp"/>
</shape>
</item>
<item android:drawable="#android:drawable/ic_dialog_map" android:gravity="center">
</item>
</layer-list>
and use it like this:
menu.findItem(R.id.menu_item_1).setIcon(ContextCompat.getDrawable(this, R.drawable.icon));
This is how I go about adding a border to a EditText. How can I go about adding a border only on one side of a EditText, and define the color and width of the border?
EditText editText = new EditText(this);
editText.setText("Find");
editText.setWidth(555);
GradientDrawable border = new GradientDrawable();
border.setColor(0xFFFFFFFF); // white background
border.setStroke(1, 0xFF000000); // black border with full
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
editText.setBackgroundDrawable(border);
} else {
editText.setBackground(border);
}
Vielen dank im voraus.
To get border on one side, you can create your own drawable like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FF0000" />
</shape>
</item>
<item android:right="5dp">
<shape android:shape="rectangle">
<solid android:color="#FFFF" />
</shape>
</item>
</layer-list>
And set this drawable as background to your EditText.
If you want to add border only one side of EditText then you should use View tag in your layout file to draw a simple line and place it near your EditText and use background property to set color of line.
To draw horizontal line use this:
<View
android:layout_width="match_parent"
android:background="#color/colorPrimary"
android:layout_height="2dp" />
I''m using a drawable for the cells in my table layout in my Android application. I wanna change the cell colors on click event. Is it possible to change the color of the drawable in run time. my drawable is,
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape= "rectangle" >
<solid android:color="#FFFFFF"/>
<stroke android:width="1dp" android:color="#483D8B"/>
</shape>
And I wanna change the white color "#FFFFFF" with the click event. can java edits the color in the XML drawable file. Is it possible.
I just edited the background color and it dispersers the borders of the drawable.
String StatusColor=GetColor(Retailer_x.getStatus());
Stts.setBackgroundColor(Color.parseColor(StatusColor));
need help. Thank you!!!!
ShapeDrawable sd1 = new ShapeDrawable(new RectShape());
sd1.getPaint().setColor(CommonUtilities.color);
sd1.getPaint().setStyle(Style.STROKE);
sd1.getPaint().setStrokeWidth(CommonUtilities.stroke);
sd1.setPadding(15, 10, 15, 10);
sd1.getPaint().setPathEffect(
new CornerPathEffect(CommonUtilities.corner));
ln_back.setBackgroundDrawable(sd1);
Use selector of the Table Cell background to change state of the table cell when user click it.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- selected -->
<item android:drawable="#drawable/red_drawable" android:state_selected="true"/>
<!-- focused -->
<item android:drawable="#drawable/gray_drawable"/>
<!-- default -->
</selector>