I'm trying to integrate a Spinner into my Layout (it was working well before AndroidX migration).
here is the code:
Spinner spinner_nationalite;
String[] data_nationalite = {"","Française","Suisse","Belge","Allemande"};
#Override
public void onCreate(Bundle savedInstanceState) {
....
spinner_nationalite = (Spinner) findViewById(R.id.update_spinner_nationalite);
ArrayAdapter adapter_nationalite = new ArrayAdapter<String>(this, R.layout.spinner_dropdown_item, data_nationalite);
adapter_nationalite.setDropDownViewResource(R.layout.spinner_dropdown_item);
spinner_nationalite.setAdapter(adapter_nationalite);
spinner_nationalite.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Log.d("KAIROSAPP","Nationalite selected: "+data_nationalite[i]);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Here is the spinner_dropdown_item.xml
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee"
android:fontFamily="sans-serif-thin"
android:textSize="17sp"
android:textStyle="bold"
android:background="#FFFFFF"
android:textColor="#000000"/>
And finally the Layout XML
<androidx.constraintlayout.widget.ConstraintLayout
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">
<Spinner
android:id="#+id/update_spinner_nationalite"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView55"/>
I can see the Spinner but there are no data listed and no dropdown visible on click.
I also tried to use AppCompatSpinner from androidx.appcompat.widget.AppCompatSpinner and also use android.R.layout.simple_spinner_dropdown_item instead of the custom one but I have the same result.
For sure I'm missing something....
There is no problem on my side. I ran your code successfully on AndroidX project.
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 tried making a dynamic list where the user can add elements by writing a text and pushing a button. The user also should be able to delete elements by clicking on them for a longer time.
The code can be build and I can start it. But while the adding works just fine, everytime I try to delete a specific element the App stops running.
public class MainActivity extends AppCompatActivity {
private ArrayList<String> ModuleList;
private ArrayAdapter<String> MyAdapter;
private ListView listView;
private Button addButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ModuleList=new ArrayList<>();
addButton=findViewById(R.id.btnPlus);
MyAdapter=new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
listView=findViewById(R.id.listView);
listView.setAdapter(MyAdapter);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addModule(view);
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
{
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
{
deleteItem(view, position);
return true;
}
});
}
private void addModule(View view) {
EditText input =findViewById(R.id.textInput);
String itemText=input.getText().toString();
if(!(itemText.equals("")))
{
ModuleList.add(itemText);
MyAdapter.add(itemText);
MyAdapter.notifyDataSetChanged();
input.setText("");
}
else
{
Toast.makeText(getApplicationContext(),"Please insert Module^...",Toast.LENGTH_LONG).show();
}
}
private void deleteItem(View view,int i)
{
Context context = getApplicationContext();
ModuleList.remove(i);
MyAdapter.remove(ModuleList.get(i));
MyAdapter.notifyDataSetChanged();
Toast.makeText(context, "Item Removed", Toast.LENGTH_LONG).show();
}
}
The XML codes looks like this:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"
tools:context=".MainActivity">
<EditText
android:id="#+id/textInput"
android:layout_width="325dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="15dp"
android:gravity="bottom|center_vertical"
android:text="Example application"
android:textSize="16dp" />
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/divider"
android:layout_marginBottom="0dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:descendantFocusability="blocksDescendants">
</ListView>
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_above="#+id/textInput"
android:layout_marginBottom="5dp"
android:background="?android:attr/listDivider" />
<Button
android:id="#+id/btnPlus"
android:layout_height="50dp"
android:layout_width="80dp"
android:layout_alignBottom="#+id/textInput"
android:layout_alignTop="#+id/textInput"
android:layout_marginStart="5dp"
android:layout_marginBottom="0dp"
android:layout_toEndOf="#+id/textInput"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
app:backgroundTint="#FF5722"
android:text="+" />
</RelativeLayout>
I'm new to Android Studio, so might be an obvious mistake, but I can't find it.
Thank you for your help.
ModuleList.remove(i);
MyAdapter.remove(ModuleList.get(i));
It seems that, in your Adapter, you want to delete an item which doesn't exist anymore. Try to switch these 2 lines.
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.
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"/>
A lot of people had this same issue before, but I haven't been able to solve it using their solutions..
I've got a ListActivity with a custom ArrayAdapter, but I can't seem to trigger the onItemClick.
I've tried it both with the default #Override protected void onListItemClick(...) which didn't trigger, and also with a myListView.setOnItemClickListener(new OnItemClickListener(){ ... });, which also didn't work.
From previous stackoverflow questions regarding this matter I've used the following pieces of code in the xml:
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"`
In the list_item.xml views
Or android:descendantFocusability="afterDescendants" in the ListView itself.
Here below is the main part of code regarding this problem:
ChecklistActivity.java:
public class ChecklistActivity extends ListActivity
{
private List<Product> products;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checklist);
// test products:
products = new ArrayList<Product>();
for(int i = 0; i < 5; i++){
Product p = new Product();
p.setName("Product " + i);
products.add(p);
}
MyAdapter adapt = new MyAdapter(this, R.layout.list_inner_view, products);
setListAdapter(adapt);
ListView lv = (ListView) findViewById(android.R.id.list);
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> adapt, View v, int position, long id){
onListItemClick(v);
}
});
...
}
private void onListItemClick(View v){
Log.i("CHECKLIST ACTIVITY", "onListItemClick triggered");
...
}
list_item.xml:
<?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"
android:layout_gravity="center_vertical"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:contentDescription="#string/checkbox_content_description"
android:src="#drawable/checkbox_unchecked"
android:background="#drawable/transparent_background"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false" />
<TextView
android:id="#+id/tv_product_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:textIsSelectable="false" />
</LinearLayout>
activity_list.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">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="afterDescendants" />
</RelativeLayout>
Ok, very stupid of me.. I removed
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
from the LinearLayout of the list_item.xml and now it works..