I'm trying to create a dynamic spinner (combobox), but I couldn't make it work, it is showing the first item in the spinner, but when I click it, the list with the other items is not showing and nothing is happening.
My activity xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="80dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/global_languages"/>
<Spinner
android:id="#+id/localesSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:popupBackground="#color/facebookBlue"
android:clickable="true"/>
</LinearLayout>
I'm trying to set items this way:
private void loadLocalesSuccess(Collection<String> locales){
Spinner localesSpinner = (Spinner) findViewById(R.id.localesSpinner);
ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, new ArrayList(locales));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
localesSpinner.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
what could be the problem?
This code snippet should work, check if the data is proper.
ArrayList<String> locales = new ArrayList<>();
locales.add("English");
locales.add("French");
Spinner localesSpinner = findViewById(R.id.localesSpinner);
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, locales);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
localesSpinner.setAdapter(adapter);
adapter.notifyDataSetChanged();
Related
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="List Type"
android:textSize="18sp"
android:padding="7dp"
android:clickable="true"
android:onClick="open_spinner"/>
string.xml
<string-array name="type">
<item>Male</item>
<item>Female</item>
</string-array>
In xml file I already know to make TextView can click. But how I open like spinner when click on TextView?
Declare a spinner with visibility gone in your xml
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:prompt="#string/spinner_title"
/>
inside your open_spinner method :
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setVisibility(View.VISIBLE);
spinner.setOnItemSelectedListener(this);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, youarrayofStrings);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
I am attempting to render two spinners on my Android activity. They both will have to take their item values from a string.xml resource file.
Here is how my code looks like currently:
#OptionsMenu(R.menu.menu_helper)
#EActivity(R.layout.activity_helper)
public class HelperActivity extends AppCompatActivity {
#ViewById(R.id.cultureSpinner)
Spinner cultureSpinner;
#ViewById(R.id.harvestSpinner)
Spinner harvestSpinner;
ArrayAdapter<CharSequence> adapter;
#AfterViews
public void initialize() {
initializeSpinners();
}
public void initializeSpinners() {
adapter = ArrayAdapter.createFromResource(this, R.array.cultures, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cultureSpinner.setAdapter(adapter);
adapter = ArrayAdapter.createFromResource(this, R.array.harvestValues, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cultureSpinner.setAdapter(adapter);
}
}
The style xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.55"
android:fillViewport="true"
android:orientation="vertical"
android:showDividers="beginning|end"
>
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:padding="10dip"
>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cultureSpinner"
android:spinnerMode="dropdown"
/>
</TableRow>
<Space
android:layout_height="3dp"/>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/harvestSpinner"
android:spinnerMode="dropdown"
/>
</TableRow>
</TableLayout>
</ScrollView>
Now when I launch the activity, The second spinner is displayed before the first one and the first spinner that is now for some reason below the second one, does not function. If I press it, the dropdown menu does not appear.
What could be the problem here?
public void initializeSpinners() {
adapter = ArrayAdapter.createFromResource(this, R.array.cultures, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cultureSpinner.setAdapter(adapter);
adapter1 = ArrayAdapter.createFromResource(this, R.array.harvestValues, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
harvestSpinner.setAdapter(adapter1);
}
Just try it. Hope its work.
I'm using the AutoCompleteTextView component to filter member data. I've set it up in my layout file as follows
<AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/autocomplete_swap_pool"
android:layout_width="match_parent"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:layout_height="#dimen/nfl_my_listings_and_my_bids_spinner_height"
android:layout_toLeftOf="#id/icon_on"
android:dropDownSelector="#color/black"
android:dropDownVerticalOffset="5dp"
android:dropDownWidth="wrap_content"
android:inputType="textAutoComplete|textAutoCorrect"
android:popupBackground="#color/white"
android:ems="10"
android:text="" />
I've then initialised it in my code like this
autoTextView = (AutoCompleteTextView) EngineGlobals.iRootActivity.findViewById(R.id.autocomplete_swap_pool);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(EngineGlobals.iApplicationContext, android.R.layout.simple_dropdown_item_1line, Cards);
autoTextView.setThreshold(1);
autoTextView.setAdapter(adapter);
When I enter text the dropdown box appears but the items are not visible, they are there because if I click on the dropdown box items appear. It looks as if they are white font on a white background.
What am I doing wrong?
I think you need to apply textColor to your hint. For that you need custom row layout for your AutoCompleteTextView.
hint_item.xml
<TextView
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textColor="#android:color/holo_green_dark" />
YourActivity.java
AutoCompleteTextView autoTextView = (AutoCompleteTextView) findViewById(R.id.autocomplete_swap_pool);
ArrayAdapter<String> autoadapter = new ArrayAdapter<String>(this, R.layout.hint_item, new String[]{"One", "Two", "Three"});
autoTextView.setAdapter(autoadapter);
Output
here are the codes
dChooser= (Spinner) findViewById(R.id.spinner1);
adapter = new ArrayAdapter<CharSequence>(this,R.array.d_choices);
dChooser.setAdapter(adapter);
and here is the xml part i dont understand what im doing wrong
<Spinner
android:id="#+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/time2"
android:layout_marginTop="24dp"
android:spinnerMode="dropdown"
android:background="#android:color/darker_gray" />
Your Adapter seems wrong use something like this
dChooser= (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.d_choices, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dChooser.setAdapter(adapter);
I am trying to use two spinners with a custom dropdown yet, only the bottom one is showing with the custom layout when pulled down. I have been trying on my own to figure out why but I cannot.
public class setup extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setuplayout);
Spinner spinner1 = (Spinner) findViewById(R.id.Spinner01);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.numberPlayers, R.layout.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
spinner1.setAdapter(adapter);
Spinner spinner2 = (Spinner) findViewById(R.id.Spinner02);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this,
R.array.gameDifficulty, R.layout.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
spinner2.setAdapter(adapter2);
}
}
This is the xml code for spinner_item mentioned above.
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:textColor="#F9B12F"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:gravity="left"
android:padding="5dip"
android:popupBackground="#000000"
android:background="#000000"
/>
This is the setuplayout xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView android:id="#+id/mainSetupImage"
android:src="#drawable/setup"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ImageView>
<ImageView
android:id="#+id/players"
android:clickable="true"
android:src="#drawable/players"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerVertical="true">
</ImageView>
<Spinner
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:drawSelectorOnTop="true"
android:id="#+id/Spinner01"
android:textColor="#F9B12F"
android:layout_toRightOf="#+id/players"
android:layout_marginLeft="25dp"
android:layout_centerVertical="true"
android:background="#drawable/spinner"/>
<Spinner
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:drawSelectorOnTop="true"
android:id="#+id/Spinner02"
android:textColor="#F9B12F"
android:layout_below="#+id/Spinner01"
android:layout_toRightOf="#+id/difficulty"
android:layout_marginTop="25dp"
android:background="#drawable/spinner"/>
<ImageView
android:id="#+id/difficulty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/Spinner02"
android:clickable="true"
android:src="#drawable/difficulty" />
</RelativeLayout>
in 2nd spinner u used adapter rather than adapter2. so use adapter2 and run your code hope it will run. :)
I figured it out with your help. I had used the code "adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)" which was overriding my custom layout. Thanks for highlighting where the two codes were different. That was a huge help. I deleted the quoted lines and it now works.
I'd like to share a little code that will make all people's code much cleaner when making ArrayAdapters.
Just define a static method in a class of your preference (even MainActivity...) to give you the Array adapter without dirting so much your code:
public static ArrayAdapter<CharSequence>
getArrayAdapter( Context c, int arrayId, int idLayout1, int idLayout2){
ArrayAdapter<CharSequence> aa;
aa = ArrayAdapter.createFromResource( c, arrayId , idLayout1);
aa.setDropDownViewResource( idLayout2);
return aa;
}
Then in your code you just need to do it:
myspinner.addAdapter(
this, ThatClass.getArrayAdapter(
R.array.myid, R.layout.id1, R.layout.id2 );
myspinner.setOnItemSelectedListener(this);
/* Remember that the default Android Spinner layout can be get passing these specific layout ids:
android.R.layout.simple_spinner_item as layout1
android.R.layout.simple_spinner_dropdown_item as layout2
*/