Android: Spinner selection aligned to the left - android

I finished the English version of my application and now I am working on the Arabic localization. Arabic is a right-to-left language, so I need to adjust a lot of things in my layout, including my spinner display.
When I choose an item from the spinner selection menu, I see the following,
As you can see "Allergy" is aligned to the left and on top of the spinner arrow. I need it to be aligned to the right.
NOTE The Arabic webservices aren't finished yet, that's why the data in the image is still in English.
EDIT
Spinner mSpecialtySpinner = (Spinner) findViewById(R.id.specialty_appointment_spinner);
ArrayAdapter<Specialty> adapter = new ArrayAdapter<Specialty>(AppointmentReservationActivity.this,android.R.layout.simple_spinner_item, specialities);
adapter.setDropDownViewResource(R.layout.spinner_item);
mSpecialtySpinner.setAdapter(adapter);

Well you can have another layout for Spinner and pass it to ArrayAdapter's constructor.
make an xml file naming spinner_layout.xml in layout like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_width="fill_parent"
android:textSize="20sp"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:gravity="right"
/>
and now pass it in ArrayAdapter's constructor like this:
new ArrayAdapter<String>(this,R.layout.spinner_layout,conversionsadd);
Please remember:
what we have provided as R.layout.spinner_layout in ArrayAdapter constructor is layout of Spinner. and what we will provide as setDropdownView() is Spinner item (drop down item).
You can have a look at my another relative answer here

All you need to do is create a new spinner dropdown layout and set android:gravity="right" on the appropriate widget.
Call it with:
yourSpinner.setDropDownViewResource(R.layout.right_aligned_spinner_dropdown_item);

Related

How to make the list of spinner items rounded? Not the spinner itself, but dropdown list

I want to display the items of spinner in rounded rectangle list, not the default rectangle. I don't mean the spinner itself, but the list which falls after arrow is clicked. Is there a way to do this? The attempt to make custom_rounded_layout and attaching to it adapter changed the layout of each item, not the list itself. However, I want the list itself to be rounded, not the items. Right now the list is the default one. I want to make the corners of the list a bit rounded. Thanks in advance
Create a layout file with a rounded background textview like this:
spinner_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="#dimen/size_40"
android:background="#drawable/bg_round"
android:gravity="center_vertical"
android:id="#+id/ssdsd"
android:layout_margin="16dp"
android:padding="10dp"
android:text="#string/app_name" />
Now pass this in the spinner adapter
Spinner spinner = findViewById(R.id.yourSpinner);
spinner.setAdapter(new ArrayAdapter<String(this,R.layout.spinner_item,R.id.ssdsd,list));
To change the popup background, use this on your Spinner xml code:
android:popupBackground="#drawable/bg_round"
NB: You might need to adjust padding and margin to make it look good, this is just an idea of how you can do it.

Spinner layout cannot be resolved or is not a field

I am trying to customize the default appearance of a Spinner item.
I have defined a layout in a file by the name of :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:textSize="20sp" />
And here when initializing the spinner I am trying to assign this layout to it:
ArrayAdapter<String> nearByLocationsAdpt = new ArrayAdapter<String>(this, android.R.layout.spinner_item_layout,nearByLocationsArray);
But it keeps saying that spinner_item_layout cannot be resolved or
is not a field however it is present in the layout folder?
Another related question is it possible to view and edit the default spinner layout
xml file i.e. android.R.layout.simple_spinner_item
The problem is this:
android.R.layout.spinner_item_layout
You are using the default layout. You have to supply the one you created instead. Depending on the how you named your layout that would be something like this:
R.layout.my_spinner_item

Android: How to make rows in a ListView have variable heights?

I have a ListView which I want to have a fixed height (say 150dp). Then, inside the ListView, for individual rows, the text can be of varying length and so I would like the rows to be of different heights.
So far, I only got all the rows having same height and so long text got cut-off.
Any way to solve this problem?
Finally, managed to get it to work.
I am using the following adapter code:
ArrayAdapter<String> mcqAnsAdapter = new
ArrayAdapter<String>(this, R.layout.mylist, mcqOptions);
I have created a new layout mylist.xml as follows:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:paddingLeft="6dip"
android:paddingRight="6dip"
/>
The above is similar to the Android defined "simple_list_item_single_choice.xml" except I
changed the height to "wrap_content".
The strange thing was that the first time I tried it didn't work. Then, I made some changes here and there and then go back to this version again, it worked. Didn't know what happened. But anyway, very happy to see it working.
Thanks to all who have tried to help. Most appreciated.
Now, another question -- can I put the checkmark on the left of the text instead of the default right side?
You can try to set the row minHeight to 150dp and the height to wrap_content
You should use a custom list_item layout, this may help: Android Lists: ListActivity And ListView II – Custom Adapter And List Item View
convertedview.setlayoutparams(new listview.setlayoutparams(width, height));
try like this. It will work.

Android: what is the best way to show a TextView in case if no item in a ListView?

I need to show a list of items, the items are read from a database, and it is possible there is no item, in this case, I just want to show a TextView saying "there is no item", I think I could implement this by using relative layout, both list and text are in center of parent, they are displayed alternatively, but is there any way better than this?
Thanks!
Adding to Aleadam , Bill Mote
You may call at any time AdapterView.setEmptyView(View v) on an AdapterView object to attach a view you would like to use as the empty view.
The snippet is as follows:
empty = (TextView)findViewById(R.id.empty1);
list = (ListView)findViewById(R.id.list1);
list.setEmptyView(empty);
Make sure you keep the ListView and TextView inside the same parent.
For detailed description please refer this
If you're using a ListActivity, that is the default behavior. If not, then you can set the ListView visibility to GONE and a TextView visibility to VISIBLE.
Aleadam is right. Here's an example XML in support of his answer:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#drawable/red"
android:gravity="center_vertical|center_horizontal"
android:textStyle="bold"
android:text="#string/error_no_groups"
/>
</LinearLayout>
The first tutorial on the Android developer website explains how to do this:
http://developer.android.com/resources/tutorials/notepad/notepad-ex1.html
(Look under Step 4)
Snippet:
The ListView and TextView can be thought as two alternative views, only one of which will be displayed at once. ListView will be used when there are notes to be shown, while the TextView (which has a default value of "No Notes Yet!" defined as a string resource in res/values/strings.xml) will be displayed if there aren't any notes to display.
The View with the empty id is used automatically when the ListAdapter has no data for the ListView. The ListAdapter knows to look for this name by default. Alternatively, you could change the default empty view by using setEmptyView(View) on the ListView.

Set, get text and value in listitem view in Android

I have listview collection that show id and name in Android. I following example from this resource site.
But actually that example only display name. I wish similar like checkboxlist in ASP.NET that contain property text and value. So the text to show name and value store id. When user select one item, I could retrieve id by viewtext.
My question is how do I set id and name in same viewtext item? I wish could pass the id to another layout.
I know that possible using multi viewtext then set invisible of viewtext using map. How that could be waste memory.
i would suggest a custom listview. you can google for "custom listview" and will find a bunch of tutorials on that. simply said, it is a listview that has a custom row that you can specify in a xml file.
here is a quite useful tutorial to start with.
here is what i coded, maybe it will help you. note that this is a bit different, because i am not defining a listactivity but a listview widget. but concerning the custom row item, it won't matter.
this will create a listview and specify an adapter for that listview.
CustomListView listview = (CustomListView) findViewById(R.id.list);
ArrayAdapter<String> arrayadapter = new ArrayAdapter<String(getApplicationContext(), R.layout.rowoflistview, R.id.label);
listview.setAdapter(arrayadapter);
my rowoflistview.xml looks like the following. it adds an image and a text to each row of the list. you can of course change it to (mostly) whatever you want.
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/rowselector"
>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/musicicon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/musicicon"
android:paddingLeft="3px"
/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26px"
android:layout_marginLeft="3px"
android:singleLine="true"
android:ellipsize="end"
android:focusable="false"
/>
</LinearLayout>
as i wanted the listview as a widget in my main activity and not as a fullscreen activity, i had to do it other than you when it comes to event listening and click listening. if you want it a bit easier, be sure to extend the listactivity in your custom listview-activity and override the default methods.
hope that was understandable to get a grip on the topic ;)
Views have an associated tag Object map, which could contain your id http://developer.android.com/reference/android/view/View.html#getTag%28int%29

Categories

Resources