i use this code for make new radio button and add it to radioGroup
RadioButton buttonh = new RadioButton(getApplicationContext());
RadioGroup.LayoutParams params_rb = new RadioGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
buttonh.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
buttonh.setGravity(Gravity.START);
params_rb.setMargins(0, 0, 12, 0);
buttonh.setTextColor(getResources().getColor(R.color.textColor));
buttonh.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
buttonh.setText(Html.fromHtml(mscs.get("time_am")));
radiox.addView(buttonh,params_rb);
and its work but its show different color of circle so i must change it to
AppCompatRadioButton buttonh = new AppCompatRadioButton(this);
but when i use this code the circle gone only the text shown any idea ?
also i try to add color to circle but seems there are no circle only text
ColorStateList colorStateList = new ColorStateList(
new int[][]{
new int[]{-android.R.attr.state_checked},
new int[]{android.R.attr.state_checked}
},
new int[]{
Color.DKGRAY
, Color.rgb (242,81,112),
}
);
buttonh.setSupportButtonTintList(colorStateList);
First, you should take a look to appCompat lib article there and to different attributs you can set:
colorPrimary: The primary branding color for the app. By default, this is the color applied to the action bar background.
colorPrimaryDark: Dark variant of the primary branding color. By default, this is the color applied to the status bar (via statusBarColor) and navigation bar (via navigationBarColor).
colorAccent: Bright complement to the primary branding color. By default, this is the color applied to framework controls (via colorControlActivated).
colorControlNormal: The color applied to framework controls in their normal state.
colorControlActivated: The color applied to framework controls in their activated (ex. checked, switch on) state.
colorControlHighlight: The color applied to framework control highlights (ex. ripples, list selectors).
colorButtonNormal: The color applied to framework buttons in their normal state.
With previous attributs you can define your own theme for RadioButton:
<style name="Theme.MyRadioButton" parent="Theme.AppCompat.Light">
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">#color/my_awesome_color</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">#color/my_awesome_darker_color</item>
<!-- colorAccent is used as the default value for colorControlActivated,
which is used to tint widgets -->
<item name="colorAccent">#color/accent</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight, and colorSwitchThumbNormal. -->
</style>
and :
<RadioButton
....................................
android:theme="#style/Theme.MyRadioButton">
</RadioButton>
Related
I want to inherit all Button styles from default material design style and preserve background colors and shadows which appear when pressing a button (so called elevation). The only thing I want to change is the color of ripple effect (when you touch a button and then move your finger around).
If I use the following style:
<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:buttonStyle">#style/MyTheme.Button</item>
</style>
<style name="MyTheme.Button" parent="Widget.AppCompat.Button.Colored">
<item name="android:background">#drawable/btn_material</item>
</style>
and drawable/btn_material is:
<ripple android:color="#ffff0000">
<item android:id="#android:id/mask"
android:drawable="#android:color/white" />
</ripple>
then my button loses its background color and elevation shadow.
Is there any way to tell the button to keep the default colors and shadows inherited from parent and only replace the ripple color with the one given? Essentially I'd like to achieve something like android:backgroundRipple property which would change only the ripple leaving everything else intact.
RippleDrawable ripple= (RippleDrawable)view.getBackground();
int[][] attr= new int[][] { new int[] { android.R.attr.state_enabled} };
int[] colors = new int[] { Color.RED }; // sets the ripple color to red
ColorStateList color = new Color(states, colors);
ripple.setColor(color);
I have been using Google design support library for Android. In order to set colour of a button different from the app theme I declare the Button in layout XML file as follows:
<Button
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="#style/MyButton" />
And then define the MyButton in styles.xml as
<style name="MyButton" parent="ThemeOverlay.AppCompat">
<item name="colorButtonNormal">#color/my_color</item>
</style>
This gives me a button as per the design support library, with the background color as the one defined with #color/my_color in my colors.xml file.
Hence basically it is to use android:theme to change the colorButtonNormal attribute in order to get the desired color.
How could I achieve the same result programatically? Basically if I could do something like
myButton.setTheme(R.style.MyButton)
...then I could set the colorButtonNormal to get the view.
I cannot set the it like
myButton.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.my_color));
or not even like
ColorStateList colorStateList = ContextCompat.getColorStateList(getActivity(), R.color.my_color);
ViewCompat.setBackgroundTintList(myButton, colorStateList);
This will remove the design support library effect of touch.
for Button I wrote this helper method:
public static ColorStateList getButtonColorStateList(Context context, int accentColor) {
// get darker variant of accentColor for button pressed state
float[] colorHSV = new float[3];
Color.colorToHSV(accentColor, colorHSV);
colorHSV[2] *= 0.9f;
int darkerAccent = Color.HSVToColor(colorHSV);
return new ColorStateList(
new int[][] {{android.R.attr.state_pressed}, {android.R.attr.state_enabled}, {-android.R.attr.state_enabled}},
new int[] { darkerAccent, accentColor, getColor(context, R.color.buttonColorDisabled) });
}
accentColor is color value for normal enabled state. For pressed state is used darker variant of accentColor and for disable state I have defined color in values:
<color name="buttonColorDisabled">#dddddd</color>
Use this method:
mButton.setSupportBackgroundTintList(Utils.getButtonColorStateList(this, accentColor));
where mButton is AppCompatButton and accentColor is value of color.
This works for me on Lollipop and above with effect of touch and on pre-Lollipop as standard color change.
If you only need one color (in spite of the state), you can use
Button button = ...;
int color = ...;
ViewCompat.setBackgroundTintList(button, ColorStateList.valueOf(color));
This will preserve ripple effect on Lollipop and newer devices (API 21+)
I've added a couple of simple buttons to a layout and I want to remove the blue shadow/border that apears around the button when I press it. How do I customize the button's apearance on "onpress" action?
there is no way to do this - all "looks" of buttons states are drawables. you can replace them creating own graphics or use Holo generator (deprected, prefering using AppCompat lib)
HERE you have a Selector example and also method for coloring button ("shadow" will stay, but will be in your color matching app style)
ImageView.setColorFilter(Color.argb(255, 93, 93, 93));
This is gray color, you can set according to your color of image background.
try to set Background drawable for your button as:
btn.setBackgroundResource(R.drawable.yellow_button);
yellow_button.xml :
<!-- pressed -->
<item android:drawable="#drawable/button_1_selected" android:state_pressed="true"/>
<!-- focused -->
<item android:drawable="#drawable/button_1_normal" android:state_focused="true"/>
<!-- default -->
<item android:drawable="#drawable/button_1_normal"/>
I have some issues with the guidelines:
On one hand, they say (here) that 500 is the chosen one for toolbars:
Toolbars and larger color blocks should use the 500 color of the
primary color of your app.
On the other hand, they show an example by scrolling on the same page (here) , showing the color of the action bar is 300, in these 2 images:
http://material-design.storage.googleapis.com/publish/material_v_3/material_ext_publish/0Bx4BSt6jniD7OU9YTFNJakVobVU/style_color_themes_light1.png
http://material-design.storage.googleapis.com/publish/material_v_3/material_ext_publish/0B6Okdz75tqQsZ2hMLXNYWld2d3c/style_color_themes_light2.png
Yet we know that "colorPrimary" is used for the actionBar, as written here.
What's the correct one?
I don't get how the colors are calculated. Why is it between 50 and 900? All I've noticed is that the darker the color, the higher the value of it.
Is there any formula to convert between the values ?
I can't find guidelines for many color types (meaning which color of the color palette to choose for each type). Only those of the actionBar ("colorPrimary" , should be 500 or 300) and the statusBar ( "colorPrimaryDark", should be 600) . What about the others? For example, those:
colorAccent (theme UI controls like checkboxes and text fields)
windowBackground , which is probably 200 according to the images I've shown on #1.
colorControlNormal, colorControlHighlight, colorControlActivated
textColorPrimary, textColorSecondary, textColorTertiary
actionMenuTextColor, actionModeBackground
Maybe there are others that I've missed.
Take a look, taht at the right corner, there is a Hexadecimal number (something like #FFFFFF), thats the real color showed.
If you want, take a look that page,http://www.materialpalette.com/ , where you can choose the color you need to your app. Put it in a res file called "colors.xml"
Afeter that, you have to modify the syle, using your colors
<resources>
<!-- inherit from the material theme -->
<style name="AppTheme" parent="android:Theme.Material">
<!-- Main theme colors -->
<!-- your app branding color for the app bar -->
<item name="android:colorPrimary">#color/primary</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="android:colorPrimaryDark">#color/primary_dark</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="android:colorAccent">#color/accent</item>
</style>
</resources>
I'm having issues creating a Material Design app with the AppCompat v21 theme. First I have my app theme using the AppCompat light with dark actionbar.
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Main theme colors -->
<!-- your app branding color for the app bar -->
<item name="colorPrimary">#color/theme_color</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">#color/theme_color_dark</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="colorAccent">#color/theme_color_accent</item>
</style>
The color branding for my app is quite dark which is why I use the DarkActionBar variant. My problem is that I have a couple layouts where I'd like to put a Spinner and an EditText inside a layout where the background is my theme_color which is dark. This results in a Spinner/EditText with black color on a dark background. How can I make the style/theme of these controls use the Dark variant so they show up with white text instead of black text. Even worse than that, I need the spinner to show white text but dropdown items of the spinner to show the light version.
I'm using a Spinner inside a Toolbar with dark background and remembered that the Google I/O Android App does the same, so I looked at its styles - more specifically, the ActionBarThemeOverlay here.
After I used the below attributes on my theme, my Spinner background color (the "arrow") changed:
<item name="colorControlNormal">#fff</item>
<item name="colorControlHighlight">#3fff</item>
colorControlHighlight is the color when the user presses the Spinner. I didn't test on an EditText, though.
So, you're looking for a way to apply a different theme to select views.
There's no xml-only way to do that for most elements (except Toolbar, which you already know)
You'll have to inflate the views manually, using a ContextThemeWrapper:
ContextThemeWrapper wrapper = new ContextThemeWrapper(getActivity(),
R.style.Theme_AppCompat); // The dark one
LayoutInflater footPump = LayoutInflater.from(getActivity()).cloneInContext(wrapper);
// Note null as second argument -- if you pass in parent view, theme will
// be taken from the parent
Spinner spinner = (Spinner) footPump.inflate(R.layout.spinner, null);
to have the layout resource for the inflater, just create a single-view xml file:
<!-- res/layout/spinner.xml -->
<?xml version="1.0" encoding="utf-8"?>
<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
How about this..
make a style for you widget, ex:
<style name="CustomEditText" parent="android:Widget.EditText">
<item name="android:textColor">#color/red</item>
</style>
then in your xml add this attribute to your EditText
style="#style/CustomEditText"