Android Spinner does not display dropdown list when tapped - android

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.

Related

Appearance issue in spinners in Android studio

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.

Android Listview display multiple items per screen

I've created a simple app that sends a search term to a php script, reads the json response generated by that php script based on the search term, processes the json response and loads the json nodes values in a listview. All goes well, the search is completed, the json gets processed, the values are loaded in the listview.
I have just one problem with the listview: each item is on a different screen.
This is my xml with the listview, called listview1.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="#string/app_mresults"
android:textColor="#color/text_color" />
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
tools:listitem="#android:layout/simple_list_item_1" >
</ListView>
</LinearLayout>
And this is the xml for each item, called listview1row.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/background"
android:orientation="vertical" >
<TextView
android:id="#+id/ref_pnr"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/ref_partnumber"
android:textColor="#color/text_color" />
<TextView
android:id="#+id/ref_pname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ref_part"
android:textColor="#color/text_color" />
<TextView
android:id="#+id/ref_supp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ref_supplier"
android:textColor="#color/text_color" />
<TextView
android:id="#+id/ref_avail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ref_available"
android:textColor="#color/text_color" />
</LinearLayout>
What am i doing wrong? I've played with the layout width and height and with the layout of the listview, and nothing. Is there a specific combination of width and height i must use?
I've looked into some listview tutorials on some websites, copied the layout details from there and still nothing. I've looked at the json response and it does not contain any breaklines or carriage returns.
Edit: Below is the part where i set the adapter for the ListView.
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
Reference2.this, productsList,
R.layout.activity_reference3, new String[] { TAG_PNR,
TAG_PNAME, TAG_PSUPP, TAG_PAVAIL},
new int[] { R.id.ref_pnr, R.id.ref_pname, R.id.ref_supp, R.id.ref_avail });
// updating listview
setListAdapter(adapter);
}
});
}
i think, because you are using ListView, you should look at LayoutInflater to load the list values.
Here is an example on how to achieve it using LayoutInflater: http://www.mysamplecode.com/2011/10/android-dynamic-layout-using-xml-add.html
Since you are using linearLayout in your list item use layout_weight attribute on items based on your need.
I have finally found what the problem was.
I was using android:background="#drawable/background". Once removed, everything worked as it should.
I came across this while i was rebuilding the listview1row.xml by hand instead of using the visual editor.
Thank you all for the answers and help!

Android: Is GridLayout is suitable for this design

In my application, I have to show some persons information. I have to show only three members at a time. On Swipe left to right i have to show another three set of members. Here is the design
First I thought , we can use three List Views namely LV1,LV2,LV3
and set a List adapter for these 3 listviews. And In the ListAdapter the first row of the listview can take the Title details and afterwards it takes an Listviews for the successive rows. i.e Every White box has individual Listview we cant predict how many white boxes exists for one member.
I can send one object called Member for one listView and One of the attributes of the member object is ArrayList
ie
List mem1 = new ArrayList();
mem1.add(member1)
List mem2 = new ArrayList();
mem2.add(member2)
List mem3 = new ArrayList();
mem3.add(member3)
I can send mem1 for LV1 adapter and mem2 for LV2 adapter and mem3 for LV3 adapter
But I cant populate project details in the white boxes.
Please provide me the best way to do this...
Better you put a List view in the form.Then create a seperate "row.xml" layout as a row of list view .
The row.xml contains 3 text boxes.
For the heading you create a text view and put in the form.Then u should populate the list view based on below coding snippet
class MyAdapter extends ArrayAdapter {
ArrayList items;
MyAdapter(ArrayList items) {
super(OrderSummary.this, R.layout.row, items);
this.items = items;
}
public View getView(int position, View convertView, ViewGroup parent)
{
//Your business login to insert data in the text boxes
}
}
MyAdapter mSchedule = new MyAdapter(mylist);
listView.setAdapter(mSchedule);
I got the solution,
I consider three listviews and create custom view for listview . Here the listview contains only one row. the design of that row is
<LinearLayout
android:id="#+id/dd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="center_horizontal"
android:textColor="#FFFFFF"
android:textSize="27dp"
android:textStyle="bold" />
<TextView
android:id="#+id/subtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textColor="#FFFFFF"
android:textSize="15dp"
android:textStyle="bold" />
<Button
android:id="#+id/action"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="#drawable/blue_button_bg"
android:text="action"
android:textStyle="bold" />
<TextView
android:id="#+id/ttt"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:textColor="#FFFFFF"
android:textSize="27dp" />
</LinearLayout>
<GridLayout
android:id="#+id/projects"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="5dip"
android:layout_marginTop="5dip"
android:layout_weight="1"
android:clipToPadding="true"
android:columnCount="1"
android:orientation="horizontal"
android:useDefaultMargins="true" >
</GridLayout>

Dynamic population of spinner with custom layout

I have a custom layout item for my listview. There is a spinner in the layout item which needs to be populated with values, usually by android:entries
The problem I have with this method is that the end user can't modify the values, which is something I would like to include. As the layout item and subsequently the spinner, is repeated multiple times on the same listview, I imagine there must be a way to populate it once, programmatically. I just can't figure it out.
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/customListItem"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/exerciselbl" />
<Spinner
android:id="#+id/Exercise"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="#array/workout_items" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/repslbl" />
<Spinner
android:id="#+id/Reps"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="#array/reps_count" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/weightlbl" />
<EditText
android:id="#+id/Weight"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
</LinearLayout>
</LinearLayout>
So yeah, I want to avoid using android:entries="#array/workout_items" as this means manually typing out every single item for the spinner in an XML resources file AND I can't dynamically add items while the program is running.
Here is a simple example of how it would be done, obviously the generics can change and there are other ways of loading the spinner.
public void populateSpinner () {
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
List<CharSequence> list = new ArrayList<CharSequence>();
for (int i = 0; i < 10; i++) {
list.add("Item " + i);
}
adapter.addAll(list);
adapter.notifyDataSetChanged();
}
One of the more important lines is adapter.notifyDataSetChanged(), this is because it tells the view that it needs to refresh the data associated with this spinner.
For more information, see the ArrayAdapter and Spinner classes.

Android: Custom spinner size problems

I've made a custom spinner but the sizing isn't exactly what I want it to be. The spinner become far to large to get the spacing in the list that I need. I want to be able to size the rows of the spinner independently of the size of the spinner button. I want the spinner to be thin and then I want the section rows to be spaced generously. (See image at the bottom):
Currently the xml for the rows of the spinner is this:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow android:id="#+id/tableRow1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:padding="5dip">
<ImageView android:layout_width="32sp" android:src="#drawable/icon"
android:id="#+id/spinnerimage" android:layout_height="32sp" />
<TextView android:textSize="22sp" android:textStyle="bold" android:textColor="#000"
android:layout_width="fill_parent" android:id="#+id/category"
android:layout_height="fill_parent" android:paddingLeft="5sp" />
</TableRow>
</TableLayout>
I wanted to use a relative layout but the table layout gave me slightly better spacing. If I try to make the height it will cut off the text and icons in the rows. The spinner in my main.xml is:
<Spinner android:id="#+id/catspinner"
android:layout_marginLeft="25dip" android:layout_marginRight="25dip"
android:layout_width="fill_parent" android:layout_centerHorizontal="true"
android:layout_height="wrap_content" android:prompt="#string/prompt"
android:background="#drawable/yellow_btn"
android:layout_centerVertical="true" android:drawSelectorOnTop="true" />
I would like to have the spinner the sizing to be the same as the standard android spinner (on right.) Mine is currently reversed the spinner is to big and the rows spacing is too small.
Any ideas??
while setting adapter say adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
this should work...
Maybe this is quite late but I'll share this anyway because I came across a similar problem.
You need to use the Views "simple_spinner_item" for the Spinner itself and "simple_spinner_dropdown_item" for the dropdown.
Here's a code snippet:
Spinner spnCat = (Spinner) findViewById(R.id.idea_category);
Cursor c = myDBHelper.getCategories();
startManagingCursor(c);
if (c != null) {
// use the simple_spinner_item view and text1
SimpleCursorAdapter adapterCat = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item,
c,
new String[] {c.getColumnName(1)},
new int[] {android.R.id.text1});
spnCat.setAdapter(adapterCat);
// use the simple_spinner_dropdown_item
adapterCat.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
}
Hope that helps.

Categories

Resources