ScrollView in Dialog anchors to wrong place - android

I have an alertdialog with a custom layout, including a scrollview.
When the dialog appears, the scrollview is "cropped", and appears to be anchored to the top of the Screen, rather than the top of the dialog
I have searched SE for similar problems, and cannot find any.
Using a popup Window has exactly the same issue.
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroller"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/rock">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/scrollbutton"
android:padding="12dp"
android:text="Room Number"
android:textColor="#android:color/black"
android:textSize="22sp" />
<TextView
android:id="#+id/contents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/scrollbutton"
android:gravity="center"
android:padding="12dp"
android:text="Here be Monsters" />
</LinearLayout>
</ScrollView>
And here is the dialog code:
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.contents, null);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setView(layout);
AlertDialog alert = alertDialog.create();
alert.requestWindowFeature(Window.FEATURE_NO_TITLE);
alert.show();
Window window = alert.getWindow();
window.setLayout((int)(screenWidth*.8), (int)(screenHeight*.8));
I have tried adding the ScrollView as the root_view in the inflater, and also adding a LinearLayout around the ScrollView and setting this as root_view, but this does not alter the results

You don't set parent to layout inflater. Let dialog manages view arranging DialogFragment

Related

Listview in dialog with header and footer

I have a listview in my dialog and then I have to use the header layout and footer layout. the way I am doing it , aligning my header layout to top and the footer layout to bottom . but it has a problem. take a look at below picture ..
But by doing that the dialog gets Longer in size which is not expected
I am traped in this situation. Well If i align my footer with the listview end then the footer gets out of the screen. which is also not expected.
What I want :
I want that My listview should be visible in the center of dialog with the header layout align top and the footer layout align to the bootm of the dialog botttom border and it should not take the full length of the screen.
Isnt there a way to align the footer to the bottom of the dialog and dialog it self maintain its height (Note: I do not want to give the dialog hard coded height as I want the dialog to be look same on all devices. )
Please help I am stuck in the situation. If we look at the google dialog whcih comes while installing the app, it has short dialog with the listview in center of it and buttons are aligning to bottom of the dialog. How they are doing it?
Here's the dynamic solution for your problem..
Sample dialog xml file name activity_list_test.xml..
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/page_bg"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Header Button" />
<ListView
android:id="#+id/lvCommon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bottom Button" /></LinearLayout>`
Here's the calling from a fragment/activity where you want..
private void showAlert() {
AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
View view = LayoutInflater.from(getActivity()).inflate(R.layout.activity_list_test, null);
dialog.setView(view);
dialog.setTitle(null);
dialog.setMessage(null);
ArrayList<String> mList = new ArrayList<>();
for (int i = 0; i < 15; i++) {
mList.add("ABC");
}
ListView lvCommon = (ListView) view.findViewById(R.id.lvCommon);
int deviceHeight = getScreenSize(getActivity()).y;
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, deviceHeight / 3,1f);
lvCommon.setLayoutParams(layoutParams);
lvCommon.setAdapter(new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, mList));
dialog.setCancelable(false);
dialog.show();
}
There device height will be auto measure and according to it the dialog size will be "deviceheight/3"..so it will be maintain in all devices..
Now you need the method getScreenSize..So here's it is::
public static Point getScreenSize(Activity act) {
int screenWidth = 0;
int screenHeight = 0;
final DisplayMetrics metrics = new DisplayMetrics();
act.getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenWidth = metrics.widthPixels;
screenHeight = metrics.heightPixels;
return new Point(screenWidth, screenHeight);
}
You can set layout to your Dialog like below way
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="#+id/header_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Header Text"
android:gravity="center|top"/>
<ListView android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:gravity="center_vertical/>
<TextView android:id="#+id/footer_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Footer Text"
android:gravity="center|bottom"/>
</LinearLayout>
It will set Header View to Top,ListView to Center and Footer View to Bottom
You can try this one,.. create one xml and copy paste this code and set your dialog xml
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0000"
android:gravity="center"
android:orientation="vertical"
>
<RelativeLayout
android:id="#+id/table_rl"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"
android:orientation="vertical">
<TextView
android:id="#+id/your_items_tv"
android:textColor="#color/text_black"
customFontPath="lato_black.ttf"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginTop="20dp"
android:text="HEADING"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_below="#+id/your_items_tv"
android:paddingBottom="5dp"
android:layout_marginTop="10dp"
android:paddingTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:textColor="#color/text_black"
customFontPath="lato_bold.ttf"
android:layout_gravity="center"
android:gravity="left"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="TOP BAR"/>
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"></LinearLayout>
</RelativeLayout>
<LinearLayout
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_below="#+id/table_rl"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff00ff">
<TextView
android:padding="5dp"
android:textSize="18dp"
android:textColor="#color/text_black"
customFontPath="lato_bold.ttf"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Bottom Bar "/>
</LinearLayout>
</LinearLayout>
</LinearLayout>

TextView in customtitle view of alert dialog is not displayed

I make setting customtitle of alertdialog builder with a view that contains two webviews for animated gif and textview between them. the webviews are shown in the title of the alert dialog but the textview is missing.
here the view XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="75dp"
android:weightSum="1"
android:background="#android:color/background_dark">
<WebView
android:layout_width="wrap_content"
android:layout_height="75dp"
android:id="#+id/Title_webView1"
android:layout_weight="0.15"
android:layout_gravity="left" />
<TextView
android:id="#+id/titletextView"
android:layout_width="match_parent"
android:layout_height="75dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_gravity="center"
android:layout_weight="0.7"
android:textAlignment="center"
android:textIsSelectable="false"
android:visibility="visible"
android:gravity="center_vertical|center_horizontal"
android:inputType="none"
android:textColor="#d0d2d116"
android:text="test123"
android:singleLine="true"
android:textSize="32dp" />
<WebView
android:layout_width="wrap_content"
android:layout_height="75dp"
android:id="#+id/Title_webView2"
android:layout_weight="0.15"
android:layout_gravity="right" />
here the view function:
private View setCustomDialogTitle(String Title)
{
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.embark_title, null);
TextView TitleText = (TextView) view.findViewById(R.id.titletextView);
TitleText.setText(Title);
WebView flashingLight1 = (WebView) view.findViewById(R.id.Title_webView1);
flashingLight1.loadUrl("file:///android_asset/flashlight751.gif");
WebView flashingLight2 = (WebView) view.findViewById(R.id.Title_webView2);
flashingLight2.loadUrl("file:///android_asset/flashlight752.gif");
return view;
}
and the function call:
AlertDialog.Builder alrtBuilder= new AlertDialog.Builder(this);
alrtBuilder.setCustomTitle(setCustomDialogTitle("Test123"));
the issue is with setting layout_width for the components and again setting them with weight.
so a small change in xml can help you out.The confusion in rendering will be the cause.
set all the layout_width attribute to "0dp"
android:layout_width="0dp"
for the three components, then it will bring you the exact look as you want. for some reference read this article, it really helps you to understand.
NB: tested and worked for your code

Using LayoutInflator to create a dialog that takes up whole screen

I am designing an "about" menu for an app and have used the following code in an options menu to generate it:
case DIALOG_ABOUT:
builder = new AlertDialog.Builder(this);
Context context = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.about_dialog, null);
builder.setView(layout);
TextView title = new TextView(this);
title.setText(R.string.about_title);
title.setGravity(Gravity.CENTER);
title.setTextSize(20);
builder.setCustomTitle(title);
builder.setPositiveButton("OK", null);
dialog = builder.create();
break;
I have created two different views for the about menu - for both horizontal and vertical viewings.
When displaying vertically, the about menu looks fine, but when displaying horizontally the bottom part of the text is being cut off. I am using the LayoutInflator and View controls without having that much knowledge about how they work, so I assume they are currently filling to some Android-specified size.
How can I create the dialog to take up the whole screen, or at least 90% of it?
Edit - My layouts looked like this:
Vertical Layout:
<ImageView
android:id="#+id/imageView1"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="#drawable/icon" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="#string/game_name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="#string/game_description"
android:textAppearance="?android:attr/textAppearanceMedium" />
Horizontal Layout:
<ImageView
android:id="#+id/imageView1"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="#drawable/icon" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="#string/game_name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="#string/game_description"
android:textAppearance="?android:attr/textAppearanceMedium" />
You may call the following on your dialog to fill entire space.
dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

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

Wrong width/height inflating AlertDialog from XML

I'm having problems inflating an AlertDialog with a custom XML. The problem is that it doesn't respect the width/height I've specified.
volume.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingLeft="6dp" android:gravity="center"
android:layout_gravity="center_horizontal" android:orientation="vertical"
android:layout_height="wrap_content" android:layout_width="250dip"
android:id="#+id/layoutRoot">
<TextView android:layout_width="wrap_content"
android:layout_gravity="left" android:id="#+id/textView1"
android:layout_height="wrap_content" android:text="Volume:"
android:layout_marginBottom="10dp" android:textSize="22dp"
android:layout_marginTop="10dp"></TextView>
<SeekBar android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="#+id/volumeBar"
android:minHeight="10dp" android:maxHeight="15dp" android:paddingLeft="10dp"></SeekBar>
<TextView android:layout_width="wrap_content"
android:layout_gravity="left" android:layout_height="wrap_content"
android:layout_marginBottom="10dp" android:textSize="22dp"
android:text="20%" android:id="#+id/volumeText"></TextView>
</LinearLayout>
Code:
Builder alertbox = new AlertDialog.Builder(this);
AlertDialog alertDialog;
LayoutInflater mInflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View VolumeView = mInflater.inflate(
R.layout.volume, (ViewGroup) findViewById(R.id.layoutRoot));
alertbox.setView(VolumeView);
alertDialog = alertbox.create();
alertDialog.show();
Your problem may be the layoutRoot you are providing as the second parameter of your call to mInflater.inflate(). This will affect the layout of the XML file being inflated.
Your question needs more information for anyone to give a better answer than this.

Categories

Resources