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
*/
Related
I have found some threads about this issue, but no solution works in my case. I just want to set the height of the spinner popup e.g. to 200dp, or to limit the displayed items in dropdown popup and make it scrollable.
mainactivity.xml:
<FrameLayout
android:id="#+id/frame1"
android:layout_below="#+id/contact_form_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#drawable/custom_spn_background">
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dropDownSelector="#color/colorAccent"
android:popupBackground="#drawable/custombg"
style="#style/Myspinner"
android:layout_margin="5dp"
android:gravity="center_vertical"
android:spinnerMode="dropdown"/>
</FrameLayout>
my_spinnerlist.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:paddingStart="8dp"
android:paddingEnd="8dp"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:gravity="start"
android:textColor="#000000"
android:paddingTop="2dip"
android:paddingBottom="2dip"
android:ellipsize="marquee"/>
spinner_item.xml:
<?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:textSize="18sp"
android:gravity="center_vertical"
android:textColor="#000000"
android:paddingStart="8dp"
android:paddingEnd="8dp"
android:paddingTop="2dp"
android:paddingBottom="4dp"
android:textStyle="normal"
/>
MainActivity.java:
final Spinner sp1 = findViewById(R.id.spinner1);
String[] arrayItems = myList.categories;
ArrayAdapter<String> adp1 = new ArrayAdapter<>(this, R.layout.spinner_item, arrayItems);
adp1.setDropDownViewResource(R.layout.my_spinnerlist);
sp1.setAdapter(adp1);
sp1.setSelected(false);
sp1.setSelection(0, true);
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
I tried to set 200dp height in all spinner related XML layout files, but it affects only the item height, not the whole popup. I also tried a suggested solution from here with Reflection, example:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
try {
Field popup = Spinner.class.getDeclaredField("mPopup");
popup.setAccessible(true);
android.widget.ListPopupWindow popupWindow = (android.widget.ListPopupWindow) popup.get(spinner);
popupWindow.setHeight(200);
}
catch (NoClassDefFoundError | ClassCastException | NoSuchFieldException | IllegalAccessException e) {
}
but this did not change the height of the dropdown list and also I have a warning : Reflective access to mPopup, which is not part of the public SDK and therefore likely to change in future Android releases.
I also tried to set <item name="android:dropDownHeight">200dp</item> in Myspinner style in styles file, but it did not affect the height.
I really don't have any solution for this. I have a list of 50 items in my dropdown and it overlaps the whole display area of my device.
Ok, the only solution that works for me is to use third party MaterialSpinner library. Maybe it will be useful also for others as there is no native way to change the dropdown height.
Just to implement: implementation 'com.jaredrummler:material-spinner:1.3.1'
The spinner.xml:
<com.jaredrummler.materialspinner.MaterialSpinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:ms_dropdown_max_height="200dp"
android:dropDownSelector="#color/colorAccent"
android:popupBackground="#drawable/custombg"
style="#style/Myspinner"
android:layout_margin="5dp"
android:gravity="center_vertical"
android:spinnerMode="dropdown"/>
Java code:
final MaterialSpinner sp1 = (MaterialSpinner) findViewById(R.id.spinner1);
String[] arrayItems = myList.categories;
ArrayAdapter<String> adp1 = new ArrayAdapter<>(this, R.layout.spinner_item, arrayItems);
adp1.setDropDownViewResource(R.layout.my_spinnerlist);
sp1.setAdapter(adp1);
sp1.setSelected(false);
sp1.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener() {
#Override
public void onItemSelected(MaterialSpinner view, int position, long id, Object item) {
}
} );
Works like a charm, see the app:ms_dropdown_max_height="200dp" in XML file.
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();
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
i am trying to make a custom dialog box with 2 spinners and 2 buttons.
i am doing the following coding
this is xml for custom GUI inside dialog box
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SORT BY" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:prompt="#string/prompt1"
android:entries="#array/ordersortby"
android:layout_gravity="center"/>
<TextView
android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ORDER"
/>
<Spinner
android:id="#+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:prompt="#string/address"
android:entries="#array/ordersortby1"
/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
/>
in strings i am declaring following
<string name="prompt1">Order Number</string>
<string-array name="ordersortby">
<item>Order Number</item>
<item>Date Submitted</item>
<item>Date Entered</item>
</string-array>
<string-array name="ordersortby1">
<item>ASC</item>
<item>DESC</item>
</string-array>
and in activity i am doing following
final Dialog dialog = new Dialog(orders.this);
dialog.setContentView(R.layout.orderpicker);
dialog.setTitle("Sort By Dialog");
dialog.show();
when running this , i am getting this
spinner
my problem is why i am not getting any data inside these pickers. please help me.
Let use below code,
spinner_ordersortby = (Spinner)findViewById(R.id.spinner_ordersortby);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.ordersortby, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_language.setAdapter(adapter);
another thing is make sure your array should inside this directory
/res/values/arrays.xml
Use below code before dialog.show(); and define your string array into string.xml file, it will solve your problem.
Spinner mSpinner1 = (Spinner)dialog.findViewById(R.id.spinner_ordersortby);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.string.ordersortby, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner1.setAdapter(adapter);
in your code try something like this
LayoutInflater factory = LayoutInflater.from(this);
View myview = factory.inflate(R.layout.orderpicker, null);
Dialog dialog = new Dialog(orders.this);
dialog.setContentView(myview);
dialog.setTitle("Sort By Dialog");
dialog.show();
First get the view.
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.yourlayout, null);
Now use this view to declare your spinner.
Spinner spinner = (Spinner) view.findViewById(R.id.spinner);
This will solve your problem.