I am trying to change the RadioButton color I have. I programmatically add RadioButtons, however after researching I am unable to change the color to a solid black border with a black filling when clicked. Here is my code where I add the buttons, I understand there may be considerations for both API Levels < 21 and > 21:
public void addRadioButtonsWithFirebaseAnswers(DataSnapshot dataSnapshot, int numberOfAnswers) {
mPollAnswerArrayList = new ArrayList<RadioButton>();
for (int i = 0; i < numberOfAnswers; i++) {
Log.e("Number of Answers", "The number of answers is " + numberOfAnswers);
mPollAnswerArrayList.add(i, new RadioButton(getActivity().getApplicationContext()));
mPollAnswerArrayList.get(i).setId(i);
String firebaseChild = String.valueOf(i + 1);
mPollAnswerArrayList.get(i).setText(dataSnapshot.child(POLL_ANSWERS_LABEL).child(firebaseChild).child("Answer").getValue().toString());
mPollAnswerArrayList.get(i).setTextColor(getResources().getColor(R.color.black));
mPollAnswerArrayList.get(i).setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.radio_button_answer_text_size));
mParams.setMargins((int) getResources().getDimension(R.dimen.radio_question_margin_left), 0, 0, (int) getResources().getDimension(R.dimen.radio_question_margin_bottom));
mPollQuestionRadioGroup.addView(mPollAnswerArrayList.get(i), mParams);
}
}
You can change the color of selected radio button using selector
selector_radio_btn/xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:drawable="#color/checkbox_active" />
<item android:state_checked="false" android:drawable="#color/checkbox_inactive" />
</selector>
checkbox_inactive.xml
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid
android:color="#android:color/transparent" />
<stroke
android:width="1dp"
android:color="#53aade" />
</shape>
checkbox_activi.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval" >
<solid android:color="#android:color/transparent" />
<stroke
android:width="1dp"
android:color="#53aade" />
</shape>
</item>
<item left="5dp" right="5dp" top="5dp" bottom="5dp">
<shape android:shape="oval" >
<solid android:color="#53aade" />
</shape>
</item>
</layer-list>
set selector_radio_btn.xml in radiobutton
mPollAnswerArrayList.get(i).setButtonDrawable(getResources().getDrawable(R.drawable.selector_radio_btn));
To address API < 21 and API > 21, I have done the following:
if (Build.VERSION.SDK_INT >= 21) {
mPollAnswerArrayList.get(indexCreated).setButtonTintMode(PorterDuff.Mode.DARKEN);
} else {
mPollAnswerArrayList.get(indexCreated).setButtonDrawable(R.drawable.black_ring);
}
I have also added the drawable as below:
black_ring.xml in my drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadiusRatio="3"
android:shape="ring"
android:thickness="3dp"
android:useLevel="false" >
<solid android:color="#color/black" />
<size
android:height="25dp"
android:width="25dp" />
</shape>
Related
I'm trying to change the background color of a button I made to a certain color when toggled and back when untoggled, but when I do the color changes the shape of the button.
Here is my code:
public void onClick(View v){
if(index < 9)
GameEngine.getInstance().setNumber(number);
else if(index == 9)
GameEngine.getInstance().setNumber(number);
else if (index == 12) {
GameEngine.getInstance().draftModeSetter();
v.setBackgroundColor(Color.parseColor("#ffb6c1"));
}
}
And here is the result before color change and the result after color change. You can clearly see the "draft" button gets becomes bigger and a sharper square.
If the background color was set using
btn.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);
it can be reset using:
btn.getBackground().clearColorFilter();
I think you should use xml : shape, selector like :
shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="5dp" />
<solid android:color="#color/blue_400" />
<stroke
android:width="1dp"
android:color="#color/blue_400" />
</shape>
selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/shape_comm_bt_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/shape_comm_bt_normal"
android:state_pressed="false" />
</selector>
use
<Button
...
android:background="#drawable/selector_common_bt"
...
/>
I have a selector for a toggle button checked and unchecked - is there a way that I can use a custom layer list with a shape and use different colours? I am adding them in at runtime but using XML as the design.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/toggle_custom"/>
<item android:state_checked="false" android:drawable="#drawable/toggle_custom_off"/>
</selector>
And my toggle_custom and toggle_custom_off
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/mainColourOn">
<shape android:shape="rectangle" />
</item>
<item android:bottom="10dp">
<shape android:shape="rectangle">
<solid android:color="#E6FFFFFF"/>
</shape>
</item>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/mainColourOff">
<shape android:shape="rectangle"/>
</item>
<item android:bottom="10dp">
<shape android:shape="rectangle">
<solid android:color="#FFF"/>
</shape>
</item>
LayerDrawable layerOn = (LayerDrawable) getResources().getDrawable(R.drawable.toggle_custom, getTheme());
LayerDrawable layerOff = (LayerDrawable) getResources().getDrawable(R.drawable.toggle_custom_off, getTheme());
GradientDrawable toggleOn = (GradientDrawable) layerOn.findDrawableByLayerId(R.id.mainColourOn);
GradientDrawable toggleOff = (GradientDrawable) layerOff.findDrawableByLayerId(R.id.mainColourOff);
int colour = persons.get(i).getColour();
toggleOn.mutate();
toggleOff.mutate();
toggleOn.setColor(colour);
toggleOff.setColor(colour);
So for example have one toggle that is using the colour red and another using blue using the same XML. Thanks
You can reuse the xml for the layer-list Drawable and create the StateListDrawable for each ToggleButton programmatically like this:
void setToggleButtonColor(ToggleButton tButton, int colour)
{
LayerDrawable layerOn = (LayerDrawable) ContextCompat.getDrawable(this, R.drawable.toggle_custom);
layerOn.mutate();
LayerDrawable layerOff = (LayerDrawable) ContextCompat.getDrawable(this, R.drawable.toggle_custom_off);
layerOff.mutate();
Drawable toggleOn = layerOn.findDrawableByLayerId(R.id.mainColourOn);
Drawable toggleOff = layerOff.findDrawableByLayerId(R.id.mainColourOff);
toggleOn.setColorFilter(colour, PorterDuff.Mode.MULTIPLY);
toggleOff.setColorFilter(colour, PorterDuff.Mode.MULTIPLY);
StateListDrawable tbBackground = new StateListDrawable();
tbBackground.addState(new int[]{android.R.attr.state_checked}, layerOn );
tbBackground.addState(StateSet.WILD_CARD, layerOff);
tButton.setBackgroundDrawable(tbBackground);
}
Let's test it with two ToggleButtons:
ToggleButton toggleButton1 = (ToggleButton) findViewById(R.id.togglebutton1);
toggleButton1.setChecked(true);
ToggleButton toggleButton2 = (ToggleButton) findViewById(R.id.togglebutton2);
toggleButton2.setChecked(true);
setToggleButtonColor(toggleButton1, ContextCompat.getColor(this, R.color.blue));
setToggleButtonColor(toggleButton2, ContextCompat.getColor(this, R.color.magenta));
Here is my confirm__button.xml
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ConfirmButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/buttonConfirm" >
</Button>
here is my styles.xml
<resources>
<style name="buttonConfirm" parent="#android:style/Widget.Button">
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">15dip</item>
<item name="android:background">#drawable/ok_button_style</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
</style>
</resources>
here is my ok_button_style.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="#F6974F"
android:centerColor="#F58A39"
android:endColor="#F48028"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#F37C22" />
<corners
android:radius="6dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item android:state_focused="true" >
<shape>
<gradient
android:startColor="#F6974F"
android:centerColor="#F58A39"
android:endColor="#F48028"
android:angle="270" />
<stroke
android:width="2dp"
android:color="#F48028" />
<corners
android:radius="6dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item android:state_enabled="true" >
<shape>
<gradient
android:startColor="#F48028"
android:centerColor="#F58A39"
android:endColor="#F6974F"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#F5812A" />
<corners
android:radius="6dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item android:state_enabled="false" >
<shape>
<solid
android:color="#FFB380" />
<stroke
android:width="1dp"
android:color="#FFA366" />
<corners
android:radius="6dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#F48028"
android:centerColor="#F58A39"
android:endColor="#F6974F"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#F37C22" />
<corners
android:radius="6dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
TestActivity.java
public class TestActivity extends Activity {
private Button btnConfirm;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnConfirm = (ConfirmButton)findViewById(R.id.confirmButton1);
btnConfirm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btnConfirm.setEnabled(false);
}
});
}
}
public class ConfirmButton extends Button{
public ConfirmButton(Context context) {
super(context);
}
public ConfirmButton(Context context, AttributeSet attrs) {
this(context, attrs, R.layout.confirm__button);
}
public ConfirmButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
I do not understand why the btnConfirm has no buttonConfirm style.
Just casting to your ConfirmButton won't "change" the type of the button. You need change to change your XML to actually reference your class. I don't know your package but,
go for this:
<com.your.package.TestActivity$ConfirmButton xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ConfirmButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/buttonConfirm" >
</com.your.package.TestActivity$ConfirmButton>
You should also EITHER make ConfirmButton a static class, or move it up into a class of it's own.
this is my question:
At the first click on button I set the background of view
public void clickOnButton(View v){
if(compareDrawable(getResources().getDrawable(R.drawable.green_button), v.getBackground())) {
v.setBackgroundResource(R.drawable.red_button);
}else{
v.setBackgroundResource(R.drawable.green_button);
}
}
Where the compare function has this code:
public boolean compareDrawable(Drawable d1, Drawable d2){
try{
Bitmap bitmap1 = ((BitmapDrawable)d1).getBitmap();
ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
bitmap1.compress(Bitmap.CompressFormat.JPEG, 100, stream1);
stream1.flush();
byte[] bitmapdata1 = stream1.toByteArray();
stream1.close();
Bitmap bitmap2 = ((BitmapDrawable)d2).getBitmap();
ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
bitmap2.compress(Bitmap.CompressFormat.JPEG, 100, stream2);
stream2.flush();
byte[] bitmapdata2 = stream2.toByteArray();
stream2.close();
return bitmapdata1.equals(bitmapdata2);
}
catch (Exception e) {
// TODO: handle exception
}
return false;
}
I've already try to use some of these comparations:
if(v.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.green_button).getConstantState()))
or
if(getResources().getDrawable(R.drawable.green_button).hashCode() == v.getBackground().hashCode())
and the code of xml files is like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#70c656" />
<stroke
android:width="1dp"
android:color="#53933f" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#70c656"
android:endColor="#53933f"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#53933f" />
<corners
android:radius="4dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
but they doesn't work, they return false every time. How can I compare these object?
Why would you need to compare Drawables just to change the image back and forth?
Instead use CheckBox or other CompoundButton and use selector xml with 4 states:
checked and pressed
checked
pressed
default
I'm trying to make buttons rounded, random background color? When I tried the code below, the button isn't rounded.
Here is the code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newtestament);
LinearLayout layout = (LinearLayout) findViewById(R.id.linearlayoutnew);
String[] values = { "Matthai", "Marka", "Luka", "Johan",
"Sawltak Tangthu", "Rom Laikhak", "Korin Masa", "Korin Nihna",
"Galati", "Efesa", "Filippi", "Kolose", "Thesalonika Masa",
"Thesalonika Nihna", "Timoti Masa", "Timoti Nihna", "Titus",
"Filemon", "Hebru", "James", "Peter Masa", "Peter Nihna",
"Johan Masa", "Johan Nihna", "Johan Thumna", "Jude",
"Maangmuhna" };
Button[] b = new Button[values.length];
for (int i = 0; i < b.length; i++) {
b[i] = new Button(this);
}
int[] btnColor = { 0xAAe60038, 0xAA9142d6, 0xAAf07b04, 0xAA1515ff,
0xAA23699e, 0xAA0a71ff, 0xAA3e3d39, 0xAA00b323, 0xAA754e45,
0xAAfa061e, 0xAAe66d2d, 0xAAff00ff };
// calling random
Random random = new Random();
// ramdomizing a color
int c = btnColor[random.nextInt(btnColor.length)];
for (int i = 0; i < values.length; i++) {
// applying button rounded xml style
b[i].setBackgroundDrawable(getResources().getDrawable(
R.drawable.round));
// setting randomized color
b[i].setBackgroundColor(c);
// layout
LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
// margin
((MarginLayoutParams) params).setMargins(5, 5, 5, 5);
// padding
b[i].setPadding(10, 10, 10, 10);
// text color
b[i].setTextColor(0xFFFFFFFF);
// text
b[i].setText(values[i]);
// text size
b[i].setTextSize(18);
Typeface face = Typeface.createFromAsset(getBaseContext()
.getAssets(), "UBUNTU-R.TTF");
b[i].setTypeface(face);
layout.addView(b[i], params);
}
}
Here is round.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- you can use any color you want I used here gray color -->
<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />
</shape>
I think the problems are setBackgroundDrawable, setBackgroundColor and romdomizing. What additional codes are needed? ??? Update:How to edit round.xml to make the button rounded?
You are setting button background as drawable and then the color also. So statements are executed sequentially. So in the end your button should have a background color set.
So try commenting one of them and run the code again.
b[i].setBackgroundDrawable(getResources().getDrawable( R.drawable.round));
// set background drawable
// first your background drawable will be set
b[i].setBackgroundColor(c);
//set background color.
bkg.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/pressed" />
<item android:state_focused="false"
android:drawable="#drawable/normal" />
</selector>
normal.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<stroke android:width="3dp"
android:color="#0FECFF" /><!-- #330000FF #ffffffff -->
<padding android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp"/>
<corners android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
</shape>
pressed.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FF1A47"/>
<stroke android:width="3dp"
android:color="#0FECFF"/>
<padding android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp"/>
<corners android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
</shape>
b[i].setBackgroundResource(R.drawable.bkg);
Snap Shot
Edit:
int colors []= {Color.RED, Color.BLACK, Color.BLUE,Color.GREEN};
Random r = new Random();
for (int i = 0; i < colors.length; i++) {
b[i].setBackgroundColor(colors[r.nextInt(3)]);
}
look at Document . view.setBackgroundDrawable(drawable) and b[i].setBackgroundColor(c); you can't use both at a time. it will take to effect last one only.