Showing a dialog box with contents of choosing file and camera - java

i want to open a dialog box that gives me two option.
1- Choose file from SD Card
2- Take a snapshot from Camera
Right now i am using this code
receipt.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(RECEIPT_DIALOG_ID);
}
});
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
AlertDialog.Builder builder = new Builder(this);
case RECEIPT_DIALOG_ID:
builder.setTitle("Choose your file");
dialog = builder.create();
return dialog;
}
Now, how can i add these two options.
Best Regards

Try this it provides both the options
final CharSequence[] items = {"Camera", "Memory Card"};
builder.setTitle(R.string.pic_option);
builder.setIcon(R.drawable.camera_icon);
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
launchCamera(item);
}
});
builder.create();
builder.show();
and the Function launchCamera(item) is here
public void launchCamera(int id){
switch (id) {
case 0:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
((Activity)getParent()).startActivityForResult(cameraIntent, 1888);
break;
case 1:
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
((Activity)getParent()).startActivityForResult(intent, 2);
break;
default:
break;
}
}

Use this directly in your method.It will show 2 buttons on dialog.
Dialog dialog;
dialog = new Dialog(getParent());
dialog.setContentView(R.layout.camdialog);
dialog.setTitle(" Select the Image");
dialog.setCancelable(true);
sdCard = (Button) dialog.findViewById(R.id.sdCard);
camDialog = (Button) dialog.findViewById(R.id.camDialog);
dialog.show();
dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, 200);

try this...
new AlertDialog.Builder(menu)
.setTitle("Choose your file")
.setMessage("Your msg")
.setPositiveButton("Choose file from SD Card",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// Your code
}
})
.setNegativeButton("Take a snapshot from Camera",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
//Your code
}
}).show();

Related

Applying a theme to AlertDialog Builder causes the title to not work correctly

I am attempting to add a title to my AlertDialog Builder. When I add the theme, my title gets moves into the selection area.
Here is the first example:
classificationButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(
new ContextThemeWrapper(mContext, android.R.style.Theme_Holo_Dialog) );
//building my selection options
builder.setItems(classificationList, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String desiredClassification = classificationList[which];
if ( !getClassification().equals(desiredClassification) ) {
CallsignContract.updateClassification(desiredClassification, mContext);
setClassification(desiredClassification);
classificationButton.setText(desiredClassification);
}
}
});
builder.setTitle(R.string.classification_alert_header)
.create().show();
}
});
This is the result.
On this second attempt, I create a alertdialog from the builder and give that a title. The result is the correct title, but the title appears again in the selection area.
classificationButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(
new ContextThemeWrapper(mContext, android.R.style.Theme_Holo_Dialog) );
//building my selection options
builder.setItems(classificationList, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String desiredClassification = classificationList[which];
if ( !getClassification().equals(desiredClassification) ) {
CallsignContract.updateClassification(desiredClassification, mContext);
setClassification(desiredClassification);
classificationButton.setText(desiredClassification);
}
}
});
AlertDialog alertDialog = builder.create();
alertDialog.setTitle(R.string.classification_alert_header);
alertDialog.show();
}
});
Thank you!
In order to show only one title, you must call alertDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE).
AlertDialog.Builder builder = new AlertDialog.Builder(
new ContextThemeWrapper(mContext, android.R.style.Theme_Holo_Dialog)
);
//building my selection options
builder.setItems(classificationList,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String desiredClassification = classificationList[which];
if (!getClassification().equals(desiredClassification)) {
CallsignContract.updateClassification(desiredClassification, mContext);
setClassification(desiredClassification);
classificationButton.setText(desiredClassification);
}
}
}
);
AlertDialog alertDialog = builder.create();
alertDialog.setTitle(R.string.classification_alert_header);
// Requesting dialog to remove the title
alertDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
alertDialog.show();

How to make AlertDialog view in Input method Service?

I would like to make an input method which is used only for SoftKeyboard. My how to make popup onkey event in input method.
I am creating Dialog but here is problem you see my logcat:
09-14 11:16:54.349: E/MessageQueue-JNI(7172): at android.inputmethodservice.KeyboardView.detectAndSendKey(KeyboardView.java:824)
Softkeyboard.java
Here java code
public void onKey(int primaryCode, int[] keyCodes) {
if (primaryCode == -2) {
// add this to your code
dialog = builder.create();
Window window = dialog.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.token = mInputView.getWindowToken();
lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
window.setAttributes(lp);
window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
// end addons
builder.show();
}
Thanks for advance..
You need to have ACTION_MANAGE_OVERLAY_PERMISSION permission to open/display Alert onkey event in input method.
Before you set your custom Keyboard Check for Overlay Permission.
final boolean overlayEnabled = Settings.canDrawOverlays(MainActivity.this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !overlayEnabled) {
openOverlaySettings();
}
#TargetApi(Build.VERSION_CODES.M)
private void openOverlaySettings() {
final Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
try {
startActivityForResult(intent, RC_OVERLAY);
} catch (ActivityNotFoundException e) {
Log.e("MainActivity", e.getMessage());
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case RC_OVERLAY:
final boolean overlayEnabled = Settings.canDrawOverlays(this);
if (overlayEnabled) {
preferenceManager.setBooleanPref(IS_CYBER_BULLING_ON, true);
Intent intent = new Intent(MainActivity.this, ImePreferences.class);
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
} else {
// switchCyberBulling.setChecked(false);
}
// Do something...
break;
}
}
Then inside your SoftKeyboard.java class, put code for alert dialog & set alert type of "TYPE_APPLICATION_OVERLAY".
AlertDialog.Builder builder = new AlertDialog.Builder(this)
//set icon
.setIcon(android.R.drawable.ic_dialog_alert)
//set title
.setTitle("Warning!")
//set message
.setMessage("This is alert dialog!")
//set positive button
.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//set what would happen when positive button is clicked
dialogInterface.dismiss();
}
})
//set negative button
.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//set what should happen when negative button is clicked
dialogInterface.dismiss();
}
});
AlertDialog alertDialog = builder.create();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
}else{
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
}
alertDialog.show();
Do not forget for Draw Overlay Permission. Hope this helps you. :)

When user click on alert button open mobile browser and display URL on android apps

When user click on alert massage know more button open mobile browser and display URL of my website on android apps what I have made.I need the code. I have tried many code like web view and others, but not working. Please help me!
Below Code not working.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_about:
AlertDialog.Builder dialog = new AlertDialog.Builder(NoteEdit.this);
dialog.setTitle("About");
dialog.setMessage("Hello!);
dialog.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}});
dialog.setPositiveButton("KNOW MORE", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
String url = "http://www.google.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
Below code perfect working in my case:
Create
Define private static final int DIALOG_EXIT = 1;
now create
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
AlertDialog.Builder builder = new Builder(this);
switch (id) {
case DIALOG_EXIT:
return new AlertDialog.Builder(MainActivity.this)
.setTitle("About")
.setMessage("Hello")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
})
.setNegativeButton("Know More",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
/*Uri uri = Uri
.parse("http://www.google.com");
Intent intent = new Intent(
Intent.ACTION_VIEW, uri);
startActivity(intent);*/
String url = "http://www.google.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
}).create();
}
dialog = builder.show();
return dialog;
}
and you just called this from your menu item selected function;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.setting:
showDialog(DIALOG_EXIT);
break;
}
return true;
}
try this this may help you.
this code works for me:
Intent i = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
startActivity(i);
You need to show your dialog. At the end, dd the line
dialog.show()
Also, in the line
dialog.setMessage("Hello!);
you are missing an ending quote.

Android alert dialog and set positive button

This is for a slider puzzle. I want to show a dialog box with OK button when the puzzle is completed. When the OK button is pressed, I use an Intent to load a website via Android browser. Only problem is that with the current code, when the puzzle is complete, it does not load a box (it does when I use null). It doesn't do anything. Any ideas?
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(!puzzle.isSolved() ? R.string.title_stats : stats.isNewBest() ? R.string.title_new_record : R.string.title_solved);
builder.setMessage(msg);
builder.setPositiveButton(R.string.label_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent("android.intent.action.VIEW", Uri.parse("http://www..com"));
Bundle b = new Bundle();
b.putBoolean("new_window", true); //sets new window
intent.putExtras(b);
startActivity(intent);
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(your_activity.this);
builder.setTitle(!puzzle.isSolved() ? R.string.title_stats : stats.isNewBest() ? R.string.title_new_record : R.string.title_solved);
builder.setMessage(msg);
builder.setPositiveButton(R.string.label_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent("android.intent.action.VIEW", Uri.parse("http://www..com"));
Bundle b = new Bundle();
b.putBoolean("new_window", true); //sets new window
intent.putExtras(b);
startActivity(intent);
}
});
builder.show();
try this
Check the below code. It may help you
AlertDialog alertDialog = new AlertDialog.Builder(
GeneralClassPhotoCaptureImageVideo.this).create(); // Read
// Update
alertDialog.setTitle("Title of dialog");
alertDialog
.setMessage("contents");
alertDialog.setButton(Dialog.BUTTON_POSITIVE, "Ok",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent intent = new Intent("android.intent.action.VIEW", Uri.parse("http://www..com"));
Bundle b = new Bundle();
b.putBoolean("new_window", true); //sets new window
intent.putExtras(b);
startActivity(intent);
}
});
alertDialog.setButton(Dialog.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
alertDialog.show();
Add following code to show the dialog.
AlertDialog alert = builder.create();
alert.show();

alert dialog box having list of item

In my android application, when i click on text view, i want to display an alert dialog box which contain list of items. How is it possible. kindly guide.
i code it as:
cus_name_txt = (TextView)findViewById(R.id.cus_name_txta);
cus_name_txt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Onclick_click1(cus_name_txt);
// TODO Auto-generated method stub
}
});
contact_no_txt = (TextView)findViewById(R.id.contact_no_txta);
attend_by_txtbx = (EditText)findViewById(R.id.attend_by_txt);
attend_by_txtbx.setText(My_Task.attend_by_txt);
ticket_no_txt = (TextView)findViewById(R.id.ticket_no_txta);
task_detail_txt = (TextView)findViewById(R.id.task_detail_txt);
How can i get the alert box of list of items by clicking on textView. Please guide. I'll be thankful to u
If you want to show the progressBar Before loading of the List in alert dialog, then use AsyncTask for that.
e.g.:
private class LoadingTask extends AsyncTask<String, Void, String> {
#Override
protected void onPreExecute(){
super.onPreExecute();
progressDialog.show();
}
#Override
protected String doInBackground(String... str) {
String response = "";
// Call Web Service here and return response
response = API.getDealsByCategory(str[0], str[1]);
// e.g.: above is my WebService Function which returns response in string
return response;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
System.out.println("result is: "+result);
new Thread(new Runnable() {
#Override
public void run() {
progressDialog.dismiss();
}
}).start();
// SHOW THE ALERT DIALOG HERE.....
}
}
Call AsyncTask as like below :
LoadingTask task = new LoadingTask();
task.execute("YOUR_PARAMETER","YOUR_PARAMETER");
//==============================
Just put below code in the Post Excution of the AsyncTask and you will get what you want.
final CharSequence[] items = {"","50","100","150","200","250","300","350","400","450","500","550","600","650","700","750","800","850","900","1000"};
AlertDialog.Builder builder = new AlertDialog.Builder(getParent());
builder.setTitle("Select Country");
//builder.setI
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
//Toast.makeText(getApplicationContext(), con.get(item).getCountrName(), Toast.LENGTH_SHORT).show();
selectDistanceTV.setText(items[item]);
System.out.println("Item is: "+items[item]);
/*CONTRY_ID = con.get(item).getCountryId();
stateET.requestFocus();*/
}
});
AlertDialog alert = builder.create();
alert.show();
Hope it will help you.
If you need more help on how to use AsyncTask then see here:Vogella
Comment me for any query.
Enjoy Coding... :)
Put following code in onClick of textView:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Color Mode");
ListView modeList = new ListView(this);
String[] stringArray = new String[] { "Bright Mode", "Normal Mode" };
ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stringArray);
modeList.setAdapter(modeAdapter);
builder.setView(modeList);
final Dialog dialog = builder.create();
dialog.show();
Or
Dialog dialog = new Dialog(**Your Context**);
dialog.setContentView(R.layout.**Your Layout File**);
dialog.show();
In this layout file you can make your layout as per your requirements. When you want to use ListView from your Dialog Layout file then you have to write
ListView listView = (ListView)**dialog**.findViewById(R.id.**Your ListView Id**)
You can pass list of items in an String Array and display it in AlertBox..
For Example:
private void SingleChoice() {
String[] selectFruit = new String[] {"Apple","orange","mango"};
Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Single your Choice");
builder.setItems(selectFruit, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,
selectFruit[which] + " Selected", Toast.LENGTH_LONG)
.show();
dialog.dismiss();
}
});
builder.setNegativeButton("cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
button.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
new AlertDialog.Builder(MainActivity.this)
.setSingleChoiceItems(arrClientName,0, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
name.setText(arrClientName[which]);
}
})
.setPositiveButton("Ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
}
})
.show();
}
});

Categories

Resources