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
Related
I'm trying to change the color of AlertDialog with MultiChoiceItems
Java :
private void displayMultiSelectDialog() {
emoji = getResources().getStringArray(R.array.photo_editor_emoji);
boolean[] checkedItems = new boolean[emoji.length];
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this, R.style.DialogTheme);
dialogBuilder.setTitle("Select Emoji");
dialogBuilder.setMultiChoiceItems(convertListEmoji(emoji), checkedItems,
(dialogInterface, which, isSelected) -> {
if (isSelected) {
selectedEmoji.add(emoji[which]);
} else {
selectedEmoji.remove(emoji[which]);
}
}
);
dialogBuilder.setPositiveButton("Done", (dialog, which) -> showSelectedColors());
dialogBuilder.create().show();
}
XML :
<style name="DialogTheme">
<item name="android:background">#000</item>
<item name="android:textColor">#586eea</item>
<item name="android:textSize">18sp</item>
<item name="android:textColorPrimary">#586eea</item>
<item name="android:colorAccent" tools:targetApi="lollipop">#586eea</item>
</style>
but I have a problem my AlertDialog background, it's BLACK so the checkbox items look invisible
how to make the checkbox looks like this :
Massive thanks in advance
Try setting a custom theme to your checkbox.
Add style in your styles.xml
<style name="MyCheckBox" parent="Theme.AppCompat.NoActionBar">
<item name="colorControlNormal">#000</item> <!-- normal border color -->
<item name="colorControlActivated">#000</item> <!-- activated color -->
<item name="android:textColor">#FFFF3F3C</item> <!-- text color -->
</style>
and set this style as your checkbox's theme as below:
<CheckBox android:id="#+id/check_agree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="I agree"
android:theme="#style/MyCheckBox"/> <!-- here apply your checkbox style -->
<CheckBox
...
android:buttonTint="#color/tint_color" />
minSdkVersion is 21+ use android:buttonTint attribute to update the color of a checkbox:
I am using TextInputLayout in which i want to change the color of hinttext when i enter text then the color of hint text should be blue other wise color should be gray and when edittext contains some value then the color of hintText should blue for more information please check my image.
Ultimate i want this after edit text containing some value.
This is my current result after using this code
<style name="labelcolor" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Hint color and label color in FALSE state -->
<item name="android:textColorHint">#868686</item>
<item name="android:textSize">16sp</item>
<item name="android:paddingTop">5sp</item>
<!-- Label color in TRUE state and bar color FALSE and TRUE State -->
<item name="colorAccent">#0ea3ff</item>
<item name="colorControlNormal">#0ea3ffr</item>
<item name="colorControlActivated">#0ea3ff</item>
</style>
please help me how can i solve this issues.
Change TextInputLayout Lebel Color Dynamicaly in on Focus change of edittext and pass color according work for me.
public static void textInoutLayoutColor(TextInputLayout textInputLayout, #ColorInt int color) {
try {
Field fDefaultTextColor = TextInputLayout.class.getDeclaredField("mDefaultTextColor");
fDefaultTextColor.setAccessible(true);
fDefaultTextColor.set(textInputLayout, new ColorStateList(new int[][]{{0}}, new int[]{ color }));
Field fFocusedTextColor = TextInputLayout.class.getDeclaredField("mFocusedTextColor");
fFocusedTextColor.setAccessible(true);
fFocusedTextColor.set(textInputLayout, new ColorStateList(new int[][]{{0}}, new int[]{ color }));
} catch (Exception e) {
e.printStackTrace();
}
}
and change the color like this.
input_layout= (TextInputLayout) findViewById(R.id.input_layout);
etuser_name.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasfocus) {
if(hasfocus){
textInoutLayoutColor(input_layout,getResources().getColor(R.color.bullet_color));
}
else
{
if(etuser_name.getText().toString().trim().length()>0) {
textInoutLayoutColor(input_layout,getResources().getColor(R.color.bullet_color));;
}
else
{
textInoutLayoutColor(input_layout,getResources().getColor(R.color.hint_grey));
}
}
}
});
Create your as bellow and try
<style name="EditTextHint" parent="TextAppearance.AppCompat">
<item name="colorAccent">#android:color/white</item>
<item name="android:textColorHint">#color/BackgroundtWhiteColor</item>
<item name="colorControlNormal">#color/BackgroundtWhiteColor</item>
<item name="colorControlActivated">#color/your color</item>
<item name="colorControlHighlight">#color/BackgroundtWhiteColor</item>
</style>
Use it as bellow
<android.support.design.widget.TextInputLayout
android:theme="#style/EditTextHint"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>
Firstly you need to set your TextInputLayout
android.support.design.widget.TextInputLayout textInputLayout = (TextInputLayout) view.findViewById(R.id.textInputLayout);
textInputLayout.setHintTextAppearance(R.style.Instyle);
and then in styles.xml file you can create your styles with the color for the hint color.
<style name="Instyle" parent="AppThemeLight">
<itemname="android:textColorPrimary">#android:color/darker_gray</item>
<item name="android:textColor">#android:color/darker_gray</item>
</style>
TextInputLayout takes colorAccent to hintTextColor when you start entering values in edittext.
So if you want it to be blue then set your colorAccent to blue colour in your theme. And when edittext has no value then set hintTextColor to grey. Do like this
<android.support.design.widget.TextInputLayout
android:id="#+id/userNameTIL"
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="#color/spinnerBg"
android:clipToPadding="false"
android:gravity="bottom"
android:paddingTop="4dp"
android:textColorHint="#color/grey"> /*Here it sets color to hintText when edittext has no value */
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="bottom"
android:background="#null"
android:hint="Username"
android:inputType="text"
android:maxLength="30"
android:paddingEnd="3dp"
android:paddingStart="25dp"
android:paddingTop="3dp"
android:singleLine="true"
android:textColor="#color/black"
android:textColorHint="#color/black"
android:textSize="20sp"/>
</android.support.design.widget.TextInputLayout>
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!--EditText hint color-->
<item name="android:textColorHint">#color/default_app_white</item> <!-- TextInputLayout text color-->
<item name="colorControlActivated">#color/default_app_green</item>
<!-- EditText line color when EditText on-focus-->
<item name="colorControlHighlight">#color/default_app_green</item>
<!-- EditText line color when EditText in un-focus-->
<item name="colorControlNormal">#color/default_app_white</item>
</style>
<!-- In your style.xml file -->
try this one but it affects your whole application
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.
I have style:
<style name="editTextInput">
<item name="android:textColor">#000000</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_weight">1</item>
<item name="android:digits">0123456780.</item>
<item name="android:inputType">numberDecimal</item>
</style>
I want add this style to my EditText. like this:
TableRow tableRowEdit = new TableRow(context);
EditText editText = new EditText(context, null, R.style.editTextInput);
tableRowEdit.addView(editText);
In layout XML this style work fine:
<TableRow style="#style/tableRowInput" >
<TextView
style="#style/tvTitleStyle"
android:text="#string/diameterSmall" />
<TextView style="#style/separatorInput" />
<EditText
android:id="#+id/diameterSmallValue"
style="#style/editTextInput" />
</TableRow>
but in programmatically dont'work.
try below code
XmlPullParser parser = getApplicationContext().getResources().getXml(R.style.editTextInput);
AttributeSet attributes = Xml.asAttributeSet(parser);
EditText editText = new EditText(context,attributes );
tableRowEdit.addView(editText);
Here is the action bar that I want to create.
Here is items that I put in menu.
<item
android:id="#+id/search"
android:title="Search"
android:showAsAction="ifRoom|collapseActionView"
android:actionLayout="#layout/search_layout"
/>
<item
android:id="#+id/search1"
android:title="PHOTO"
android:showAsAction="ifRoom"
android:actionLayout="#layout/search_layout"
/>
collapseActionView - collapses search menu on startup.
After click on search item it shows edit text like on picture 1. But I need always see it, not only after click.
If I set it to Always it will pop the menu items from the screen because of fill_parent.
<EditText
android:id="#+id/txt_search"
android:inputType="text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
Is it possible to get width of title bar layout without menu items? In this case I can set fixed width to edittext item.
Is it any other approaches to make it work?
In result it must be custom title layout + menu items. This custom layout must fill empty space between menu items and icon.
Using Custom ActionBar, you can solve your Problem. For Custom ActionBar you have to create on Custom Layout as per following:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:orientation="horizontal"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:paddingEnd="8dip"
android:paddingRight="8dip"
android:paddingLeft="8dip">
<EditText
android:layout_width="200dip"
android:layout_height="wrap_content"
android:id="#+id/action_bar_edt_search"/>
<ImageButton
android:layout_width="50dip"
android:layout_height="50dip"
android:id="#+id/action_bar_img_search"
android:background="#drawable/search_ico"/>
<Button
android:id="#+id/action_bar_search"
android:layout_alignParentRight="true"
android:layout_gravity="right"
style="#style/ActionBarButtonWhite" />
<Button
android:id="#+id/action_bar_photo"
android:layout_alignParentRight="true"
android:layout_gravity="right"
style="#style/ActionBarButtonOffWhite" />
<Button
android:id="#+id/action_bar_video"
android:layout_alignParentRight="true"
android:layout_gravity="right"
style="#style/ActionBarButtonOffWhite" />
<Button
android:id="#+id/action_bar_mobile"
android:layout_alignParentRight="true"
android:layout_gravity="right"
style="#style/ActionBarButtonOffWhite" />
color.xml
<resources>
<color name="action_bar">#ff0d0d0d</color>
<color name="white">#ffffffff</color>
<color name="off_white">#99ffffff</color>
</resources>
styles.xml
<style name="MyActionButtonStyle" parent="android:Widget.ActionButton">
<item name="android:minWidth">28dip</item>
</style>
<style name="ActionBarButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">#null</item>
<item name="android:ellipsize">end</item>
<item name="android:singleLine">true</item>
<item name="android:textSize">#dimen/text_size_small</item>
</style>
now you need to create ViewGroup and ActionBar in your java file as per following:
final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(
R.layout.actions,
null);
// Set up your ActionBar
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(actionBarLayout);
// You customization
final int actionBarColor = getResources().getColor(R.color.action_bar);
actionBar.setBackgroundDrawable(new ColorDrawable(actionBarColor));
final EditText actionBarEditSearch = (EditText) findViewById(R.id.action_bar_edt_search);
actionBarEditSearch.setVisibility(View.GONE);
final ImageButton actionBarImgSearch = (ImageButton) findViewById(R.id.action_bar_img_search);
actionBarImgSearch.setVisibility(View.GONE);
final Button actionBarSearch = (Button) findViewById(R.id.action_bar_search);
actionBarSearch.setText("SEARCH");
final Button actionBarPhoto = (Button) findViewById(R.id.action_bar_photo);
actionBarPhoto.setText("PHOTO");
final Button actionBarVideo = (Button) findViewById(R.id.action_bar_video);
actionBarVideo.setText("VIDEO");
final Button actionBarMobile = (Button) findViewById(R.id.action_bar_mobile);
actionBarMobile.setText("MOBILE");
actionBarSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
actionBarEditSearch.setVisibility(View.VISIBLE);
actionBarImgSearch.setVisibility(View.VISIBLE);
actionBarSearch.setVisibility(View.GONE);
}
});
actionBarImgSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
actionBarEditSearch.setVisibility(View.GONE);
actionBarImgSearch.setVisibility(View.GONE);
actionBarSearch.setVisibility(View.VISIBLE);
}
});
<style name="ActionBarButtonWhite" parent="#style/ActionBarButton">
<item name="android:textColor">#color/white</item>
</style>
<style name="ActionBarButtonOffWhite" parent="#style/ActionBarButton">
<item name="android:textColor">#color/off_white</item>
</style>
You can achieve this by setting a custom view in place of the action bar title.
ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.search_layout);
actionBar.setDisplayShowCustomEnabled(true);
The result is an EditText that fills the entire width of the action bar, except for the action buttons. It looks exactly like the first image in the question.