Nothing happens when I click on the Spinner item - android

I have fetched data from the server from the category class.The getcategories method return the List of String containing spinner items. When I click on the spinner item. Nothing happens. Is there any mistake in my code. Please help.
This is my java code.
public void fetchPropertyType(){
category = new Category(); //Spinner Item model
//categories is a array list of String which contains the items of spinner
categories = category.getCategories(AddPropertyActivity.this);
//Property Type Spinner Adapter
propertyTypeSpinner = (Spinner) findViewById(R.id.property_type_spinner);
Log.e("Test", "Just a test message");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
propertyTypeSpinner.setAdapter(dataAdapter);
propertyTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(position).toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Log.e("Test", "Nothing selected on spinner activity");
}
});
}
This is my layout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_weight="1">
<TextView
android:id="#+id/spinner_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="50dp"
android:gravity="center"
android:text="Property Type"
android:textAlignment="textEnd"/>
<Spinner
android:id="#+id/property_type_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"/>
</RelativeLayout>

You just should do this.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:background="#drawable/border">
<TextView
android:id="#+id/spinner_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="50dp"
android:gravity="center"
android:text="Property Type"
android:textAlignment="textEnd"/>
<Spinner
android:id="#+id/property_type_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"/>
</RelativeLayout>
In the Spinner
Change
android:layout_height="match_parent"
To
android:layout_height="wrap_content"
Because you use android:layout_height="match_parent",so you can't see your list item.And nothing happens.

As you are using
<Spinner
android:id="#+id/property_type_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"/>
Which wraps the height of spinner so try to give custom height to it so that i t will be clickable
I did this
<Spinner
android:id="#+id/property_type_spinner"
android:layout_width="match_parent"
android:layout_height="50dp"
android:spinnerMode="dropdown"/>
and keep your text view height wrap_content as
<TextView
android:id="#+id/spinner_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"
android:gravity="center"
android:text="Property Type"/>

Related

Dropdown Spinner in NestedScrollView in Fragment

I am making a dropdown menu (spinner) inside a fragment. I also need that page to have a Recyclerview that extends and scrolls with the page, so I use the nested scroll view. Java Code:
Spinner dropdown = root.findViewById(R.id.dropdown);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),android.R.layout.simple_spinner_dropdown_item,arrayList);
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
dropdown.setAdapter(adapter);
dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getContext(),parent.getItemAtPosition(position).toString(),Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Xml Code:
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:paddingBottom="65dp"
tools:context=".ui.dashboard.ShelvesFragment">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Spinner
android:id="#+id/dropdown"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#color/honeyBright"
android:textColor="#545454"
android:spinnerMode="dropdown"
android:layout_margin="15dp"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/shelves"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.core.widget.NestedScrollView>
When I run this code it just shows this as the drowpdown: Dropdown Spinner Image
It did work when I used the R.array value and the CharSequence Adapter, but I need it to be an array list and I'm not sure the exact code I used then.
Edit:
I figured out that the ArrayList actually didn't have any items in it, that was why it wasn't showing up. So I just fixed that and it worked fine.

TextView taller than containing TableRow after calling spinner.setdapter() in same row

I have a xml view that I'm inflating. I'm then doing a network call and with the results populating all these spinners. This is what the view looks like before I call setadapter on the spinners.
However, when I call spinner.setadapter(), my last TextView gets cut off.
As you can see, the TextView is larger than the TableRow.
Here is snippet of xml layout.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fadeScrollbars="false"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/bcast_table">
<TableRow style="#style/row">
<CheckBox style="#style/chbx"
android:text="#string/title_num_retx"
android:id="#+id/num_retx_chbx" />
<Spinner style="#style/spinner"
android:id="#+id/num_retx_spinner" />
</TableRow>
<TableRow style="#style/row">
<CheckBox style="#style/chbx"
android:text="#string/title_cwm0"
android:id="#+id/cwm0_chbx" />
<Spinner style="#style/spinner"
android:id="#+id/cwm0_spinner" />
</TableRow>
</TableLayout>
</LinearLayout>
And here is how I setadapter()
static void populateSpinner(int spinId, ArrayList<String> fields, String currPos, View template, Context context){
Spinner spinner = (Spinner) template.findViewById(spinId);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
android.R.layout.simple_spinner_item, fields);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
int position = adapter.getPosition(currPos);
spinner.setSelection(position);
}
Thanks for the help!

OnItemClickListener not called with custom layout row in ListView

I have such a problem. I'm trying to get id of row in ListView. To do so I'm using setOnItemClickListener:
wyswietlenieZadan = (ListView)findViewById(R.id.ListView);
mAdapter = new ArrayAdapter<>(this, R.layout.zadania, R.id.opisZadania, listaZadan);
wyswietlenieZadan.setAdapter(mAdapter);
wyswietlenieZadan.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "Twoje id: " +id, Toast.LENGTH_SHORT).show();
}
});
Here is my zadania.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical">
<TextView
android:id="#+id/opisZadania"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:textSize="20sp"
android:layout_marginStart="32dp"
android:layout_alignParentStart="true"
android:layout_marginTop="12dp"
android:clickable="true"
android:contextClickable="true" />
<ImageButton
android:layout_width="wrap_content"
app:srcCompat="#android:drawable/ic_menu_close_clear_cancel"
android:id="#+id/task"
android:layout_marginEnd="19dp"
android:layout_height="wrap_content"
style="#android:style/Widget.ImageButton"
android:background="#android:drawable/ic_delete"
android:onClick="usunZadanie"
android:layout_alignTop="#+id/opisZadania"
android:layout_alignParentEnd="true"
android:focusable="false" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#android:drawable/ic_menu_edit"
android:id="#+id/imageView"
android:layout_toStartOf="#+id/task"
android:layout_alignTop="#+id/task"
android:onClick="edytujZadanie"
android:focusable="false" />
</RelativeLayout>
It doesnt work but when I use simple_list_item_1.xml as layout instead of zadania.xml, I can get id of each record. Anybody knows why it doesn't work with my custom layout?
Change:
Toast.makeText(getApplicationContext(), "Twoje id: " +id, Toast.LENGTH_SHORT).show();
to:
Toast.makeText(getApplicationContext(), "Twoje id: " +position, Toast.LENGTH_SHORT).show();
as the position variable gives you the position of the item you just clicked. If you clicked the first item it will show Twoje id:0 and if you clicked the second item it would show Twoje id:1 and so on...

Android: OnItemClickListener does not work

I implemented my custom list but when I click an element of my list a Toast message should be displayed. It looks like that the setOnItemClickListener does not work.
Here there is the code of of my fragment layout in which there is my ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<ListView
android:id="#+id/listSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:paddingStart="15dp"
android:paddingEnd="15dp"
android:focusable="false"/>
</LinearLayout>
here there is the structure of my list (each single line)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/imageContact"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp"
android:focusable="false"/>
<TextView
android:id="#+id/nameSurnameContact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#33CC33"
android:focusable="false"/>
<TextView
android:id="#+id/idContact"
android:layout_width="match_parent"
android:layout_height="0dp"
android:visibility="invisible"
android:focusable="false"
/>
</LinearLayout>
</LinearLayout>
and here there is the mothod of my fragment that creates the layout and populate the list
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedBundle) {
View firstAccessView;
if(savedBundle==null) {
firstAccessView = inflater.inflate(R.layout.search_fragment_layout, null);
for(int i=0; i<8; i++){
Contact c = new Contact(idContact[i], nameSurname[i], facebookId[i], timeStamp[i]);
this.rows.add(c);
}
adapter = new SearchListAdapter(getActivity(), this.rows);
list = (ListView) firstAccessView.findViewById(R.id.listSearch);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getActivity().getApplicationContext(), "item selected", Toast.LENGTH_SHORT).show();
}
});
list.setOnScrollListener(this);
}else{
firstAccessView = getView();
}
return firstAccessView;
}
is there something that I missed in the xml files of in the Fragment file?
Remove android:focusable="false" from ListView.
android:focusable="false"
Remove this line from your listview xml file

Android ListView creates the views but doesn't display them

I am having problems with ListView on android. I have an Activity with an EditText view and a button. The idea is user should enter some info in the EditText field, touch the Search button which retrieves a list from a web service that it is displayed in the same activity below the EditText and the Button. Everything fine, I got the data from Internet but items weren't displaying. I was using the notifyDataSetChanged(). After a couple of hours with no success, I decided trying to put some items manually and it turns out that again nothing was displayed, hence I think I am doing something wrong when I am trying to set the listview and the adapter. So here is the code..
the xml of the activity activity_search.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp">
<EditText
android:id="#+id/edit_search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:inputType="text"
android:hint="Enter info"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button_search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:drawable/ic_menu_search"
android:contentDescription="Search"
android:ems="10" />
</LinearLayout>
<ListView
android:id="#+id/listview_items"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false" />
The XML of the item row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="5dp">
<TextView
android:id="#+id/row_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Common_TextContent"
android:textAppearance="#android:style/TextAppearance.Holo.Medium" />
<TextView
android:id="#+id/row_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="#string/Common_TextContent"
android:textAppearance="#android:style/TextAppearance.Holo.Small" /></LinearLayout>
the custom Adapter:
public class ItemsListAdapter extends ArrayAdapter<ItemsList> {
private int[] colors = new int[] { 0x30ffffff, 0x30808080 };
public AssetListAdapter(Context context,
List<ItemsList> itemsList) {
super(context, 0, itemsList);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, null);
}
convertView.setBackgroundColor(colors[position % colors.length]);
TextView code = (TextView) convertView.findViewById(R.id.row_code);
code.setText(getItem(position).getCode());
TextView name = (TextView) convertView.findViewById(R.id.row_name);
name.setText(getItem(position).getName());
return convertView;
}
and the onCreate method on the Activity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
_listView = (ListView) findViewById(R.id.listview_items);
_listItems = new ArrayList<ItemsList>();
_listItems.add(new ItemsList ("cosa1", "cosa1", "cosa1", "cosa1"));
_listItems.add(new ItemsList ("cosa2", "cosa2", "cosa2", "cosa2"));
_listItems.add(new ItemsList ("cosa3", "cosa3", "cosa3", "cosa3"));
_adapter = new ItemsListAdapter(this, _listItems);
_listView.setAdapter(_adapter);
}
The ItemsList is just an Object with 4 strings with all the getters implemented.
When I debug in the getView method of the Adapter, the view (convertView) is created and it has the right information. It is just that is not showing those in the screen. What am I doing wrong?
Thanks...
Your second LinearLayout has a height of match_parent. Try wrap_content instead.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">

Categories

Resources