Android's spinner text align right - android

I has a spinner contains 3 items:"English", "Simplified Chinese", "Traditional Chinese".
If I select "English", the spinner will show as below picture.
I means after select and show the selected one, but not all items list as below:
My layout.xml as below:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#drawable/spinner_selector"
android:gravity="right" />
</RelativeLayout>
I want to set the text align right.
But the width seems be set as the longest item.
And the text align left.
How can I modify it?

Related

TextView not clickable in Spinner when spinner contains ImageButton

I am using below layout xml as spinner row element (custom spinner). I see items (textView and ImageButton) getting displayed in spinner, but the item is not clickable (disabled), though i can click on ImageButton. I would like to have item especially text to be shown in the spinner when text is selected.
I am using android api 28.
Please suggest on how to do this.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageButton
android:id="#+id/buttonIcon"
android:scaleType="centerCrop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_remove"/>
</LinearLayout>

What is the correct layout for this structure?

What is the structure of such a layout? The text in the left column aligned to the right, the text of right column aligned to the left. Also rows are separated with a line. Thank you for hints!
You could make a ListView of your own items. That would give you a separator between all rows.
To set your own layout for ListView's item, you have to prepare your layout's xml file and set it like this:
yourListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.layout_file_for_your_item, items));
Item's layout (LinearLayout) could consist of 2 LinearLayouts. In first one (left) you'd have TextView that would be aligned right in parent. Second one (right) would have TextView aligned left in parent and margin-left set to some specific value to make a gap between two layouts.
Blue: ListView
Red: ListView's item (LinearLayout)
Green: First LinearLayout
Yellow: Second LinearLayout
You can try create listview field in your layout (with empty inside, not textviews or images) and then create another layout something like that:
Main layout:
<?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:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bla bla" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bla bla 2" />
<!-- listview with populated items -->
<ListView
android:id="#+id/mylist"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Populated items:
<TextView
android:id="#+id/itemleft"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:textStyle="bold"
android:gravity="right|center_vertical"
android:text="Location" />
<TextView
android:id="#+id/itemright"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:gravity="left|center_vertical"
android:text="Los Angeles, US" />
</LinearLayout>
Here is a preview of this settings:
There is of course only one item but it will be populated then. It's just a preview.
(Populated items will be used in listView later) Then try use for example base adapter implementation in you code, check here example: link. I hope you will understand it, however using custom listview wasn't the problem in this title :)

Android layout : Align a TextView and a Spinner

I would like to align a textview (saying : "Day :") with a Spinner where the user can choose the day of the week he wants (Monday, Tuesday, etc.)
when I try to align them :
<TextView android:id="#+id/labelSpinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/textSpinner1"
android:layout_toRightOf="#+id/spinner_days"
android:layout_alignParentTop="true"/>
<Spinner android:id="#+id/spinner_days"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/labelSpinner1"
android:layout_alignParentLeft="true"
android:drawSelectorOnTop="true"/>
the result I get is that I only see the Spinner, and the TextView isn't showing (or is underneath the Spinner)
Thank you for your help!
I guess you want the Spinner to the right of the TextView? Check the following code:
<TextView
android:id="#+id/labelSpinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="#string/textSpinner1" />
<Spinner
android:id="#+id/spinner_days"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/labelSpinner1"
android:layout_toRightOf="#id/labelSpinner1"
android:drawSelectorOnTop="true" />
Your problem was that the spinner filled the whole view (android:layout_width="fill_parent") while you forced the TextView to be right of the Spinner (so outside of the screen --> invisible for you)

Android custom spinner

I have a custom spinner that uses two custom views, one for the drop down and one for the currently selected item.
My problem is, the custom view for the currently selected item is always at least as wide as the longest item in the spinner list and it leaves a sizeable gap on the left hand side of my custom view. If I select the longest item from the spinner, the gap disappears. I have tried android:gravity="left" for my custom view. Also note that this only seems to be an issue with Android 3.0+.
Here is the code for my custom spinner view:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/folder_detail" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_width="wrap_content">
<TextView android:text="Folder Name"
android:id="#+id/folder_name" android:textColor="#ffffff"
android:layout_width="wrap_content" android:textSize="16sp"
android:textStyle="bold"
android:layout_height="wrap_content" android:layout_marginTop="0dp">
</TextView>
<TextView android:id="#+id/folder_count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="13sp" android:text="(0) " android:textColor="#color/light_blue" android:layout_marginLeft="2dp"/>
<ImageView android:id="#+id/icon" android:src="#android:drawable/arrow_down_float"
android:layout_marginRight="1dp" android:scaleType="fitXY"
android:layout_gravity="bottom" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>
This is intentional. It prevents the spinner from drastically changing size as the selection changes.
You're not meant to include an arrow as part of the item view; this is normally included as part of the background 9-patch of the spinner itself. The gravity setting on the spinner itself controls the alignment of the selected item view within the spinner. The Holo theme aligns it to the left, while the legacy themes horizontally center.

How to show Android Right-aligned ListItem data without overlapping the scrollbar?

I've basically the same thing as ListView row styling - left aligned text, and right-aligned icon : a listview with listitems in it that are right-aligned:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:text="Name"
android:id="#+id/list_item_name"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
>
</TextView>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:text="0x0 0s"
android:id="#+id/list_item_value"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
>
</TextView>
</RelativeLayout>
and it works fine.. .except that the list_item_value text is obscured by the scrollbar. There's plenty of blank space between the names and values, scooting all the values to the left a little would be fine, but I don't know how to do that either.
try setting android:layout_marginRight on list_item_value. Try a value like 10dp or so - just to get a feel, then adjust accordingly.

Categories

Resources