I have a form like this :
Image
With the code :
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="570dp"
android:layout_marginTop="160dp" >
<RadioButton
android:id="#+id/button_service"
style="#style/type_action"
android:text="#string/service" />
<RadioButton
android:id="#+id/button_reception"
style="#style/type_action"
android:layout_alignLeft="#id/button_service"
android:layout_alignTop="#id/button_service"
android:layout_marginTop="10dp"
android:text="#string/defense" />
<RadioButton
android:id="#+id/button_passe"
style="#style/type_action"
android:layout_alignLeft="#id/button_service"
android:layout_alignTop="#id/button_reception"
android:layout_marginTop="10dp"
android:text="#string/passe" />
<RadioButton
android:id="#+id/button_attaque"
style="#style/type_action"
android:layout_alignLeft="#id/button_service"
android:layout_alignTop="#id/button_passe"
android:layout_marginTop="10dp"
android:text="#string/attaque" />
<RadioButton
android:id="#+id/button_bloc"
style="#style/type_action"
android:layout_alignLeft="#id/button_service"
android:layout_alignTop="#id/button_attaque"
android:layout_marginTop="10dp"
android:text="#string/bloc" />
<RadioButton
android:id="#+id/button_faute"
style="#style/type_action"
android:layout_alignLeft="#id/button_service"
android:layout_alignTop="#id/button_bloc"
android:layout_marginTop="10dp"
android:text="#string/faute" />
</RadioGroup>
And style.xml
<style name="type_action">
<item name="android:textSize">30sp</item>
<item name="android:textColor">#EEEEEE</item>
<item name="android:background">#000000</item>
<item name="android:textStyle">bold</item>
<item name="android:layout_width">200dp</item>
<item name="android:layout_height">50dp</item>
<item name="android:gravity">center</item>
</style>
I want to remove the default icons that are on my buttons.
And I wish instead of that, when a button is checked, it's is highlighted in becoming yellow for example.
How can I do that ?
Thanks for the help (and sorry for my bad english).
And I wish instead of that, when a button is checked, it's is
highlighted in becoming yellow for example.
You can define your own radiobutton_selector.xml for radio button:
<?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/unchecked" />
<item android:state_checked="false" android:drawable="#drawable/checked" />
</selector>
and then set it to RadioButton like
<RadioButton
android:id="#+id/button_faute"
android:button="#drawable/radiobutton_selector"
style="#style/type_action"
android:layout_alignLeft="#id/button_service"
android:layout_alignTop="#id/button_bloc"
android:layout_marginTop="10dp"
android:text="#string/faute" />
//EDIT
also i found this tutorial or tutorial1 with full source code for customizing radio buttons in way you as you want to :P
Related
When I click on Radio Button, I want the color of the text and the color of the circle to change at the same time as in the image below.
NOTE
The color of choice for the circle and text is none of the colors of the colorPrimary or colorAccent.
If you want to change the text and color of the circles at the same time, follow these steps
1.add style below
<style name="radioButton" parent="AppTheme">
<item name="colorControlNormal">#0f0</item>
<item name="colorAccent">#f00</item>
</style>
2.add this selector.xml to drawable
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#0f0" android:state_pressed="true" />
<item android:color="#f00" android:state_checked="true" />
<item android:color="#0f0" />
</selector>
3.add selector and style to RadioButton
<RadioButton
android:id="#+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:text="select"
android:textColor="#drawable/selector"
android:textSize="12sp"
android:theme="#style/radioButton"
app:useMaterialThemeColors="false" />
FULL CODE:
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_margin="16dp"
android:checkedButton="#+id/one"
android:layoutDirection="ltr"
android:orientation="horizontal">
<RadioButton
android:id="#+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:text="select"
android:textColor="#drawable/selector"
android:textSize="12sp"
android:theme="#style/radioButton"
app:useMaterialThemeColors="false" />
<RadioButton
android:id="#+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="not select"
android:textColor="#drawable/selector"
android:textSize="12sp"
android:theme="#style/radioButton"
app:useMaterialThemeColors="false" />
</RadioGroup>
And Result
Note: I used Google Material
radiobutton.setOnCheckedChangeListener { buttonView, isChecked ->
if(radiobutton.isChecked())
{
// Changing radio button 1 color on checked.
radiobutton.setTextColor(Color.parseColor("#00ff00"))}}
The MaterialRadioButton uses as default a selector where the colors are based on colorOnSurface when unchecked and colorControlActivated when checked defined in your app theme.
You can override them using a android:theme attribute.
Something like:
<com.google.android.material.radiobutton.MaterialRadioButton
...
android:theme="#style/ThemeOverlay.RadioButton"/>
with:
<style name="ThemeOverlay.RadioButton" parent="">
<item name="colorControlActivated">#color/.....</item>
<item name="colorOnSurface">#color/.....</item>
<item name="colorSurface">#color/.....</item>
</style>
You can also set the useMaterialThemeColors attribute to false
<com.google.android.material.radiobutton.MaterialRadioButton
...
useMaterialThemeColors="false"/>
and apply a custom selector using CompoundButtonCompat#setButtonTintList.
with a selector like:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="..." android:state_enabled="true" android:state_checked="true" />
<item android:color="..." android:state_enabled="true" android:state_checked="false" />
<item android:color="..." android:state_enabled="false" android:state_checked="true" />
<item android:color="..." android:state_enabled="false" android:state_checked="false" />
</selector>
I am trying to maintain the material design ripple effect for devices with lollipop and above (21+) and stylize the button so that it doesn't have a large margin/space around it.
Example 1: Buttons with ripple effect but with margin/gap:
Example 2: Buttons with NO ripple effect and without the margin/gap:
I want the layout of Exmaple 2 with the ripple effect that is used in Example 1.
What my styles-v21 look like for Example 1 :
<?xml version="1.0" encoding="utf-8"?>
<resources>
... other styles ...
<style name="FacebookButton" parent="android:Widget.Material.Button">
<item name="android:colorButtonNormal">#ff3b5998</item>
<item name="android:layout_margin">0dp</item>
<item name="android:borderlessButtonStyle">#style/Widget.AppCompat.Button.Borderless</item>
</style>
<style name="GoogleButton" parent="android:Widget.Material.Button">
<item name="android:colorButtonNormal">#ffdd4b39</item>
<item name="android:layout_margin">0dp</item>
<item name="android:borderlessButtonStyle">#style/Widget.AppCompat.Button.Borderless</item>
</style>
<style name="TwitterButton" parent="android:Widget.Material.Button">
<item name="android:colorButtonNormal">#ff55acee</item>
<item name="android:layout_margin">0dp</item>
<item name="android:borderlessButtonStyle">#style/Widget.AppCompat.Button.Borderless</item>
</style>
<style name="SkipButton" parent="android:Widget.Material.Button">
<item name="android:colorButtonNormal">#ffdfa800</item>
<item name="android:layout_margin">0dp</item>
<item name="android:borderlessButtonStyle">#style/Widget.AppCompat.Button.Borderless</item>
</style>
</resources>
What my button layout looks like for Example 1:
<Button
android:id="#+id/login_with_facebook"
android:text="#string/login_with_facebook"
android:fontFamily="sans-serif-condensed"
android:textSize="16sp"
android:drawableLeft="#drawable/facebook_icon"
android:drawablePadding="25dp"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_weight="16.88"
android:textColor="#ffffff"
android:gravity="left|center_vertical"
android:paddingLeft="45dp"
android:theme="#style/FacebookButton" />
<Button
android:id="#+id/login_with_google"
android:text="#string/login_with_google"
android:fontFamily="sans-serif-condensed"
android:textSize="16sp"
android:drawableLeft="#drawable/google_icon"
android:drawablePadding="25dp"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_weight="16.88"
android:textColor="#ffffffff"
android:gravity="left|center_vertical"
android:paddingLeft="45dp"
android:theme="#style/GoogleButton" />
<Button
android:id="#+id/twitter_login_button"
android:text="#string/login_with_twitter"
android:fontFamily="sans-serif-condensed"
android:textSize="16sp"
android:drawableLeft="#drawable/twitter_icon"
android:drawablePadding="25dp"
android:layout_height="45dp"
android:gravity="left|center_vertical"
android:layout_width="match_parent"
android:layout_weight="16.88"
android:textColor="#ffffffff"
android:paddingLeft="45dp"
android:theme="#style/TwitterButton" />
<Button
android:id="#+id/login_anonymously"
android:text="#string/login_anonymously"
android:fontFamily="sans-serif-condensed"
android:textSize="16sp"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="50"
android:textColor="#ffffffff"
android:theme="#style/SkipButton" />
Any idea how I can achieve the look of Example 2 while using the ripple methodology I've presented? Any links to resources or help would be appreciated. Thanks.
Extra:
I've already read over the following
Coloring Buttons in Android with Material Design and AppCompat
How to properly style material buttons in android
You can use android:background="?android:attr/selectableItemBackground" to get a rectangular ripple.
Alternatively, you can create your own drawable. Here's the XML for the framework's selectable item background:
drawable/item_background_material.xml:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:attr/colorControlHighlight">
<item android:id="#android:id/mask">
<color android:color="#android:color/white" />
</item>
</ripple>
I have used Switch button, everything works fine when the app run it on 4.2 and above, but the text color not at all changed to white in 4.0.4 i have tried all possible solution
My Switch :
<Switch
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_below="#+id/facilityassetdescription"
android:layout_centerHorizontal="true"
android:layout_marginTop="7dp"
android:background="#drawable/offbuttonbg"
android:textColor="#style/toggle_text"
android:textOff="OFF"
android:textOn="ON"
android:thumb="#drawable/switchselector" />
My style file
<style name="toggle_text">
<item name="android:textColor">#color/toggle</item>
</style>
res/color/toggle_text.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#ffffff"/>
<!-- Default State -->
<item android:color="#ffffff"/>
</selector>
Kindly give any idea to get rid of this problem
You can use Switch-Compat intend of Switch using support v7
library.Try new concept
my.xml
<android.support.v7.widget.SwitchCompat
android:id="#+id/sc_push"
style="#style/switchStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:theme="#style/switchStyle"
app:theme="#style/switchStyle" />
style.xml
<style name="switchStyle">
<item name="colorControlActivated">#color/red</item>
<item name="android:colorForeground">#color/gray</item>
</style>
Best of luck
Use below style:
<style name="toggle_text" parent="#android:style/TextAppearance.Small">
<item name="android:textColor">#color/toggle</item>
</style>
And, In xml file for switch mention android:switchTextAppearance attribute instead of using android:textColor:
<Switch
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_below="#+id/facilityassetdescription"
android:layout_centerHorizontal="true"
android:layout_marginTop="7dp"
android:background="#drawable/offbuttonbg"
android:switchTextAppearance="#style/toggle_text"
android:textOff="OFF"
android:textOn="ON"
android:thumb="#drawable/switchselector" />
Use support 7 SwitchCompat
and put <item name="colorSwitchThumbNormal">#E21D1D</item> in your style
and <item name="colorSwitchThumbNormal">#E21D1D</item> in your theme
<android.support.v7.widget.SwitchCompat
android:id="#+id/switch_options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:padding="5dp"
app:showText="false" >
</android.support.v7.widget.SwitchCompat>
Gud Luck!!
for anyone if the above answers did not work try this..
<androidx.appcompat.widget.SwitchCompat
android:id="#+id/switch_is_in_stock"
style="#style/proximaNovaNormalColorBlack.size14"
android:layout_width="0dp"
android:layout_height="0dp"
android:maxLines="1"
android:padding="4dp"
android:textColor="#color/switch_thumb_selector"/>
answer is on this xml line -> android:textColor="#color/switch_thumb_selector"
I need to design a this type button which should have a background with another small image in the center and the text at the bottom.
While the current I am using is a textview as below
<TextView
android:id="#+id/homeButton1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#ffffff"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center"
android:text="Button"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#drawable/button_color"
android:textStyle="bold" />
But i cannot put an image in its center.
If I use android:drawableTop
this is the image I get
How can this be feasible?
try compound drawables here is an example
<TextView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/image"
android:gravity="center"
android:text="#string/text" />
output:
Put a bellow line to your TextView xml and change "img_src" to your image.
android:drawableTop="#drawable/img_src"
UPDATE 1 :
drawableTop only support to draw above the text.
If you want to draw image on the center, here is a sample code.
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background_image" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/center_image" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
android:text="TextView" />
</RelativeLayout>
Check this one, works exactly as you want
<Button
android:id="#+id/btn"
style="#style/MyButton"
android:drawableTop="#drawable/btn_my"
android:text="SomeText" />
btn_my.xml // add this inside drawable folder
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_launcher"
android:state_focused="true"
android:state_pressed="true" />
<item android:drawable="#drawable/ic_launcher"
android:state_focused="false"
android:state_pressed="true" />
<item android:drawable="#drawable/ic_launcher" android:state_focused="true" />
<item android:drawable="#drawable/ic_launcher"
android:state_focused="false"
android:state_pressed="false" />
</selector>
copy the below into styles.xml
<style name="MyButton">
<item name="android:layout_gravity">center_vertical</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:drawablePadding">2dp</item>
<item name="android:textSize">16dp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#ff29549f</item>
<item name="android:background">#null</item>
</style>
Here is the OUTPUT
I made a RadioButton with images.
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/radioType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/radioAlc"
style="#style/navbar_button"
android:checked="true"
android:drawableTop="#drawable/policeradio"
android:text="Barrage" />
<RadioButton
android:id="#+id/radioCrash"
style="#style/navbar_button"
android:drawableTop="#drawable/carcrashradio"
android:layout_marginLeft="-10dp"
android:text="Accidente" />
<RadioButton
android:id="#+id/radioMarch"
style="#style/navbar_button"
android:drawableTop="#drawable/marchradio"
android:layout_marginLeft="-10dp"
android:text="Manifestation" />
<RadioButton
android:id="#+id/radioRadar"
style="#style/navbar_button"
android:drawableTop="#drawable/radarradio"
android:text="Radar" />
</RadioGroup>
the problem is that with
<item name="android:button">#null</item>
I can remove the radio button, to let the user click the image, but it seems that it keeps the space to show it. In my case, I would like to remove that extra space that is bothering me !
Is it possible???
I apply that style
<style name="navbar_button">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:button">#null</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:layout_weight">1</item>
<item name="android:textSize">12dp</item>
</style>
I don't know if you use other styles we can't see here, but if I copy your code in a layout file and add this to my styles.xml:
<style name="navbar_button">
<item name="android:button">#null</item>
</style>
it works fine and there is no extra space.
EDIT:
You have to add the item <item name="android:padding">0dp</item> to your navbar_button style, then it works on Android 2.3, too.