I put two spinners in the same activity, one for City and the other for Town. When the user chooses a City, the Town spinner should populate with items according to the City that was chosen.
My problem is, the color of the background and the text is always different from the first one, however they have the same style and attributes. I couldn't find any logical solution and I didn't find any suggestion in web.
Do you have any idea about the reason or the solution?
<?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="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="20dp"
android:onClick="pickDate"
android:text="Select date" />
<TextView
android:id="#+id/viewDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Distribution date"
android:textAlignment="center" />
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="The governorate"
android:textAlignment="center" />
<Spinner
android:id="#+id/static_spinner"
style="#style/Widget.AppCompat.Light.Spinner.DropDown.ActionBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="The district"
android:textAlignment="center" />
<Spinner
android:id="#+id/district_spinner"
style="#style/Widget.AppCompat.Light.Spinner.DropDown.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textView15"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="Place ID" />
<EditText
android:id="#+id/plcID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="" />
<Button
android:id="#+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:onClick="next"
android:text="Next" />
</LinearLayout>
</LinearLayout>
MainInfoActivity file
public class MainInfoActivity extends Activity {
TextView textView, plcID;
Spinner staticSpinner;
Spinner dynamicSpinner;
Spinner districtSpinner;
CharSequence[] arrayDistrict;
ArrayAdapter<CharSequence> districtAdapter;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_info_activity);
textView = (TextView) findViewById(R.id.viewDate);
plcID = (TextView) findViewById(R.id.plcID);
//Drop down lists
staticSpinner = (Spinner) findViewById(R.id.static_spinner);
// Create an ArrayAdapter using the string array and a default spinner
final ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter.createFromResource(
this, R.array.governorate_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner.setAdapter(staticAdapter);
districtSpinner = (Spinner) findViewById(R.id.district_spinner) ;
districtAdapter = ArrayAdapter.createFromResource(getApplicationContext(),
R.array.Anbar, android.R.layout.simple_spinner_item);
districtAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
staticSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view1, int i, long l) {
districtAdapter = ArrayAdapter.createFromResource(getApplicationContext(),
R.array.Anbar, android.R.layout.simple_spinner_item);
districtAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
districtSpinner.setAdapter(districtAdapter);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}
I recreated your code and it works fine for me.
The only difference I can find meaningful are in the lines below:
ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter
.createFromResource(this, R.array.governorate_array,
android.R.layout.simple_spinner_item);
and
districtAdapter = ArrayAdapter
.createFromResource(getApplicationContext(),
R.array.Anbar,
android.R.layout.simple_spinner_item);
instead of using getApplicationContext() can you try to use this as you did in the staticAdapter?
I once faced something similar where my application had a theme different from my activity and the widgets looked slightly different as well.
UPDATE
I would like to add some more info about the reason why I suggested the change above.
Using the right Context is linked to another one of those behaviors. While the framework will not complain and will return a perfectly good view hierarchy from a LayoutInflater created with the application context, the themes and styles from your app will not be considered in the process. This is because Activity is the only Context on which the themes defined in your manifest are actually attached. Any other instance will use the system default theme to inflate your views, leading to a display output you probably didn’t expect.
Quote from this link: https://possiblemobile.com/2013/06/context/
By default, drop-down views are inflated against the theme of the
{#link Context} passed to the adapter's constructor. More.......
ArrayAdapter source code: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/ArrayAdapter.java#461
I was going to ask for more code, but then I found this similar solution in another question that can be useful for you:
Spinner Theme is Dark
which basically consist on create your own theme for the upper spinner and bottom spinner. Check if this is close or related to your question. If don`t upload the activity where the spinners are added, for recreate the situation.
Related
I have a custom popup that I created in XML and in that popup there is a spinner that is supposed to have a dropdown list when tapped. The list is dynamically populated by an arraylist. When I tap the spinner in the app, it does not show the list at all and the tap basically does nothing but just highlight the spinner bar.
However, if I populate the spinner from a pre-created strings.xml array, it works just fine. Problem is that I need the list to dynamically change and I cannot do that with a pre-created array.
I have poured through pages and pages of other stackoverflow posts that talk about a similar issues and have essentially tried every suggestion that I've come across but no luck. I have also followed many different tutorials on how to properly create spinners and each time the same thing happens.
Here is the XML for the custom popup and the java activity code used to initialize the spinner.
Layout xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="300dp"
android:layout_height="200dp"
android:background="#474747"
android:padding="5dp"
android:layout_gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#color/white"
android:text="Enter Current Floor #"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="15dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/btn_dropdown"
android:spinnerMode="dropdown"/>
</LinearLayout>
<Button
android:id="#+id/btnGo"
android:onClick="goClicked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GO"
android:layout_marginTop="25dp"
android:layout_gravity="center_horizontal"
android:textColor="#color/white"
android:backgroundTint="#14B5DB"/>
</LinearLayout>
This is the activity code:
Dialog myDialog;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDialog = new Dialog(this);
myDialog.setContentView(R.layout.custompopup);
addItemsOnSpinner();
}
public void addItemsOnSpinner() {
Spinner dropdown = (Spinner)
myDialog.findViewById(R.id.spinner);
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String> .
(myDialog.getContext(),
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dropdown.setAdapter(dataAdapter);
}
There are no error messages or anything, just does not display the list when the spinner is tapped. Sorry for the poor formatting, this is basically my first time posting code here. Any help would be greatly appreciated as I have been trying to solve this for a while now and it doesn't seem like I can find a solution to my problem.
I am a beginner in android. I have a question why the spinner is not showing in design window?. For your help I have added a code below
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text View!"
android:paddingRight="100dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="#+id/brand"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:layout_below="#+id/color"
android:layout_alignLeft="#+id/color"
android:id="#+id/find_beer"
/>
<Spinner
android:id="#+id/color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp"
>
</Spinner>
enter image description here
The Constraints you are using are incorrect. Change your layout as follows:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Spinner
android:id="#+id/color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></Spinner>
<Button
android:id="#+id/find_beer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
app:layout_constraintBottom_toBottomOf="#+id/color"
app:layout_constraintStart_toEndOf="#+id/color"
app:layout_constraintTop_toBottomOf="#+id/color" />
<TextView
android:id="#+id/brand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="57dp"
android:layout_marginRight="57dp"
android:paddingRight="100dp"
android:text="Text View!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Setting following attribute:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Means following:
The view should be only big enough to enclose its content
Even if you correct your constraints, remember to add some content to the Spinner otherwise you will just see drop down arrow on the screen.
change the layout type like a linearlayout with orientation vertical or relativelayout set layout after you set value with using java code or xml using resources
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
java code
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
using layout xml into spinner tag
android:entries="#array/planets_array"
XML code.
<Spinner
android:id="#+id/spCountry"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="25dp"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:background="#android:color/transparent"
android:gravity="center"
android:spinnerMode="dropdown" />
Code for activity
Spinner spCountry;
spRole = (Spinner)getView().findViewById(R.id.spCountry);
String[] arRoleSpinner = {"India","USA","NWZ","SA", "WI","ENG"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, arRoleSpinner);
adapter.setDropDownViewResource(R.layout.spinner_item);
spRole.setAdapter(adapter);
spRole.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
((TextView) adapterView.getChildAt(0)).setTextColor(Color.WHITE);
((TextView) adapterView.getChildAt(0)).setTextSize(12);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
use this code
<Spinner
android:id="#+id/color"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp" ></Spinner>
because in spinner (drop down list) we add items at run time (in java code) so it show when we run app. and also every change in java code like color change , Visibility etc show when we run the app.
and also visit this https://www.javatpoint.com/android-spinner-example
In my application I've got 3 spinners - one on the top, which when used provides the user with 2 more aligned below it.
I looked at this question where I found the answer I was looking for, but only half-way.. Now the two supplementary spinners have their text shown in the center, but the first one - no.
Looking at the .xml for the spinners I can see only one difference regarding to centering and it is that the two supplementary ones have the android:layout_gravity="center" attribute, which for some reason is not applicable to the first spinner. Why is this so? Could this be the reason? By not applicable I mean it is not in the properties for the spinner in the design view and when I add it manually in the .xml nothing changes.
So ideally the answer which I am looking for is why the 2 supplementary spinners have their text centered, but the first one - no? Note I am not interested in centering the elements in the popup, this is purely optional. I just want to have the selected option to be in the center.
Here is a screenshot of how it looks now with the problem in red:
Here is the .xml I am using:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/some_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="packagename"
tools:showIn="#layout/specific_activity_layout"
android:gravity="center_horizontal"
android:background="#FF9800">
<Spinner
android:layout_width="fill_parent"
android:layout_height="50dp"
android:id="#+id/spinner1"
android:entries="#array/spinner1entries"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:gravity="center|top"
android:background="#drawable/rounded_corners"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:popupBackground="#FFB74D"
android:layout_alignParentEnd="false"
android:textAlignment="center" />
<LinearLayout
android:id="#+id/horizontal_layout_spinners"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="false"
android:layout_alignParentStart="false"
android:visibility="invisible"
android:layout_below="#+id/spinner1">
<Spinner
android:layout_width="wrap_content"
android:layout_height="50dp"
android:id="#+id/spinner2"
android:visibility="visible"
android:background="#drawable/rounded_corners"
android:layout_marginRight="5dp"
android:popupBackground="#FFB74D"
android:textAlignment="center"
android:gravity="center|top"
android:layout_weight="0.5"
android:layout_gravity="center" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="50dp"
android:id="#+id/spinner3"
android:visibility="visible"
android:layout_marginLeft="5dp"
android:background="#drawable/rounded_corners"
android:popupBackground="#FFB74D"
android:textAlignment="center"
android:gravity="center|top"
android:layout_weight="0.5"
android:layout_gravity="center" />
</LinearLayout>
</RelativeLayout>
And the rounded_corners file:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFB74D"/>
<stroke android:width="0dip" android:color="#B1BCBE" />
<corners android:radius="10dip"/>
<padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
</shape>
Try adding :
android:dropDownWidth="wrap_content" to the spinner.
You can try to set the text style programatically after an item is selected from the spinner. Set an setOnItemSelectedListener and inside it set TextView to be centred.
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View view, int arg2, long arg3) {
// Set the text style after item is selected.
setSpinnerSelectedItemParams(spinner1, getActivity());
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
/**
* Sets the spinner text style to a different one after one value has been chosen.
* #param spinner Spinner currently in use
*/
public void setSpinnerSelectedItemParams(Spinner spinner, Context context) {
if (spinner.getChildCount() > 0){
TextView tvSpinner = ((TextView) spinner.getChildAt(0));
tvSpinner.setPadding(0, 0, 0, 0);
tvSpinner.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
params.setMargins(0, 0, 0, 0);
tvSpinner.setLayoutParams(params);
}
}
If you want the text before selecting it in centre as well, try setting a custom resource when creating the adapter.
In Activity or Fragment:
ArrayAdapter<String> yourAdapter = new ArrayAdapter(activity, R.layout.default_spinner_adapter_view, yourDataToFillTheSpinner);
"default_spinner_adapter_view.xml" file:
<?xml version="1.0" encoding="utf-8"?>
<!-- This is the view for each individual item of the spinner-->
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tv_default_spinner"
style="?android:attr/spinnerItemStyle"
android:layout_width="match_parent
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ellipsize="marquee"
android:gravity="center"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/black" />
If you want to customize the dropdown list items you will need to create a new layout file. Let’s call it spinner_dropdown_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee"
android:textColor="#aa66cc"/>
Another change in the declaration of the spinner:
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,R.array.planets_array, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spinner.setAdapter(adapter);
Make your custom list item of spinner and set as you want and that should have a TextView id text1 and use this list_item.xml like below
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner
layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.your_list_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.your_drop_down_list_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
change relative layout to linear layout vertical and width of the spinner should be match_parent.
Make sure all spinners are using the same type of adapter—or at least another adapter using the same item layout—the adapter provides the layout.
I would like to create a listview that allows multiple choice. The typical solutions is to get a cursor and use the SimpleCursorAdapter.
SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this,
R.layout.simple_list_item_multiple_choice, cur2, cols2, views2);
I am able to get this working when using the R.layout.simple_list_item_multiple_choice. I get the checkmarks to work when multiple items are selected.
So I decided to try it with a custom made layout. Here is the XML code for my 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:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/lookup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/hasphone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
<CheckedTextView
android:id="#+id/checkedTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"/>
</LinearLayout>
So here is my issue. The layout is inflated fine using the same code and setting the ChoiceMode to multiple on my listview. The data from the cursor is populated fine.
However, the issue I have is that the checkmarks do not show up as selected (a check in the box) when I click on the item. Is there something I am missing that would not involve creating a custom adapter?
l2.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
l2 is my listview.
I'm not up on CheckedTextView... from my understanding, it should toggle the check when you click the line.
I suppose you could try and force it like this:
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
CheckedTextView chkTxt = (CheckedTextView) v.findViewById(R.id.CheckedTextView1);
chkTxt.toggle();
}
Note this would be a hack and you should really find the root problem, but it might get you going in the meantime.
EDIT
After much googling, I found the issue... the root of the row layout needs to be checkable to use CheckedTextView, and a LinearLayout is not.
More googling found a solution... override the LinearLayout class.
Instructions/code here
The problem with "the issue I have is that the checkmarks do not show up as selected" is because of your wrong layout. It should be without LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/lookup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/hasphone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
<CheckedTextView
android:id="#+id/checkedTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorMultiple" />
Then the SimpleCursorAdapter can set properly your "checkedTextView1" (with visible effect).
In my case I used the ArrayAdapter and it worked.
list.setAdapter(new ArrayAdapter<String>(this, R.layout.category_layout,
R.id.category_checkedText, this.categories));
I am using a spinner in several places in my program, but I will stick with one case.
I have two xml files -- small_new_system and new_system. They both have the a spinner that is named state_spinner.
The odd thing is that when I use this code on a tablet running 3.2, which uses new_system, they are displayed but when I put the app on my phone that is running 2.1, which uses small_new_system, they do not show up. The items are in the spinner list, but there is no text being displayed. I have tried naming the spinners differently, as well as not using a custom spinner layout.
The other odd thing is that when I use the identical layouts, which do not look very good, on the small device they are not populated with the text either.
Thank you for any help! My code is as follows:
Code to populate the spinner:
states = (Spinner) findViewById(R.id.state_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter
.createFromResource(this, R.array.states,
R.layout.spinner_layout);
adapter.setDropDownViewResource(R.layout.spinner_layout);
states.setAdapter(adapter);
states.setOnItemSelectedListener(new MyItemsOnSelectListener());`
Spinner in small_new_system:
<Spinner
android:id="#+id/state_spinner"
android:layout_width="200dp"
android:layout_height="55dp"
android:layout_below="#+id/city_edit"
android:layout_margin="5dp"
android:layout_alignParentRight="true"
android:inputType="textPersonName"
android:textSize="40dp" >
</Spinner>
Spinner in new_system:
<Spinner
android:id="#+id/state_spinner"
android:layout_width="500dp"
android:layout_height="75dp"
android:layout_below="#+id/city_edit"
android:layout_margin="10dp"
android:layout_alignParentRight="true"
android:textSize="60dp" >
</Spinner>`
Code in custom spinner:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinnerTarget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#00000000"
android:textSize="40dp" >
</TextView>`
Try removing:
android:inputType="textPersonName"
from small_new_system. Then it would look like:
<Spinner
android:id="#+id/state_spinner"
android:layout_width="200dp"
android:layout_height="55dp"
android:layout_below="#+id/city_edit"
android:layout_margin="5dp"
android:layout_alignParentRight="true"
android:textSize="40dp" >
</Spinner>
Or you might try adding a background color:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinnerTarget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#00000000"
android:background="#FFFFFFFF"
android:textSize="40dp" />
The solution I came up with was to use android.R.layout.simple_dropdown_item for my spinner when an sdk less than 11 is detected, and when the sdk is greater than 11 I use my custom spinner. This seems to work ok. Not my favorite solution, but it does work.
Try this one ..
spnPlan = (Spinner) findViewById(R.id.set_spinner1);
ArrayAdapter<CharSequence> planAdapter = ArrayAdapter.createFromResource(
this, R.array.plan_size, android.R.layout.simple_spinner_item);
planAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnPlan.setAdapter(planAdapter);
spnPlan.setOnItemSelectedListener(new MyOnItemSelectedListener());