Android - Spinner + setOnClickListener - java

So, i want to assign a Spinner OnClick, but i get the following error
java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
I need it to collect data when i click the spinner, then use it later.
I know i did it before, but i lost the code. Googled for an answer, i didn't find anything that works.
If this helps, I am using pagerview layout.
Here is my code:
public class DATA extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
ScrollView GPU_LAYOUT = (ScrollView)inflater.inflate(R.layout.gpu, container, false);
final TextView higher_than =(TextView) GPU_LAYOUT.findViewById(R.id.gpu_higher_than_value);
final Spinner min_max =(Spinner) GPU_LAYOUT.findViewById(R.id.min_max_spinner_gpulay);
min_max.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String TMP=min_max.getSelectedItem().toString();
higher_than.setText(TMP);
}
});
return GPU_LAYOUT;
}
Is there any way i can do it? Thank you!

You can do it with setOnTouchListener in that way:
spinner.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Toast.makeText(getBaseContext(), "CLICK", Toast.LENGTH_SHORT).show();
return false;
}
});

I'm doing it like this:
public void addListenerOnSpinnerItemSelection() {
mySpinner = (Spinner) findViewById(R.id.GPU_LAYOUT);
mySpinner.setOnItemSelectedListener(new myOnItemSelectedListener());
}
public class myOnItemSelectedListener implements OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View arg1, int pos,long arg3) {
int position=Arrays.asList(getResources().getStringArray(R.array.GPU_LAYOUT)).indexOf(fecha); }
#Override
public void onNothingSelected(AdapterView<?> arg0) { }
}
Also I suggest to get the position of the group you're using and then handle it.

min_max.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
//whatever your logic is put it here
}
});

you can't use setOnClickListener directly on a Spinner. If you want to do it here is a trick.
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (view.getId()) {
case R.id.sp_category:
if (motionEvent.getAction() == MotionEvent.ACTION_UP)//replacing with onclick
{
//Perform any action
}
break;
}
return true;
}
In this code MotionEvent.ACTION_UP will make this touch act like onclick.
Hope it will help you!

Related

Recycler View in Android not Clicking

I am working on an Android app. I am facing some problem with opening a popup menu when I click on an Item in the Recycler View.
Show.java (the Activity containing the Recycler View). mRecycler is the object associated with the RecyclerView.
mRecycler.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), mRecycler, new RecyclerViewClickListener() {
#Override
public void onClick(View view, final int position) {
PopupMenu menu = new PopupMenu(Show.this,mRecycler);
MenuItem itemView = (MenuItem) findViewById(R.id.three);
if(method.equals("Completed"))
itemView.setTitle("Mark as imcomplete");
menu.getMenuInflater().inflate(R.menu.menu_popup,menu.getMenu());
menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){
public boolean onMenuItemClick(MenuItem item){
int id=item.getItemId();
switch (id){
case R.id.one:
Intent intent = new Intent(Show.this,Add.class);
intent.putExtra("Task",tasks.get(position));
startActivity(intent);
finish();
startActivity(getIntent());
break;
case R.id.two:
deleteRecord(position);
break;
case R.id.three:
toggleComplete(position);
}
return true;
}
});
}
#Override
public void onLongClick(View view, int position) {
onClick(view,position);
}
}));
Code snippet for xml of RecyclerView
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:scrollbars="vertical"
android:clickable="true"
android:contextClickable="true"
android:longClickable="true" />
RecyclerViewClickListener.java
public interface RecyclerViewClickListener {
void onClick(View view,int position);
void onLongClick(View view,int position);
}
RecyclerTouchListener.java - class that implements the OnItemTouchListener
public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener{
private GestureDetector gestureDetector;
private RecyclerViewClickListener clickListener;
public RecyclerTouchListener (Context context,final RecyclerView recyclerView,
final RecyclerViewClickListener clickListener){
this.clickListener = clickListener;
gestureDetector = new GestureDetector(context,new GestureDetector.SimpleOnGestureListener(){
#Override
public boolean onSingleTapUp(MotionEvent e){
return true;
}
#Override
public void onLongPress(MotionEvent e){
View child = recyclerView.findChildViewUnder(e.getX(),e.getY());
if (child != null && clickListener!=null)
clickListener.onLongClick(child,recyclerView.getChildAdapterPosition(child));
}
});
}
#Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e){
View child = rv.findChildViewUnder(e.getX(),e.getY());
if (child !=null && clickListener !=null && gestureDetector.onTouchEvent(e))
clickListener.onClick(child,rv.getChildAdapterPosition(child));
return false;
}
#Override
public void onTouchEvent(RecyclerView rv,MotionEvent e){}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntecept){}
}
Nothing happens when I click an item on the RecyclerView. The android system does not even acknowledge that it was clicked. Please check what is the problem with my code.
Would You please try this
mRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), mRecyclerView, new ClickListener() {
#Override
public void onClick(View view, int position) {
menuCardIndex = position;
PopupMenu popupMenu = new PopupMenu(Show.this, view);
popupMenu.setOnMenuItemClickListener(Show.this);
popupMenu.inflate(R.menu.menu_popup);
popupMenu.show();
}
#Override
public void onLongClick(View view, int position) {
}
}));
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.one:
Your_First_Operation;
return true;
case R.id.two:
Your_Second_Operation;
return true
case R.id.three:
Your_Third_Operation;
return true
}
return false;
}
finally put this one your activity
public class Show extends AppCompactActivity implements PopupMenu.OnMenuItemClickListener
hopefully by this way, You can listen your click listener. if it's working properly, You can set your logic.
If your RecyclerView items have "clickable" children in their XML file, try to declare the property:
android:clickable="false"
Leave the clickable true only on the father of your list item.
If this is not your case, or you absolutely need clickable elements in your list item, there is another solution.
If you are using a RecyclerView, you should have implementend a RecyclerView Adapter somewhere in your code.
In the OnBindViewHolder method, try the following code:
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
//YOUR CODE ...
holder.itemView.setOnTouchListener(yourOnTouchListener);
//MORE CODE ...
}
This will set the OnTouchListener to the single ItemView at the moment the list is generated.
I hope my answer is helpful and understandable enough.

onItemLongClick is undefined?

I'm trying to implement a long click in my listview item but it doesn't work and i get an error that says is undefined. Here's the code:
protected void setOnItemLongClickListener(ListView l, View v, int position, long id) {
super.onItemLongClick(l, v, position, id);// Error
ApplicationInfo app = applist.get(position);
try {
Intent intent = packageManager
.getLaunchIntentForPackage(app.packageName);
if (null != intent) {
startActivity(intent);
}
} catch (ActivityNotFoundException e) {
Toast.makeText(MainActivity.this, e.getMessage(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
Someone has an idea how solve the problem?Thanks
The reason for this is most likely that you don't implement the listener. Something like
public class ActivityName extends Activity implements OnItemLongClickListener{
Try changing
protected void setOnItemLongClickListener
to
protected boolean setOnItemLongClickListener{
// your code
return true;
You need to use the proper return type for the method which is boolean then return true so the listener knows it was a success.
Docs
try this listener for Listview :
istView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(arg0.getContext(), ((TextView)arg1).getText(), Toast.LENGTH_SHORT).show();
return false;
}
});
use this code
yourListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//YOUR_CODE_HERE
return false;
}
});
Please replace
public class MainActivity extends Activity implements OnItemLongClickListener
and add unimplemented method
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
return false;
}
by default you can do this by right clicking on OnItemLongClickListener select Quick fix
try to add this lines to your list adapter
view.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return false;
}
});
and the method is try to overwrite your method
#Override
public boolean onItemLongClick(
AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
return false;
}

Where to initialize onLongClickListener

I am struggling with trying to implement an OnLongClick feature - I can't understand where to add a listener and to define the resulting method.
The implementation i have used uses an adapter - and does not have an onClickListener, but works jsut fine. can anyone suggest where/how to implement OnLongClick listener
I don't need every item in the list to perform different actions - just for anywere on the screen to pick up the long press
public class CombChange extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ListEdit(this, symbols));
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String selectedValue = (String) getListAdapter().getItem(position);
if (lastPressed.equals(selectedValue) ){
count++;}
}
public class ListEdit extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public ListEdit(Context context, String[] values) {
super(context, R.layout.activity_comb_change, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.activity_comb_change, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
textView.setText(values[position]);
// Change icon based on name
String s = values[position];
if (s.equals("a")) {
imageView.setImageResource(R.drawable.a);
return rowView;
}
}
Try this:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
v.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
String selectedValue = (String) getListAdapter().getItem(position);
if (lastPressed.equals(selectedValue) ){
count++;}
return false;
}
});
}
It is unfortunate that a ListActivity does not have a protected onListItemLongClick() method similar to the onListItemClick() function.
Instead, you can add setOnLongClickListener() to the top-level layout item (or any View) in your adapter's getView() function.
Example:
myView.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// Do something here.
return true;
}
});
Warning, the OnLongClickListener you put onto your list item may hide exposure to the onListItemClick() function you already have working for the list. If this is the case, you will also have to add setOnClickListener() to getView() and use it instead.
in your getView you can say
rowview.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View arg0) {
//Do your stuff here
return false;
}
});

how to implement a long click listener and onclicklistener in single listview

I am creating a app in android. In that i am using list view. now i want use both click event and long click event. if is possible can any help me to do.
You just need to return true
list.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(final AdapterView<?> p, View v,final int po, long id) {
// your code
return true;
}
});
It basically tells the system that the Long press event has been handled (default is false), and no further events need to be handled (i.e. a single press, which inadvertently would happen in a long press event)
see this
Click & Long-Press Event Listeners in a ListActivity
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> av, View v, int pos, long id) {
onListItemClick(v,pos,id);
}
});
..
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
return onLongListItemClick(v,pos,id);
}
});
You should use ListView.setOnItemClickListener for a simple click.
For the long click, you have a choice. If you want to perform a single action use ListView.setOnLongClickListener. If you want a context menu then register the list for a context menu, create the menu and the actions for it.
registerForContextMenu(ListView);
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
// menu code here
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
// menu habdling code here
return super.onContextItemSelected(item);
}
use ListView.setOnItemClickListener(listener) and ListView.setOnItemLongClickListener(listener)
http://developer.android.com/guide/topics/ui/layout/listview.html
Just use setOnItemClickListener() and setOnItemLongClickListener() on your listview.
listView = (ListView) findViewById(R.id.listview);
listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
}
});
listView.setOnItemLongClickListener(new OnItemLongClickListener()
{
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int position, long arg3)
{
}
});
use the following code.
list.setOnItemClickListener(this);
list.setOnItemLongClickListener(this);
Listener definitions will be :
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
return false;
}
itemToclick is the visible portion on which click u want some action
itemToClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do your logic on click
});
itemToClick.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// do your logic for long click and remember to return it
return true; }});

Spinner with on Click Listener

I am using spinner that shows error when i am trying to extract the item id of the selected spinner item.
My Code goes here:
public void dispspi()
{
spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter <String> adap= new ArrayAdapter(this, android.R.layout.simple_spinner_item, level);
spinner.setAdapter(adap);
spinner.setOnItemClickListener(new OnItemClickListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
int item = spinner.getSelectedItemPosition();
p=item;
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
}
How to get the item id of the spinner? Any help is appreciated..Thanks in advance
IIRC, you should be using a selected listener, not click:
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
Then you can add the override tag to your selected method.
private String selecteditem;
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView adapter, View v, int i, long lng) {
selecteditem = adapter.getItemAtPosition(i).toString();
//or this can be also right: selecteditem = level[i];
}
#Override
public void onNothingSelected(AdapterView<?> parentView)
{
}
});
spinner3.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v,
int postion, long arg3) {
// TODO Auto-generated method stub
String SpinerValue3 = parent.getItemAtPosition(postion).toString();
Toast.makeText(getBaseContext(),
"You have selected 222 : " + SpinerValue3,
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Yes you can use some OnItemSelectedListener for work with selected item. But sometimes we would like to handle exactly click for spinner. For example hide keyboard or send some analytics etc. In this case we should use TouchListener because OnClickListener doesn't work properly with Spinner and you can get error. So I suggest to use TouchListener like:
someSpinner.setOnTouchListener { _, event -> onTouchSomeSpinner(event)}
fun onTouchSomeSpinner(event: MotionEvent): Boolean {
if(event.action == MotionEvent.ACTION_UP) {
view.hideKeyBoard()
...
}
return false
}
you should have this in the listener(OnItemSelectedListener)
public void onNothingSelected(AdapterView<?> arg0) {
}
It might works without it but put it to be consistent
but there might be other errors also, can you provide the error log ?

Categories