Style of RadioButton magically changes when put into a Listview? - android

I made a DialogFragment with a RadioButton ontop and a ListView below containing more RadioButtons. And now I'm just wondering what style the ListView uses that the RadioButtons don't look the same as the "stand-alone" one.
Here is a snippet of my dialog.xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/buttonGroup"
android:layout_alignParentTop="true"
android:orientation="vertical">
<RadioButton
android:id="#+id/none"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/none" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/white" />
<ListView
android:id="#+id/conferences"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
And my list_item.xml that I inflate as the rows in getView of my ArrayAdapter:
<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/conference"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
My AppTheme is based on "Theme.AppCompat". I didn't change any style of a specific item in my View.
The "stand-alone" RadioButton has a white circle with a blue dot when selected and even has a different style while you press it down. The RadioButtons in the ListView are all black with a black dot and don't have a "press-down" style. It would be cool if I wouldn't need to programatically set styles for not checked/"press-down"/checked. I already tried to set 2 base Android themes that had "RadioButton" in their names on the ListView but nothing changed. Maybe there is a way to get the style of the "stand-alone" RadioButton and set it in the xml files for the other RadioButtons?
I would post an image but I'm new here so not already allowed to do this.
Update
I changed my AppTheme to this:
<style name="AppTheme" parent="Theme.AppCompat">
<item name="android:textViewStyle">#style/TextAppearance.AppCompat.Large</item>
<item name="android:radioButtonStyle">#style/RadioButtonStyle</item>
</style>
<style name="RadioButtonStyle">
<item name="android:textAppearance">#style/TextAppearance.AppCompat.Large</item>
</style>
And the RadioButtons in the ListView changed but not the "stand-alone" one. When I add the style to the RadioButton:
<RadioButton
android:id="#+id/none"
style="#style/RadioButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/none" />
the text changes but the RadioButton-circle is still there. For the RadioButtons in the ListView the circles are gone...

Make sure that LayoutInflater in your Adapter uses Activity context and not the application context.
Update:
Please remove android prefix from your style:
<item name="radioButtonStyle">#style/RadioButtonStyle</item>
And update RadioButtonStyle as follows:
<style name="RadioButtonStyle" parent="#style/Widget.AppCompat.CompoundButton.RadioButton" />

Related

How to change text color in spinner without using textview

I want to change text color for spinner without using textView, I have already searched and found some tutorials Android: Where is the Spinner widget's text color attribute hiding?
but the main thing is they have used textView.
<Spinner
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:entries="#array/Gender_Selection_arrays"
android:prompt="#string/Gender_Selection"
android:id="#+id/gendersel"
android:popupBackground="#67656c"/>
I know this code is not to change text color.
I have no idea how to do that,please guide me for doing the same.
Any help would be appreciable.
I got solution for this, Create a new xml "spinner_style.xml"
<?xml version="1.0" encoding="UTF-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#color/Black" />
And fix this layout as your spinner style in your activity:
ArrayAdapter<String > gender_adapter = new ArrayAdapter<String> (getActivity(), R.layout.spinner_style,gender_spinner );
Your Spinner
<Spinner
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:entries="#array/Gender_Selection_arrays"
android:prompt="#string/Gender_Selection"
android:id="#+id/gendersel"
style="#style/mySpinnerItemStyle"
android:popupBackground="#67656c"/>
mySpinnerItemStyle (Add this to styles.xml)
<style name="mySpinnerItemStyle" parent="Base.Widget.AppCompat.Spinner">
<item name="android:textColor">#color/my_spinner_text_color</item>
</style>
And finally in colors.xml
<color name="my_spinner_text_color">#FFFFFF</color>
mySpinnerItemStyle inherits from Base.Widget.AppCompat.Spinner and in that android:textColor attribute changes the color of the spinner text

TableLayout not shown properly

I'm having trouble with the layout of my Android app.
The graphical preview editor shows me an image like (and this is how I want it to look like):
I am using a table layout that looks like this:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:shrinkColumns="*"
android:stretchColumns="*" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:padding="5dp" >
<TextView
style="#style/leftColumn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Branche" />
<TextView
android:id="#+id/textViewBranche"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/rightColumn"
android:text="Branche"
/>
</TableRow>
</TableLayout>
styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!-- ... -->
<style name="leftColumn">
<item name="android:background">#2B60DE</item>
<item name="android:textColor">#ffffff</item>
</style>
<style name="rightColumn">
<item name="android:background">#C0C0C0</item>
<item name="android:textColor">#ffffff</item>
</style>
</resources>
If I run my app it looks like this:
As you can see only one text view is displayed. I would like the table layout to have 2 columns (just like in the preview). The first one (blue) indicating what I am displaying and the second (grey) the information provided by my adapter. However I only see the provided information of my adapter (in this case "sonstige (others)". So basically I only see the right column.
I have no idea why I can't see the content of the first text view (respectively the first row).
I played around with some properties like layout_width and layout_height but it doesn't change.
I'm running this app on my S3.
Any help would greatly be appreciated.
Cheers

The Same Selector Does Not Work Android

I am using a selector working fine in all activities as you can see its XML below.
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="#drawable/active_date_horizontal_5_pressed" /> <!-- pressed -->
<item
android:state_focused="true"
android:drawable="#drawable/active_date_horizontal_5_pressed" /> <!-- focused -->
<item
android:drawable="#drawable/active_date_horizontal_5" /> <!-- default -->
Except one activity, it does not work ridiculously. You can see three screenshots below which the selector works fine in the first two screenshots, and does not work in the last screen shot.
1)
2)
3)
These are customized GridViews which filled with customized BaseAdapter. Cells in the GridViews have an Xml Layout which its background is the selector. Any ideas?
It seems I solved my own problem.
You can see the Xml Layout which I am applied the selector below
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#drawable/calendar_cell_selector" >
<TextView
android:id="#+id/txtWeeklyEventName"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/imgCornerWeekly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/txtWeeklyEventName"
android:layout_alignRight="#+id/txtWeeklyEventName"
android:src="#drawable/corner_orange"
android:visibility="visible" />
</RelativeLayout>
You can see the background of the RelativeLayout is the selector, but the TextView fills upon the whole layout so Android cannot handle the selector.
Thus, I removed the background from the RelativeLayout and set it for the TextView, it totally works.
Thanks to me!

Android: Change text color in ListView for singleChoice

I have a listview set to use singleChoice. All I want to do is change the default background color to white and the text color to black. I cannot figure out how to do this. Here is my xml layout:
<ListView
android:id="#+id/lvSpeeds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/llToolbar"
android:layout_below="#id/rgSpeedUnits"
android:textColor="#android:color/black"
android:choiceMode="singleChoice"
android:background="#ffffff"
android:cacheColorHint="#00ffffff"
android:clickable="true"
android:divider="#ff000000"
android:dividerHeight="1dp"
android:focusable="true"
android:scrollingCache="true" />
EDIT: I should have pointed out that I want to change this only using xml layout files and NOT in code. I already know how to do this in code. Using a custom layout other than android.R.layout.simple_list_item_single_choice forces you to implement an adapter, bind, write more code, and so on. From viewing a lot more posts, it does not appear possible to change the text color using only xml. In fact, it doesn't seem possible to change anything on a row as the underlying layout android.R.layout.simple_list_item_single_choice is not accessible.
for a list view Use android selector
like this
and save it with anyname.xml and giveits reference to background of your list
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="#drawable/about_btn_hover"></item>
<item android:drawable="#drawable/about_btn"></item>
</selector>
and for changing text color add color folder in your res directory
create an xml save it textchangecolor.xml
and add the following lines to it
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#color/whiteColor"></item>
<item android:color="#color/bluetxt"></item>
</selector>
and give its refernce to the textcolor
try to this:
insert to this code in adapter in getview mathod:
LinearLayout mRowLayout = (LinearLayout) vi
.findViewById(R.id.list_item_layout);
final TextView title = (TextView) vi
.findViewById(R.id.list_item);
title.setText(Massage[position]);
mRowLayout.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
title.setTextColor(Color.RED);
});
here list_item code:
<?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="match_parent"
android:id="#+id/list_item_layout"
android:orientation="vertical" >
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/row_single"
android:layout_margin="5dp">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="#android:color/black"
android:padding="10dp"
android:textSize="16sp"
android:layout_toLeftOf="#+id/arrow"
android:id="#+id/list_item"/>
</RelativeLayout>
</LinearLayout>
The best solution I could find was to use styles.xml and set a theme for my dialogs.
styles.xml
<style name="DialogTheme" parent="AppTheme">
<item name="android:textColor">#color/text</item>
<item name="android:textColorAlertDialogListItem">#color/text</item>
+ other styles
</style>
In Java Built Dialogs:
ContextThemeWrapper theme;
theme = ContextThemeWrapper(view.getContext(), R.style.DialogTheme);
AlertDialog.Builder builder = new AlertDialog.Builder(theme);
In XML built Dialogs:
<myLayout
....
android:theme="#style/DialogTheme" />

How to I tell my own custom Spinner Layout to use my Theme?

How to I tell my own custom Spinner Layout to use my Theme?
Style:
<style name="SpinnerText" parent="#android:style/Widget.TextView.SpinnerItem">
<item name="android:textAppearance">#style/AnswerTextElement</item>
<item name="android:gravity">center_vertical|center_horizontal</item>
</style>
Theme:
<style name="ApplicationTheme" parent="android:style/Theme.NoTitleBar">
<item name="android:buttonStyle">#style/Button</item>
<item name="android:spinnerStyle">#style/Spinner</item>
<item name="android:spinnerItemStyle">#style/SpinnerText</item>
</style>
This works for default Spinners, however does not work with my custom layout:
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:layout_height="wrap_content" android:id="#+id/text1"
android:text="label name" android:layout_width="fill_parent"
android:layout_toLeftOf="#+id/check1"></TextView>
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/check1"
android:layout_alignParentRight="true" android:clickable="false"
android:focusable="false" android:focusableInTouchMode="false"
style="#style/CheckBoxPlainBackground"></CheckBox>
</RelativeLayout>
SOLVED:
You need to add
?android:attr/spinnerItemStyle
to the style attribute of the layout. Now it will act like the actual Spinner Item and all changed to the Theme will apply.
By the way - I found this in the SDK under YOUR_SDK\platforms\android-1.6\data\res\layout\simple_spinner_item.xml
You will be able to find other styles for other controls in this directory.
You want your TextViews in the RelativeLayout to use the same text style?
Add "style=#style/SpinnerText" attribute to the TextView.

Categories

Resources