I'm trying to make a SingleChoiceItems dialog activated from a listview onItemLongCLick which has 3 options via radiobuttons 1.View Profile 2.Send Message 3.Delete Friends and from the way I have it set up,only the first option toast works on all 3 options.
How would I make it that options 2 and 3 does their actions instead of doing option 1's actions?
Here is the code to my dialog,
#Override
public Dialog onCreateDialog(int id) {
String[] items = { "View Profile", "Send Message", "Remove Friend" };
final DBAdapter db = new DBAdapter(this);
db.open();
switch (id) {
case 0:
return new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("Select a option")
.setSingleChoiceItems(items, id,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
int choice = 0;
// TODO Auto-generated method stub
mChoice = choice;
}
})
.setPositiveButton("OK", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int choice) {
// TODO Auto-generated method stub
if (mChoice == 0) {
Toast.makeText(getBaseContext(), "Test 1",
Toast.LENGTH_SHORT).show();
} else if (mChoice == 1) {
Toast.makeText(getBaseContext(), "Test2",
Toast.LENGTH_SHORT).show();
} else if (mChoice == 2) {
TextView friends = (TextView) findViewById(R.id.textview_friends);
String deletedfriend = friends.getText()
.toString();
db.DeleteFriends(deletedfriend);
Toast.makeText(getBaseContext(),
"Friend Removed", Toast.LENGTH_SHORT)
.show();
}
}
}
)
.setNegativeButton("Cancel", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int choice) {
}
})
.create();
}
return null;
}
Remove choice = 0;
.setSingleChoiceItems(items, id,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
mChoice = which;
}
})
Related
I want to change the dialog of exit app with twi choices choice 1 = "rate" and choice 2 = "exit" now i can only show exit or stay dialog but i want to convert it to what i described here is the code :
#Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setNeutralButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
PicSelect.this.finish();
}
})
.setNegativeButton("No", null)
.show();
}
and this the class code `
public class PicSelect extends SherlockActivity {
private GridView photoGrid;
private int mPhotoSize, mPhotoSpacing;
private Itemadapter imageAdapter;
private AdView mAdView;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_picselct);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#c5d951")));
mAdView = (AdView) findViewById(R.id.adViewad);
mAdView.loadAd(new AdRequest.Builder().build());
mPhotoSize = getResources().getDimensionPixelSize(R.dimen.photo_size);
mPhotoSpacing = getResources().getDimensionPixelSize(R.dimen.photo_spacing);
photoGrid = (GridView) findViewById(R.id.albumGrid);
Model.LoadModel();
String[] ids = new String[Model.Items.size()];
for (int i= 0; i < ids.length; i++){
ids[i] = Integer.toString(i+1);
}
imageAdapter=new Itemadapter(getApplicationContext(), R.layout.photo_item,ids,"CATIMAGE");
photoGrid.setAdapter(imageAdapter);
// get the view tree observer of the grid and set the height and numcols dynamically
photoGrid.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (imageAdapter.getNumColumns() == 0) {
final int numColumns = (int) Math.floor(photoGrid.getWidth() / (mPhotoSize + mPhotoSpacing));
if (numColumns > 0) {
final int columnWidth = (photoGrid.getWidth() / numColumns) - mPhotoSpacing;
imageAdapter.setNumColumns(numColumns);
imageAdapter.setItemHeight(columnWidth);
}
}
}
});
photoGrid.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Log.e("FolderName", Model.GetbyId(position+1).FolderName);
String FolderName=Model.GetbyId(position+1).FolderName;
String CategoryName=Model.GetbyId(position+1).Name;
Intent i=new Intent(PicSelect.this,PicItem.class);
i.putExtra("Folder", FolderName);
i.putExtra("Category", CategoryName);
startActivity(i);
}
});
}
#Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setNeutralButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
PicSelect.this.finish();
}
})
.setNegativeButton("No", null)
.show();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.home, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem menuItem)
{
switch (menuItem.getItemId())
{
case R.id.rateapp:
final String appName = getPackageName();//your application package name i.e play store application url
try {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id="
+ appName)));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(
Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id="
+ appName)));
}
return true;
case R.id.moreapp:
startActivity(new Intent(
Intent.ACTION_VIEW,
Uri.parse(getString(R.string.play_more_apps))));
return true;
default:
return super.onOptionsItemSelected(menuItem);
}
}
}
`
finaly if anyone know how to style the text thank you for your help
.setNegativeButton("Rate App", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent i = new Intent(android.content.Intent.ACTION_VIEW);
i.setData(Uri.parse("market://details?id=[your package name]"));
startActivity(i);
}
})
This will give you a negative option that says Rate App that when clicked opens the market to your app.
Improve your app with this snippet in onBackPressed():
if(isTaskRoot()) {
if (this.lastBackPressTime < System.currentTimeMillis() - 2000) {
toast = Toast.makeText(this, getString(R.string.hinweisBeendeApp), Toast.LENGTH_SHORT);
toast.show();
this.lastBackPressTime = System.currentTimeMillis();
} else {
if (toast != null) {
toast.cancel();
}
super.onBackPressed();
}
}else {
super.onBackPressed();
}
I am trying to have an consumable item for my app.
i currently use this code:
private void BuyManager() {
mHelper.flagEndAsync();
mHelper.launchPurchaseFlow(this, "jm.admin", 1011, new IabHelper.OnIabPurchaseFinishedListener() {
#Override
public void onIabPurchaseFinished(IabResult result, Purchase info) {
AlertDialog.Builder succces = new AlertDialog.Builder(MainActivity.this);
int proccesed = 1;
try {
proccesed=mservice.consumePurchase(3,getPackageName(), "inapp:"+getPackageName()+":jm.admin");
} catch (RemoteException e) {
e.printStackTrace();
}
String succs = "nee";
if (proccesed==0){
succs = "ja";
}
succces.setMessage("We checken uw aankoop Aankoop gegevens: " + result + " Aankoop succesvol: "+succs);
succces.setPositiveButton("Oke", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
succces.show();
if (result.isSuccess()) {
AlertDialog.Builder succces2 = new AlertDialog.Builder(MainActivity.this);
succces2.setMessage("Uw aankoop is voltooid");
succces2.setPositiveButton("Oke", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
succces2.show();
}
}
});
}
but when i buy the item it not consumed at all and it haves the code 7 response (item already bought).
Whats wrong with my code?
I have an item in my menu
case R.id.theme:
ShowRadioDialog();
return true;
With a method that shows an Alert dialog with 3 radio buttons. When i click the item the dialog shows but when i choose some item inside the dialog and i tap in the positiove buttons nothing happens. This is the method:
public void ShowRadioDialog() {
final CharSequence[] items={"Rosso","Verde","Blu"};
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("Seleziona un colore");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#SuppressLint("NewApi")
#Override
public void onClick(DialogInterface dialog, int which) {
if (wich== 1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.red));
toolbar.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.red));
Toast.makeText(MainActivity.this, "Rosso OK", Toast.LENGTH_SHORT).show();
Log.i("Colors", "Rosso Ok");
}
} else if (wich ==2) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.green_welcome));
}
} else if (wich == 3){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.primary_dark_blue));
}
}
}
});
builder.setSingleChoiceItems(items,-1, new DialogInterface.OnClickListener() {
#SuppressLint("NewApi")
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
if ("Rosso".equals(items[which])) {
which = 1;
} else if ("Verde".equals(items[which])) {
which = 2;
} else if ("Blu".equals(items[which])) {
which = 3;
}
}
});
builder.show();
}
I'm not sure it's the correct way. However, not either one (log in the logcat and the Toast in the application) appears. Seems that the positive button not accept the choice. Something is wrong?
Define a variable on top level and use that to access the selected item.
int index = -1;
public void ShowRadioDialog() {
final CharSequence[] items={"Rosso","Verde","Blu"};
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("Seleziona un colore");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#SuppressLint("NewApi")
#Override
public void onClick(DialogInterface dialog, int which) {
if (index == 1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.red));
toolbar.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.red));
Toast.makeText(MainActivity.this, "Rosso OK", Toast.LENGTH_SHORT).show();
Log.i("Colors", "Rosso Ok");
}
} else if (index ==2) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.green_welcome));
}
} else if (index == 3){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.primary_dark_blue));
}
}
}
});
builder.setSingleChoiceItems(items,-1, new DialogInterface.OnClickListener() {
#SuppressLint("NewApi")
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
if ("Rosso".equals(items[which])) {
index = 1;
} else if ("Verde".equals(items[which])) {
index = 2;
} else if ("Blu".equals(items[which])) {
index = 3;
}
}
});
builder.show();
}
If you want to save the value of index in shared preference on click of ok button do like this
Store in SharedPreferences
SharedPreferences preferences = getSharedPreferences("myPref", getApplicationContext().MODE_PRIVATE);
Editor editor = preferences.edit();
editor.putInt("choice", index);
editor.commit();
Fetch from SharedPreferences when required
SharedPreferences preferences = getSharedPreferences("myPref", getApplicationContext().MODE_PRIVATE);
int index = preferences.getInt("choice",-1);
This question already has answers here:
How can I open a URL in Android's web browser from my application?
(41 answers)
Closed 8 years ago.
I have this dialog fragment, but i'm trying to get it so that when a user clicks on the "My Other Apps" Button they get redirected to a web address. Is there any way of doing this.
Any help is appreciated
Thanks
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// TODO Auto-generated method stub
AlertDialog.Builder theDialog = new AlertDialog.Builder(getActivity());
theDialog.setTitle("About");
String d1 = "Thanks For Downloading! ";
String d2 = "Made by ME";
theDialog.setMessage(d1 +"\n"+ d2 );
theDialog.setPositiveButton("My Other Apps", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(), "Clicked My Other Apps",
Toast.LENGTH_SHORT).show();
}
});
theDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(), "Clicked Cancel",
Toast.LENGTH_SHORT).show();
}
});
return theDialog.create();
}
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// TODO Auto-generated method stub
AlertDialog.Builder theDialog = new AlertDialog.Builder(getActivity());
theDialog.setTitle("About");
String d1 = "Thanks For Downloading! ";
String d2 = "Made by ME";
theDialog.setMessage(d1 +"\n"+ d2 );
theDialog.setPositiveButton("My Other Apps", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
theDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(), "Clicked Cancel",
Toast.LENGTH_SHORT).show();
}
});
return theDialog.create();
}
}
Do this on your button click:
#Override
public void onClick(DialogInterface dialog, int which) {
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
I am a beginner, so if anyone would help me out. I created a list in the dialogue box , now how do i use those options? Like click one and it does something , click another and it does something else.
CharSequence features[] = new CharSequence[] {"Save", "Send", "Something", "Something"};
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("Options");
alertDialog.setItems(features, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"Eta chu ma aile",
Toast.LENGTH_LONG).show();
}
});
alertDialog.show();
return true;
}
If you know exact position of every item, just compare it with which param.
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
// handle "Save" option
} else if (which == 1) {
// handle "Send" option
} ...
}
You can use following code:
Somewhere in another function:
String title = "My Alert Box";
String msg = "Choose Option";
alertfunc(title,msg);
The main alert function:
private void alertfunc(String title, String msg) {
if (title.equals(TASK_VIEW_PROFILE)) {
new AlertDialog.Builder(MainActivity.this)
.setTitle(title)
.setMessage(msg)
.setPositiveButton("Save",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
//Do something
}
})
.setNegativeButton("Send",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which)
{
//Do something
}
}).create().show();
.setNegativeButton("Something",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which)
{
//Do something
}
}).create().show();
//...and so on
}
}