Android : Custom ArrayAdapter showContextMenu onClick - android

In my Android application I have a custom ArrayAdapter for managing rows in a List that implements onCLickListener.
In the onClick() method I want to show the context menu.
public class MyAdapter extends ArrayAdapter<MyClass> implements
OnClickListener {
public void onClick(View v) {
v.showContextMenu();
}
}
In my main List Activity class I override the onLongListItemClick() method that starts a different Activity.
When I click on a row in the list - the context menu is shown correctly but a second later the onLongListItemClick() is also executed. How to stop this happening?
thanks!

After your comments, I think I have a solution. Don't use the row onClick for the context menu, set a click listener in your adapter for the checkbox and show your context menu from there. That way you avoid the onLongListItemClick.
To make both the checkbox and the row clickable, add this to the checkbox definition in your XML:
android:focusable="false"
android:focusableInTouchMode="false"
EDIT
This should do the trick:
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked){
View v = buttonView.getParent(); // get the parent row view containing the checkbox
v.showContextMenu(); // show the context menu
}
}
}

Related

GetSelectedItem on OnItemClickListener (Android)

let's say you have a listview with custom xml-objects on it in a row.
But you want to address specifically the textview, if it's press in the OnItemClickListener. What's the way - or bette - the best practise to do so? If i check in the OnItemClick method, the specifically element in the row (e.g. the textview, doesn't get recognized.
You can add onClickListener to that textView in the getView method of your adapter like this.
viewHolder.myTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG,"item= "+data.get(position).getTitle());
}
});
For this to work create a class which extends BaseAdapter class and implements all its method
1.Now in the getView() method of this class initialize all the required views in a single item of listview
2.After that attach onClickListener() to the desired textView.
3.After that select your custom adapter class in the listView.setAdapter() method
This should help you if it doesn't work use RecyclerView instead and do all the above task in the onCreateViewHolder() method of RecyclerView.adapter Class
if you have model class name **User** now you can get user info by using
viewHolder.tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
User user = data.get(pos);
user.getTitle();
Log.d(TAG,"item= "+data.get(position).getTitle());
}
});

Is there a way to hide specific listview items from the main activity?

Just wondering if what I'm trying to do is possible. So i have a custom adapter for a listview. It contains a textview and two buttons. I would like one of the listview buttons to remain hidden unless a specific button is pressed on the main activity.
So far I have the listview buttons performing their intended function but I have no idea how I would even begin to get what I'm wanting.
Sorry, for clarification, I have one button completely separate from the listview that is just always there. When I press this button I would like to toggle the visibility of a button that is on each listview item all together. The best example of this that I can think of is having a list of items and a button that can toggle off the 1. 2. 3. 4. that comes in front of each item.
Create a method in your adapter for knowing you have clicked the button from your main activity like this
public void buttonIsClicked(){ //in your adapter
buttonhide.setVisibility(visibility?View.VISIBLE:View.GONE);
}
And call this method from your activity on btnclick.
like
yourAdapter.buttonIsClicked();
and call this method for notifying the adapter about the change.
yourAdapter.notifyDatasetChanged().
or
You can use an interface for listening to the clicks in main activity and implement that listener in your adapter
Set visibility gone to the button you want to hide by calling code
buttonhide.setVisibilty(VIEW.GONE);
hide it in oncreate() of your activity and make it shown on the button click event by calling code buttonhide.setVisibilty(VIEW.VISIBLE);
Below is the code
btnView.setVisibility(View.GONE);
yes it is possible to do that.
First have a Model class to back the listview data and keep a flag in that model which indicates whether to show the button in that row's data model. On certain condition change that model's flag and call notifyDataSetChanged() on adapter.
Ex:
class Model{
String label;
boolean showBtn;
}
in adapter's getView()
Model model = list.get(position)
if(model.showBtn){
btn.setVisibility(View.VISIBLE);
}else{
btn.setVisibility(View.GONE);
}
in Activity
disableButton(){
modelList.get(0).setShowBtn(false);
adapter.notifyDataSetChanged();
}
This code will hide button in first row
Add a Boolean value in your dataset which represent the Visibility state of the button.
public class Dataset {
private boolean visible;
public boolean isVisible() {
return this.visible;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
//..more items
}
Then in your getView method of the Adapter check this Boolean value to show/hide the button.
boolean visibility = yourDataset.get(position).isVisible();
yourButton.setVisibility(visibility?View.VISIBLE:View.GONE);
And when the Button outside of your listview is clicked Update your dataset. And call yourAdapter.notifyDatasetChanged().
What you are attempting is: manipulating the visibility of the button declared in the Adapter from the containing activity. Simple, put a controlling variable in the activity and pass it a parameter to adapter.
Boolean mShowButton; //a controlling variable
void onCreate(Bundle savedInstanceState) {
...
mAdapter=new MyAdapter(...,mShowButton);
mButton.setOnClickListener(actionShow );
}
OnClickListener actionShow = new OnClickListener() {
#Override
public void onClick(View button) {
mShowButton=true;
mAdapter.notifyDataSetChanged();
mListView.invalidateViews();
}
};
And do this in your adapter,
Boolean showButton;
public MyAdapter(Context context, List<String> myList, Boolean showButton) {
...
this.showButton=showButton;
}
public View getView(int position, View rowView, ViewGroup parent) {
...
if(showButton){
mButtonTwo.setVisibility(View.GONE);
}else{
mButtonTwo.setVisibility(View.VISIBLE);
}
}

Android: Inform main activity about CheckBox of custom ListView

I have a Custom BaseAdapter showing a ListView containing a TextView and a CheckBox. The TextView has an OnClickListener implemented to perform a specific action when clicked on the text. Within the Adapter I have a OnCheckedChangeListener registered which keeps track of which item is checked (because of the ListView recycling).
I want to start an ActionMode when I check a CheckBox and stop it when I uncheck it.
How can I inform the main activity hosting the adapter that a check has been made?
Create a listener call back interface, something like:
public interface ListenerCheckBox
{
public void onRowChecked(int rowNun);
}
Then, make your main activity implement this listener:
public class ActivityMain implements ListenerCheckBox
Then, when you instantiate your custom BaseAdapter, pass in the listener:
//kv 3rd parameter would be listener
CustomBaseAdapter customBaseAdapter = new CustomBaseAdapter(this, items, this);
Then, in the constructor of your CustomBaseAdapter, set a member field to the listener:
public CustomBaseAdapter(Context context, ArrayList<Item> items, ListenerCheckBox listenerCheckBox)
{
mListenerCheckBox = listenerCheckBox;
...
}
Then, every time an item is checked, call:
mListenerCheckBox.onRowChecked(rowNum);
Here is a Android Studio project that shows an example of how to do what you are asking. In particular, check out the check listener and its onClick():
#Override
public void onClick(View v) {
if (mMode != null) {
mMode = mListView.startActionMode(new ExampleMultiChoiceModeListener(mActivity));
}
mListView.setItemChecked(mPosition, ((Checkable) v).isChecked());
}

Disable listview row when checkbox is checked

In my application I am using Custom Listview with row as TextView and CheckBox.
Now I want that when I check CheckBox at that time that respective row have to Disable(unfocasable).
What should be the code inside CheckBox OnCheckedChangeListener
I also tried to disable rowlayout inside Listener but not got success...
following is code I am using inside getView method of Adapter
final RelativeLayout RL=(RelativeLayout)convertView.findViewById(R.id.VTRLmain);
check.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
{
if (isChecked)
{
RL.setEnabled(false);
templist.add(getItem(position));//using for another activity
}
}
});
give in xml code for checkbox
android:focusable="false"
it won't hide listview textboxs.
and check the application theme.
In the adapter there is a method named isEnabled which you can overide. Try to do in your custom adapter
Try this code:
#Override
public boolean isEnabled(int position) {
if(checkbox1.isChecked()){
return false;
}
return true;
}

Android Listview, Handle clickable textviews

In my android project I am Using a custom listview to show information. In each element of the listview(Each row in list view) have several clickable forcussable textviews. And I added lstvw.setItemsCanFocus(true), for listview and now i can select any clickable textview and click.
----------------
textviewclick1
textviewclick2
----------------
textviewclick1
textviewclick2
----------------
continue......
But can't figure out how exactly handle the onClick event for each clickable textview as getView() method in the BaseAdapter class is bit confusing for me.
I implemented onclickListener in the ListviewAdapter class that I extended BaseAdapter and created.
public class ListVWAdapter extends BaseAdapter implements OnClickListener{ implementation...}
can anyone suggest me how to handle onClick(View view) method to handle all the clickable textview items.
I tried following way but did't work correct. I globally declared the ViewHolder viewholder since I want to access it in this method rather than declaring it in normal way within the getView() method. But initializing normal way by checking if (convertView == null)
#Override
public void onClick(View v) {
if(v==holder.txtViewTitle)
Toast.makeText(v.getContext(), "Link1 : "+ String.valueOf(currentPosition), Toast.LENGTH_LONG).show();
if(v==holder.txtViewDescription)
Toast.makeText(v.getContext(), "Link2 : "+ String.valueOf(currentPosition), Toast.LENGTH_LONG).show();
}
In list item xml, for <TextView..> set android:onClick="onFirstLinkClick". Similarly for second TextView.
Then in activity class implement this
public void onFirstLinkClick(View V) {
// handle click
}
Similarly another method for second textview. That should work.

Categories

Resources