custom dialog not coming properly - android

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

Related

How to create a custom PopupMenu with fonts in menu item in Android

I want to change the default font of Popup Menu items and use from my custom font for them.Hows is it possible ?
This is the code that I used for creating Popup Menu:
<item
android:id="#+id/Setting"
android:title="Setting"/>
<item
android:id="#+id/About"
android:title="About"/>
<item
android:id="#+id/Help"
android:title="Help"/>
I want to change the typeface of my items using custom typeface (ttf) files.
you can you PopupWindow for this, check the below code:
pop_layout.xml
<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="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="22dp"
android:text="Item 1"
android:id="#+id/one"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:id="#+id/two"
android:layout_marginTop="22dp"
android:text="Item 2"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="22dp"
android:text="Item 3"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="22dp"
android:text="Item 4"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
PopupWindow:
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View popupView = layoutInflater.inflate(R.layout.pop_layout, null);
TextView one = (TextView)popupView.findViewById(R.id.one);
TextView two = (TextView)popupView.findViewById(R.id.two);
TextView three = (TextView)popupView.findViewById(R.id.three);
TextView four = (TextView)popupView.findViewById(R.id.four);
// Do your customised stuff
PopupWindow popupWindow = new PopupWindow(
popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
//TODO do sth here on dismiss
}
});
popupWindow.showAsDropDown(view);// your view instance in which click you want to show menu

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();

Unable to get proper custom AlertDialog

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>

Without using inflater and RecyclerView what's the best way to recreate my xml in code?

I'd like to display this under a toggle button when the toggle button is pressed
then disappear when the toggle button is pressed again, I've looked into expandable list views but it's seems over complicated for a few items that wont change visually.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:textSize="40dp"
android:id="#+id/button"
android:text="Dummy Job"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<!-- <android.support.v7.widget.RecyclerView
android:id="#+id/jobStats"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView> -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40dp"
android:text="New Text"
android:id="#+id/viewMachine" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40dp"
android:text="New Text"
android:id="#+id/viewItem" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40dp"
android:text="New Text"
android:id="#+id/viewDate" />
<LinearLayout
android:id="#+id/testing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40dp"
android:text="Button"
android:id="#+id/bJobOpen"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40dp"
android:text="Button"
android:id="#+id/bJobEdit"
android:layout_weight="1" />
<Button
android:layout_width="245dp"
android:layout_height="wrap_content"
android:text="Button"
android:id="#+id/bJobExport"
android:textSize="40dp"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
Here is what i have for it's creation.
LinearLayout layout = (LinearLayout) findViewById(R.id.ReportsLayout);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f);
//layout.setLayoutParams(lp);
//layout.setLayoutParams(lp);
//layout.setOrientation(LinearLayout.HORIZONTAL);
TextView viewMachine = new TextView(Reports.this);
TextView viewItem = new TextView(Reports.this);
TextView viewDate = new TextView(Reports.this);
LinearLayout horLayout = new LinearLayout(context);
Button enterJob = new Button(Reports.this);
Button editJob = new Button(Reports.this);
Button exportJob = new Button(Reports.this);
//enterJob.setLayoutParams(lp);
//editJob.setLayoutParams(lp);
//exportJob.setLayoutParams(lp);
if( button.isChecked()==(true)) {
layout.addView(viewMachine,lp);
layout.addView(viewItem,lp);
layout.addView(viewDate,lp);
layout.addView(enterJob,lp);
layout.addView(editJob, lp);
layout.addView(exportJob, lp);
}
else{
//layout.setVisibility(View.INVISIBLE);
}
}
};
}
Problems are when i create this it looks like this
setting the view to invis
suggestion to use this lib directly in your app.
ExpandableLayout

Categories

Resources