I'm trying to make a very very simple spinner at least, as follows:
XML:
<Spinner
android:id="#+id/spinner_categories"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:layout_weight="1"
android:textColor="#000000"
android:spinnerMode="dropdown"/>
JAVA:
spinnerCategories = findViewById(R.id.spinner_categories);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getApplicationContext(),
android.R.layout.simple_spinner_item,
categories);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerCategories.setAdapter(adapter);
Log.d(Utilities.LOG_FLAG, "SPINNER: " + spinnerCategories.toString());
spinnerCategories.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
Toast.makeText(context, categories.get(position).toString(), Toast.LENGTH_SHORT).show();
Log.d(Utilities.LOG_FLAG, "SELECTED");
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
Log.d(Utilities.LOG_FLAG, " NOT SELECTED");
}
});
I can see the entire list, but once I click on an item, nothing happans, and it wont show the selection at all, even if I use setSelection(), and if I try to do spinnerCategories.getSelectedItem().toString() I get a NullPointerException.
I tried searching the web a lot, but none solution seems to help me...
Edit
For some reason when I load the page and then I go out of the page and reenter it, only then it will show the selected items of the spinner
On the first load it shows for a very brief second and then it's gone until the page is reentered the second time
The setOnItemSelectedListener gets triggered when you click on an element from the drop down menu .. to fetch the selected item you need to use .getSelectedItemPosition() something like
categoriesList.get(spinner.getSelectedItemPosition)
Related
I have a spinner that i fill with 4 elements. During startup onItemSelected method gets executed correctly and the toast message is displayed. However when i open the spinner and try to click any item, no event is called and the spinner popup will not close unless i press the spinner arrow. In other words I can not interact with spinner items.
Below is the activity code for the spinner
<Spinner
android:id="#+id/sp_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
this is my java code
ArrayList<String> strItemsize = new ArrayList<String>(Arrays.asList("H 1", " H 2", " H 3", "H 4", "H 5"));
final ArrayList<Integer> Hsize = new ArrayList<>(Arrays.asList(18, 16, 14, 12, 10));
ArrayAdapter<String> adapterSize = new ArrayAdapter<String>(App.CurentActivity, android.R.layout.simple_spinner_dropdown_item, strItemsize);
adapterSize.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spSize.setAdapter(adapterSize);
spSize.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
App.ShowMessage().ShowToast(""+i, ToastEnum.TOAST_SHORT_TIME);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
i searching but dose not solve it.this Link is my problem like but dos not my solotion.
Set in XML for spinner clickable=true
Or use Widget.AppCompat.Spinner instead. Seems to be Marshmallow bug
You should set spinner attribute android:clickable=true in your XML.
<Spinner
android:id="#+id/sp_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable=true />
i am having a list view in which i would like to give a background color to a row when it is pressed.When i select an item its changing the color but the problem is when i scroll down other rows too are got colored.I don't want that.
Here is my code:
Thanks in advance
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
final String item = (String) parent.getItemAtPosition(position);
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
view=parent.getChildAt(position);
view.setBackgroundColor(Color.GREEN);
}
});
}
I think you should handle that in your adapter. A listview re-uses listelements and in your case the one with green background will be reused when you scroll. Instead you should tell your adapter which item was clicked
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
((YourAdpater)listView.getAdapter()).setSelected(position);
}
});
And in your Adapter's getView() you can then set a background if the current position equals the selected one or not...
Or look at the listview's choice mode http://developer.android.com/reference/android/widget/AbsListView.html#attr_android:choiceMode
I'm not really familiar with it, but it might be helpful...
The nicest way to handle this is to set listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);, which allows you to select multiple list items.
To highlight the selected items, all you have to do is create a custom background selector, which defines how the items will look like in which state. For your example this would be something like this:
selector.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:color/white" android:state_activated="false"/>
<item android:drawable="#android:color/green" android:state_activated="true"/>
</selector>
Put this file in your drawable folder and give your list items this background like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/selector">
...
</LinearLayout>
This way you don't have to handle clicks on the items.
You can retrieve the selected items via listview.getCheckedItemPositions()
To highlight a ListView you dont have to manually colour them, Android already has a function for it.
m_ResultsArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_activated_1,m_ResultsArray)
Instead of using a simple_list_item_1 use a simple_list_item_activated_1. This will allow ur list to be highlighted and you can even check which one is checked by using
this.m_ResultsListView.getCheckedItemPosition();
Hopefully this answers your question :>
I would like to have a fix value that is always displayed in the Spinner and when clicking on the Spinner this value should not be listed in the drop down selection.
Until now I have the following
Definition XML:
<Spinner
android:layout_width="76dp"
android:layout_height="40dp"
android:id="#+id/right_shift"
android:layout_row="0"
android:layout_column="0"/>
Java:
final Spinner right = (Spinner) findViewById(R.id.right_shift)
ArrayList<String> rightShift = new ArrayList<String>();
rightShift.add(" >>"); //THIS SHOULD BE THE VALUE THAT IS ALWAYS DISPLAYED
for (int i=0; i<5; i++)
...//add other values to arraylist
...//set values of arraylist to spinner
right.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
right.setSelection(0);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
But when clicking on the Spinner the preselected Item will again be shown in the drop down and the right.setselection(0) is not executed fast enough so I still see the selected Item for about 0.5sec... Is there an other/easier way to perform this?
you can add
android:prompt=" >>"
in xml and in java set default position in spinner to be -1.
I implemented the android listview with the ListActivity. Here I have the problem that when i click on the list item no action is performed when the flash color is also not coming that is the orange color. So do you have any idea about this kindly answer to my question.
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_SHORT)
.show();
}
I put this code also into the Main ListActivity.
The first thing what you have to note here is, whenever there are Clickable elements like Buttons or ImageButtons present in your ListView element, they take the control of click events. And so your ListView won't get the chance to accept the click event.
What you simply have to do is, set the focusable attribute to false for the Button or ImageButton you have in your ListView. But still they will work without any problem and also your ListView's onListItemClick will also work.
Try this,
<Button android:id="#+id/textsize_increaser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/back_button"
android:focusable="false"
android:text=" A + "/>
Here I have added this android:focusable="false" and it works fine. try it.
Have you set the choice mode of ListView to SINGLE :
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
And if you have any clickable imageview or textview or button in the list item, then make them not focusable (in your Adapter class):
yourButton.setFocusable(false);
yourButton.setFocusableInTouchMode(false);
Are you using custom Adapter? and inflating layout with button or any view that eats away the list list view focus as child, then it won't work obviously. make sure to set
android:focusable="false"
to such view in xml file. hope this works for you.
Set this in your listactivity java file
listview1.setFocusable(false);
Actually there is a parameter meant for that to prevent children views from getting focus, just add the following in the parent layout:
android:descendantFocusability="blocksDescendants"
As the documentation explains:
The ViewGroup will block its descendants from receiving focus.
Eclipse suggested me to add textIsSelectable="true" to my TextViews in the layout xml which was used for list view.
Well, if you want to click the items in the list then you should not add those tags.
make sure that you are
Not using Scroll View with List View
Not using Scroll View in your row item layout for List View
If Scroll View is present at any of above place remove it
refer to this post for a solution:
Click is not working on the Listitem Listview android
View v = parent.getChildAt(position);
parent.requestChildFocus(v,view);
v.setBackground(res.getDrawable(R.drawable."Some drawable for clicked row"));
int count = parent.getChildCount();
for(int i=0; i<count; i++)
{
if(i!=position)
{
v = parent.getChildAt(i);
v.setBackground(res.getDrawable(R.drawable."some drawable for not clicked row));
}
}
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int pos,
long id) {
Toast.makeText(v.getContext(), exm.get(pos).getDefinition(),
Toast.LENGTH_SHORT).show();
}
});
listItemButton.setFocusable(false);
listItemButton.setFocusableInTouchMode(false);
Set the above in your adapter. It's not working in XML
So in my application I am using a ListView to display data from an ArrayList which holds objects. The data is displayed using the same method as the tutorial on the android developer website:
// automatically adds a ListView to fill the entire screen of this activity
setListAdapter(new ArrayAdapter<Part>(this, R.layout.list_item, Main.parts));
ListView lv = getListView();
// allows the user to start typing to filter the list
lv.setTextFilterEnabled(true);
// set the click listener for each list item
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
The objects are currently displayed in each list_item:
<!-- Defines the layout for each item being placed in the ListView. -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
So I return each object as such:
public String toString() {
return "Item Number: " + itemNmbr + "\nPrice: " + price + "\nDescription: " + desc;
}
An example of how the list currently looks:
http://imageshack.us/photo/my-images/689/devicet.png/
The problem is, I need to format the title separately from the data. (because it needs to be bold, and possibly spaced out a little further.)
Any ideas? I'm currently testing on how to get two textviews to work together.
Thank you!
I would recommend using a custom adapter extending BaseAdapter.
http://developer.android.com/reference/android/widget/BaseAdapter.html
See this link for an example.
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/