How to set same appearance for spinner in XML design? - android

I'm using two spinner in my activity but each spinner having different appearance. Anyone can help me to fix this kind of error???
This is first spinner image
This is second spinner image (Please see the image)
String [] values =
{"All Town","Paris","Kodambakkam","Nungambakkam","T.Nagar","Egmore"};
Spinner spinner = (Spinner) v.findViewById(R.id.town);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(adapter);
String [] values1 =
{"Select Doctor","Doctor1","Doctor2","Doctor3","Doctor4"};
Spinner spinner1 = (Spinner) v.findViewById(R.id.doctor);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values1);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner1.setAdapter(adapter1);
XML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/town" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/doctor" /></LinearLayout>
Please help me. Thanks in advance!!!!

Try this on second spinner... you have pass first adapter object to second adapter
String [] values1 =
{"Select Doctor","Doctor1","Doctor2","Doctor3","Doctor4"};
Spinner spinner1 = (Spinner) v.findViewById(R.id.doctor);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values1);
adapter1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner1.setAdapter(adapter1);

Related

android spinner drop down items not shown

I'm trying to show simple spinner
mSpinnerHeaderType = (Spinner) findViewById(R.id.spinner);
String[] items = new String[]{Constants.TYPE_112R, Constants.TYPE_314R};
ArrayAdapter<String> adapter = new ArrayAdapter<>(mContext, android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinnerHeaderType.setAdapter(adapter);
In layout xml
<Spinner
android:id="#+id/spinner"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|left"
android:spinnerMode="dropdown"></Spinner>
But when I click on spinner,
either 1. the dropdown list width is almost zero
OR 2. there are no itmes in dropdown
I tried, 1. giving spinner width as match_parent in xml layout and 2. using dropDownWith property for spinner etc, but nothing working
See image below:
What wrong I'm doing?
try this.
mSpinnerHeaderType = (Spinner) findViewById(R.id.spinner);
String[] items = {Constants.TYPE_112R, Constants.TYPE_314R};
ArrayAdapter<String> adapter = new ArrayAdapter<>(mContext, android.R.layout.simple_spinner_item, items);
mSpinnerHeaderType.setAdapter(adapter);
xml file
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"></Spinner>

Why does the second spinner doesn't have margins/paddings

These are my string arrays that work just fine:
<string-array name="spinner1">
<item>KB</item>
<item>MB</item>
<item>GB</item>
<item>TB</item>
</string-array>
<string-array name="spinner2">
<item>Kb/s</item>
<item>Mb/s</item>
<item>Gb/s</item>
<item>Tb/s</item>
</string-array>
This is the XML:
<android.support.v7.widget.AppCompatSpinner
android:spinnerMode="dropdown"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/spinner1"
android:layout_weight="20"
android:layout_gravity="center_vertical" />
<android.support.v7.widget.AppCompatSpinner
android:spinnerMode="dropdown"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/spinner2"
android:layout_weight="20"
android:layout_gravity="center_vertical" />
This is how they look: spinner1 and spinner2
So far I can't find anything similar happening to someone else in other posts, wonder if any of you ran into this problem at all.
Here's what's in my MainActivity.java's OnCreate():
final Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
final Spinner spinner = (Spinner) findViewById(R.id.spinner1);
LoadSpin(spinner, spinner2);
Then outside of the OnCreate I have the LoadSpin() method:
public void LoadSpin(Spinner spin, Spinner spin2) {
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.spinner1, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this,
R.array.spinner2, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin2.setAdapter(adapter2);
}
Aaaaand now I just found my mistake. Right as I was editing this question I realize that my adapter2 wasn't setting the setDropDownViewResource correctly because I mistyped "adapter" instead of "adapter2" while copy/pasting:
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin2.setAdapter(adapter2);
So I just needed to correct it by doing this:
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin2.setAdapter(adapter2);
Damn copy/paste mistakes! lol. Took me days to find this out. Think I need a break...

Android spinner not displaying items

I was working with android application and a simple spinner is behaving like a freak.
I am trying to populate a spinner dynamically, and the spinner simply doesn't show me the list of the elements. All I see is the first element selected in the spinner.
private String[] cSpinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_s_details);
this.cSpinner = new String[] {
"A", "B", "C", "D", "E"
};
Spinner cName = (Spinner) findViewById(R.id.cmbCName);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_spinner_dropdown_item, cSpinner);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cName.setAdapter(adapter);
}
Here is what my spinner looks like :
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/cmbCName"
android:spinnerMode="dropdown"
android:clickable="false" />
What is going wrong ??
I've found the problem!
android:clickable="false" --> Source of the problem
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/cmbCName"
android:spinnerMode="dropdown"
android:clickable="true" />
try your adapter like this:
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, android.R.id.text1, cSpinner)
i hope it will work.

Android Spinner items space

I have two spinners in my app. One spinner statically loads List items from XML file to display. Other spinner gets list of strings from database and displays. I am using same XML attributes for both spinners. But the spacing between individual items is different for both. The spinner with static list of strings has more spacing between items. The spinner which loads items from database has some sort of wrapped height. The items are close to each other making it difficult for user to select.
Any solution for this problem?
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Spinner
android:id="#+id/spinner3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
java code
static_sp = (Spinner) findViewById(R.id.spinner1);
List<String> array_karant = Arrays.asList(getResources().getStringArray(R.array.karant_list));
ArrayAdapter<String> karant_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array_karant);
karant_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
static_sp.setAdapter(karant_adapter);
static_sp.setSelection(0);
static_sp.setOnItemSelectedListener(new select_karant());
database_sp = (Spinner) findViewById(R.id.spinner3);
return_likes = db.getAllLikeList();
ArrayAdapter<String> like_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, return_likes);
like_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
database_sp.setAdapter(like_adapter);
database_sp.setOnItemSelectedListener(new select_like());
Link to image is here:
Thanks,
Sameer
create a layout simple_spinner_item
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
and change your code
static_sp = (Spinner) findViewById(R.id.spinner1);
List<String> array_karant = Arrays.asList(getResources().getStringArray(R.array.karant_list));
ArrayAdapter<String> karant_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array_karant);
karant_adapter.setDropDownViewResource(R.layout.simple_spinner_item);
static_sp.setAdapter(karant_adapter);
static_sp.setSelection(0);
static_sp.setOnItemSelectedListener(new select_karant());
database_sp = (Spinner) findViewById(R.id.spinner3);
return_likes = db.getAllLikeList();
ArrayAdapter<String> like_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, return_likes);
like_adapter.setDropDownViewResource(R.layout.simple_spinner_item);
database_sp.setAdapter(like_adapter);
database_sp.setOnItemSelectedListener(new select_like());
If u want spacing u have to added the padding to the textview
create a layout simple_spinner_item.xml, like this
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"/>
The last two lines is for add space between each item, then you have modify your class, in this line:
karant_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array_karant);
Because you are using the android layout and we need use our layout (delete the word android.):
karant_adapter = new ArrayAdapter<String>(this, R.layout.simple_spinner_item, array_karant);

Adding Items to ListView on android

I have a list view on this XML:
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_above="#+id/txtMsg" />
I want to add items to the list view in my code. This is my code:
ListView mesList = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> adapter;
if(messages.size()>0) {
adapter = new ArrayAdapter<String>(this, android.R.layout.activity_list_item, messages);
}
messages is an ArrayList of String and is not empty. For some reason the list view is empty on the screen. What should I put instead of android.R.layout.activity_list_item? I think this is my problem. Thank you in advance!
set adapter of listview
mesList.setAdapter(adapter);
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, messages);

Categories

Resources