Unable to get proper custom AlertDialog - android

I have been trying to set a custom AlertDialog but was not able to get correctly.
prompt.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffe3e3"
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="HardcodedText">
<TextView
android:id="#+id/prompttitile"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|left"
android:padding="5dp"
android:text="Error Title Here"
android:textColor="#c50200"
android:textSize="16sp"
android:textStyle="normal"
android:visibility="visible"
/>
<TextView
android:id="#+id/promptmessage"
android:layout_below="#+id/prompttitile"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:padding="5dp"
android:text="Error Title Here"
android:textColor="#c50200"
android:textSize="16sp"
android:textStyle="normal" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/promptmessage"
android:weightSum="2"
android:padding="10dp"
android:background="#333"
>
<Button
android:text="#android:string/no"
android:id="#+id/promptno"
android:layout_width="0sp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:padding="7dp"
android:textColor="#android:color/black"
android:textSize="14sp"
android:textStyle="bold"
android:background="#drawable/button_selector"
android:layout_marginRight="5dp"
android:gravity="center"
/>
<Button
android:text="#android:string/ok"
android:id="#+id/promptok"
android:layout_width="0sp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:padding="7dp"
android:textColor="#android:color/black"
android:textSize="14sp"
android:textStyle="bold"
android:background="#drawable/button_selector"
android:gravity="center"
/>
</LinearLayout>
</RelativeLayout>
I am getting black color background at the top of the AlertDialog and I want to only the AlertDialog to be displayed.
Dialog code
Dialog d=new Dialog(mContext);
d.setContentView(R.layout.prompt);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
d.getWindow().setAttributes(lp);
d.show();

Dialog d=new Dialog(this);
//Add this
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setContentView(R.layout.prompt);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
d.getWindow().setAttributes(lp);
d.show();

Try this
Dialog dialog = new Dialog(MainAppActivity.this, R.style.PauseDialog);
dialog.setContentView(R.layout.prompt);
dialog.setCancelable(true);
dialog.show();
and in style.xml
<style name="PauseDialog" parent="#android:style/Theme.Translucent.NoTitleBar"></style>

Related

how to remove title from DialogFragment?

I am using DialogFragment in my project and I'am disabling the Title using
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
but my dialog has become crooked. I want to leave the dialog as it is and just remove Title. How can I do?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff2d35ff">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="39dp"
android:layout_marginLeft="39dp"
android:layout_gravity="center_horizontal"
android:background="#ff6cff23">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ItemName"
android:id="#+id/itemName"
android:layout_weight="1"
android:background="#ffd861ff"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ItemPrice"
android:id="#+id/itemPrice"
android:layout_weight="1"
android:background="#ff2ff8ff"/>
</LinearLayout>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/itemImage"
android:layout_gravity="center_horizontal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="some test"
android:id="#+id/textView9"
android:layout_gravity="center_horizontal"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:background="#ffffff2c">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/itemMinus"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1"
android:id="#+id/textView11"
android:layout_weight="1"
android:gravity="center_horizontal"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/itemPlus"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/itemAdd"
android:src="#drawable/positive"
android:background="#android:color/transparent"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/itemCancel"
android:src="#drawable/negative"
android:background="#android:color/transparent"/>
</LinearLayout>
I realized that when I remove Title dialog can not found match_parent
I usually override onCreate in my DialogFragment's subclass and call setStyle(STYLE_NO_TITLE, 0);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_TITLE, 0);
}
Here you can find the documentation for setStyle. The documentation for STYLE_NO_TITLE says:
Style for setStyle(int, int): don't include a title area.
you must override onCreateDialog method in your dialogfragment class.
#Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
RelativeLayout root = new RelativeLayout(getActivity());
root.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(root);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
return dialog;
}
You can implement is simpler way like below code -
Dialog dialog = new Dialog(mContext, android.R.style.Theme_Black_NoTitleBar);
or
Dialog dialog = new Dialog(mContext, android.R.style.Theme_Black_NoTitleBar_Fullscreen);

custom alertDialog in android

I'm working with custom alertDialog in android and found a problem. My code is simple:
LayoutInflater inflatre = getLayoutInflater();
View v = inflatre.inflate(R.layout.dialog_view, null);
AlertDialog.Builder dialog = new AlertDialog.Builder(EditPageActivity.this);
dialog.setView(v);
dialog.show();
And the layout xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/bg"
android:padding="10dp"
android:gravity="center" >
<Button
android:id="#+id/gallery_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Gallery" />
<Button
android:id="#+id/photo_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/gallery_b"
android:text="Take Photo" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/gallery_b"
android:layout_centerHorizontal="true"
android:text="Choose Image"
android:textSize="20sp"
android:textColor="#color/splash_font"
android:layout_marginBottom="10dp" />
But the output is like this:
Feeling helpless after searching google :(...can anyone describe me anything
Try this layout for Dialog, It should work.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/bg"
android:padding="10dp"
android:gravity="center">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Choose Image"
android:textSize="20sp"
android:textColor="#color/splash_font"/>
<Button
android:id="#+id/gallery_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:text="Gallery"
android:layout_marginTop="10dp"/>
<Button
android:id="#+id/photo_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/gallery_b"
android:text="Take Photo" />
</RelativeLayout>
Try this
Dialog dialog = new Dialog(YourActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.yourlayout); // your layout id
Button dialogButton = (Button) dialog
.findViewById(R.id.dialogButtonOK); // button in your layout
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
dialog.show();
//Try this one:
final Dialog dialog = new Dialog(mContext);
dialog.requestWindowFeature((int) Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.your_layout_here);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
dialog.getWindow().setAttributes(lp);
dialog.show();

how to show icon with title in dialog

I'm using a dialog in my code with a custom layout;
the problem is that I'm confused on how to show an icon with a title.
I'm not using alert dialog: I'm using a dialog.
Below is my code.
When I'm using this line:
// dialog2.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_launcher);
the app crashes
dialog2 = new Dialog(context);
View vLoad = LayoutInflater.from(ActivityHome.this).inflate(
R.layout.timer, null);
dialog2.setContentView(vLoad);
TextView text1 = (TextView) dialog2.findViewById(R.id.text1);
TextView text2 = (TextView) dialog2.findViewById(R.id.text2);
Button closedailog = (Button) dialog2.findViewById(R.id.dailogexit);
String next = "<font color='#EE0000'>red</font>";
String[] documentlisencearray = documentlisence.split(",");
if(documentlisencearray[1]!=null)
{String newString1 = new String(documentlisencearray[1].toString());
String[] documentobject = newString1.split("=");
text1.setText(documentobject[0]);
text2.setText(documentobject[1]);
}
dialog2.setTitle("Documents Due Alert");
dialog2.setTitle( Html.fromHtml("<font color='#ff0000'>Documents Due Alert</font>"));
// dialog2.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_launcher);
dialog2.setCancelable(false);
dialog2.show();
closedailog.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//
dialog2.dismiss();
}
});
}
this is my xml///timer.xml///
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:id="#+id/layouttimer">
<TextView
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:textSize="15dp"
android:textColor="#000000" ></TextView>
<TextView
android:id="#+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:textSize="15dp"
android:textColor="#000000" ></TextView>
<TextView
android:id="#+id/text3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:textSize="15dp"
android:textColor="#000000" ></TextView>
<TextView
android:id="#+id/text4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:textSize="15dp"
android:textColor="#000000" ></TextView>
<TextView
android:id="#+id/text5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:textSize="15dp"
android:textColor="#000000" ></TextView>
<TextView
android:id="#+id/text6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:textSize="15dp"
android:textColor="#000000" ></TextView>
<Button
android:id="#+id/dailogexit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:text="OK " >
</Button>
</LinearLayout>
You need to use this for your Dialog
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog.setContentView(R.layout.yourlayoutfile);
dialog2.setTitle("Documents Due Alert");
dialog.show();
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.your_icon)
Here is solution
final Dialog dialog = new Dialog(this);
**dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);**
dialog.setTitle(R.string.my_dialog_title);
dialog.setContentView(R.layout.my_dialog_layout);
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.some_icon);
dialog.show();

custom dialog not coming properly

In my app, I tried to make custom dialog so I wrote an XML for that and In my XML its appearing like this,
dialog.xml,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="50.0dip"
android:layout_marginTop="5.0dip"
android:layout_margin="5.0dip">
<TextView android:id="#+id/textViewOTPDialogMessage" android:text="#string/OTPDialogActivityMessage" android:gravity="center" android:textStyle="bold" android:textColor="#ff4d4d4d"
android:layout_width="wrap_content" android:textSize="18dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true" />
<View android:layout_marginTop="1.0dip" android:layout_below="#+id/textViewOTPDialogMessage" android:layout_width="fill_parent" android:layout_height="1dp" android:background="#android:color/darker_gray"/>
</RelativeLayout>
<RelativeLayout android:layout_width="fill_parent" android:layout_height="50.0dip" android:layout_marginTop="5.0dip">
<TextView android:text="E-Mail Id OTP* " android:id="#+id/textViewEmailOTP" android:textStyle="bold" android:textColor="#ff4d4d4d"
android:gravity="left" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginLeft="10.0dip" android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<EditText
android:id="#+id/editTextEmailIdOtp" android:background="#drawable/custom_edittext"
android:paddingLeft="5.0dip" android:layout_width="fill_parent" android:layout_height="35.0dip"
android:layout_marginLeft="150.0dip" android:layout_marginRight="10.0dip"
android:singleLine="true" android:layout_alignParentLeft="true"
android:layout_centerVertical="true" android:imeOptions="actionNext" />
</RelativeLayout>
<RelativeLayout android:layout_width="fill_parent" android:layout_height="50.0dip">
<TextView android:text="Mobile OTP* " android:id="#+id/textViewMobileOTP" android:textStyle="bold" android:textColor="#ff4d4d4d"
android:gravity="left" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginLeft="10.0dip" android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<EditText
android:id="#+id/editTextMobileOtp" android:background="#drawable/custom_edittext"
android:paddingLeft="5.0dip" android:layout_width="fill_parent" android:layout_height="35.0dip"
android:layout_marginLeft="150.0dip" android:layout_marginRight="10.0dip"
android:singleLine="true" android:layout_alignParentLeft="true"
android:layout_centerVertical="true" android:imeOptions="actionNext" />
</RelativeLayout>
<Button android:id="#+id/btnValidate" android:text="Validate" android:textSize="18.0sp" android:textStyle="bold" android:textColor="#ff4d4d4d"
android:layout_gravity="center_horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginTop="10.0dip" android:layout_marginBottom="10.0dip" />
</LinearLayout>
and when I am running it on my device then,
Its coming like this,
I tried to increase width size of dialog programmatically but getting no difference,
private void callOTPDialog()
{
Dialog myDialog = new Dialog(this);
myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
myDialog.getWindow().setLayout(600, 400);
myDialog.setContentView(R.layout.otpdialog);
myDialog.setCancelable(false);
Button btnValidate = (Button) myDialog.findViewById(R.id.btnValidate);
EditText email = (EditText) myDialog.findViewById(R.id.editTextEmailIdOtp);
EditText mobile = (EditText) myDialog.findViewById(R.id.editTextMobileOtp);
myDialog.show();
btnValidate.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
//your login calculation goes here
}
});
}
In XML I have mentioned these lines,
android:layout_width="fill_parent"
android:layout_height="wrap_content"
then also don't know what is happening here.
What to do now,
Please help,
Thanks.
Try like below:
LayoutInflater factory = LayoutInflater.from(Splash.this);
View DialogView = factory.inflate(
R.layout.custom_progress_layout, null);
Dialog main_dialog = new Dialog(Splash.this,R.style.Theme_Dialog);
main_dialog.setContentView(DialogView);
main_dialog.show();
And create Theme_Dialog into values\styles.xml
<style name="Theme_Dialog" parent="android:Theme.Holo.Dialog">
<item name="android:backgroundDimEnabled">false</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
This is perfect working in my case.
Update: you just need to change both your RelativeLayout attribute width with android:layout_width="wrap_content" into your layout.xml.
add the following line in your code:
myDialog.setWindowLayoutMode(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
final Dialog dialog = new Dialog(mContext);
dialog.requestWindowFeature((int) Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.your_layout_here);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
dialog.getWindow().setAttributes(lp);
Try this one
put this code in OnCreate of your Dialogue
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
I found same problem before and i tried to fix with this
#Override
public void onStart() {
super.onStart();
LayoutParams params = getDialog().getWindow().getAttributes();
params.width = LayoutParams.MATCH_PARENT;
getDialog().getWindow().setAttributes(params);
}
hope it helps

How to set a view for AlertDialog in android?

I want to set a custom view for my alert dialog. Actually I have a grid view and I want to show a dialog when each item clicked, I built a function as ToastWarning and it builds my alert dialog. This is my xml for custom view: an imageview, textview and 2 button:
But this code is not working.why?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/linear_toast"
android:gravity="right"
android:background="#drawable/white"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right"
>
<TextView
android:id="#+id/tv_Toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="18sp"
android:textColor="#FFFFFF"
android:layout_margin="10dp" />
<ImageView
android:id="#+id/iv_toast"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="7dp"
android:layout_marginTop="10dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right"
android:weightSum="10"
android:padding="5dp">
<Button
android:id="#+id/bt_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_weight="5"
android:textColor="#FFFFFF" />
<Button
android:id="#+id/bt_yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_weight="5"
android:textColor="#FFFFFF" />
</LinearLayout>
</LinearLayout>
It's my code for building a alert dialog:
private void ToastWarning() {
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.toast, null);
iv_alert=(ImageView)view.findViewById(R.id.iv_toast);
tv_alert=(TextView)view.findViewById(R.id.tv_Toast);
bt_yes=(Button)view.findViewById(R.id.bt_yes);
bt_yes=(Button)view.findViewById(R.id.bt_no);
iv_alert.setBackgroundResource(R.drawable.facebook);
tv_alert.setTypeface(face);
bt_yes.setTypeface(face);
bt_no.setTypeface(face);
bt_yes.setText(PersianReshape.reshape("بله"));
bt_no.setText(PersianReshape.reshape("خیر"));
tv_alert.setText(PersianReshape.reshape("فیلدهای مورد نظر پر شود."));
AlertDialog MyDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(view);
MyDialog=builder.create();
MyDialog.show();
}

Categories

Resources