Android custom Dialog with ScrollView - android

I've a problem with dialog display dimension. With following code i make my custom dialog with ScrollView inside, but when it shown, ScrollView fit dialog dimension, but the inside linear layout has some controls out of scrollable area.
I can't understand why and how to resolve.
Into phone with little display it's a problem because some controls are unreachable.
Dialog creation:
AlertDialog.Builder alert = new AlertDialog.Builder(DataMainPage.this);
startNewTestSessionDialog = new StartTestDialog(DataMainPage.this);
alert.setView(startNewTestSessionDialog);
alert.setPositiveButton("Start", new DialogInterface.OnClickListener() {<...>});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {<..>});
Dialog d = alert.create();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
d.show();
d.getWindow().setAttributes(lp);
Dialog view xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:sample="http://schemas.android.com/apk/res/ips.tecnos.saildata"
android:id="#+id/startTest_dialogView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="65dp"
android:fillViewport="true" >
<LinearLayout
android:id="#+id/startTest_viewContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clipToPadding="false"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal"
android:weightSum="2"
android:layout_marginBottom="5dp" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:layout_marginRight="7dp"
android:gravity="right|center_vertical"
android:text="#string/testStart_disableWind"
android:textAppearance="?android:attr/textAppearanceMedium" />
<CheckBox
android:id="#+id/startTest_disableWind"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:layout_marginTop="10dp"
android:text="#string/testStart_boatType"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal"
android:weightSum="3"
android:layout_marginBottom="5dp" >
<Spinner
android:id="#+id/startTest_boatSelect"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center|right"
android:layout_weight="2"
android:gravity="center|right" />
<Button
android:id="#+id/startTest_modifyBoatPolar"
android:layout_width="0dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/testStart_modboatPolar" />
</LinearLayout>
<...other controls with same logic...>
</LinearLayout>
</ScrollView>
Screenshoot (Nexus5): ScrollView is at its end, and as you can see button and hidden spinner is out of scrollView border.
Screnshoot

With some casual changes i found solution!
The problem was property of ScrollView:
android:layout_gravity="center"
removing it, the layout is right positioned.

Related

Trouble opening images on touching gridview

What should the code be if I want the kind of interface that is shown in the photo? When I touch the image in the ListView,it should expand (enter image description here) and when I touch anywhere around the image, it should disappear (enter image description here).
try this using custom dialog you can achieve this
create a custom_layout file like this
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#5a000000"
android:padding="5dp"
android:text="Title"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:src="#drawable/disha2" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_favorite_fill" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_favorite_fill" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_favorite_fill" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_favorite_fill" />
</LinearLayout>
</LinearLayout>
now in activity file add this code for custom dialog
final Dialog filterDialog = new Dialog(this);
filterDialog.setContentView(R.layout.custom_layout);
Window window = filterDialog.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
filterDialog.show();
ask me in case of any query

How to display Relative layout content from top of the screen

I am having following layout in file mylayout.xml.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/yellow"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:id="#+id/Layout5"
android:layout_alignParentTop="true"
android:layout_gravity="top"
android:gravity="top"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Layout6"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="top">
<ImageView
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#color/green"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="5dp"
android:id="#+id/imageView"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_below="#+id/imageView">
<Button
android:id="#+id/btn1"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="#string/text_btn_1"
/>
<Button
android:id="#+id/btn2"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="#string/text_btn_2"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
With this layout when i am doing setContentView(R.layout.mylayout) for my activity, which has transparent theme, my layout is getting displayed in the center of the screen.
I want to change this layout positioning dynamically, so center(default) and bottom(by using following code) display is working.
LinearLayout lLayout = (LinearLayout) findViewById(R.id.Layout6);
RelativeLayout.LayoutParams relativeParas = (RelativeLayout.LayoutParams) lLayout.getLayoutParams();
relativeParas.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lLayout.setLayoutParams(relativeParas);
But somehow i am not able to figure out how to display this layout dynamically to the top of the screen.
Any help will be appreciated.
Try removing the rule that you made earlier (setting it to align bottom), and add a new rule:
//your code.
LinearLayout lLayout = (LinearLayout) findViewById(R.id.Layout6);
RelativeLayout.LayoutParams relativeParas = (RelativeLayout.LayoutParams) lLayout.getLayoutParams();
relativeParas.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lLayout.setLayoutParams(relativeParas);
//remove rule
relativeParas.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
relativeParas.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lLayout.setLayoutParams(relativeParas);
Note that removeRule() is available from API 17 onwards.
Try modifying following way:
layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Layout5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#color/yellow"
android:padding="2dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:background="#color/green"/>
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/imageView"
android:orientation="horizontal">
<Button
android:id="#+id/btn1"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="button1"
/>
<Button
android:id="#+id/btn2"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="button2"
/>
</LinearLayout>
</RelativeLayout>
class:
RelativeLayout Layout5 = (RelativeLayout)findViewById(R.id.Layout5);
Layout5.setGravity(Gravity.BOTTOM);
I found the following fix for this issue.
I added filler Liner layout in the end under Relative layout.
To display in center don't do anything.
To display in top use following code. Just move filler layout to bottom.
LinearLayout lLayout = (LinearLayout) findViewById(R.id.filler);
RelativeLayout.LayoutParams relativeParas = (RelativeLayout.LayoutParams) lLayout.getLayoutParams();
relativeParas.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lLayout.setLayoutParams(relativeParas);
To display in bottom use following code. i.e. move layout6 to bottom.
LinearLayout lLayout = (LinearLayout) findViewById(R.id.Layout6);
RelativeLayout.LayoutParams relativeParas = (RelativeLayout.LayoutParams) lLayout.getLayoutParams();
relativeParas.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lLayout.setLayoutParams(relativeParas);

Android - Make AlertDIalog buttons a uniform size

I have an alert dialog with a positive and negative button added programatically. In the dialog layout there are also two buttons, above the AlertDialog's native buttons.
When these two are right next to each other, I realized that in this dialog, the native positive/negative/neutral buttons are not equally weighted. The content of their text determines their horizontal weight, rather than each taking up 50% (or 33% if 3 buttons) of the dialog. So in my case, where the negative button's text is longer than that positive button's text, we get something that looks like the image I posted above.
I can't find a way to fix this, does anyone have an idea?
Thanks!
XML as requested:
<?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" >
<TextView android:id="#+id/rcd_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="8dp"
android:text="#string/rating_dialog_text"
android:textSize="14dp"
android:textColor="#android:color/white"
android:gravity="center"/>
<RatingBar android:id="#+id/rcd_rating_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/rcd_msg"
android:layout_margin="8dp"
android:paddingBottom="10dp"
android:numStars="5"
android:rating="0"
android:layout_centerHorizontal="true"/>
<RelativeLayout android:layout_height="56dp"
android:layout_width="match_parent"
android:layout_below="#id/rcd_rating_bar">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout android:id="#+id/rcd_image"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:background="#color/selectable_transparent"
android:layout_weight="1">
<TextView android:id="#+id/rcd_image_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:paddingLeft="0dp"
android:gravity="center"
android:text="#string/add_image"
android:textColor="#android:color/white"
android:textSize="16sp"
android:clickable="true"/>
<ImageView android:id="#+id/rcd_image_img"
android:layout_height="32dp"
android:layout_width="32dp"
android:layout_centerVertical="true"
android:layout_margin="8dp"
android:layout_toLeftOf="#id/rcd_image_text"
android:scaleType="centerInside"
android:src="#android:drawable/ic_menu_camera"/>
</RelativeLayout>
<RelativeLayout android:id="#+id/rcd_comment"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:background="#color/selectable_transparent"
android:layout_weight="1">
<TextView android:id="#+id/rcd_comment_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:paddingRight="6dp"
android:gravity="center"
android:text="#string/add_comment"
android:textColor="#android:color/white"
android:textSize="16sp"
android:clickable="true"/>
<ImageView android:id="#+id/rcd_comment_img"
android:layout_height="32dp"
android:layout_width="32dp"
android:layout_centerVertical="true"
android:layout_margin="4dp"
android:layout_toRightOf="#id/rcd_comment_text"
android:scaleType="centerInside"
android:src="#drawable/ic_menu_comment"/>
</RelativeLayout>
</LinearLayout>
<LinearLayout android:layout_width="1dp"
android:layout_height="38dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:layout_margin="10dp"
android:background="#color/transparent_white" />
</RelativeLayout>
</RelativeLayout>
Figured out a solution... don't know why I didn't think of it before.
I do the following:
mRateConcertDialog.setOnShowListener(new OnShowListener() {
#Override
public void onShow(DialogInterface d) {
Button posButton = mRateConcertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
Button negButton = mRateConcertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
LayoutParams posParams = (LayoutParams) posButton.getLayoutParams();
posParams.weight = 1;
posParams.width = LayoutParams.MATCH_PARENT;
LayoutParams negParams = (LayoutParams) negButton.getLayoutParams();
negParams.weight = 1;
negParams.width = LayoutParams.MATCH_PARENT;
posButton.setLayoutParams(posParams);
negButton.setLayoutParams(negParams);
}
});
Basically, you have to manipulate the size of the buttons AFTER the dialog is shown. So create an onShowListener and you can just set weights and widths that way. Thanks for the comments, I hope this can help someone in the future.

Custom dialog layout widget drawing issue

I am trying to create a custom dialog configured with a couple of textviews, an edittext, scrollview and a "Ok" button. For some reasons, I have not been able to correctly align the "Ok" button in the dialog.
My snipped code for the creation of dialog is as below:
mLineupDialog=new Dialog(context,R.style.CustomDialogTheme);
mLineupDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
View view = LayoutInflater.from(context).inflate(R.layout.custom_dialog, null, false);
rg = (RadioGroup) view.findViewById(R.id.radiogroup);
RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < lobjs.length; i++){
rb = new RadioButton(context);
rb.setText(lobjs[i].carrierName);
rb.setId(i);
// rb.setTextColor
rg.addView(rb, layoutParams);
}
zipEntry.setText(mZipCode);
mPrefs.edit().putString(sharedZipEntry, mZipCode).commit();
}
mLineupDialog.setContentView(view);
Button Ok =(Button)view.findViewById(R.id.lineupdialogButton);
My Layout file is as below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linlayoutBase"
android:layout_width="#dimen/favorite_dialog_width"
android:layout_height="match_parent"
android:background="#drawable/add_remove_to_favs_question_bottom_bubble"
android:focusableInTouchMode="false"
android:orientation="vertical" >
<TextView
android:id="#+id/lineup_heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/favorite_dialog_text_margin_left"
android:layout_marginTop="#dimen/favorite_dialog_text_margin_top"
android:text="#string/sel_lineup"
android:textColor="#color/white_font"
android:textSize="#dimen/fav_alert_text_size"
android:typeface="sans" >
</TextView>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/zipcode_prompt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/favorite_dialog_text_margin_left"
android:text="#string/sel_zipcode"
android:textColor="#color/white_font"
android:textSize="#dimen/fav_alert_text_size"
android:typeface="sans" >
</TextView>
<EditText
android:id="#+id/zipEntry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#color/white_font"
android:textSize="#dimen/fav_alert_text_size"
android:typeface="sans" >
</EditText>
</LinearLayout>
<ScrollView
android:id="#+id/scrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_weight="1" >
<LinearLayout
android:id="#+id/linearMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/favorite_dialog_text_margin_left" >
<RadioGroup
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/favorite_dialog_text_margin_left"
android:orientation="vertical"
android:textColor="#color/white_font"
android:textSize="#dimen/fav_alert_text_size"
android:typeface="sans" >
</RadioGroup>
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/lineupdialogButton"
android:layout_width="#dimen/favorite_dialog_button_width"
android:layout_height="wrap_content"
android:layout_marginLeft="200dp"
android:gravity="center_horizontal"
android:text="#string/favorite_dialog_button_ok_text"
android:textColor="#xml/info_panel_button_text_watch_selector"
android:textSize="#dimen/favorite_dialog_button_text_size"
android:textStyle="bold"
android:typeface="sans" >
<!-- android:layout_weight="1" -->
</Button>
</LinearLayout>
The problem is the OK button is just getting laid out partially out of the bottom border of dialog box. . Any suggestion to fix this would be greatly appreciated! Thanks!!
Image of the custom dialog attached

Height of an AlertDialog

I try to show an AlertDialog with the following code:
final int requestLayout = R.layout.triprunning_pause;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(requestLayout, null);
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.runningTrip_pause_title);
builder.setIcon(android.R.drawable.ic_media_pause);
builder.setView(layout);
dialog = builder.create();
dialog.show();
The XML I'll show looks like:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="#+id/triprunning_pause_root">
<TextView android:id="#+id/questionText" android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="#string/runningTrip_pause_text"
android:textSize="22sp" android:gravity="center_vertical|center_horizontal"
android:paddingBottom="5dp" />
<View android:id="#+id/buttons_helper" android:layout_width="0dp"
android:layout_height="0dp" android:layout_centerHorizontal="true" />
<Button android:id="#+id/continueButton" android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="Fortsetzen"
android:layout_below="#id/questionText" android:layout_toLeftOf="#id/buttons_helper" />
<Button android:id="#+id/stopButton" android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="Beenden"
android:layout_below="#id/questionText" android:layout_toRightOf="#id/buttons_helper" />
And what I get is:
screenshot http://img546.imageshack.us/img546/2057/devicef.png
I can not really figure out, why there is this black space after the tow buttons? Any idea?
edit: get it to work with the following XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:id="#+id/triprunning_pause_root" android:orientation="vertical">
<TextView android:id="#+id/questionText" android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="#string/runningTrip_pause_text"
android:textSize="22sp" android:gravity="center_vertical|center_horizontal"
android:paddingBottom="5dp" />
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button android:id="#+id/continueButton" android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="Fortsetzen"
android:layout_below="#id/questionText" android:layout_toLeftOf="#id/buttons_helper"
android:layout_weight="1" />
<Button android:id="#+id/stopButton" android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="Beenden"
android:layout_below="#id/questionText" android:layout_toRightOf="#id/buttons_helper"
android:layout_weight="1" />
</LinearLayout>
Not the best solutions as i got a senseless nesting but it works...
Have you tried to use android:layout_height="wrap_content" for your relative layout?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:id="#+id/triprunning_pause_root">
I use a LinearLayout to get the dialog you're going for.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/triprunning_pause_root">

Categories

Resources