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">
Related
I am inflating a view using Dialog Fragment.Everything works fine till API 23+ but when I am testing it on API 19, I am getting a blue line over my view.I can not even find the color of this line in my color.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#B3000000"
android:orientation="vertical">
<TextView
android:id="#+id/add_text_done_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_margin="20dp"
android:background="#drawable/rounded_border_text_view"
android:padding="10dp"
android:text="Done"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="15sp" />
<EditText
android:id="#+id/add_text_edit_text"
style="#style/EditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/add_text_color_picker_relative_layout"
android:layout_below="#+id/add_text_done_tv"
android:background="#null"
android:gravity="center"
android:inputType="textMultiLine"
android:textSize="40sp" />
<LinearLayout
android:id="#+id/add_text_color_picker_relative_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/add_text_font_picker_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:background="#android:color/black"
tools:listitem="#layout/font_picker_list_item" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/add_text_color_picker_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:background="#android:color/black"
tools:listitem="#layout/color_picker_item_list" />
</LinearLayout>
</RelativeLayout>
I have added this code in my class onStart() to set the size of layout.
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.MATCH_PARENT;
dialog.getWindow().setLayout(width, height);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
The following is your answer.
https://stackoverflow.com/a/37738741/9224262
Also I think you should have searched first. Anyway good luck.
You should hide the title:
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
So I have an XML for my activity, and I want to put another view on top of whatever this XML has. The new view covers the entire screen (match_parent for both width and height). I use LayoutInflater.inflate(int,View) to inflate the new view into my root view. The new view is in front of everything but the last button. Even calling bringToFront or bringChildToFront doesn't do anything. If I wrap this button in another LinearLayout, it will solve the problem, but I don't want to do that. This only happens with Button.
Activity XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activities.LoginActivity"
android:background="#drawable/background"
android:id="#+id/root_view">
<LinearLayout
android:id="#+id/middle_views"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true"
android:gravity="center">
<EditText
android:id="#+id/user_name_edit_text"
android:layout_width="300dp"
android:layout_height="40dp"
android:padding="10dp"
android:layout_margin="#dimen/small_dp"
android:background="#drawable/grey_edit_text_background"
android:inputType="textEmailAddress"
android:hint="#string/email_label"
android:fontFamily="#font/bariol"
android:textSize="#dimen/medium_sp"/>
<EditText
android:id="#+id/password_edit_text"
android:layout_width="300dp"
android:layout_height="40dp"
android:padding="10dp"
android:layout_marginBottom="#dimen/big_dp_1"
android:background="#drawable/grey_edit_text_background"
android:inputType="textPassword"
android:hint="#string/password_label"
android:fontFamily="#font/bariol"
android:textSize="#dimen/medium_sp"/>
<Button
android:onClick="onSignInClick"
android:layout_width="120dp"
android:layout_height="40dp"
android:background="#drawable/orange_gradient_button"
android:text="#string/login_label"
android:textColor="#fff"
android:fontFamily="#font/bariol"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/middle_views"
android:gravity="center"
android:orientation="vertical"
android:layout_margin="#dimen/medium_dp">
<ImageView
android:layout_width="250dp"
android:layout_height="100dp"
android:src="#drawable/banner"
android:layout_marginBottom="#dimen/big_dp_3"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/middle_views"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_marginTop="#dimen/small_dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/or_label"
android:fontFamily="#font/bariol"
android:textSize="#dimen/big_sp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="45dp"
android:layout_height="45dp"
android:background="#drawable/facebook_icon"
android:layout_margin="#dimen/medium_dp"/>
<Button
android:layout_width="45dp"
android:layout_height="45dp"
android:background="#drawable/twitter_icon"
android:layout_margin="#dimen/medium_dp"/>
<Button
android:layout_width="45dp"
android:layout_height="45sp"
android:background="#drawable/google_icon"
android:layout_margin="#dimen/medium_dp"/>
</LinearLayout>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:paddingStart="#dimen/big_dp_1"
android:paddingEnd="#dimen/big_dp_1"
android:layout_margin="#dimen/big_dp_2"
android:background="#drawable/white_button"
android:text="#string/create_new_account_label"
android:textSize="#dimen/small_sp"
android:textAllCaps="false"
android:textStyle="normal"
android:fontFamily="#font/bariol"/>
</RelativeLayout>
New view XML (InfoDialog is a subclass of RelativeLayout):
<?xml version="1.0" encoding="utf-8"?>
<com.sseo242.airua.CustomViews.InfoDialog
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/semiBlack"
android:focusable="true"
android:clickable="true">
<LinearLayout
android:id="#+id/dialog_board"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical"
android:gravity="center"
android:background="#drawable/dialog_background">
<TextView
android:id="#+id/info_dialog_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/medium_dp"
android:text="title"
android:textStyle="bold"
android:fontFamily="#font/bariol"
android:textSize="#dimen/large_sp"
android:textColor="#003f66"/>
<TextView
android:id="#+id/info_dialog_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/big_dp_1"
android:layout_marginStart="#dimen/big_dp_2"
android:layout_marginEnd="#dimen/big_dp_2"
android:gravity="center"
android:text="Content"
android:textColor="#333"
android:fontFamily="#font/bariol"
android:textSize="#dimen/big_sp"/>
<Button
android:id="#+id/info_dialog_ok_button"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_marginBottom="#dimen/big_dp_1"
android:background="#drawable/orange_gradient_button"
android:text="#string/ok_button"
android:textColor="#fff"
android:fontFamily="#font/bariol"
android:textStyle="bold"
android:textSize="#dimen/big_sp"/>
</LinearLayout>
</com.sseo242.airua.CustomViews.InfoDialog>
Method I use to show the new view:
public void showInfoDialog(BaseActivity activity, String title, String message,
#Nullable DialogOnClickHandler handler){
LayoutInflater inflater = activity.getLayoutInflater();
ViewGroup rootView = activity.getRootView();
int index = rootView.getChildCount();
InfoDialog dialog = (InfoDialog)((ViewGroup) inflater.inflate(R.layout.dialog_info, rootView))
.getChildAt(index);
rootView.bringChildToFront(dialog);
dialog.setContent(title, message, handler);
}
I would suggest you to use FrameLayout instead of RelativeLayout. You can easily overlap two views using that. An example for this can be found here :-
How to layer views
Place one view over another
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
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.
I am using alert dialog builder in application. I have checked in api level16 getting black color background for dialog but not in api level 8.Can anybody tell how to avoid this?
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent" >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="260dp"
android:layout_height="wrap_content" android:background="#drawable/dark_blue">
<TextView android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="#string/save_wishlist_title" android:layout_alignParentTop="true" android:layout_centerInParent="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout android:id="#+id/linearLayout1" android:layout_centerInParent="true" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:layout_marginTop="10dp" android:layout_below="#+id/textView1" android:orientation="vertical">
<Button android:id="#+id/phoneButton" android:layout_width="120dp" android:layout_height="wrap_content"
android:text="#string/phone_button" />
<Button android:id="#+id/emailButton" android:layout_width="120dp" android:layout_height="wrap_content"
android:text="#string/email_buton" android:layout_marginTop="5dp"/>
<Button android:id="#+id/accountButton" android:layout_width="120dp" android:layout_height="wrap_content"
android:text="#string/account_button" android:layout_marginTop="5dp"/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
Inflate this layout in any activity where you want to set the alert dialog.
LayoutInflater li = LayoutInflater.from(VMAdjustFrame.this);
promptsView = li.inflate(R.layout.wishlist_save_prompt, null);
alertDialogBuilder = new AlertDialog.Builder(VMAdjustFrame.this);
alertDialogBuilder.setView(promptsView);
final AlertDialog dialog = alertDialogBuilder.show();