Radio Button style programmatically - android

I would to create a number of radio button dinamically in a fragment, I only have problem with style. If I put radiobutton code in the xml file, default style is applied correctly, but when I create radiobutton through a function I see different style!
XML
<RadioGroup
android:id="#+id/radiogroup"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:animationCache="false">
<RadioButton
android:text="RadioButton 1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/radioButton3" />
<RadioButton
android:text="RadioButton 2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/radioButton4" />
</RadioGroup>
RESULT
JAVA CODE
This code is put in onCreateView in the fragment
public void addRadioButton(Context ctx,int num){
RadioGroup radioGroup= (RadioGroup) alertInflatedView.findViewById(R.id.radiogroup);
for (int i = 1; i <= num; i++) {
RadioButton radioButton = new RadioButton(ctx);
radioButton.setId(1+i);
radioButton.setText("Radio " + radioButton.getId());
radioButton.setTextColor(getResources().getColor(R.color.black));
radioGroup.addView(radioButton);
}
}
RESULT
As you can see radio buttons have different style, someone could help me, if is possibile, to apply default style programmatically?

you have to create style on drawable or style.xml, as your requirement.
drawable/null_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#android:color/transparent" />
</selector>
Set each button to use it (and to center the text) like this (R.drawable.null_selector is selector XML):
Now, In your Activity, you must be implement such style.
RadioButton radioButton = new RadioButton(ctx);
radioButton.setText(Integer.toString(i));
radioButton.setGravity(Gravity.CENTER);
radioButton.setButtonDrawable(R.drawable.null_selector);
I think, this will help you for implementing custom style in Radio Button.

Thanks Dharma, I followed your suggestion, changing something, and I solved!
JAVA CODE
public void addRadioButton(Context ctx,int num){
RadioGroup radioGroup= (RadioGroup) alertInflatedView.findViewById(R.id.radiogroup);
RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.MATCH_PARENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
for(int i=0; i<num; i++){
RadioButton radioButton = new RadioButton(ctx);
radioButton.setId(1+i);
radioButton.setText("Radio"+i);
radioButton.setTextSize(16);
radioButton.setTextColor(getResources().getColor(R.color.black));
radioButton.setButtonDrawable(R.drawable.radio_button_selector);
radioButton.setPadding(80,0,0,0);
radioButton.setGravity(Gravity.CENTER_VERTICAL);
radioButton.setLayoutParams(layoutParams);
radioGroup.addView(radioButton);
}
}
XML RADIO BUTTON SELECTOR with checked and unchecked button image
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="false" android:drawable="#drawable/unchekedradiobutton" />
<item android:state_checked="true" android:drawable="#drawable/checkedradiobutton" />
<item android:drawable="#drawable/unchekedradiobutton" /> <!-- default -->

Use an Inflater instance to inflate a custom layout and easily get a custom Radiobutton
private RadioButton createCustomRadioButton(Context context){
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.radio_button,null);
RadioButton radioButton = (RadioButton) v.findViewById(R.id.radio_button);
radioButton.setText("It Works!");
((ViewGroup)radioButton.getParent()).removeView(radioButton);
return radioButton;
}
radio_buttom.xml
<RadioButton
android:id="#+id/radio_button"
style="#style/radio"
android:background="#drawable/style_line" />
style.xml
<style name="radio">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginLeft">30dp</item>
<item name="android:layout_marginRight">30dp</item>
<item name="android:layout_marginTop">10dp</item>
<item name="android:layout_marginBottom">10dp</item>
<item name="android:padding">10dp</item>
<item name="android:drawablePadding">5dp</item>
<item name="android:textColor">#color/whiteColor</item>
<item name="android:textColorHint">#color/hintColor</item>
<item name="android:editTextColor">#color/whiteColor</item>
</style>
by Ubirajara (México)

Programmatically, I recommend setting a ColorStateList to each radio button in the loop like this:
radioButton.setButtonTintList(getRadioButtonColors());
Then
private ColorStateList getRadioButtonColors() {
return new ColorStateList (
new int[][] {
new int[] {android.R.attr.state_checked}, // checked
new int[] {android.R.attr.state_enabled} // unchecked
},
new int[] {
Color.GREEN, // checked
Color.BLUE // unchecked
}
);
}
Where android.R.attr.state_checked defines the color for the checked button (green) and android.R.attr.state_enabled defines the color for unchecked buttons (blue).
Personally I think this is a better solution because it's more concise than creating styles and other dependencies elsewhere in the codebase. Whenever possible use the most concise and efficient approach.

Related

Adding RadioButtons Dynamically To RadioGroup breaks Mutually Exclusive Selection

This is inside a custom view class that extends RelativeLayout not inside an Activity or Fragment.
My RadioGroup exists in my xml and I'm trying to dynamically add RadioButtons to it. The only time I've seen it where the mutual exclusivity does not work, is if you do not add an id but I am here. What happens is when I tap a radio button more than 1 button is selected at a time. What am I missing here?
for(String buttonName : mButtonNames){
RadioButton radioButton = new RadioButton(getContext());
radioButton.setId(ViewCompat.generateViewId() );
radioButton.setText(buttonName);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.weight = 1;
radioButton.setLayoutParams(layoutParams);
radioButton.setBackground(mRadioBckgrdDrawable);
radioButton.setButtonDrawable(null);
radioButton.setGravity(Gravity.CENTER);
radioButton.setTextSize(mTextSize);
radioButton.setTextColor(mTextColor);
mRadioGroup.addView(radioButton);
}
Background drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected-->
<item android:drawable="#drawable/two_stage_toggle_button_on"
android:state_checked="true" />
<!-- When not selected-->
<item android:drawable="#drawable/two_stage_toggle_button_off"
android:state_checked="false"/>
</selector>

TabLayout tab text color state list

I want my TabLayout tabs have three different text colors depending on tab state:
default - red
disabled - yellow
selected - green
I'm adding my TabLayout:
<android.support.design.widget.TabLayout
android:id="#+id/tlBetTypes"
style="#style/BetTabLayoutStyle"
android:layout_width="match_parent"
android:layout_height="38dp">
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/single"/>
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/multi"/>
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/system"/>
</android.support.design.widget.TabLayout>
then BetTabLayoutStyle:
<style name="BetTabLayoutStyle">
<item name="tabIndicatorHeight">0dp</item>
<item name="tabTextAppearance">#style/BetTabTexStyle</item>
<item name="tabBackground">#drawable/background_tab_bet_type</item>
</style>
then BetTabTexStyle:
<style name="BetTabTexStyle">
<item name="android:textSize">#dimen/text_size_12</item>
<item name="android:textColor">#color/tab_test</item>
<item name="android:fontFamily">#font/roboto_medium</item>
<item name="textAllCaps">true</item>
</style>
then tab_test.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/red"/>
<item android:color="#color/yellow" android:state_enabled="false"/>
<item android:color="#color/green" android:state_selected="true"/>
</selector>
But it's not working.
I also tried to create a ColorStateList from code and set and set is using setTabTextColors.
Could you please explain me what I'm doing wrong?
Two steps to solve this:
set ColorStateList from code because TabLayout resets text appearance in constructor if you inflate it in xml.
private void setupTabsStyle() {
int[][] states = new int[][]{
new int[]{android.R.attr.state_selected},
new int[]{android.R.attr.state_enabled},
new int[]{-android.R.attr.state_enabled}
};
#ColorRes int[] colorRes = new int[]{
R.color.orange_1,
R.color.grey_16,
R.color.grey
};
#ColorInt int[] colors = new int[colorRes.length];
for (int i = 0; i < colorRes.length; i++) {
colors[i] = ContextCompat.getColor(getContext(), colorRes[i]);
}
ColorStateList colorList = new ColorStateList(states, colors);
mTabLayout.setTabTextColors(colorList);
}
Disable TextView inside the tab, not only the tab itself. In particular we need to disable/enable only the TextView inside the TabView to achieve text appearance effect, but I disable the ImageView as well.
private void enableTab(int tabIndex, boolean doEnable) {
LinearLayout tabStrip = ((LinearLayout) mTabLayout.getChildAt(0));
LinearLayout tabView = (LinearLayout) tabStrip.getChildAt(tabIndex);
tabView.setEnabled(doEnable);
for (int i = 0; i < tabView.getChildCount(); i++) {
View childAt = tabView.getChildAt(i);
childAt.setEnabled(doEnable);
}
}
Did you tried this?
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:tabGravity="fill"
app:tabTextColor="#color/defaultColor"
app:tabSelectedTextColor="#color/whenTabSelectedColor"/>
try this
<style name="BetTabLayoutStyle" parent="Widget.Design.TabLayout">
<item name="tabIndicatorHeight">0dp</item>
<item name="tabTextAppearance">#style/BetTabTexStyle</item>
<item name="tabBackground">#drawable/background_tab_bet_type</item>
</style>

How to define the text color of all children in an Android Layout?

I am currently filling a TableLayout with data from external sources. After creating the TableRows and TextViews programmatically, their text color is set to white (seems to be the default).
I know that I can set the color by using myTextView.setTextColor(Color.BLACK);, but I am looking for a way to define the color in the XML layout file (I want to separate visuals from the logic). Does anyone know how to do this?
My layout (filled with example rows):
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/mytable"
android:stretchColumns="0,1,2"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
style="#style/AppTheme" >
<TableRow>
<TextView android:text="field1" />
<TextView android:text="field2" />
<TextView android:text="field3" />
</TableRow>
</TableLayout>
My java code:
public void updateTable() {
TableLayout table = (TableLayout) findViewById(R.id.mytable);
table.removeAllViewsInLayout();
TableRow row = new TableRow(getApplicationContext());
TextView text;
// Add Heading
text = new TextView(getApplicationContext());
text.setText("Head1");
text.setTextColor(this.textColor);
row.addView(text);
text = new TextView(getApplicationContext());
text.setText("Head2");
text.setTextColor(this.textColor);
row.addView(text);
text = new TextView(getApplicationContext());
text.setText("Head3");
text.setTextColor(this.textColor);
row.addView(text);
table.addView(row);
for(Data d : this.getData()) {
TableRow r = new TableRow(getApplicationContext());
TextView t1 = new TextView(getApplicationContext());
TextView t2 = new TextView(getApplicationContext());
TextView t3 = new TextView(getApplicationContext());
t1.setTextColor(this.textColor);
t2.setTextColor(this.textColor);
t3.setTextColor(this.textColor);
t1.setText(d.getData1());
t2.setText(d.getData2());
t3.setText(d.getData3());
r.addView(t1);
r.addView(t2);
r.addView(t3);
table.addView(r);
}
}
Create a styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme">
<item name="android:textViewStyle">#style/MyTextViewStyle</item>
</style>
<style name="MyTextViewStyle" parent="android:Widget.TextView">
<item name="android:textColor">#F00</item>
<item name="android:textStyle">bold</item>
</style>
</resources>
Then just apply that theme to your application in AndroidManifest.xml:
<application […] android:theme="#style/MyTheme">…
1.Create one table_item.xml file of TextView
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text1"
android:text="#string/hello_world"
android:textStyle="bold"
android:textColor="#ff00ff"/>
2.Inflate table_item.xml
LayoutInflater inflater = LayoutInflater.from(getContext());
TextView text = (TextView)inflater.inflate(R.layout.table_item, this);
3.Add into TableLayout
r.addView(text);
table.addView(r);
If you want to do dynamically then after initializing mytable in java and then proceed as
for (int i = 0; i < mytable.getChildCount(); i++) {
View view = mytable.getChildAt(i);
if(view instanceof EditText)//
((EditText)view).setTextColor(Color.BLACK);
}
Create one common style in style.xml in values folder
<style name="WelcomeTextViewTitleStyle">
<item name="android:textSize">#dimen/welcome_textview_title_textsize</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:gravity">center</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#color/white</item>
</style>
apply to in your textview
<TextView android:id="#+id/tv_title2"
style="#style/WelcomeTextViewTitleStyle"
android:text="#string/wantToKnow" />
try this code
<TextView android:text="assads"
android:textColor="#000000"/>
</TableRow>

How to set Holo style for android radiobutton

I have set the radiobutton drawble to make it appears to the right of the text.
But I want to set the Holo style for this radiobutton. how ?
code:
<RadioButton
android:id="#+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#null"
android:drawableRight="#android:drawable/btn_radio"
android:paddingLeft="40dip"
android:tag="2"
android:text="#string/Conflict_of_intrest" />
Use ?android:attr/listChoiceIndicatorSingle as right-drawable while disabling default radio button look(android:button="#null")
<style name="rtlRadioBtn" parent="android:Widget.Holo.Light.CompoundButton.RadioButton">
<item name="android:button">#null</item>
<item name="android:drawableRight">?android:attr/listChoiceIndicatorSingle</item>
<item name="android:gravity">right|center_vertical</item>
</style>
Code:
Drawable radio;
try {
TypedValue typedValue = new TypedValue();
Theme theme = getContext().getTheme();
theme.resolveAttribute(android.R.attr.listChoiceIndicatorSingle, typedValue, true);
radio = getContext().getResources().getDrawable(typedValue.resourceId);
} catch (Exception e) {
radio = getContext().getResources().getDrawable(android.R.drawable.btn_radio);
}
XML:
?android:attr/listChoiceIndicatorSingle

Changing TextView color by selector tag

I have a TextView, I want to change its color by clicking it and save after exiting app, I used selector tag in a xml file in drawable folder but the problem is default color is right but nothing happens on click why?
Selector xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#FFA500"/>
<item android:color="#FF0000"/>
</selector>
TextView xml:
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:text="Transferef Money"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#drawable/selector" />
While color can be changed through a click handler
TextView textView = new TextView(this);
textView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
textView.setTextColor(Color.BLACK);
}
});
add an onClick attribute to your XML
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:text="Transferef Money"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#drawable/selector"
android:onClick="colorise"
/>
and then set the color in colorise method.
Also, I don't think the state will be saved via an XML file, so you will have to handle it programitically.
You could avoid the XML completely and focus on it in your Java activity class:
TextView lbl = (TextView)findViewById(R.id.myTextView);
lbl.setOnClickListener (new View.OnClickListener()
{
public void onClick(View v)
{
TextView lbl = (TextView)findViewById(R.id.myTextView);
lbl.setTextColor(Color.parseColor("#FF0000"));
lbl.setTextColor(Color.rgb(int, int, int));
lbl.setTextColor(Color.RED);
}
});
First define color in colors.xml, and use them in your selector, this works well for me.
Example:
<item android:state_focused="true" android:color="#color/blue_2"/>
<item android:state_pressed="true" android:color="#color/blue_2"/>
<item android:state_checked="true" android:color="#color/blue_2"/>
<item android:color="#color/black_1"/>

Categories

Resources