Gridview Button Adapter context menu - android

I have a phrasebook, with the ability to save the sample to SD. I use a Gridview set up with the following code in place for the button adapter:
public View getView(int position, View convertView, ViewGroup parent) {
try {
final Sample sample = board.getSamples().get(position);
if (sample != null) {
Button button = new Button(context);
button.setText(sample.getName());
button.setTextColor(Color.WHITE);
button.setTextSize(12);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
context.play(sample);
}
});
// TODO Implement this correctly.
button.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
return context.saveToSD(sample);
}
});
return button;
}
} catch (IndexOutOfBoundsException e) {
Log.e(getClass().getCanonicalName(), "No sample at position "
+ position);
}
return null;
}
I am looking to integrate a context menu here on a Long press, to give the option of where to save the sample. I don't seem to be able to register the button for the context menu within this method (ie registerForContextMenu(button), as it gives me errors.
I am a bit stumped here, any pointers would be a great help.
Thanks

I take it that this is an old post but I came across it today as I was looking for an answer on the same topic. Like the question here I have a grid of items and wanted to show a context menu on long click.
I am not using a contextmenu but instead I am using an AlertDialog.
gridview.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id)
{
showOptionsMenu(position);
return true;
}
});
public void showOptionsMenu(int position)
{
new AlertDialog.Builder(this)
.setTitle("test").setCancelable(true).setItems(R.array.myOptions,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i) {
//take actions here according to what the user has selected
}
}
)
.show();
}
Hope this helps.

Related

Long-press on item message to change layout

Long press
When I long-click on an item of the message, the item will display and the layout changes like the picture. I want to make this , but i don't have a keyword to find this solution. I need a keyword or some example to make it.
Many ways you can do. I am going to share one example.
Implement View.OnLongClickListener as follows
private void setupLongPress() {
imageButton.setOnLongClickListener(new View.OnLongClickListener(){
#Override
public boolean onLongClick(View v){
// here your staff
// we added dialog method here as follows
createPreviewDialog();
return false;
}
});
}
Now use LayoutInflater to inflate new layout as a popup windows
private Dialog createPreviewDialog() {
View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_preview, null);
LinearLayout closeButton = view.findViewById(R.id.close);
closeButton.setOnClickListener (new View.OnClickListener (){
#Override
public void onClick ( View view ) {
dismiss();
}
});
View okButton = view.findViewById(R.id.ok);
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dismiss();
// here your staff
}
});
builder.setView(view);
return builder.create();
}

How to increase the ActionDown Time in OnTouchEvent In Android?

In my application,When Touch on List view I need to open a image and When Action_UP called i need to release the image.Here my problem is ,i need to press item 5 second after that i will open image,but when i using onTouch event just i touch on list view the image will open.I need to increase the time of touch event please can any one help me?
Thanking in Advance.
Put a Handler in your onItemClickListener of your ListView inside of your adapter class (in getView function) :
row.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
}
}, 5000);
}
});
You can use setOnItemLongClickListener() of a listview. with an example given below:
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
// TODO Auto-generated method stub
Log.v("long clicked","pos"+" "+pos);
return true;
}
});
I think this will help...
You can useGestureDetector. Just declare a new GestureDetector and in onTouchEvent method just make a switch clause by EventType. If it is ACTION_DOWN open an image, if it is ACTION_UP release it
NOTE : Do not setOnItemClickListener() on listview.
You can achieve this by doing some trick in your list adapter.
Go to your list adapter and in getView() method.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView.setOnLongClickListener(new OnLongClickListener() {
//your code
#Override
public boolean onLongClick(View arg0) {
//do your code to open image.
return true;
}
});
return convertView;
}

setting onClick listeners for multiple items in an array

I am trying to implement a select list. Each item is an imageview.
When the user clicks a view, a custom dialog opens up showing the 56 ImageViews in a list.
The user can then click on one to select it.
The imageviews have images named like this items_r1_c1 ... items_r56_c1.
I have to implement onClickListeners to each of the imageviews.
Instead I did this:
private int i; // This is int the outer class.
...
private ImageView [] spec = new ImageView[56];
myView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//set up dialog
try {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.myCustomList);
dialog.setTitle("Select one of 56");
dialog.setCancelable(true);
dialog.show();
String s = null;
//This is where I automate the ImageView object creation
for (i=2; i<=56; i++) {
s = "items_r"+Integer.toString(i)+"_c1";
spec[i] = (ImageView) findViewById(getResources().getIdentifier(s,"drawable",getPackageName()));
spec[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
myItem.setItem(Integer.toString(i));
if(i == 0) myItem.setItem("invalid");
Log.e(tag, myItem.getItem());
dialog.dismiss();
}
});
}
} catch (Exception e) {
Log.e(tag, e.toString());
}
}
However I am not getting the behavior I expected.
What am I doing wrong? What is the efficient way of doing this instead of writing 56 onClick listeners.
Thank you.
For starters I'd put the for( ... ) loop before the call to dialog.show().
To answer your more general question, look at the method ListView.setOnItemClickListener() http://developer.android.com/reference/android/widget/AdapterView.html#setOnItemClickListener(android.widget.AdapterView.OnItemClickListener).
You'd only have to register one listener on your ListView. When an item in your ListView is clicked you'd end up in the code under void onItemClick(AdapterView<?> parent, View view, int position, long id). The position would be the index of the row clicked (corresponding to i in your loop).

android-How to set the text size of list item in alert dialog

In My application one button is there when you click on that one alert dialog will be appear. that alert dialog consists of single choice list items. Here i want to set the text size of single choice list item.
is it possible? if yes how to do it.
The following is my code
sclist.java
package com.examples.scl;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class sclist extends Activity {
private static final int DIALOG_SINGLE_CHOICE = 1;
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_SINGLE_CHOICE:
return new AlertDialog.Builder(sclist.this)
.setIcon(R.drawable.alert_dialog_icon)
.setTitle("Single choice list")
.setSingleChoiceItems(R.array.select_dialog_items2, 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked on a radio button do some stuff */
}
})
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked Yes so do some stuff */
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked No so do some stuff */
}
})
.create();
}
return null;
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Display a radio button group */
Button radioButton = (Button) findViewById(R.id.radio_button);
radioButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG_SINGLE_CHOICE);
}
});
}
}
I just encountered this problem myself on a matching game I'm working on. My solution isn't simple but I wanted to use a custom font, and I didn't see an easy way to do it with the 2.2 Android interface (which is what I'm targeting). The trick is to attach an OnShowListener to the alert dialog before you show it. In that listener, get ListAdapter out of the ListView and wrap it with a proxy object that forwards all the calls except the getView. In that function, cast the View to a TextView, set the typeface and size, and return the view. Here's my code:
// Add your list with builder up here
AlertDialog alert = builder.create();
alert.setOnShowListener(new OnShowListener() {
#Override
public void onShow(DialogInterface alert) {
ListView listView = ((AlertDialog)alert).getListView();
final ListAdapter originalAdapter = listView.getAdapter();
listView.setAdapter(new ListAdapter()
{
#Override
public int getCount() {
return originalAdapter.getCount();
}
#Override
public Object getItem(int id) {
return originalAdapter.getItem(id);
}
#Override
public long getItemId(int id) {
return originalAdapter.getItemId(id);
}
#Override
public int getItemViewType(int id) {
return originalAdapter.getItemViewType(id);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = originalAdapter.getView(position, convertView, parent);
TextView textView = (TextView)view;
textView.setTypeface(MyFontUtil.getTypeface(MyActivity,MY_DEFAULT_FONT));
textView.setTextColor(Color.BLACK);
textView.setTextSize(25); // FIXIT - absolute size
return view;
}
#Override
public int getViewTypeCount() {
return originalAdapter.getViewTypeCount();
}
#Override
public boolean hasStableIds() {
return originalAdapter.hasStableIds();
}
#Override
public boolean isEmpty() {
return originalAdapter.isEmpty();
}
#Override
public void registerDataSetObserver(DataSetObserver observer) {
originalAdapter.registerDataSetObserver(observer);
}
#Override
public void unregisterDataSetObserver(DataSetObserver observer) {
originalAdapter.unregisterDataSetObserver(observer);
}
#Override
public boolean areAllItemsEnabled() {
return originalAdapter.areAllItemsEnabled();
}
#Override
public boolean isEnabled(int position) {
return originalAdapter.isEnabled(position);
}
});
}
});
alert.show();
If you want to see it in action look on the Android Market in a few weeks. Search for metaphyze (my publisher id). I haven't decided what to call it yet. (It's not "FlashMatch Chinese I Free". That was my first game. This is a kid's matching game. Play the game and tap the picture at the end. You'll see the AlterDialog with the style list.).
Good question. I believe you'd have to use the AlertDialog.Builder constructor that also takes a theme AlertDialog.Builder(Context context, int theme), see if you can see anything useful in that description, I've never tried it myself.

Issue Invoking a context menu when an onClickListener is applied to an activity

Please find the code sample below:
public class Abc extends Activity implements OnClickListener{
private ListView displayList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlayout);
displayList =(ListView)findViewById(R.id.addressbooklistview);
addressbookAdapter = new CustomListAdapter(this,addressbookList);
displayList.setAdapter(addressbookAdapter);
registerForContextMenu(displayList);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo)
{
Log.e("", "Entered Context Menu");
}
public void onClick(View v) {
Log.e("", "Click Detected");
}
}
I am not able to invoke the context menu on long press.
Please let me know any solution for the same.
I had this problem. Originally the "show context menu" functionality was working, but when I added normal "click" functionality, the context menu no longer worked.
My problem was that I actually had the onClick() code on a TextView within the Listview item, not on the ListView itself. Presumably, it was stealing the click from the listview. To fix this, I removed that code, and in my Activity's onCreate method, I call setOnItemClickListener() for the ListView. So now I have this:
// This creates the context menu functionality.
registerForContextMenu(findViewById(R.id.list_item));
// This creates the click functionality for the listview item.
ListView listView = (ListView) findViewById(R.id.list_item);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// code here
}
});
What worked for me is stating explicitly that the longClick was not handled by adding a OnLongClickHandler to the view as well:
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
// do something
}
});
view.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return false; // ignore and bubble up
}
});

Categories

Resources