I would like to create a customized popup dialog which contains a spinner. The dialog needs to be launched from an Adapter class, below is my code:
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.myPopup);
Spinner spinner = (Spinner)dialog.findViewById(R.id.spinner);
ArrayAdapter<String> arrayadapter = new ArrayAdapter<String>(mContext, 0);
arrayadapter.add("AddSomeStrings");
spinner.setAdapter(arrayadapter);
dialog.show();
This code is executed fine, but sometime after the "show()", I see an exception: Resources$NotFoundException. The last item in the callstack is Resources.loadXmlResourceParser. If I don't assign the spinner using findViewById, but instead assign it via spinner = new Spinner(dialog.getContext()), then I don't get the error (but then of course I cannot see my dialog).
myPopup layout contains:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:prompt="#string/group_prompt"
/>
</LinearLayout>
Any thoughts what I am doing wrong? thanks!
Your using the array adapter with Constructor Context and TextView ID, but your just passing it 0 as the text view resource id.
See the API here : ArrayAdapter
Try:
ArrayAdapter<String> arrayadapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item);
or other resource of you choice
Related
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>
I'm trying to add some items in a ListView. When I swith the Layout (startActivity), I call the class Listing.
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listing);
ListView lv = (ListView) findViewById(R.id.listViewLIST);
String[] items = {"Item1", "Item2", "Item3", "Item4"};
ArrayList<String> arrayList = new ArrayList<>(Arrays.asList(items));
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.activity_listing, R.id.listViewLIST, arrayList);
lv.setAdapter(adapter);
}
I read some tutorials an followed every step but the app stops working always in the last line of code
lv.setAdapter(adapter);
What am I missing?
Edit 27.12.2015
The activity_listing.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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_height="match_parent" android:fitsSystemWindows="true"
tools:context="cigarkings.cigarking.Listing">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listViewLIST"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="51dp" />
<SearchView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/searchView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/listView"
android:layout_alignEnd="#+id/listView" />
<include layout="#layout/content_listing" />
</android.support.design.widget.CoordinatorLayout>
Assuming missing statement android:layout_width="match_parent" for your Coordinator Layout as a typographical error,the major error is in setting up array adapter.
In the statement : ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.activity_listing, R.id.listViewLIST, arrayList); the constructor parameters (2nd parameter:R.layout.activity_listing and 3rd parameter:R.id.listViewLIST) are incorrect.Array Adapter takes resource id of a textview as 3rd argument and not of a listview, and the layout resource id of the layout containing the textview is passed as 2nd argument.
So, one solution for this is to create a separate xml layout containing a textview and passing the layout resource id as 2nd argument and textview id as 3rd argument to the constructor of Array Adapter
First of all activity listing should not be in this pattern.
If you are at beginning stage then First try this Tutorial...
The best explanation ever for beginners.
Then you have to implement custom listView using BaseAdaptor class and custom XML.
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);
Beginner question here I wonder if someone can point me in the right direction. I need a listview of 'accounts' where each item will contain a textview, and a spinner to choose from a preset amount, so each item will be like so:
________________________
|TextView---------Spinner|
|________________________|
Here's what I have so far:
activity_topup.xml This is the main xml for the activity
<ListView
android:id="#+id/topup_accounts_listview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/stq_grey"
android:divider="#android:color/transparent"
android:dividerHeight="20.0sp"/>
And then in my java file TopUpActivity.java
String[] listAccounts = { "Acc1", "Acc2", "Acc3"};
accountListAdapter = new ArrayAdapter<String>(this, R.layout.listview_item_accounts, R.id.accounts_list_tv, listAccounts);
listView = (ListView)findViewById(R.id.topup_accounts_listview);
listView.setAdapter(accountListAdapter);
listview_item_accounts.xml contains the content of each item (a text view and spinner)
<TextView
android:id="#+id/accounts_list_tv"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/accounts_list_amt_spinner"
android:background="#drawable/list_item_selector_grey"
style="#style/ListText" />
<Spinner
android:id="#id/accounts_list_amt_spinner"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:prompt="#string/topup_amt_prompt"
android:background="#drawable/list_item_selector_blue"
style="#style/ListText" />
Am I on the right track?! The result is kind of correct but I can't quite figure out how to populate the spinners, all I can populate is the text views using this method I think. I added this to the java file in an attempt to populate the spinners too, but the spinners remain empty:
//Inflate the listview items
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = inflater.inflate(R.layout.listview_item_accounts, null);
//Get the spinner
amt = (Spinner)vi.findViewById(R.id.accounts_list_amt_spinner);
//Add the values to the spinner by setting this adapter
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.topup_amts, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
amt.setAdapter(adapter);
Here is the result (The spinners have the blue background):
And when I click on one of the spinners they are empty, I get this
You have to find your sub views in it's parent.For example in getView of your adapter,you iflate your item(from listview_item_accounts.xml) as GroupView with name "item",then you have to find spinner in it:
Spinner amt = (Spinner)item.findViewById(R.id.accounts_list_amt_spinner);
If you use
Spinner amt = (Spinner)findViewById(R.id.accounts_list_amt_spinner);
you tried to find a view that it's id is equal to "R.id.accounts_list_amt_spinner" in content view,not in each item.
Hi I want to set the background for the dialog of my spinner. I am using following code
<Spinner android:id="#+id/my_ac_debt_card_spinner"
android:layout_toRightOf="#+id/my_ac_debt_card_text" android:entries="#array/my_ac_debt_array"
android:layout_marginRight="10dip" android:layout_marginLeft="50dip"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="#drawable/my_ac_spinner_bg" android:paddingLeft="5dip"
android:gravity="center_vertical" android:popupBackground="#drawable/my_ac_round_rect"/>
Java code is like this:
Spinner spin = (Spinner) findViewById(R.id.my_ac_debt_card_spinner);
final ArrayAdapter<String> a = new SpinnerAdapter(
getActivity(),
android.R.layout.simple_spinner_dropdown_item,
debit_array);
spin.setAdapter(a);
But I am not able to set background of the popup dialog.
Can anyone please help me to solve this issue.
Thanks in advance.
Try putting this code into your ListAdapter for the Spinner in getDropDownView:
if (parent != null && parent instanceof ListView) {
((ListView) parent).setBackgroundResource(R.drawable.background);
}
Try changing
android:popupBackground="#drawable/my_ac_round_rect" to
to
android:background="#drawable/my_ac_round_rect"
Also, here's a good blog I found on customizing a spinner: http://www.gersic.com/blog.php?id=57