Android - Custom dialog showing weird layout - android

I'm making a custom dialog from a layout file
Java code:
Dialog dialog = new Dialog(CategoryActivity.this);
dialog.setContentView(R.layout.content_privacy_dialog);
TextView dialog_title = (TextView) dialog.findViewById(R.id.dialog_title);
dialog_title.setText(getString(R.string.nav_privacy));
dialog.show();
Layout file:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#color/dialogBg"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:padding="24dp"
android:layout_height="wrap_content"
android:id="#+id/dialog_title"
android:text="About"
android:textSize="20sp"
android:textColor="#android:color/white"
/>
</LinearLayout>
I'm getting this result:
How can I hide the white space on top and the blue bar?
Any help will be appreciated.

try this
Dialog dialog = new Dialog(CategoryActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.content_privacy_dialog);
TextView dialog_title = (TextView) dialog.findViewById(R.id.dialog_title);
dialog_title.setText(getString(R.string.nav_privacy));
dialog.show();

Instead of using simple Dialog i would recomend you to use Material Dialogs. You can get help from this link
https://material.google.com/components/dialogs.html#dialogs-specs

Related

Show the whole setTitle in AlertDialog.Builder

I am trying to display the whole sentence of the setTile in AlertDialog.Builder but I am just getting a part display. How can I manage that with AlertDialog.Builder?
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Please help us to track the route, has this route arrived the stop? ");
I appreciate any help.
For long text (or the only text you're showing) use setMessage:
builder.setMessage("Please help us to track the route, has this route arrived the stop? ");
Use setTitle for something short and snappy in addition to a message or use no title at all.
More reading about dialogs:
http://www.google.com/design/spec/components/dialogs.html#dialogs-content
As what #Eugene H suggests, you can remove the title of AlertDialog and use a custom layout XML with a TextView that will serve as your Title.
To do that you need to remove the Window.FEATURE_NO_TITLE from the Dialog.
final View dialogView = View.inflate(this, R.layout.custom_dialog, null);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
TextView tvTitle = (TextView)dialogView.findViewById(R.id.tvTitle);
tvTitle.setText("Please help us to track the route, has this route arrived the stop?");
custom_dialog.xml
<?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:padding="8dp"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#android:drawable/divider_horizontal_textfield">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the title"
android:id="#+id/tvTitle"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the Message"
android:id="#+id/tvMessage"/>
<Button
android:id="#+id/date_time_set"
android:layout_weight="1"
android:layout_width="match_parent"
android:text="OK"
android:layout_height="0dp" />
</LinearLayout>

AlertDialog is wider than it should expected

Today problem is that on Android 2.3.5 showing custom dialog is wrong. Dialog width is wider than it should be.
I tried using 20dp instead of wrap_content in TextView as in LinearLayout with no success. I would like get rectangle shape with wrap_content as it is in Android 4.
Here is xml code for this custom dialog's view.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Please wait" >
</TextView>
</LinearLayout>
Here is a a custom dialog's code, anything else is not important.
AlertDialog.Builder builder = new AlertDialog.Builder(context);
AlertDialog dialog;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.dialog_progressdialog,
null);
builder.setView(layout);
//initializing textview
dialog = builder.create();
//do other stuff
On android 4.0.3 I get rectangle, so it looks ok - it is as I wanted. What might be the problem, does anyone find this kind of problem? Sorry, I don't have sample for Android 4 - no phone today to make sample.
If I put inside also progressbar I am able to create square in android 4, but it is impossible to do it in Android 2.3.
Android 2.3.5
Android 4.0.4
I dont know whats wrong with your code. I want to have the ProgressDialog at the center of the screen. I have implemented it in to the Android 2.2.* and it works perfect in all devices even in android 4.*
I addition i have use style to the custom progressDialog. Please see below my code and you will come to know about it.
Java code:
final Dialog dialog = new Dialog(SplashActivity.this,R.style.CustomDialogTheme);
dialog.getWindow();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.internet_dialog);
dialog.setCancelable(false);
Button retryBtn = (Button) dialog.findViewById(R.id.retryBtn);
Button cancel = (Button) dialog.findViewById(R.id.cancelBtn);
retryBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
dialog.dismiss();
// Some task
}
});
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
dialog.dismiss();
// Some task
}
});
dialog.show();
}
CustomDialogTheme
<style name="CustomDialogTheme" parent="#android:style/Theme.Dialog">
<item name="android:windowIsFloating">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:fitsSystemWindows">true</item>
<!-- <item name="android:colorBackgroundCacheHint">#00000000</item>
<item name="android:background">#00000000</item> -->
</style>
And XMl file of my layout is: internet_dialog.xml
<?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:background="#00000000"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/popup_shape"
android:layout_margin="10dp"
android:orientation="vertical"
>
<TextView
android:id="#+id/net_popup_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/internet_popup_heading"
android:layout_marginTop="15dp"
android:textColor="#ffffff"
android:textSize="20dp" />
<TextView
android:id="#+id/net_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="20dp"
android:gravity="center"
android:text="#string/internet_popup_msg"
android:textColor="#ffffff"
android:textSize="14dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:padding="5dp" >
<Button
android:id="#+id/retryBtn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:background="#drawable/blue_button"
android:textColor="#000000"
android:textStyle="bold"
android:text="#string/internet_popup_retry" />
<Button
android:id="#+id/cancelBtn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_marginLeft="2dp"
android:layout_height="wrap_content"
android:background="#drawable/blue_button"
android:textColor="#000000"
android:textStyle="bold"
android:text="#string/internet_popup_exit" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
here, i havent give ant fix height/width but it seems perfect in all the layout of the any device.
try to adding style to your dialog and see result.
Hope it will help you.
Feel free for comments. Enjoy Coding... :)
Just to ensure that your code behaves same on all the platforms, you can add layout params to your dialog view like this:
layout.setLayoutparams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
dialog.setView(layout);
Ok its a work around, but if you are out of ideas , as I am for this issue, you can try:
calulate length of the text View by using Paint.measureText(); and set it to dialog view param.
layout.setLayoutparams(new LinearLayout.LayoutParams(LENGTH_CALCULATED, LayoutParams.WRAP_CONTENT));
try this
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
builder.setTitle("Title");
alertDialog = builder.create();
alertDialog.show();
alertDialog.getWindow().setLayout(600, 400); //Controlling width and height.
Also try this
alertDialog.show();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(alertDialog.getWindow().getAttributes());
lp.width = 150;
lp.height = 500;
lp.x=-170;
lp.y=100;
alertDialog.getWindow().setAttributes(lp);

Dialog with AlertDialog style

In my project I need use custom Dialog instead AlertDialog. But I have two problems with Dialog style:
Too small width
I cannot remove title space
So, I need
But get:
Calling code:
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.write_message);
dialog.show();
Layout 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" >
<EditText
android:id="#+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textMultiLine"
android:minLines="3" >
<requestFocus />
</EditText>
<Button
android:id="#+id/send_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/send_message" />
</LinearLayout>
How to fix this problems? Help, please.
Have you tried :
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); //for the title space
and
dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); //for the width
You can add padding in the layout xml if you need it
?

AlertDialog in android

How can I show in AlertDialog two EditText and 2 TextView?
custom_dialog.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout android:orientation="horizontal"
android:id="#id/layout_root" android:padding="10.0dip"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Libellé"
android:id="#+id/Text_Libelle"
/>
<EditText android:id="#+id/Edit_Libelle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
<EditText android:id="#+id/Edit_Url"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
On click on button I want to show this interface in alert dialog.
You basically have the layout created so all you have to do is assign it to your custom dialog like so..
Set the custom layout to the dialog after initializing..
Dialog dialog = new Dialog(someContext);
dialog.setContentView(R.layout.customdialoglayout); //Set dialog view to custom layout
To wire up your handlers you will have to do something to the effect of..
EditText myText = (EditText)dialog.findViewById(R.id.Edit_Libelle);
http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog

Are there buttons like these built into the Android that Devs can use?

I want to try to get nice buttons that will work similar to tabs down the bottom of my app, but I am wondering if there are buttons like this built in:
It would make navigation better, and developing a lot easier & faster if they are.
Your example screenshot is showing a mostly-untouched stock Android 2.2 launcher. Since it's open source you can take a look at how it's done. Here's the layout file it uses.
Note the RelativeLayout at the bottom containing the three "hotseat" buttons. Each of them uses a custom style to get the appearance, those styles are defined here.
What you are showing in the picture is HTC Sense Launcher. It's an add-on and not part of core Android OS.
BTW it's easy to do custom transparent button like this. Let me know if you need any help with this.
Update:
Use this example to create a custom Dialog, that is not modal and is floating and transparent.
dialog = new Dialog(activityRequestingProgressDialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.progress_upload);
progressBar = (ProgressBar) dialog.findViewById(R.id.progressBar);
progressText = (TextView) dialog.findViewById(R.id.progressText);
progressText.setText("0 %");
progressText.setTextSize(18);
Button buttonCancel = (Button) dialog.findViewById(R.id.btnCancel);
buttonCancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
cancelProgressDialog();
}
});
Window window = dialog.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
window.setGravity(Gravity.BOTTOM);
dialog.show();
And the dialog layout is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/progressDialog"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerVertical="true">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="18sp"
android:padding="10dp"
android:text="#string/progress_title"/>
<LinearLayout android:id="#+id/progressDialog"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_centerVertical="true">
<ProgressBar android:id="#+id/progressBar"
android:layout_width="150dp"
android:layout_height="34dp"
android:paddingRight="10dp"
android:max="100"
android:progress="0"
android:fadingEdge="vertical"
style="?android:attr/progressBarStyleHorizontal"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/progressText"
android:paddingRight="10dp"/>
<Button android:id="#+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dip"
android:text="#string/button_options_text"
android:textColor="#color/button_text_grey"
android:drawableTop="#drawable/button_options"
android:drawablePadding="-5dip"
android:background="#null"/>
</LinearLayout>
</LinearLayout>
This is an example of a progress dialog with progress bar, text and cancel button. You can easily change this to three buttons. Note that button is transparent with icon and text.

Categories

Resources