Ripple effect position when checkbox button is set to null - android

Ripple effect problem when checkbox button is set to null:
Style used
<style name="MyRow">
<item name="android:button">#null</item>
<item name="android:drawableRight">#drawable/abc_btn_radio_material</item>
<item name="android:drawableEnd">#drawable/abc_btn_radio_material</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:padding">#drawable/dafaultPadding</item>
This is the radiogroup containing the radio buttons.
<RadioGroup android:layout_height="wrap_content" android:layout_width="match_parent">
<android.support.v7.widget.AppCompatRadioButton style="#style/MyRow"
android:id="#+id/one"
android:text="#string/someText"
/>
<android.support.v7.widget.AppCompatRadioButton style="#style/MyRow"
android:id="#+id/two"
android:text="#string/someText"
/>
<android.support.v7.widget.AppCompatRadioButton style="#style/MyRow"
android:id="#+id/three"
android:text="#string/someText"
/>
</RadioGroup>
Ripple animation works as expected when the android:button property is set to some drawable.
When null, it has this behaviour on Marshmallow (ripples at the middle of the row).
Click Here to see the image
In lollipop works correctly except for the fact that it has no bounds and the ripple expands to the whole window.
The only way to solve this in my mind is to create custom views for each row containing a textview on the left of a radiobutton.
But in this way I will have to create a monster-like controller (to select checkboxes when I press on the textview) and I will lose all the advantages of the radiogroup.

Try using following style for your radio button:
<style name="RadioButton">
<item name="android:background">?selectableItemBackground</item>
<item name="android:button">#null</item>
<item name="android:drawableRight">?android:attr/listChoiceIndicatorSingle</item>
<item name="android:drawablePadding">#dimen/yourDrawablePadding</item>
<item name="android:padding">#dimen/buttonPadding</item>
</style>
First of all, ?selectableItemBackground will fix your ripple problem.
Also use ?android:attr/listChoiceIndicatorSingle as your drawable to allow beautiful material animation on checking/unchecking radio button.
You can see the difference below (Google Pixel XL, 7.1.2):

I had similar problem. Try to set <item name="android:background">?attr/selectableItemBackground</item> in your style or use custom ripple and set it.

I has same issue with radiobutton, try adding android:button="#drawable/radiobutton_drawable" with android:paddingLeft="10dp" and remove android:drawableRight

set radio button background transparent
android:background="#android:color/transparent"

Related

Make Android ImageButton look like Button

The documentation for ImageButton says:
By default, an ImageButton looks like a regular Button
This is obviously wrong because when I put both into a layout:
<Button
android:id="#+id/button2"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_margin="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/imageButton3"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_margin="20dp"
app:layout_constraintStart_toStartOf="#+id/button2"
app:layout_constraintTop_toBottomOf="#+id/button2" />
and run it, it looks like this:
The Button has a color and a shadow while the ImageButton is just a gray rectangle (with round corners I think).
Unfortunately I don't understand at all how styles work. My main question though is how do I need to change the ImageButton's configuration to actually make it look like a regular Button.
Are you using material design? Your Button does not look like the regular button that default style is #style/Widget.AppCompat.Button, but like the material button, which default style is #style/Widget.MaterialComponents.Button. If you set the style of the Button to #style/Widget.AppCompat.Button, you will see it looks the same as the ImageButton with default style #style/Widget.AppCompat.ImageButton
This is #style/Widget.AppCompat.Button, you can see that it has been expanded on the basis of #style/Widget.AppCompat.Button:
<style name="Widget.MaterialComponents.Button" parent="Widget.AppCompat.Button">
<item name="enforceMaterialTheme">true</item>
<item name="android:background">#empty</item>
<item name="enforceTextAppearance">true</item>
<item name="android:textAppearance">?attr/textAppearanceButton</item>
<item name="android:textColor">#color/mtrl_btn_text_color_selector</item>
<item name="android:paddingLeft">#dimen/mtrl_btn_padding_left</item>
<item name="android:paddingRight">#dimen/mtrl_btn_padding_right</item>
<item name="android:paddingTop">#dimen/mtrl_btn_padding_top</item>
<item name="android:paddingBottom">#dimen/mtrl_btn_padding_bottom</item>
<item name="android:insetLeft">0dp</item>
<item name="android:insetRight">0dp</item>
<item name="android:insetTop">#dimen/mtrl_btn_inset</item>
<item name="android:insetBottom">#dimen/mtrl_btn_inset</item>
<item name="android:stateListAnimator" ns2:ignore="NewApi">#animator/mtrl_btn_state_list_anim</item>
<item name="cornerRadius">#null</item>
<item name="elevation">#dimen/mtrl_btn_elevation</item>
<item name="iconPadding">#dimen/mtrl_btn_icon_padding</item>
<item name="iconTint">#color/mtrl_btn_text_color_selector</item>
<item name="rippleColor">#color/mtrl_btn_ripple_color</item>
<item name="backgroundTint">#color/mtrl_btn_bg_color_selector</item>
<item name="shapeAppearance">?attr/shapeAppearanceSmallComponent</item>
</style>
So, you can customize a new style for ImageButton, and then add those new style attributes of material button to the custom style (mainly cornerRadius, elevation, and rippleColor)
or set the button's style to #style/Widget.AppCompat.Button, while will also make the ImageButton looks like the Button :P

Android API 21 changes my custom button background

I am testing out my app for API21 on Android! Unfortunately, my buttons seem weird - there is some kind of background color added. The background images did not change - they are completely transparent except for the border. (The different text and size is due to the screenshot).
Here you see the buttons before and since API 21: http://imgur.com/9EEcl0o,yVEnJkI#0
I already tried android:elevation="0dp" and android:background="#android:color/transparent". Anybody knows why my buttons change? Thank you very much!
layout.xml:
<Button android:id="#+id/sm_achievements_btn"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1.1"
android:background="#drawable/menu_btn_selector"
android:onClick="showAchievements"
android:text="#string/sm_achievements_btn"
android:padding="#dimen/padding_std"
android:layout_marginTop="#dimen/margin_small"
android:textColor="#color/button_text"
android:textSize="#dimen/text_xlarge"
style="#style/lbm_button"/>
menu_btn_selector.xml:
<item android:drawable="#drawable/menu_button"
android:state_pressed="false"/>
style.xml:
<style name="lbm_button" parent="android:Theme.Holo.Light">
<item name="android:textAllCaps">false</item>
</style>
Material buttons have a default stateListAnimator that provides state-based elevation (e.g. 0dp when disabled, 1dp when enabled). You can clear it by setting android:stateListAnimator="#null" in your style or directly on the Button.
Here is what that would look like on your button XML:
<Button android:id="#+id/sm_achievements_btn"
...
android:textAllCaps="false"
android:stateListAnimator="#null" />
Also, you're using the wrong parent for your button style. You should never set a theme as the parent for a widget style. Regardless, here is what that should look like if you prefer that route:
<Button android:id="#+id/sm_achievements_btn"
...
style="#style/MyButtonStyle" />
<style name="MyButtonStyle" parent="android:Widget.Material.Light.Button">
<item name="android:textAllCaps">false</item>
<item name="android:stateListAnimator">#null</item>
</style>

How do I set EditText background and ellipsize attributes via #style?

I have EditText in my XML as
<!-- Inside RelativeLayout -->
<EditText
android:id="#+id/edtEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/edtPhone"
android:layout_alignLeft="#id/strut"
android:layout_alignBaseline="#+id/txtLabelEmail"
android:inputType="textEmailAddress"
android:singleLine="true"
android:imeOptions="actionDone"
android:hint="#string/profile_email_hint"
style="#style/listTextValue"
android:background="#drawable/edittext_bg"
android:ellipsize="end" />
Style
<style name="listTextValue" parent="android:Widget.EditText">
<item name="android:textSize">#dimen/listTextSize</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#color/black</item>
<item name="android:background">#drawable/edittext_bg</item>
<item name="android:focusable">true</item>
<item name="android:focusableInTouchMode">true</item>
<item name="android:singleLine">true</item>
<item name="android:ellipsize">end</item>
</style>
If I remove background attribute from EditText in Layout xml, background image will be disappeared.
If I remove ellipsize attribute from EditText in Layout xml, hint text will be expand to two lines.
As this attribute is available in Style from Eclipse's suggestion, why don't they work when put in the style?
Edit: My bad, GUI editor didn't show the background. It was another screen.
I have another layout XML with the same attributes for EditText, the background was shown correctly.
Difference between the layout would be that the first one was used inside a Fragment while the latter used directly within Activity.
This is really strange :S
I think you should try android:textAppearance="#style/your_style". It works for TextView and TextView is parent of EditText. I think it should also works.

Button background as transparent

I have a button. When I press the button I have to make text as bold otherwise normal. So I wrote styles for bold & normal.
<style name="textbold" parent="#android:style/TextAppearance">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textStyle">bold</item>
</style>
<style name="textregular" parent="#android:style/TextAppearance">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textStyle">normal</item>
</style>
Now I have a button_states.xml as:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true"
style="#style/textbold" />
<item android:state_focused="false" android:state_pressed="true"
style="#style/textregular" />
<item style="#style/textregular" />
</selector>
In my layout for this button, I have to make the background as transparent too...How will I do it? My layout code is :
<Button android:id="#+id/Btn" android:background="#drawable/button_states" />
How will I include background as transparent in my style?
To make a background transparent, just do android:background="#android:color/transparent".
However, your problem seems to be a bit deeper, as you're using selectors in a really weird way. The way you're using it seems wrong, although if it actually works, you should be putting the background image in the style as an <item/>.
Take a closer look at how styles are used in the Android source. While they don't change the text styling upon clicking buttons, there are a lot of good ideas on how to accomplish your goals there.
Try new way to set background transparent
android:background="?android:attr/selectableItemBackground"
You may also use:
in your xml:
android:background="#null"
or in code:
buttonVariable.setBackgroundColor(Color.TRANSPARENT);
use #0000 (only four zeros otherwise it will be considered as black) this is the color code for transparent. You can use it directly but I recommend you to define a color in color.xml so you can enjoy re-usefullness of the code.
Add this in your Xml - android:background="#android:color/transparent"
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button"
android:background="#android:color/transparent"
android:textStyle="bold"/>
I achieved this with in XML with
android:background="#android:color/transparent"
I used
btn.setBackgroundColor(Color.TRANSPARENT);
and
android:background="#android:color/transparent"
Selectors work only for drawables, not styles. Reference
First, to make the button background transparent use the following attribute as this will not affect the material design animations:
style="?attr/buttonBarButtonStyle"
There are many ways to style your button. Check out this tutorial.
Second, to make the text bold on pressed, use this java code:
btn.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
// When the user clicks the Button
case MotionEvent.ACTION_DOWN:
btn.setTypeface(Typeface.DEFAULT_BOLD);
break;
// When the user releases the Button
case MotionEvent.ACTION_UP:
btn.setTypeface(Typeface.DEFAULT);
break;
}
return false;
}
});
We can use attribute android:background in Button xml like below.
android:background="?android:attr/selectableItemBackground"
Or we can use style
style="?android:attr/borderlessButtonStyle" for transparent and shadow less background.
You can achieve that by setting the colors alpha channel.
The color scheme is like this #AARRGGBB there A stands for alpha channel(transparency), R stands for red, G for green and B for blue.
Step 1:
Create a new resource file in drawable and copy paste
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:color="#fff" android:width="2dp"/>
<corners android:radius="25dp"/>
<padding android:right="15dp" android:top="15dp" android:bottom="15dp" android:left="15dp"/>
</shape>
save it as ButtonUI(let's say)
Step 2: Apply the UI to the button xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="join the crew"
android:background="#drawable/ButtonUI"
android:textColor="#fff"/>
I'd say extend Borderless style.
<style name="Button" parent="Widget.AppCompat.Button.Borderless">
<item name="android:textColor">#color/blue</item>
<item name="android:textAllCaps">true</item>
</style>
Code:
button.setVisibility(View.INVISIBLE);
Xml:
android:background="#android:color/transparent"
You can do it easily by adding below attribute in xml file. This code was tested plenty of time.
android:background="#android:color/transparent"
You apply the background color as transparent(light gray) when you click the button.
ButtonName.setOnClickListener()
In the above method you set the background color of the button.

Problem with Android togglebutton

I tried to set up my togglebutton properties layout using style.xml.
Here's my code:
<ToggleButton android:id="#+id/button_toggle_1" style="button_test_1"/>
<ToggleButton android:id="#+id/button_toggle_2"
android:textOff="test2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ToggleButton android:id="#+id/button_toggle_3" style="button_test_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
For ID=button_toggle_1, I apply a style (style.xml), here's the code:
<style name="button_test_1">
<item name="android:textOff">test1</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="button_test_3">
<item name="android:textOff">test3</item>
</style>
I can see button 2 and 3 but the text for button 3 is different from what I set it to be. button 3 text = "APAGADO". From this test, I conclude that togglebutton simply cannot set its properties using style.xml. Is this true? or did I do something wrong?
If toggle button is "special", anyone knows the reason why? is this "special" condition applies to other things as well?
Thanks!
You are not referencing the styles correctly. It is style="#style/style_name". See documentation.

Categories

Resources