Android not sizing Custom Dialog big enough - android

I am using a custom Dialog that contains a text field, an image, and a button. The text can contain HTML. Sometimes the bottom of the dialog gets chopped off the bottom when the text is long enough. How can I prevent this? I want Android to determine the size of the dialog but it doesn't seem to be doing that. DO I need to size the Dialog myself in this case?
Here is the layout...
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/alert_root_incorrect"
style="#style/AlertDialogTheme"
android:background="#drawable/rounded_alert"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/rounded_alert"
>
<TableLayout
android:stretchColumns="0"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:textStyle="bold"
android:text="Sorry, that's wrong!"
android:textColor="#color/gray_dark" />
<ImageView
android:id="#+id/check"
android:background="#drawable/xmark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dp" />
</TableRow>
</TableLayout>
<TextView
android:id="#+id/alert_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="none"
android:text="In fact, this is where the explanation will go. Something about how this passage related to the topic"
android:textColor="#000000" />
<Button
android:id="#+id/okay_button"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:background="#drawable/rounded_alert_button"
android:text="Okay"
android:textColor="#color/white"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
And here is the code I am using to load it...
if ( null == layout ) {
this.layout = inflater.inflate(R.layout.alert_incorrect, (ViewGroup) findViewById(R.id.alert_root_incorrect));
}
TextView message = (TextView) layout.findViewById(R.id.alert_text);
message.setText(Html.fromHtml(card.getConclusion()));
((Button) layout.findViewById(R.id.okay_button)).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dismissDialog(INCORRECT_DIALOG);
nextQuestion();
}
});
layout.requestLayout();
dialog = new Dialog(this, R.style.AlertDialogTheme);
dialog.setContentView(layout);
dialog.setCancelable(false);
return dialog;
And here's a snap of what I mean..
Thanks,
John

This is not perfect but I've corrected it by setting the layout of the dialog relative to the default display.
dialog = new Dialog(this, R.style.AlertDialogTheme);
dialog.setContentView(layout);
Window window = dialog.getWindow();
window.setLayout(
(int)(window.getWindowManager().getDefaultDisplay().getWidth() * .90),
(int)(window.getWindowManager().getDefaultDisplay().getHeight() * .90 ));
dialog.setCancelable(false);
Just tweak the ".90" values until it feels right.

Here is the solution:
You should add a Linearlayout at the outside of your dialog's xml file
Then set this Linearlayout's gravity as "center"
the last step is creating a LayoutParams and set it to the dialog
android.view.WindowManager.LayoutParams lp = new android.view.WindowManager.LayoutParams();
lp.width = android.view.WindowManager.LayoutParams.FILL_PARENT;
lp.height = android.view.WindowManager.LayoutParams.FILL_PARENT;
alertDialog.getWindow().setAttributes(lp);

Related

TextView's text is not changing

ProgressDialog is depracted thats why I create a layout and put a progressbar and a textview and put this layout into a dialog. Until now, everything works well but in runtime I cannot change the textview's text.
Here is my code (in OnCreate Method was called)
View progressLayout = LayoutInflater.FromContext(this).Inflate(Resource.Layout.progressbar,null);
var txtMessage = progressLayout.FindViewById<TextView>(Resource.Id.loading_msg);
txtMessage.Text = "My message is this";
txtMessage.SetText("my tesst", TextView.BufferType.Normal);
progressLayout.FindViewById<TextView>(Resource.Id.loading_msg)
.SetText("hobaa",TextView.BufferType.Normal);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.SetView(Resource.Layout.progressbar);
Dialog dialog = builder.Create();
dialog.SetTitle("My Title");
dialog.SetCancelable(false);
dialog.Show();
and my progress.axml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:padding="13dp"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ProgressBar
android:id="#+id/loader"
android:layout_marginEnd="5dp"
android:layout_width="45dp"
android:layout_height="45dp"
/>
<TextView
android:layout_width="wrap_content"
android:text="Loading..."
android:textAppearance="?android:textAppearanceSmall"
android:layout_gravity="center_vertical"
android:id="#+id/loading_msg"
android:layout_toEndOf="#+id/loader"
android:layout_height="wrap_content" />
</LinearLayout>
Change this
builder.SetView(Resource.Layout.progressbar);
to
builder.SetView(progressLayout);

edittext does not grow downwards, text goes out of the visible area

Problem :- In case i have more than 1 line in my edittext, the previous line goes out of the visible area. I was expecting it to grow downwards so that all lines are visible.
Xml for my compound control which has this edittext:-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/composite_control_layout"
android:background="#null"
tools:context="com.gp.app.professionalpa.layout.ListItemLayout" >
<Button android:id="#+id/compositeControlAddItem"
android:layout_height="30dip"
android:layout_width="30dip"
android:text="#string/plus"/>
<ImageView
android:id="#+id/compositeControlImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#null"
android:contentDescription="#string/note_image"
android:scaleType="fitStart"
android:adjustViewBounds="true"
android:layout_toRightOf="#id/compositeControlAddItem"
android:layout_alignBottom="#id/compositeControlAddItem"
android:layout_alignParentLeft="true"
android:visibility="invisible"/>
<!-- <ImageButton
android:id="#+id/compositeControlBulletButton"
android:layout_width="35dip"
android:layout_height="#dimen/composite_control_height"
android:layout_marginRight="2dip"
android:contentDescription="#string/set_importance"
android:src="#drawable/ic_pin_black_bullet_point" /> -->
<EditText
android:id="#+id/compositeControlTextBox"
android:layout_width="250dip"
android:layout_height="#dimen/composite_control_height"
android:background="#null"
android:hint="#string/add_note"
android:layout_marginLeft="2dip"
android:layout_toRightOf="#+id/compositeControlAddItem"
android:layout_alignBottom="#+id/compositeControlAddItem"
android:inputType="textAutoCorrect|textMultiLine|textAutoComplete|textCapSentences"
android:maxLines="7"
android:maxLength="200"/>
<Button android:id="#+id/compositeControlDeleteItem"
android:layout_height="30dip"
android:layout_width="30dip"
android:layout_toRightOf="#+id/compositeControlTextBox"
android:layout_alignBottom="#+id/compositeControlTextBox"
android:text="#string/minus"/>
</RelativeLayout>
In my activity i am creating this compound contrl using the following code:-
private void addNewListItem()
{
ListViewItemLayout currentAddedListItem = new ListViewItemLayout(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.alignWithParent = true;
if(lastAddedListItem != null)
{
layoutParams.addRule(RelativeLayout.BELOW, lastAddedListItem.getId());
}
currentAddedListItem.setLayoutParams(layoutParams);
activityLayout.addView(currentAddedListItem, listItems.size());
listItems.add(currentAddedListItem);
Button button = currentAddedListItem.getAddButton();
lastAddedListItem = currentAddedListItem;
}
Image showing that the previous lines becomes invisible in case edittext enters new line:-
Try editing the layout height of your edit text ... prob looks with your composite height used
strong textjust use android:paddingEnd = "your "

Android: Dialog not full screen OR AlertDialog without weird background in Android 2.3

I'm developing an Android application where I have Dialogs with custom layout. At first I decided to use AlertDialog without title and without buttons, since I have my own Buttons in my custom layout. This works fine in Android 4.4, but not in Android 2.3.
This is how it's seen in Android 4.4:
This is how it's seen in Android 2.3:
Please notice the weird gray background behind my Dialog.
In SO I saw that some people solved it using Dialog instead of AlertDialog. I tried it and it really fixed it, but now my Dialog fills the whole screen:
And I don't want that, I want the Dialog to have some margin like the AlertDialog has.
This is my layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/dialog_background"
android:orientation="vertical" >
<TextView
android:id="#+id/adTitleTextView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_marginTop="1dp"
android:background="#drawable/background_green"
android:gravity="center"
android:textColor="#FFFFFFFF"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#FFFFFFFF" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:paddingBottom="15dp"
android:paddingTop="10dp"
android:textColor="#FFFFFFFF" />
<LinearLayout
android:id="#+id/adButtonBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<Button
android:id="#+id/adNegativeButton"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/selector_button_alert_dialog"
android:textColor="#FFFFFFFF"
android:textSize="14sp" />
<Button
android:id="#+id/adNeutralButton"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/selector_button_alert_dialog"
android:textColor="#FFFFFFFF"
android:textSize="14sp" />
<Button
android:id="#+id/adPositiveButton"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/selector_button_alert_dialog"
android:textColor="#FFFFFFFF"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
This is how I create my Dialog:
Dialog mDialog = new Dialog( this, android.R.style.Theme_Dialog );
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
View content = getLayoutInflater().inflate( R.layout.alert_dialog_layout, null);
((TextView)content.findViewById(R.id.adTitleTextView)).setText( title );
((TextView) content.findViewById( R.id.adBodyTextView )).setText( message );
mDialog.setContentView( content );
Button b = (Button) content.findViewById(R.id.adPositiveButton);
b.setText( posText );
b.setOnClickListener( this );
b = (Button) content.findViewById(R.id.adNegaveButton);
b.setText( negText );
b.setOnClickListener( this );
mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
mDialog.show();
And this is how I create the AlertDialog:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( ctx );
alertDialogBuilder.setView( content );
final AlertDialog mDialog = alertDialogBuilder.create();
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
...
Anybody knows how can I remove that weird background from AlertDialog in Android 2.3 OR how to make Dialog not to fill the whole screen?
Thank you!
I finally found a solution for this, although I don't like it much. I'm solving the Dialog not full screen issue.
Basically, I set the width programmatically and, if the height exceeds a certain value, I set it too:
content.getLayoutParams().width = (int) (screenWidth * 0.95);
content.post(new Runnable() {
#Override
public void run() {
int newMaxHeight = content.findViewById(R.id.adTitleTextView).getHeight() +
content.findViewById(R.id.adBodyContainer).getHeight() +
content.findViewById(R.id.adButtonBar).getHeight();
if( newMaxHeight > screenHeight * 0.92 ){
content.getLayoutParams().height = (int) (screenHeight * 0.92);
content.findViewById(R.id.adBodyContainer).getLayoutParams().height =
(int) (screenHeight * 0.92) - content.findViewById(R.id.adTitleTextView).getHeight()
- content.findViewById(R.id.adButtonBar).getHeight();
}
}
});
With this code I can limit the maximum width and height of the Dialog. If anybody finds a better solution, please post it here.
Try this :
dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

DialogFragment set height of Dialog

I just used created by first Dialog using DialogFragment.
Everything works great except I can't get the Dialog to wrap it's layout.
My layout has the height of all elements to wrap_content.
In MyFragmentDialog I can't even find a method that would imply that it can be used to set the height of the FragmentDialog. What am I missing? How do I make a DialogFragment fit it's content?
The DialogFrament's onCreateView method:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Set title for this dialog
getDialog().setTitle("Backup & Restore");
getDialog().setCancelable(true);
View v = inflater.inflate(R.layout.backup_restore, container, false);
TextView msg = (TextView) v.findViewById(R.id.br_label_message);
msg.setText("Backups are placed in the Downloads Directory:\n" + BACKUP_PATH.getAbsolutePath());
// TextView files_label = (TextView) v.findViewById(R.id.label_restore);
Spinner files = (Spinner) v.findViewById(R.id.br_restore_file);
if (BACKUP_PATH.exists()) {
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String filename) {
File sel = new File(dir, filename);
return filename.contains(FTYPE) || sel.isDirectory();
}
};
mFileList = BACKUP_PATH.list(filter);
} else {
mFileList = new String[0];
}
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getActivity(), android.R.layout.simple_spinner_item, mFileList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
files.setAdapter(adapter);
files.setOnItemSelectedListener(this);
Button backup = (Button) v.findViewById(R.id.br_backup_btn);
Button restore = (Button) v.findViewById(R.id.br_restore_btn);
Button cancel = (Button) v.findViewById(R.id.br_cancel_btn);
backup.setOnClickListener(this);
restore.setOnClickListener(this);
cancel.setOnClickListener(this);
return v;
}
This is the layout:
<?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="wrap_content"
android:orientation="vertical"
android:padding="8dp" >
<TextView
android:id="#+id/br_label_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="8dp"
android:text=""
android:textSize="14sp" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/br_tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/br_label_restore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="8dp"
android:text="Restore file"
android:textSize="14sp" />
<Spinner
android:id="#+id/br_restore_file"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow>
<Button
android:id="#+id/br_restore_btn"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="Restore"
android:textSize="14sp" />
<Button
android:id="#+id/br_backup_btn"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="Backup"
android:textSize="14sp" />
</TableRow>
</TableLayout>
<Button
android:id="#+id/br_cancel_btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Cancel"
android:textSize="14sp" />
</LinearLayout>
Thanks,
It turns out to be an issue with LinearLayout despite setting a height on it, in DialogFragment it seems to be taking more space than I want it to. I switched layout to be a RelativeLayout the content Dialog seems to resize to fit the content.
You can make it work with all kind of layout in adding the size of the layout in the onResume of your dialogFragment :
#Override
public void onResume()
{
super.onResume();
Window window = getDialog().getWindow();
window.setLayout(300, 300);
window.setGravity(Gravity.CENTER);
}
I took a leap into the Dialog api so I am certainly not sure but you could try to call getWindow on the dialog and then call setLayout(width, height)
getDialog().getWindow().setLayout(300,300);
I don't see the top portion of your LinearLayout tag, but I've seen cases where all that was needed was the
android:orientation="vertical"
XML attribute. It can cause a headache but is fortunately easy to fix.

EditText Field Is Crashing My Android App

Hi all i have updated my post to show the full xml file, which will show if i am doing something wrong.
i implemented a custom dialog with an edit text field and it keeps crashing. I will
also like to access the values filled in the text field. Any idea where i am going wrong? Thanks.
Below is the offending/troublesome??? code
First i show the xml file which contains my layout for the alert dialog.
<?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">
<ImageView android:id="#+id/ImageView01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<ScrollView android:id="#+id/ScrollView01"
android:layout_width="wrap_content" android:layout_below="#+id/ImageView01"
android:layout_height="200px">
<TextView android:text="#+id/TextView01" android:id="#+id/TextView01"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<EditText
android:id="#+id/lastname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="blah"
/>
</ScrollView>
<Button android:id="#+id/Button01"
android:layout_below="#id/ScrollView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Cancel" />
</RelativeLayout>
Then main activity file which implements
my button click events...etc.
.......
setContentView(R.layout.main);
//this button will show the dialog
Button button1main = (Button) findViewById(R.id.Button01main);
button1main.setOnClickListener(new View.OnClickListener() {
public void onClick(View OnClickListener) {
//set up dialog
final Dialog dialog = new Dialog(MoredialogActivity.this);
dialog.setContentView(R.layout.maindialog);
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText(R.string.lots_of_text);
//set up image view
ImageView img = (ImageView) dialog.findViewById(R.id.ImageView01);
img.setImageResource(R.drawable.icon);
//set up button
Button button = (Button) dialog.findViewById(R.id.Button01);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
//finish();
}
});
dialog.show();
}
});
}
}
You have two items in your ScrollView. According to the doc :
A ScrollView is a FrameLayout, meaning you should place one child in
it containing the entire contents to scroll; this child may itself be
a layout manager with a complex hierarchy of objects.
So you should had another layout, like a linear layout :
<ScrollView
android:id="#+id/ScrollView01"
android:layout_width="wrap_content"
android:layout_below="#+id/ImageView01"
android:layout_height="200px">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:text="#+id/TextView01"
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/lastname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="blah" />
</LinearLayout>
</ScrollView>

Categories

Resources