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>
Related
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();
}
I had problems with this when I was programming it. I thought I'd cured it by specifying a fixed percentage height for the body of the dialog. I thought this was bad form, because the user might have a slim display, or set large fonts, which would cut of the EditText box.
Anyway that solution has failed beyond the emulator.
It looks like Android is assigning a fixed height to a dialog, and if my custom title has devoured too much of this height, squeezing everything else off. Is this correct and how do I fix it?
Problem
public class GetUserNameDialogFragment extends DialogFragment {
final String TAG = "GetUserNameDialog";
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyAlertDialogStyle);
//TODO getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = getActivity().getLayoutInflater();
final View sunburst =inflater.inflate(R.layout.dialog_user_name, null);
builder.setView(sunburst);
builder.setCancelable(false)
.setPositiveButton("Let's go!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.wtf(TAG, "button press");
EditText name = (EditText) sunburst.findViewById(R.id.nameEditText);
String userName = name.getText().toString().trim();
//TODO needs to be validated
SharedPreferences sharedPref = getActivity().getSharedPreferences("userDetails", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("userName", userName );
editor.commit();
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
Here's the xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
app:srcCompat="#drawable/ic_ball_sunburst_classic"
android:background="#color/colorAccent"
/>
<LinearLayout
android:layout_width="250dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:layout_height="125dp">
<EditText
android:id="#+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:hint="Enter your first name"/>
</LinearLayout>
</LinearLayout>
All help very much appreciated, the first impression of my app - what a disaster!
Why don't you use Dialog instead of AlertDialog ?
I built the example using a Dialog and it works perfectly
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:layout_width=" 290dp"
android:layout_height="wrap_content"
android:background="#4CAF50">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:src="#drawable/base"
android:background="#color/colorAccent"
/>
<EditText
android:textColor="#fff"
android:textColorHint="#fff"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:id="#+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your first name"/>
<Button
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:id="#+id/letsGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Let's Go"
android:textColor="#fff"
android:background="#android:color/transparent"/>
final Dialog dialog = new Dialog(context);
if(dialog.getWindow() != null){
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
dialog.setContentView(R.layout.dialog);
Button letsGO = (Button) dialog.findViewById(R.id.letsGo);
EditText nameEditText = (EditText) dialog.findViewById(R.id.nameEditText);
letsGO.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "Lets GO!", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
dialog.show();
I want to create a different theme for all of alertDialog instances. I need my own title view instead of the usual black title background. All text should have a blue color, and set the edge of the alertDialog to a round shape.
Is it possible to create our own theme for alert dialogs, using any style or creating a class which extends AlertDialog.Builder? I need a common theme or style for my all instances of alertDialog. I am using alertDialog in many places - one for singleChoice items, one with ArrayAdapter.
My alertDialog with array adapter:
String[] items = {"Edit profile","Change user","Change password","Logout"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Loged.this,
R.layout.my_spinner_layout, items);
settingMenu.setAdapter(adapter, listener);
My alertDialog with single choice items:
alertDelete = new AlertDialog.Builder(getParent());
alertDelete.setTitle("Delete");
alertDelete.setCancelable(true);
CharSequence[] choice = {"this user","All user"};
alertDelete.setSingleChoiceItems(choice, 0,
For my all alertDialog, I need a common theme, like:
please check this link. here am creating my own alertDialog class
and its a simple method if you reuse the alertDialog in many situation in your application
how Create setAdapter() for a AlertDialog
I solved my problem by creating a class which extends Dialog, and I created my own functions. for example setMessage,setTitle,setPositiveButton,setNegativeButton etc.
But am confusing on how we can replace the setAdapter() and setSingleChoice().
Here one example For Dialog:
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Give a Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Put here any custom text!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton = (Button) dialog.findViewById(R.id.ButtonOK);
// if button is clickedthen dialog will closed
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
Custom AlertDialog
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext;
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View urlayoutfile = inflater.inflate(R.layout.custom_dialog_xmlfile,(ViewGroup)findViewById(R.id.Yourlayout_id));
builder = new AlertDialog.Builder(this);
builder.setView(urlayoutfile);
alertDialog = builder.create();
alertDialog.show();
To use a specific style, use
ctw = new ContextThemeWrapper(this, R.style.MyStyle);
new AlertDialog.Builder(ctw)
.setTitle(...)
You can use layout as Different themes, styles, Backgrounds
customdialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
style="#android:style/Theme.Black.NoTitleBar.Fullscreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/layoutsample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/dialoghdrbg"
android:orientation="horizontal" >
<ImageView
android:id="#+id/dialogheaderimage1"
android:layout_width="50dp"
android:layout_height="48dp"
android:scaleType="fitXY"
/>
<TextView
android:id="#+id/dialogheadertext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:layout_marginLeft="1dp"
android:gravity="center_vertical|right"
android:textColor="#000000"
android:textSize="25dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/dialogcontentbg"
android:orientation="vertical" android:gravity="center">
<TextView
android:id="#+id/dialogmessgetext1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#ffffff"
android:textSize="23dp"
android:text=""
android:gravity="center"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|center_horizontal"
android:gravity="center|center_horizontal"
android:layout_marginTop="20dip"
android:orientation="horizontal">
<Button
android:id="#+id/dialogokbutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:background="#drawable/buttonanimation"
android:text="Retry"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="#+id/dialogcancelbutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:background="#drawable/buttonanimation"
android:text="Report"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
customDialog.java
final Dialog favDialog = new Dialog(Myclass.this,
android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
favDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
favDialog.getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
favDialog.setContentView(R.layout.reloadurlpopuplayout);
favDialog.setCancelable(false);
ImageView dialogImage = (ImageView) favDialog
.findViewById(R.id.dialogheaderimage1);
dialogImage.setBackgroundResource(R.drawable.questionmark);
TextView dialogMesage = (TextView ) favDialog
.findViewById(R.id.dialogmessgetext1);
TextView dialogHeader = (TextView) favDialog
.findViewById(R.id.dialogheadertext1);
String descText = getString(R.string.RetryReportMessage);
dialogMesage.setBackgroundColor(0x00000000);
dialogMesage.setText(descText);
dialogHeader.setText(R.string.BrockenLinkHeader);
try {
favDialog.show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
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.
Is there any similar theme generator as HoloColors or Action Bar Style Generator which would help us change theme of Dialogs and AlertDialogs in our apps?
My app uses Holo theme and I managed to change style of views such as EditTexts and Buttons but they remain unchanged when they appear in a Dialog. I would also like to change color of the blue line under a title.
I've found a few question and answers related to this topic but they practically say it's not even possible. I cannot believe it isn't.
At least for the AlertDialog it is possible. here i posted my solution of a function which is called at some point (for example button click) in your code and creates an alert dialog own layout over your screen.. the layout is a normal layout.xml which you can define the way you want. works perfect for me!
private void showAddUserGeneratedStationDialog() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
AmplifyActivity.this);
LayoutInflater inflater = getLayoutInflater();
//inflate your layout.xml
alertDialog
.setView(inflater.inflate(R.layout.dialog_add_station, null));
//show dialog over activity screen
final AlertDialog ad = alertDialog.show();
//put actions to your ui-elements
Button cancelBtn = (Button) ad
.findViewById(R.id.list_user_generated_cancelBU);
cancelBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ad.cancel();
}
});
Button doneBtn = (Button) ad
.findViewById(R.id.list_user_generated_doneBU);
doneBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//get sth from your layout f.e. some textInput
String stationUrl = ((TextView) ad
.findViewById(R.id.list_user_generated_stationURLInput))
.getText().toString();
// did some other things
ad.dismiss();
}
});
}
and my layout.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_user_generated_addStationDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#99000000"
android:clickable="true"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="300dp"
android:layout_height="200dp"
android:padding="10dp"
android:background="#000"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/list_user_generated_cancelBU"
style="#style/ButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|right"
android:text="#string/cancel" />
<TextView
style="#style/AmplifyHeadlineElement"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="#string/list_userGenerated_addStation" />
<Button
android:id="#+id/list_user_generated_doneBU"
style="#style/ButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|right"
android:text="#string/done" />
</LinearLayout>
<EditText
android:id="#+id/list_user_generated_stationURLInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="10dp"
android:background="#FFF"
android:ems="10"
android:hint="#string/list_userGenerated_addUrlExample"
android:inputType="textNoSuggestions" >
</EditText>
<!-- MORE BUT I DELETED IT FOR BETTER OVERVIEW -->
</LinearLayout>
</LinearLayout>