Radio Button Text over Button - android

I'm trying to create a view in android that looks like:
Text
Radio Button 1
...
...
I thought it was simple enough but at least in the layout preview and emulator I'm seeing the text for the RadioButton appear on top of the actual radio button (the circle), see the attached screen shot. I'm guessing this is something very trivial but I'm messed around with gravity and every other setting I can think of and nothing. Android 3.1 if that matters.
Here's my layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#color/white"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="#+id/question_text"
android:textColor="#color/primary_gray"
android:layout_width="match_parent"
android:text="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
android:layout_height="wrap_content"/>
<RadioGroup android:id="#+id/multiple_choice_one_answer_group"
android:background="#color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:background="#color/primary_gray"
android:textColor="#color/black"
android:textSize="10sp"
android:text="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" />
</RadioGroup>
</LinearLayout>
Screenshot:

The problem is setting the background. If you remove the background from the RadioButton and keep it on the RadioGroup, it has the effect you're looking for. In general, setting the background of a Button to everything removes the button look; try it on a regular Button and you'll see. This is because the background for a Button is already set to something, and you're replacing it with a flat color.
Try this instead:
<RadioGroup android:id="#+id/multiple_choice_one_answer_group"
android:background="#color/primary_gray"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:textColor="#color/black"
android:textSize="10sp"
android:text="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" />
</RadioGroup>

Setting the background in the java code instead of the layout solves this as well.

Related

Android: Button is faded gray when it should be transparent

I want a basic button that has a completely transparent background (so the only thing visible is the text.)
As per other answers, I've set android:background="#0000", but still, it has a faded gray color.
Here is the complete layout code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#3176C0">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0000"
android:text="TRANSPARENT?" />
</RelativeLayout>
What it looks like:
What I want:
Turns out that it is not a background showing, but a shadow from the default button style.
Therefore, the answer is that the button style needs to be changed by adding this to the button:
style="?android:attr/borderlessButtonStyle"
Try this code ...
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:text="Transparent"/>
Answer is borderlessbutton.
Refer: https://developer.android.com/guide/topics/ui/controls/button#Borderless
Try this:
android:background="#android:color/transparent"

Android: Strange gap between buttons

I am just beginning with Android. I am making a layout with buttons, but a very strange thing is happening — there is a small gap between them. Setting margin and padding to 0 is also not helping.
Here is my simple layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
It is looking like this:
Make your own flat Button style with ripple effect [lollipop] and without it [pre-lollipop]:
https://gist.github.com/dmytrodanylyk/3513e42839ae4309d2b5
You have to use android:background to change the background of button. For example if you use android:background = "#FFFFFF" your visual problem is fixed. In reality , there is no space between the buttons, their background creates an "illusion"

listview item row with textview and imagebutton only show ripple effect on imagebutton

I have my listview, with a text and a ImageButton near to it, should be on the right side in the future. I want to see a ripple effect when i touch on the text or white field. How can i do this?
At the moment, theres only a ripple effect when i touch the image button.
Thats my code for list_item, where i set up the layout for a row:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text"
android:elevation="2dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textColor="#android:color/black"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:layout_centerVertical="true"
android:layout_marginTop="40dp"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
/>
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:src="#drawable/ic_delete_black_24dp"
android:tint="#F44336"
android:backgroundTint="#color/background"
/>
</RelativeLayout>
I found out, that i have to use a LinearLayout, but i dont know, how i have to use it correctly. My ListView looks like this:
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#E0E0E0"
android:dividerHeight="1dp"
android:drawSelectorOnTop="true"
>
</ListView>
Here's a screenshot how it looks currently:
Any suggestions to fix? ^^ Thanks in advance
EDIT:
I forgot to say, that the OnItemClickListerner from listview, doesnt work anymore, when i have defined the imagebutton
As per my knowledge, Ripple is only shown to clickable views (ListView's row, Buttons or other clickable objects). As per material theory animation shown from those objects which responds (I don't think that TextView needs to respond on click). In your case TextView is not showing because simply it dosen't work normally but it works on buttons (ImageButton is Button).
If you still want this simply use one of these third partly libraries -
https://github.com/lgvalle/Material-Animations
or
https://github.com/balysv/material-ripple
Its the easiest way!

Text on right of CheckBox in Android

I'm trying to imitate the CheckBoxs like in the Settings app on my phone.
It looks like that:
I've tried using separate TextViews, but that way only the checkmark is clickable, rather than the text and the checkmark.
Is there a way to accomplish that?
I also tried a CheckedTextView but couldn't find the right drawable to use.
As far as I know this should work
<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:text="Text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:button="#null"
android:drawableRight="?android:attr/listChoiceIndicatorMultiple"/>
With android:button="#null" you remove standard checkbox button / image, and after you just add checkbox image as right drawable (or use drawableEnd to support RTL)
android:drawableRight="?android:attr/listChoiceIndicatorMultiple"
Is there a way to accomplish that?
I also tried a CheckedTextView but couldn't find the right drawable to use.
CheckedTextView will solve your issue perfectly:
Please look for example on simple_list_item_checked.xml (it is android embedded layout)
CheckedTextView have next attribute:
<!-- Drawable used for the check mark graphic. -->
<attr name="checkMark" format="reference"/>
So you only need to set checkMark to proper selector (If you do not have resource you could use btn_check.xml. It is inside android):
<CheckedTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkMark="#drawable/your_selector_with_proper_checked_states"
/>
try the following Xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="10"
tools:context="com.example.pager.MainActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Anything you want here" />
<CheckBox
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="8" />
</LinearLayout>
It works fine

Change background color dynamically and use a border

I have the following template
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/borders" >
<RelativeLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<ImageView
android:id="#+id/category_starred_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/rating_not_important"
android:contentDescription="#string/app_name" />
<TextView
android:id="#+id/category_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/category_starred_icon"
android:textIsSelectable="false"
android:textSize="18sp"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:textColor="#android:color/white"
android:hint="#string/category" />
</RelativeLayout>
</LinearLayout>
i get a warning This RelativeLayout layout or its LinearLayout parent is possibly useless; transfer the background
attribute to the other view, i want solve it, but i have the follow problem i have a border in the LinearLayout is like a drop shadow but when i change the background color in my code
mLayout.setBackgroundColor(Color.paseColor(mColorString));
i lost the border, how i can solve the warning witout lose the border, the main problem is the background colors are dynamically
Have a look at this: http://developer.android.com/guide/topics/resources/drawable-resource.html#LayerList
This is an drawable that organizes other drawables in a layer list (one on top of the other). With it, you can use other drawables, like shape, to define the borders and the background the way you want.
I can't make a complete answer now, but if you search for this, I'm sure you can do it. When I have more time, I may come back to help you, if you haven't found out how to solve this yet.

Categories

Resources