I am new to android. I want to have a dialog box with positive and negative buttons when i click on a gridview item. Right now when i click on an item, it populates another activity with the information of the item that i clicked on.
For example, using putExtra and getExtras , populating the activity works great. Now all i want is that instead of the action being performed on gridView click, i want the action being performed on the ok button of the Alertdialog box.
How should i set the gridview.setOnCLickListener so that it pops up an alertDialog Box when an item is clicked??
gridView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
gridView.setClickable(true);
item= (Hotel) parent.getItemAtPosition(position);
String names = item.getName();
Intent i=new
Intent(HotelList.this,UserViewActivity.class);
i.putExtra("names",names);
startActivity(i);
}
});
Try out the following code:
#Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
item= (Hotel) parent.getItemAtPosition(position);
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setCancelable(false);
dialog.setTitle("Your Title");
dialog.setMessage("Your Message" );
dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
String names = item.getName();
Intent i= new Intent(HotelList.this,UserViewActivity.class);
i.putExtra("names",names);
startActivity(i);
alert.cancel();
}
})
.setNegativeButton("Cancel ", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which) {
alert.cancel();
}
});
final AlertDialog alert = dialog.create();
alert.show();
}
});
Hope this helps.
First, remove gridView.setClickable(true). It's simply not necessary. Second, You can create the Dialog in your code:
AlertDialog ad = new AlertDialog.Builder(this).setContentView(R.layout.activity_dialog).show();
Related
I'm trying to create a Listview which refresh itself as soon as I press a certain button in my alert dialog. This is my code, which correctly loads the item as soon as I open the activity, but when I click on the negative button of the dialog it successfully does the operation inside it, but does not refresh the list. This is the code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
user = loadUser();
final ArrayAdapter<String> arAd = new ArrayAdapter<String>(this, R.layout.user_list,user);
setListAdapter(arAd);
ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, final View view, final int position, long id) {
new AlertDialog.Builder(UserList.this)
.setTitle("Gestisci test")
.setMessage("Scegli un'operazione")
.setPositiveButton("Apri test", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//to handle
}
})
.setNegativeButton("Elimina Test", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
File dir = new File("..");
dir.delete();
//Here I should refresh the list
arAd.notifyDataSetChanged();
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
});
}
You should remove the item from the adapter after or before deleting the file and call notifyDataSetChanged(), removing a file from the file system and calling notifyDataSetChanged() on the adapter won't refresh the list in the adapter
I'm attempting to make an alert dialog remove an item in a ListView but I can't find a solution to how I do it.
Here is my code:
contactsListView.setLongClickable(true);
contactsListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(ContactsActivity.this);
builder.setTitle("Contact Removal");
builder.setMessage("Are you sure you want to remove this contact?");
builder.setCancelable(false);
builder.setPositiveButton("Yes, I'm sure!", new HandleAlertDialogListener());
builder.setNegativeButton("No, I've changed my mind", new HandleAlertDialogListener());
AlertDialog dialog = builder.create();
dialog.show();
return true;
}
});
Here is my HandleAlertDialogListener()
private class HandleAlertDialogListener implements DialogInterface.OnClickListener {
#Override
public void onClick(DialogInterface dialog, int which) {
}
}
the problem is that I can't refer to the position of the item I want removed.
Another question is what are the which values for the dialog's buttons?
Any help would be appreciated.
In your onItemLongClick() make your position parameter final , then create your alert dialog like this :
new AlertDialog.Builder(this).setTitle("Delete").setMessage("Review").setPositiveButton(R.string.positive_delete, new DialogInterface.OnClickListener() {
#Override
public void onClick(final DialogInterface dialog, final int id) {
// use position here.
}
}).setNegativeButton(R.string.negative_reask, new DialogInterface.OnClickListener() {
#Override
public void onClick(final DialogInterface dialog, final int id) {
dialog.dismiss();
}
}).create().show();
Hope this helps!
I guess you have an Adapter to show items in a ListView?
If so, you should delete an item like I said:
myDataList.remove(position);
myAdapter.notifyDataSetChanged();
Where position is an attribute in this method:
onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
I'm trying to make an Activity that deletes the item pressed on the Spinner using an AlertDialog. I'm not deleting anything yet, I just added a Toast to make sure it will work.
This is my code:
public class SpinnerActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner);
final Spinner spinner = (Spinner) findViewById(R.id.spinner);
final Context context = getApplicationContext();
// Create an ArrayAdapter using the string array and a default spinner layout
final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
int selectionCurrent = spinner.getSelectedItemPosition();
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (selectionCurrent != position) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// set title
alertDialogBuilder.setTitle(R.string.dialogtitle);
//set dialog message
alertDialogBuilder.setMessage(R.string.dialogtext).setCancelable(false)
.setPositiveButton(R.string.si,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked,
Toast.makeText(context, "Eliminar", Toast.LENGTH_SHORT).show();
}
}) .setNegativeButton(R.string.no
, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, do nothing
dialog.cancel();
}
});
alertDialogBuilder.setView(spinner);
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
selectionCurrent = position;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
When I run the code, the following error is displayed: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
I tried to use ((ViewGroup)spinner.getParent()).removeView(spinner); before the alertDialog.show(); but it still not working.
It says: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
Anyone knows how to solve the problem?
First create a variable to inflate the ArrayAdapter:
String[] mTestArray;
Get the data from resources:
mTestArray = getResources().getStringArray(R.array.planets_array);
Inflate the ArrayAdapter with this array:
final ArrayList<String> list =new ArrayList<String>(Arrays.asList(mTestArray));
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, list);
And finally remove it in your dialog:
final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
mTestArray , android.R.layout.simple_spinner_item);
String item = list.get(position);
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (selectionCurrent != position) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// set title
alertDialogBuilder.setTitle(R.string.dialogtitle);
//set dialog message
alertDialogBuilder.setMessage(R.string.dialogtext).setCancelable(false)
.setPositiveButton(R.string.si,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
list.remove(position);
adapter.notifyDataSetChanged();
// if this button is clicked,
Toast.makeText(context, "Eliminar", Toast.LENGTH_SHORT).show();
}
}) .setNegativeButton(R.string.no
, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, do nothing
dialog.cancel();
}
});
alertDialogBuilder.setView(spinner);
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
selectionCurrent = position;
}
I have a Settings Activity in Android Studio and a password List item that when clicked prompts an EditTextPreference dialogue with InputType password. How can I make it so that when the user enters the password, another Dialog pops up asking the user to confirm the password change.
Or even better, how can I make the EditTextPreference multi line and prompt for old password, new password, confirm new password within the same Dialog?
I added the following in my main activity's onCreate
ListView lv;
Context ctx=this;
onCreate() {
lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 1) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(ctx);
alertDialog.setTitle("Values");
final EditText oldPass = new EditText(ctx);
final EditText newPass = new EditText(ctx);
final EditText confirmPass = new EditText(ctx);
oldPass.setTransformationMethod(PasswordTransformationMethod.getInstance());
newPass.setTransformationMethod(PasswordTransformationMethod.getInstance());
confirmPass.setTransformationMethod(PasswordTransformationMethod.getInstance());
LinearLayout ll = new LinearLayout(ctx);
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(oldPass);
ll.addView(newPass);
ll.addView(confirmPass);
alertDialog.setView(ll);
alertDialog.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
alertDialog.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = alertDialog.create();
alert11.show();
}
}
});
}
But there is no change in the behavior of the app. The listItem I'm focusing on is second from the top so presumably position == 1.
I created a LinearLayout and added two textBoxes into it and then gave it to the alertBox.
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("Values");
final EditText oldPass = new EditText(MainActivity.this);
final EditText newPass = new EditText(MainActivity.this);
final EditText confirmPass = new EditText(MainActivity.this);
oldPass.setTransformationMethod(PasswordTransformationMethod.getInstance());
newPass.setTransformationMethod(PasswordTransformationMethod.getInstance());
confirmPass.setTransformationMethod(PasswordTransformationMethod.getInstance());
oldPass.setHint("Old Password");
newPass.setHint("New Password");
confirmPass.setHint("Confirm Password");
LinearLayout ll=new LinearLayout(MainActivity.this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(oldPass);
ll.addView(newPass);
ll.addView(confirmPass);
alertDialog.setView(ll);
alertDialog.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
alertDialog.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = alertDialog.create();
alert11.show();
Check the passwords with the object references oldPass and newPass.
If you want to add anymore objects on to it, just create and add to the view.
To deal with the problem that when I cancel or pressed yes in this new dialog, the old dialog would appear, I changed "EditTextPreference" to simply "Preference" in my pref_general.xml file. Now the old dialogue does not appear or show up at all and the problem is fixed.
here is the code. When I long click on an item and dont drag my finger away from it, the menu still pops up, but it also activates my onClick listener. I have no idea why. I have tried looking for solutions online, and I only found something that told me to check the return statement. I am returning True, so I do not know what else to do.
listView.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(AddClaim.this, "Clicked "+list.get(position), Toast.LENGTH_SHORT).show();
//adapter expenses
setContentView(R.layout.add_expense);
ListView expView = (ListView) findViewById(R.id.ExpenseListView);
Collection<Expense> expenses = list.get(position).getExpenses();
final ArrayList<Expense> expense = new ArrayList<Expense>(expenses);
final ArrayAdapter<Expense> expAdap = new ArrayAdapter<Expense>(AddClaim.this, android.R.layout.simple_list_item_1, expense);
expView.setAdapter(expAdap);
}
});
//LONG CLICK FUNCTIONS
listView.setOnItemLongClickListener(new OnItemLongClickListener(){
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
final int finalPosition = position;
PopupMenu popup = new PopupMenu(AddClaim.this, view);
popup.getMenuInflater().inflate(R.menu.add_claim, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
//DELETE button check.
if (item.getTitle().equals("Delete")){
AlertDialog.Builder adb = new AlertDialog.Builder(AddClaim.this);
adb.setMessage("Delete "+ list.get(finalPosition).toString()+"?");
adb.setCancelable(true);
adb.setPositiveButton("Delete",new OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
Claim claim = list.get(finalPosition);
ClaimListController.getClaimList().deleteClaim(claim);
}
});
adb.setNegativeButton("Cancel",new OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
adb.show();
}//end of delete button check
//START of ADD EXPENSE check
if (item.getTitle().equals("Add Expense")){
Intent intent = new Intent(AddClaim.this, ExpenseAdd.class);
intent.putExtra("somename", finalPosition);
startActivity(intent);
}
//end of add expense check
return true;
}
});
popup.show();
return false;
}
});
}
Look carefully at your code,and count your braces but read my comments
listView.setOnItemLongClickListener(new OnItemLongClickListener(){ //function starts here
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) { // longclick starts here
final int finalPosition = position;
PopupMenu popup = new PopupMenu(AddClaim.this, view); // your menu code starts here
popup.getMenuInflater().inflate(R.menu.add_claim, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
//DELETE button check.
if (item.getTitle().equals("Delete")){
AlertDialog.Builder adb = new AlertDialog.Builder(AddClaim.this);
adb.setMessage("Delete "+ list.get(finalPosition).toString()+"?");
adb.setCancelable(true);
adb.setPositiveButton("Delete",new OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
Claim claim = list.get(finalPosition);
ClaimListController.getClaimList().deleteClaim(claim);
}
});
adb.setNegativeButton("Cancel",new OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
adb.show();
}//end of delete button check
//START of ADD EXPENSE check
if (item.getTitle().equals("Add Expense")){
Intent intent = new Intent(AddClaim.this, ExpenseAdd.class);
intent.putExtra("somename", finalPosition);
startActivity(intent);
}
//end of add expense check
return true; // you only return true if onmenu item is clicked.. which is too late
}
});
popup.show(); // and your menu code ends here, because this is where it is shown..
return false; // you are returning false.. this is {star line}
} //longclick ends here
}); //function ends here
}
{start line} should return true