Android PopupMenus - android

I am looking for an easy popupmenu I could use for phones that operate on 2.3.3 and up.
Something like this, and works differently from a ContextMenu in that it doesn't require a listview:

Try the following :
Create a file dialog.xml in your layout folder and add the following code :
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/ErrorMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Do you want to un-install this app?" />
<LinearLayout
android:layout_below="#+id/ErrorMsgDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button android:id="#+id/Cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="15sp"
android:text="Cancel" />
<Button android:id="#+id/Ok"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="15sp"
android:text="OK" />
</LinearLayout>
and then in your class file use following code to display the dialog :
AlertDialog.Builder builder = new AlertDialog.Builder(context);
View view = LayoutInflater.from(context).inflate(
R.layout.dialog, null);
builder.setView(view);
dialog = builder.create();
dialog.setCanceledOnTouchOutside(true);
Button cancel = (Button) view.findViewById(R.id.Cancel);
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
Button ok = (Button) view.findViewById(R.id.Ok);
ok.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Do something here for OK action
}
});
dialog.show();

The easy way to do this is just create an AlertDialog.Builder object and configured its positive and negative buttons and then set the Title and the Messages and done. Removing the need for any layout files.

Related

Android Pop Up during opening of Application

I am developing an Android application, where I need to show a popup containing some message on application opening (Image attached for reference).
I have tried to achieve this using toast message and snack bar but could not do exact same.
Please suggest which component in Android to be used to achieve this functionality?
You can achieve the desired results using a dialog. I am sharing the code, you can modify it as you want but make sure you put it inside the onCreate method of the launcher activity.
This is the code for a simple dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Look at this dialog!")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do things
}
});
AlertDialog alert = builder.create();
alert.show();
The one shown in the picture shared by you is a custom dialog where you have to make your own layout. Here is an example,
1. Create your own layout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffffff">
<ImageView
android:layout_width="match_parent"
android:layout_height="120dp"
android:id="#+id/a"
android:gravity="center"
android:background="#DA5F6A"
android:src="#drawable/dialog_cross"
android:scaleType="fitCenter" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEXT"
android:id="#+id/text_dialog"
android:layout_below="#+id/a"
android:layout_marginTop="20dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="20dp"
android:textSize="18sp"
android:textColor="#ff000000"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="OK"
android:id="#+id/btn_dialog"
android:gravity="center_vertical|center_horizontal"
android:layout_below="#+id/text_dialog"
android:layout_marginBottom="20dp"
android:background="#drawable/btn_flat_red_selector"
android:layout_centerHorizontal="true"
android:textColor="#ffffffff" />
</RelativeLayout>
2. Create a class for the custom dialog
public class ViewDialog {
public void showDialog(Activity activity, String msg){
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.dialog);
TextView text = (TextView) dialog.findViewById(R.id.text_dialog);
text.setText(msg);
Button dialogButton = (Button) dialog.findViewById(R.id.btn_dialog);
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
3. Make an object in your launcher activity of the custom dialog class and call the function showDialog
ViewDialog alert = new ViewDialog();
alert.showDialog(getActivity(), "Thank you for installing the Paytm
App");
Or you could use an external library for getting the desired results
Pop.on(this).with().title(R.string.title).layout(R.layout.custom_pop).show();
where R.layout.custom_pop is the custom layout of your dialog.
Try this for same result:
dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right">
<Button
android:id="#+id/btnClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="Hello, this is your message"
android:textSize="20sp" />
</LinearLayout>
code:
private void showDialog() {
final Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.dialog);
Window window = dialog.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.BOTTOM;
wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(wlp);
dialog.findViewById(R.id.btnClose).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.cancel();
}
});
dialog.show();
}

How to get onclick of a view in custom dialog in an activity in android?

How to get onclick of a view in custom dialog in an activity in android?
My XML is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/default_green"
android:orientation="vertical" >
<TextView
android:id="#+id/tvSetNotificationsHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:text="Set Notifications On"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/white" />
<RadioButton
android:id="#+id/rbFriendRequest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:text="Friend Request"
android:textColor="#android:color/white" />
<RadioButton
android:id="#+id/rbMessages"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:text="Messages"
android:textColor="#android:color/white" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:onClick="saveSetNotificationsDialog"
android:text="Save" />
</LinearLayout>
My code is:
public void showSetNotificationsDialog(View v) {
dialog = new Dialog(Activity_Settings.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_setnotificationsdialog);
rbFriendRequest = (RadioButton) findViewById(R.id.rbFriendRequest);
rbMessages = (RadioButton) findViewById(R.id.rbMessages);
dialog.show();
}
I am able to show dialog view but no able to get on click of dialog save button in activity class. Error was no method found exception.
use instance of dialog for findViewById()
Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
declineButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
use as following code
dialog = new Dialog(Activity_Settings.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
View view = View.inflate(getActivity(), R.layout.dialog_setnotificationsdialog, null);
dialog.setContentView(view);
//always find your view view.findViewbyid
yourView= (RadioButton) view.findViewById(R.id.rbFriendRequest);
//then you can add on click listner to any of your view i.e.
yourView.setonClickListener(this);(new OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});

Using drawables instead of text for AlertDialog Buttons

I'm trying to set the Positive, Negative and Neutral buttons of an AlertDialog to drawables rather than text.
I've been successful thus far using this:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setPositiveButton("Save", new DialogInterface.OnClickListener() {...})
.setNeutralButton("Trash", new DialogInterface.OnClickListener() {...})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {...});
AlertDialog alert = builder.create();
alert.show();
Button button0 = alert.getButton(AlertDialog.BUTTON_POSITIVE);
button0.setCompoundDrawablesWithIntrinsicBounds(this.getResources().getDrawable(R.drawable.ic_menu_save), null, null, null);
button0.setText("");
Button button1 = alert.getButton(AlertDialog.BUTTON_NEUTRAL);
button1.setCompoundDrawablesWithIntrinsicBounds(this.getResources().getDrawable(R.drawable.ic_menu_delete), null, null, null);
button1.setText("");
Button button2 = alert.getButton(AlertDialog.BUTTON_NEGATIVE);
button2.setCompoundDrawablesWithIntrinsicBounds(this.getResources().getDrawable(R.drawable.ic_menu_close_clear_cancel), null, null, null);
button2.setText("");
This is a workaround though, because I'm really just erasing the text after the fact. My problem is that you can't seem to instantiate a button without setting some kind of text.
Setting "[blank_space]" from the beginning yields the same result with the image being pushed to the left. Setting null or "" in the same place draws the AlertDialog without the button at all. You can see how it pushes to the left in the picture here:
Is there anyway to use pictures only? It would be much nicer than trying to handle translations for my simple situation.
AlertDialog is deprecated. Consider using DialogFragments instead. You'll have much more control and reuse ability. Here is a good google blog that demonstrated how to use it and customize it.
From android document,
setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom)
Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text.
Since you put your image on the left, therefore the image will display on the left of the button. You may try to place it at top/bottom.
The other way is to create your custom layout with 3 ImageButtons, and set it to the alert by
builder.setView(customLayout);
You have just to create your own custom dialog!! You can specify your ImageButton in a layout, then create a dialog with that layout, you don't need to go near the positive, negative and neutral buttons. You can find here a good tutorial just use ImageButton instead of Buttons in your custom layout.
Try this code:
LayoutInflater inflater=LayoutInflater.from(YourActivityName.this);
View view=inflater.inflate(R.layout.buttton, null);
AlertDialog.Builder builder=new AlertDialog.Builder(YourActivityName.this);
builder.setView(view);
builder.setTitle("Are you sure you want to exit?");
Button posButton=(Button) view.findViewById(R.id.pos);
Button neuButton=(Button) view.findViewById(R.id.neu);
Button negButton=(Button) view.findViewById(R.id.neg);
builder.create();
builder.show();
inflate buttton xml file as below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/pos"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"/>
<Button
android:id="#+id/neu"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"/>
<Button
android:id="#+id/neg"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"/>
</LinearLayout>
And at the end you can give click events to the individuals.Hope this will help you.
CustomDialog dialog = new Dialog(MyActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.login);
final EditText editTextEmailAddress = (EditText) dialog
.findViewById(R.id.editTextEmailAddress);
final EditText editTextPassword = (EditText) dialog
.findViewById(R.id.editTextPassword);
TextView txtViewForgetPswd = (TextView) dialog
.findViewById(R.id.txtViewForgetPswd);
txtViewForgetPswd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// RETRIVE PASSWORD
dialog.dismiss();
}
});
ImageButton imgBtnSubmit = (ImageButton) dialog
.findViewById(R.id.imgBtnSubmit);
imgBtnSubmit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// CALL WEBSERVICE OR ASYNTASK TO LOG IN USER
String userName = editTextEmailAddress.getText()
.toString();
String password = editTextPassword.getText().toString();
if (userName.equals("") && password.equals("")) {
Toast.makeText(BibleActivity.this,
"Username or password cannot be empty.",
Toast.LENGTH_SHORT).show();
} else {
}
}
});
ImageButton imgBtnCancel = (ImageButton) dialog
.findViewById(R.id.imgBtnCancel);
imgBtnCancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// CLOSE DIALOG HERE FROM CROSS BUTTON
dialog.dismiss();
}
});
ImageButton btnCancelCross = (ImageButton) dialog
.findViewById(R.id.btnCancelCross);
btnCancelCross.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// CLOSE DIALOG HERE FROM CROSS BUTTON
dialog.dismiss();
}
});
dialog.show();
////XML LAYOUT "login"///
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="480dp"
android:background="#drawable/mbc_login" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_alignParentTop="true" >
<ImageButton
android:id="#+id/btnCancelCross"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:layout_marginRight="20dp"
android:background="#drawable/cross" >
</ImageButton>
</FrameLayout>
<TextView
android:id="#+id/txtViewEmailLbl"
android:layout_width="200dp"
android:layout_height="20dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Email address"
android:textColor="#000000" >
</TextView>
<EditText
android:id="#+id/editTextEmailAddress"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_below="#+id/txtViewEmailLbl"
android:layout_centerHorizontal="true"
android:hint="example#eg.com"
android:imeOptions="actionNext" >
</EditText>
<TextView
android:id="#+id/txtViewPswdLbl"
android:layout_width="200dp"
android:layout_height="20dp"
android:layout_below="#+id/editTextEmailAddress"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="Password"
android:textColor="#000000" >
</TextView>
<EditText
android:id="#+id/editTextPassword"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_below="#+id/txtViewPswdLbl"
android:layout_centerHorizontal="true"
android:hint="password123"
android:imeOptions="actionNext"
android:inputType="textPassword" >
</EditText>
<TableRow
android:id="#+id/tblRowSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editTextPassword"
android:layout_centerHorizontal="true"
android:layout_margin="10dp" >
<ImageButton
android:id="#+id/imgBtnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="#drawable/btn_submit" >
</ImageButton>
<ImageButton
android:id="#+id/imgBtnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/btn_cancel" >
</ImageButton>
</TableRow>
<TextView
android:id="#+id/txtViewForgetPswd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tblRowSubmit"
android:layout_centerHorizontal="true"
android:layout_margin="5dp"
android:text=" Forget Password ? "
android:textColor="#306EFF"
android:textStyle="italic" >
</TextView>
</RelativeLayout>

Android AlertDialog issue

I have one AlertDialog which is working fine.I have set some background images to it with following code:
Button buttonPositive = (Button)dialog.getButton(DialogInterface.BUTTON_POSITIVE);
Button buttonNegative = (Button)dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
buttonPositive.setBackgroundResource(R.drawable.custom_button);
buttonPositive.setTextColor(Color.WHITE);
buttonNegative.setBackgroundResource(R.drawable.custom_button);
buttonNegative.setTextColor(Color.WHITE);
Now after setting image the two buttons are touching each other, i mean they have no space between them.I have tried with setPadding(...),it's not working.Even if i am changing the image size(i.e. width) it is not working.Any help !!!
I think its better to create layout xml file what you want ...
and set Like alertDialog.setContentview(R.layout.mylayout);
try this code
private Dialog myDialog;
myDialog = new Dialog(ShowReportActivity.this);
myDialog.setContentView(R.layout.alert);// your xml
myDialog.setTitle("Send Email");
myDialog.setCancelable(true);
Button set = (Button) myDialog .findViewById(R.id.alert_bnt_send_email);
Button exit = (Button) myDialog.findViewById(R.id.alert_bnt_exit);
set.setTextColor(Color.WHITE);
set.setBackgroundResource(R.drawable.custom_button);
getMailId = (EditText) myDialog.findViewById(R.id.alert_editT_email_Id);
send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
.........
myDialog.dismiss();
});
exit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
myDialog.dismiss();
}
});
myDialog.show();
use custome dialog using code like given below
Dialog windialog = new Dialog(YourActivity.this);
windialog.setContentView(R.layout.win_dialog);
windialog.setTitle("Congratulation");
windialog.setCancelable(true);
final EditText et_emailverification=EditText)windialog.findViewById(R.id.et_emailveri);
et_emailverification.setText(UserEmailOrName);
Button submit=(Button)windialog.findViewById(R.id.bt_sub_que);
submit.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//write here your code what you want onclick
}
});
Button cancel=(Button)windialog.findViewById(R.id.bt_sq_cancel);
cancel.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
windialog.cancel();
});
windialog.show();
and xml like win_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email "
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/et_emailveri"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_weight="1" >
</EditText>
</LinearLayout>
<RelativeLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/bt_sub_que"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
<Button
android:id="#+id/bt_sq_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="Cancel" />
</RelativeLayout>
</LinearLayout>

android Dialog alert

i am using a layout file for showing a dialog. My xml file is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<EditText android:id="#+id/dialogEditText" android:text="Enter Your Text Here" android:layout_height="wrap_content" android:layout_width="match_parent"></EditText>
<DatePicker android:id="#+id/datPicker" android:layout_height="wrap_content" android:layout_width="match_parent" ></DatePicker>
<LinearLayout android:id="#+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<Button android:text="Add" android:id="#+id/dialogAddBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true"></Button>
<Button android:text="Cancel" android:id="#+id/dialogCancelBtn" android:layout_width="200px" android:layout_height="wrap_content" android:layout_centerHorizontal="true"></Button>
</LinearLayout>
</LinearLayout>
I want to do something when user clicks on "OK" or Cancel button . So i have created anonymous handlers for those button. My Coding is
LayoutInflater inflater = LayoutInflater.from(obj);
View inflatedView1= inflater.inflate(R.layout.dialog_view,null);
final Dialog d= new Dialog(obj);
final Window window= d.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
window.setTitle("Add Item");
window.setContentView(R.layout.dialog_view);
final EditText input= (EditText) inflatedView1.findViewById(R.id.dialogEditText);
Button okBtn= (Button)inflatedView1.findViewById(R.id.dialogAddBtn);
okBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast msg= Toast.makeText(obj.getApplicationContext(),"Hello",Toast.LENGTH_LONG);
msg.show();
}
});
Button cancelBtn= (Button) inflatedView1.findViewById(R.id.dialogCancelBtn);
cancelBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
d.dismiss();
}
});
d.show();
When i am clicking on "Ok" or "Cancel button" nothing is happening. I am not able to understand why this is happening.
Please suggest me some solution.
you are setting the event handlers on your inflated view but at the same setting the content view to your plain layout:
window.setContentView(R.layout.dialog_view);
Just change this line to
window.setContentView(inflatedView1);
Or, perhaps a better approach without using the layout inflater would be to use
d.setContentView(R.layout.dialog_view);
And then setting the event handlers via
Button okBtn = (Button) d.findViewById(R.id.dialogAddBtn);
okBtn.setOnClickListener(...
Michael

Categories

Resources