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();
Related
Somehow the scroll view is not working . The message alert dialog box is showing is really big and so I need to implement vertical scrollbar. I tried to get data from previous asked question but it isn't solving my issue please help.
I need to show the alert dialog on button click event.
benefits.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog ad = new AlertDialog.Builder(Panchgavya.this).create();
ad.setCancelable(false); // This blocks the 'BACK' button
ad.setMessage(getString(R.string.benefits));
ad.setTitle("Benefits");
ad.setButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ad.show();
TextView textView = (TextView) ad.findViewById(android.R.id.message);
textView.setScroller(new Scroller(Panchgavya.this));
textView.setVerticalScrollBarEnabled(true);
textView.setMovementMethod(new ScrollingMovementMethod());
}
});
My XML File Code:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Design.Panchgavya"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#color/Green"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Panchgavya"
android:textColor="#color/white"/>
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6"
android:orientation="vertical">
<TextView
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:text="#string/panchgavya"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="vertical">
<TextView
android:id="#+id/panchgavya_tv_cow_dung"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cow Dung"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textStyle="bold"
android:textColor="#color/black"/>
<TextView
android:id="#+id/panchgavya_tv_cow_urine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Cow Urine"
android:textStyle="bold"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#color/black"/>
<TextView
android:id="#+id/panchgavya_tv_cow_milk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Cow Milk"
android:textStyle="bold"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#color/black"/>
<TextView
android:id="#+id/panchgavya_tv_ghee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:text="Ghee"
android:textStyle="bold"
android:textColor="#color/black"/>
<TextView
android:id="#+id/panchgavya_tv_dahi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:text="Dahi"
android:textStyle="bold"
android:textColor="#color/black"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
<Button
android:id="#+id/panchgavya_btn_benefits"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Benefits"
android:background="#color/Green"
android:textColor="#color/white"
/>
</LinearLayout>
</LinearLayout>
You can create custom layout for your dialog and set any property easily.
For your requirement, Crate a layout for your required Dialog. Put android:scrollbars = "vertical" in your textView inside your layout. And textview.setMovementMethod(new ScrollingMovementMethod());.
You can set custom layout on your by following method.
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();
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Textview
android:id="#+id/txtDescription"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/margin_16dp"
android:paddingRight="#dimen/margin_16dp"
android:paddingBottom="#dimen/margin_16dp"
android:paddingTop="#dimen/margin_10dp"
android:text="#string/dummy_text_"
android:textSize="#dimen/font_14dp"
android:textColor="#color/colorPrimary"
android:layout_below="#+id/imgClose"/>
<ImageView
android:id="#+id/imgClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_close"
android:layout_alignParentRight="true"
android:layout_marginRight="#dimen/margin_10dp"
android:layout_marginTop="#dimen/margin_10dp" />
</RelativeLayout>
</ScrollView>
// in your java file put below code
private void showPopup() {
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.dialog_terms_services,
null);
dialogBuilder.setView(dialogView);
TextView txtDescription = dialogView.findViewById(R.id.txtDescription);
ImageView imgClose = dialogView.findViewById(R.id.imgClose);
txtDescription.setText(message);
final AlertDialog b = dialogBuilder.create();
b.show();
imgClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
b.dismiss();
}
});
}
// after than call that showPopup() methos on button click.Hope it works for you
class extending DialogFragment can have UI elements as per your requirement.
public class OrderDetailFragment extends DialogFragment{
#Override
public void onStart() {
super.onStart();
Dialog d = getDialog();
if (d!=null){
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.WRAP_CONTENT;
d.getWindow().setLayout(width, height);
}
}
public static OrderDetailFragment getInstance(GeneralListDataPojo dataList){
OrderDetailFragment orderDetailFragment=new OrderDetailFragment();
Bundle bundle = new Bundle();
bundle.putParcelable(DATA, dataList);
orderDetailFragment.setArguments(bundle);
return orderDetailFragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root_view = inflater.inflate(R.layout.fragment_order_detail, container, false);
GeneralListDataPojo mDataList = getArguments().getParcelable(DATA);//My class which holds data implemented Parcelable
//population of data from mDataList
return root_view;
}
void closeDialog(){
this.dismiss();
}
}
fragment_order_detail is having my requirement specific element's (ScrollView/LinearLayouts/Buttons etc)
GeneralListDataPojo is the class which holds data implements Parcelable for transferring data between components
Now you can invoke this DialogFragment from your Fragment like this. (Im invoking from a Fragment. Change the FragmentManager retrieval accordingly if you are using from Activity)
OrderDetailFragment orderDetailFragment=OrderDetailFragment.getInstance((GeneralListDataPojo) responseObj);
FragmentManager fragmentManager=getFragmentManager();
orderDetailFragment.show(fragmentManager,"OrderDetailFragment");
Create The Custom Dialog layout Using Scroll View in your own custom Layout
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog= new Dialog(getApplicationContext());
dialog.setContentView(R.layout.activity_dialog);
Button click= dialog.findViewById(R.id.click);
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
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 wrote an application which have to call dialog to get name. I need the custom view in dialog so I used .setContentView(). But in android with api less 20 I have problems with displaying it.
This is calling dialog:
public void setName(View v) {
nameDialog = new Dialog(this);
//using our dialog
nameDialog.setContentView(R.layout.new_name);
EditText text = (EditText) nameDialog.findViewById(R.id.form2_dialog_name);
//if we have previous name we show it
if (haveName) {
text.setText(((Button) findViewById(R.id.form2_name_button)).getText());
}
//request focus and call keyboard for input name
text.requestFocus();
text.selectAll();
nameDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
nameDialog.show();
}
the xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_dark"
tools:context=".CreateNoteActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/form2_dialog_name_of_event"
android:id="#+id/textView11" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="#+id/form2_dialog_name"
android:maxLines="1" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/close_button"
android:id="#+id/button8"
android:onClick="onCancelDialog"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/okay_button"
android:id="#+id/okay_form2_dialog_button"
android:onClick="onFinishDialog" />
</LinearLayout>
</LinearLayout>
How it looks when api upper then 20:
http://i.imgur.com/pJ1oZky.png
Less then 20:
http://i.imgur.com/TJGEwXh.png
OK the answer is quite simple: Use the android.support.v7.app.AlertDialog
Your setName will have to look like this then:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.new_name, null);
builder.setView(dialogView);
EditText text = (EditText) dialogView.findViewById(R.id.form2_dialog_name);
//if we have previous name we show it
if (haveName) {
text.setText(((Button) findViewById(R.id.form2_name_button)).getText());
}
//request focus and call keyboard for input name
text.requestFocus();
text.selectAll();
AlertDialog dialog = builder.create();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
dialog.show();
I guess you will have to play a little with your layout file because the buttons are quite a bit too far away but I think this will be just a little problem ;)
I'm trying to get a custom Dialog to work. It should have no Title, a basic TextMessage and my Custom Layout where normally Buttons appear.
I tried to accomplish this with an AlertDialog.Builder, extending Dialog, call methods on the Dialog and still not get the expected result.
This is the layout, I Like to use as a custom ButtonArea (layout/dialog_footer):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/dialog_checkbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/never_show_again"
android:textColor="#color/black"
android:visibility="gone"
android:background="#color/dialog_button_background"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/dialog_checkbox"
android:layout_alignWithParentIfMissing="true"
android:orientation="horizontal"
android:background="#color/dialog_button_background"
android:paddingBottom="-10dip">
<Button
android:id="#+id/dialog_btn_positive"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="#string/ok"
android:gravity="center"
android:visibility="gone"
/>
<Button
android:id="#+id/dialog_btn_negative"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="#string/btn_cancel"
android:visibility="gone" />
</LinearLayout>
</RelativeLayout>
The simplest try goes like this:
AlertDialog.Buiilder builder = new AlertDialog.Builder(context);
builder.setMessage("my Message");
Dialog dialog = builder.create();
//builder has `setView for Message or setCustomTitle, but no setCustomFooter
LayoutInflater inflater = LayoutInflater.from(context);
View footer = inflater.inflate(R.layout.dialog_footer);
//either NullPointer Exception
dialog.addContentView(footer, footer.getLayoutParams);
//or AndroidRuntimeException (requestFeature() must be called befoire adding content)
dialog.addContentView(footer, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
If I try to emulate the "MessageArea" by putting a TextView on Top of my Layout it works, but has very ugly black borders at the bottom (I guess its the custom Dialog-Theme).
Whats the best way to keep the "look and feel" of a System Dialog, but replace the Buttons with my own View and handle anything myself?
Hello friend please check this post it may be helpful for your problem
Android - Custom AlertDialog Background Color
Here which i have build my own Custom Edit Dialog for my app see the Layout code
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Name: "
android:textColor="#fff" />
<EditText
android:id="#+id/txtDelName"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp" />
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Contact No :"
android:textColor="#fff" />
<EditText
android:id="#+id/txtDelAge"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:ems="10" >
<requestFocus />
</EditText>
</TableRow>
<TableRow android:layout_height="wrap_content" >
<Spinner
android:id="#+id/spinDiagDept"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_span="2"
android:prompt="#string/Rel_prompt"
android:visibility="visible" />
</TableRow>
</TableLayout>
And here is the alert dialog code
public class Alerts {
public static AlertDialog ShowEditDialog(final Context con,final Emergencydb emp)
{
AlertDialog.Builder b=new AlertDialog.Builder(con);
b.setTitle("Emergency Contact Details");
LayoutInflater li=LayoutInflater.from(con);
View v=li.inflate(R.layout.editdialog, null);
b.setIcon(android.R.drawable.ic_input_get);
b.setView(v);
final TextView txtName=(TextView)v.findViewById(R.id.txtDelName);
final TextView txtAge=(TextView)v.findViewById(R.id.txtDelAge);
final Spinner spin=(Spinner)v.findViewById(R.id.spinDiagDept);
Utilities.ManageDeptSpinner(con, spin);
for(int i=0;i<spin.getCount();i++)
{
long id=spin.getItemIdAtPosition(i);
if(id==emp.getDept())
{
spin.setSelection(i, true);
break;
}
}
txtName.setText(emp.getName());
txtAge.setText(String.valueOf(emp.getAge()));
b.setPositiveButton("Modify", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
emp.setName(txtName.getText().toString());
emp.setAge(String.valueOf(txtAge.getText().toString())); emp.setDept((int)spin.getItemIdAtPosition(spin.getSelectedItemPosition()));
try
{
#SuppressWarnings("rawtypes")
DatabaseHelper db=new DatabaseHelper(con);
db.UpdateEmp(emp);
Toast.makeText(getBaseContext(), "Modified Successfully", Toast.LENGTH_SHORT).show();
}
catch(Exception ex)
{
}
}
});
b.setNeutralButton("Delete", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
#SuppressWarnings("rawtypes")
DatabaseHelper db=new DatabaseHelper(con);
db.DeleteEmp(emp);
}
});
b.setNegativeButton("Cancel", null);
return b.create();
//diag.show();
}
protected static Context getBaseContext() {
// TODO Auto-generated method stub
return null;
}
public static void ShowEmpAddedAlert(
android.view.View.OnClickListener onClickListener) {
// TODO Auto-generated method stub
}
}
with the help of Aleks G I managed to get it work:
the code flow goes like this:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("MyMessage");
LayoutInflater inflater = LayoutInflater.from(context);
View footerView = inflater.inflate(R.layout.apo_plus_dialog_footer, null);
AlertDialog alertDialog = builder.create();
alertDialog.setView(footerView, 0,0,0,0);
if (!hasTitle){
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
}
alertDialog.show();
just in case someone wants to have a similar Dialog behavior than me.
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>