Dynamic radio button doesn't wrap_content - java

I have created a radio button with a custom drawable like this.
I used my custom selector
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/est_status_radio_checked" android:state_checked="true" android:state_enabled="true" />
<item android:drawable="#drawable/est_status_radio_checked" android:state_checked="true" android:state_enabled="false" />
<item android:drawable="#drawable/est_status_radio_unchecked" android:state_checked="false" /></selector>
with the radio button like the following
<style name="AppTheme.StatusRadioButtons">
<item name="fontPath">fonts/SFUIText-Semibold.ttf</item>
<item name="android:textSize">10sp</item>
<item name="android:background">#drawable/establishment_status_selector</item>
<item name="android:elevation">5dp</item>
<item name="android:paddingLeft">5dp</item>
<item name="android:textColor">#drawable/est_status_text_color</item>
<item name="android:button">#null</item>
</style>
<RadioButton
style="#style/AppTheme.StatusRadioButtons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:checked="false"
android:gravity="center"
android:text="Stopped" />
Everything works fine , but now the requirement is to create dynamic buttons.
For some reason when i create a dynamic radio button , the drawable appears stretched :#
I have created the button like this
private RadioButton createRadioForStatus(EstablishmentStatus establishmentStatus) {
RadioButton radioButton = new RadioButton(getContext());
radioButton.setCompoundDrawablePadding(0);
radioButton.setPadding(0,0,0,0);
radioButton.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.establishment_status_selector));
radioButton.setButtonDrawable(null);
radioButton.setTextSize(10);
radioButton.setElevation(getResources().getDimension(R.dimen.unit_small));
radioButton.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));// wrap content buttons are working perfectly in xml but for some motherfucking reason are not wrapping the content properly when done programatically
radioButton.setText(establishmentStatus.getName());
return radioButton;
}
My selector drawable is like this
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/colorPrimary" />
<corners android:radius="30dp" />
</shape>
</item>
<item
android:bottom="5dp"
android:left="5dp"
android:right="55dp"
android:top="5dp">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#color/colorWhite" />
<size
android:width="10dp"
android:height="10dp"></size>
</shape>
</item>

You should set your src of radio-button in separate directory and then find it by R

Your RadioButton should be inside the same RadioGroup, Something like this:
RadioGroup radioGroup = new RadioGroup(this);
RadioButton radioButton1= new RadioButton(this);
RadioButton radioButton2= new RadioButton(this);
radioGroup.addView(radioButton1);
radioGroup.addView(radioButton2);
I hope this will help.

Related

my application ignores any changes in background color of buttons

i'm trying to customize my alert dialog. but i can not change the background color of buttons.
i tried drawable button designing, style, setting direct color in xml and also setting color by java codes in the activity but i could not change it.
the shape of the button changes but the color only can be either the default color or transparent color.
the xml coding of button itself:
<Button
android:id="#+id/btnDenied"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="35dp"
android:layout_marginBottom="15dp"
android:background="#drawable/button_selected" //// or #drawable/button_costume
android:text="#string/button_no" />
the selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed = "true">
<shape>
<solid android:color="#FFEB3B"/>
</shape>
</item>
<item android:state_pressed = "false">
<shape>
<solid android:color="#FFC107"/>
</shape>
</item>
</selector>
and another drawable file:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="50dp"/>
<solid
android:color="#color/colorHeader"/>
</shape>
and i also tried this style:
<style name="Button_Style"
parent="#android:style/Widget.Button">
<item name="android:textSize">14sp</item>
<item name="android:background">#color/colorHeader</item>
<item name="android:textColor">#1B1B22</item>
<item name="android:textStyle">bold</item>
<item name="android:layout_margin">2dp</item>
</style>
but non of them could change the color of the button.
The code that I have written is 100 percent correct, but sometimes Android Studio does some wrong tasks, so solve these issues either by running the program again, or doing the following steps:
Clean Project , From the build menu .
Rebuild Project .
And I got this problem with me not to show errors in xml file,
I hope this thing helped you.

Select item from xml to apply it on a button

Here's what I have :
Buttonshape.xml :
<?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="#FF008AB8"/>
<stroke android:color="#0299D0"/>
<corners android:radius="15dp"/>
</shape>
</item>
<item android:drawable="#drawable/b_popcorn"></item></layer-list>
BlueFragment.xml :
<Button
android:id="#+id/BtePlay" ...
android:background="#drawable/Buttonshape"
/>
Everything is working greate, my button corner is rounded and it apply the drawable, but I have a question :
Is it possible to add more items in my buttonshape.xml and select a specific drawable in my BlueFragment.xml
For example doing something like this :
Buttonshape.xml :
...
<item android:drawable="#drawable/b_popcorn"></item></layer-list>
<item android:drawable="#drawable/b_2"></item></layer-list>
and in my BlueFragment.xml :
<Button
android:id="#+id/BtePlay" ...
android:background="#drawable/Buttonshape|b_popcorn"
/>
<Button
android:id="#+id/BtePlay2" ...
android:background="#drawable/Buttonshape|b_2"
/>
Something like this to set only one drawable for each button instead of making 50+ xml files for each buttons ...
Thanks
we have only option that is using selector, but its only depend on view state
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/bg_checked" android:state_checked="true"/>
<item android:drawable="#drawable/bg_unchecked" android:state_checked="false"/>
</selector>
so I think we can not do any thing like you expected, so sorry for that !

Change Button custom shape states color programmatically

There are a lot of answers on this site regarding changing button colors, but none that I have managed to use in my case.
I want to be able to dynamically change a button's color, that button still needs to have a visual feedback on press, and it needs rounded corners.
The rounded corners part was decided upon recently, so previously I used something like this:
StateListDrawable states = new StateListDrawable();
states.addState(new int[]{android.R.attr.state_pressed},
new ColorDrawable(hsvDarkenColor(theme.get_buttonsBgColor())));
states.addState(new int[]{android.R.attr.state_focused},
new ColorDrawable(hsvDarkenColor(theme.get_buttonsBgColor())));
states.addState(new int[]{},
new ColorDrawable(Color.parseColor(theme.get_buttonsBgColor())));
((Button) button).setBackgroundDrawable(states);
//this is for the focused/pressed state of the button
private static int hsvDarkenColor(String originalColor)
{
float[] hsv = new float[3];
int color = Color.parseColor(originalColor);
Color.colorToHSV(color, hsv);
hsv[2] *= 0.8f; // value component
return Color.HSVToColor(hsv);
}
However this doesn't preserve the rounded corners shape of the buttons, it makes them into squares.
My default buttons' backgrounds are a list of states each with it's own drawable, for pressed and unpressed, etc.
In styles.xml:
<style name="xx.Light.ScanButton" parent="android:style/Widget.Button">
<item name="android:focusable">true</item>
<item name="android:background">#drawable/xx_scan_button</item>
<item name="android:textStyle">normal</item>
<item name="android:textColor">#color/text_light</item>
</style>
In drawable/xx_scan_button.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item
android:state_focused="false"
android:state_selected="false"
android:state_pressed="false"
android:drawable="#drawable/xx_scan_button_normal"
/>
<item
android:state_focused="false"
android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/xx_scan_button_pressed"
/>
<!-- Focused states -->
<item
android:state_focused="true"
android:state_selected="false"
android:state_pressed="false"
android:drawable="#drawable/xx_scan_button_pressed"
/>
<item
android:state_focused="true"
android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/xx_scan_button_pressed"
/>
<!-- Pressed -->
<item
android:state_pressed="true"
android:drawable="#drawable/xx_scan_button_pressed"
/>
<!-- Disabled -->
<item
android:state_enabled="false"
android:drawable="#drawable/xx_scan_button_normal"
/>
</selector>
And in drawable/xx_scan_button_normal.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners android:bottomRightRadius="5dp"
android:bottomLeftRadius="0dp"
android:topLeftRadius="0dp"
android:topRightRadius="5dp"/>
<solid
android:color="#C74700"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="270dp"
android:height="60dp"
/>
</shape>
TL;DR: I need some way to extract the shape drawable from a drawable state list from a button in order to change it's color.
Or if you guys have a better solution, I'm all ears.
You will have to make separate drawables for individual color and change drawable programmatically not color. Changing color from program overrides the drawble.

How can I add shadow to ImageButton?

this is my first time posting. And I've been using this website as a great resource.
But I have come upon an issue. I've tried applying a shadow effect to my ImageButton but it doest work? What I'm trying to achieve is the shadow you can see on the floating bubble (Material Design). I have a circle ImageButton, but I wish to add a shadow around it. How can I achieve that?
This is circle_button.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#C62666"/>
</shape>
This is circle_button_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#E92D6C"/>
</shape>
This is my selector which combines both drawable's into one. button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/circle_button"
android:state_focused="true"
android:state_pressed="true" />
<item android:drawable="#drawable/circle_button_pressed"
android:state_focused="false"
android:state_pressed="true" />
<item android:drawable="#drawable/circle_button_pressed"
android:state_focused="true" />
<item android:drawable="#drawable/circle_button"
android:state_focused="false"
android:state_pressed="false" />
</selector>
And this is how I apply it to my ImageButton
<ImageButton
android:id="#+id/btn_apply"
android:layout_width="68dp"
android:layout_height="68dp"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/scrollView1"
android:layout_marginRight="45dp"
android:layout_marginTop="94dp"
android:adjustViewBounds="true"
android:background="#drawable/button"
android:scaleType="fitCenter"
android:src="#drawable/apply_two" />
So again? How can I add a shadow to my button? Thanks for your time.
may help you :
You can use a Button:
<ImageButton
android:id="#+id/fab"
android:background="#drawable/ripple"
android:stateListAnimator="#anim/anim"
android:src="#drawable/ic_action_add"
android:elevation="4dp"
/>
where the ic_action_add is your icon.
drawable/ripple.xml is:
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="?android:colorControlHighlight">
<item>
<shape android:shape="oval">
<solid android:color="?android:colorAccent" />
</shape>
</item>
</ripple>
anim/anim.xml is:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="true"
android:state_pressed="true">
<objectAnimator
android:duration="#android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueFrom="#dimen/button_elevation"
android:valueTo="#dimen/button_press_elevation"
android:valueType="floatType" />
</item>
<item>
<objectAnimator
android:duration="#android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueFrom="#dimen/button_press_elevation"
android:valueTo="#dimen/button_elevation"
android:valueType="floatType" />
</item>
</selector>
Dimens.xml is :
<resources>
<dimen name="fab_size">56dp</dimen>
<dimen name="button_elevation">2dp</dimen>
<dimen name="button_press_elevation">4dp</dimen>
With the elevation attribute you should set the Outline via code:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layoutfab);
//Outline
int size = getResources().getDimensionPixelSize(R.dimen.fab_size);
Outline outline = new Outline();
outline.setOval(0, 0, size, size);
findViewById(R.id.fab).setOutline(outline);
}
}

Changing checkbox style to Cross - Android [duplicate]

This question already has answers here:
How to change the color of a CheckBox?
(26 answers)
Closed 9 years ago.
I have been coding an Android Application, and using CheckBox for GUI.
Generally, the style of CheckBox is a green colored tick, but I want to make it red colored cross.
How to do it.? Please Help.
You can set any color of tick mark of Checkbox. but you need to make the custom checkbox for that.
Look here and here.
Source : Android: Set color of CheckBox
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#drawable/cbchk_blue"
android:state_focused="false">
<shape android:shape="rectangle">
<stroke android:width="2dp" android:color="#color/blue_dark" />
<gradient android:endColor="#color/white" android:startColor="#color/blue_dark" android:type="sweep" />
<size android:height="30dp" android:width="30dp" />
</shape>
</item>
<item android:state_checked="true"
android:drawable="#drawable/cbchk_blue"
android:state_focused="true">
<shape android:shape="rectangle">
<stroke android:width="2dp" android:color="#color/white" />
<gradient android:endColor="#color/white" android:startColor="#color/blue_dark" android:type="sweep" />
<size android:height="30dp" android:width="30dp" />
</shape>
</item>
<item android:state_checked="false"
android:drawable="#drawable/cbunchk_blue"
android:state_focused="false">
<shape android:shape="rectangle">
<stroke android:width="2dp" android:color="#color/blue_dark" />
<size android:height="30dp" android:width="30dp" />
</shape>
</item>
<item android:state_checked="false"
android:drawable="#drawable/cbunchk_blue"
android:state_focused="true">
<shape android:shape="rectangle">
<stroke android:width="2dp" android:color="#color/white" />
<size android:height="30dp" android:width="30dp" />
</shape>
</item>
Take a Custom Drawable selector with one image for unchecked state and another for checked state and set it as drawable resource for your Checkbox
Example:
Create a xml file in res/drawable folder
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:drawable="#drawable/crossimagecheckbox" />
<item android:state_checked="false" android:drawable="#drawable/uncheckecheckbox" />
</selector>
You need to create a selector for checkbox.
in layout checkbox add the following:
android:background="#drawable/checkbox_selector"
in checkbox_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
android:drawable="#drawable/normal_picture" />
<item android:state_checked="true"
android:drawable="#drawable/checked_picture" />
</selector>
Now create normal_picture and checked_picture according to your requirement.
I believe this link is highly relevant. Read Jean's answer:
Checkboxes being children of Button you can just give your checkbox a
background image with several states as described here, under "Button
style":
http://developer.android.com/reference/android/widget/Button.html
...and exemplified here:
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

Categories