I'm trying to create a dialog popup window with a custom layout, but my custom layout height and popup window height is not matching. Also, the popup window is hiding a button. What should I do, how can I match my popup window height with a custom layout?
Please help me.
I want my popup window to look like this
But it is looking like this
My custom layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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="336dp"
android:layout_height="156dp"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_gravity="bottom|center"
android:background="#drawable/rectangle"
android:layout_marginBottom="88dp">
<ImageView
android:id="#+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="58dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/bikepic" />
<ImageView
android:id="#+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginEnd="64dp"
android:layout_marginBottom="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/delivery" />
<TextView
android:id="#+id/biketextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="69dp"
android:layout_marginTop="17dp"
android:background="#drawable/biketextview"
android:gravity="center"
android:text="Bike"
android:textColor="#FFFFFF"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/deliverytextView"
android:layout_width="64dp"
android:layout_height="18dp"
android:layout_marginTop="17dp"
android:layout_marginEnd="64dp"
android:background="#drawable/deliverytextview"
android:gravity="center"
android:text="Delivery"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView7"
android:layout_width="23dp"
android:layout_height="14dp"
android:layout_marginStart="78dp"
android:layout_marginTop="8dp"
android:gravity="center"
android:text="74"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView5" />
<TextView
android:id="#+id/textView8"
android:layout_width="23dp"
android:layout_height="14dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="80dp"
android:gravity="center"
android:text="39"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView6" />
popup dialog code
popAddPost = new Dialog(this);
popAddPost.setContentView(R.layout.pop_up_confirm_pickup);
//popAddPost.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//popAddPost.getWindow().setLayout(Toolbar.LayoutParams.MATCH_PARENT,Toolbar.LayoutParams.WRAP_CONTENT);
popAddPost.getWindow().getAttributes().gravity = Gravity.BOTTOM;
In your xml, the parent layout is using RelativeLayout LayoutParams on a ConstraintLayout
Remove android:layout_alignParentLeft="true" and android:layout_alignParentEnd="true"
ConstraintLayout also doesn't support layout_gravity so you can also remove that.
<androidx.constraintlayout.widget.ConstraintLayout
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="#drawable/rectangle"
android:layout_marginBottom="88dp">
Code
popAddPost = new Dialog(this);
popAddPost.setContentView(R.layout.pop_up_confirm_pickup);
Objects.requireNonNull(popAddPost.getWindow()).setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popAddPost.show();
EDIT
To position the dialog at the bottom use this:
Window window = popAddPost.getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.gravity = Gravity.BOTTOM;
window.setAttributes(params);
This is how i do it:
private AlertDialog alertDialog;
alertDialog = buildDialog();
alertDialog.show();
private AlertDialog buildDialog(){
final AlertDialog dialogBuilder = new AlertDialog.Builder(context).create();
LayoutInflater inflater = LayoutInflater.from(context);
View dialogView = inflater.inflate(R.layout.dialog, null);
dialogBuilder.setView(dialogView);
dialogBuilder.setButton(DialogInterface.BUTTON_NEGATIVE, "CLOSE", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialogBuilder.dismiss();
}
});
return dialogBuilder;
}
I think dialog is not a good solution for your problem as a dialog would have its own window with overlay.
As I can see you want the dialog to be at the bottom of the screen, which makes the BottomSheet as a perfect match for your problem.
You can use the following to get started with:
https://www.androidhive.info/2017/12/android-working-with-bottom-sheet/
https://material.io/develop/android/components/bottom-sheet-behavior/
https://medium.com/#droidbyme/android-bottom-sheet-7e9cfcec6427
With BottomSheet, you would get a separate window just like dialog without the overlay you are facing.
Related
I need my Dialog width and height fill 70% of screen space. I'm using ConstraintLayout as root layout and tried to achieve this by using app:layout_constraintWidth_percent but it's not working.
It would be great to achieve this in XML without Java code.
Here is my code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/dialog_background_shape"
android:padding="#dimen/app_padding">
<TextView
android:id="#+id/textView13"
style="#style/text_plain3"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:text="#string/add_member_title"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/button_add_member_close"
style="#style/text_title2"
android:textStyle="bold"
android:layout_width="28dp"
android:layout_height="28dp"
android:background="#drawable/button_dialog_close"
android:gravity="center"
android:text="X"
android:textColor="#color/text_plain3"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/add_member_group_id"
style="#style/text_group_id"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="64dp"
android:layout_marginEnd="32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView13" />
<TextView
android:id="#+id/textView17"
style="#style/text_card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="#string/add_member_annotation"
app:layout_constraintStart_toStartOf="#+id/add_member_group_id"
app:layout_constraintTop_toBottomOf="#+id/add_member_group_id"/>
<EditText
android:id="#+id/add_member_user_id"
android:layout_width="248dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="#string/add_member_hint_id"
android:importantForAutofill="no"
android:inputType="textPersonName"
android:maxLength="28"
app:layout_constraintEnd_toEndOf="#+id/add_member_group_id"
app:layout_constraintStart_toStartOf="#+id/add_member_group_id"
app:layout_constraintTop_toBottomOf="#+id/textView17" />
<Button
android:id="#+id/button_add_member"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:layout_marginBottom="48dp"
android:text="#string/add_member_button_add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/add_member_group_id"
app:layout_constraintStart_toStartOf="#+id/add_member_group_id"
app:layout_constraintTop_toBottomOf="#+id/add_member_user_id"/>
</androidx.constraintlayout.widget.ConstraintLayout>
In order to have a constraint width/height DialogFragment or (any specific width or height in general):
wrap your ConstraintLayout into another ConstraintLayout in order to make the outer take the full size of the screen, and the inner get the desired width/height percentage:
Make the android:background of the outer to be transparent
Constraint the width & height of the inner ConstraintLayout, make it centered in parent, and add the percentage constraint to the width & height:
app:layout_constraintHeight_percent="0.7"
app:layout_constraintWidth_percent="0.7"
Now the layout looks like:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/main_layout"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_gravity="center"
android:background="#drawable/dialog_background_shape"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.7"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.7"
android:padding="#dimen/app_padding">
<TextView
android:id="#+id/textView13"
style="#style/text_plain3"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:text="#string/add_member_title"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/button_add_member_close"
style="#style/text_title2"
android:textStyle="bold"
android:layout_width="28dp"
android:layout_height="28dp"
android:background="#drawable/button_dialog_close"
android:gravity="center"
android:text="X"
android:textColor="#color/text_plain3"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/add_member_group_id"
style="#style/text_group_id"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="64dp"
android:layout_marginEnd="32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView13" />
<TextView
android:id="#+id/textView17"
style="#style/text_card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="#string/add_member_annotation"
app:layout_constraintStart_toStartOf="#+id/add_member_group_id"
app:layout_constraintTop_toBottomOf="#+id/add_member_group_id" />
<EditText
android:id="#+id/add_member_user_id"
android:layout_width="248dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="#string/add_member_hint_id"
android:importantForAutofill="no"
android:inputType="textPersonName"
android:maxLength="28"
app:layout_constraintEnd_toEndOf="#+id/add_member_group_id"
app:layout_constraintStart_toStartOf="#+id/add_member_group_id"
app:layout_constraintTop_toBottomOf="#+id/textView17" />
<Button
android:id="#+id/button_add_member"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:layout_marginBottom="48dp"
android:text="#string/add_member_button_add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/add_member_group_id"
app:layout_constraintStart_toStartOf="#+id/add_member_group_id"
app:layout_constraintTop_toBottomOf="#+id/add_member_user_id" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
In your custom DialogFragment:
Designate the width & height of the dialog to MATCH_PARENT:
dialog?.window?.setLayout(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
)
Set no background the theme:
<style name="NoBackgroundDialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="android:windowBackground">#null</item>
</style>
Custom Dialog Fragment:
Java:
public class MyDialogFragment extends DialogFragment {
#Nullable
#org.jetbrains.annotations.Nullable
#Override
public View onCreateView(#NonNull #NotNull LayoutInflater inflater, #Nullable #org.jetbrains.annotations.Nullable ViewGroup container, #Nullable #org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
return inflater.inflate(
R.layout.dialog_layout, container,
false
);
}
#Override
public int getTheme() {
return R.style.NoBackgroundDialogTheme;
}
#Override
public void onStart() {
// Making the dialog full screen
if (getDialog() != null)
getDialog().getWindow().setLayout(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
);
super.onStart();
}
}
Kotlin:
class MyDialogFragment : DialogFragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return inflater.inflate(
R.layout.dialog_layout, container,
false
)
}
override fun getTheme(): Int = R.style.NoBackgroundDialogTheme
override fun onStart() {
super.onStart()
// Making the dialog full screen
dialog?.window?.setLayout(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
)
}
}
Note: I am assuming that the dialog layout is R.layout.dialog_layout
UPDATE
now if I click on empty space around the Dialog, it doesn't close.
This is because the dialog fragment consumes the entire screen size; you can solve this by the below workaround:
Add an ID to the outer most ConstraintLayout, assume it is root >> Updated on the top layout
Add an ID to the inner ConstraintLayout, assume it is main_layout >> Updated on the top layout
Dismiss the dialog when the root is clicked
Do nothing when the main_layout is clicked to consume the event so that it won't be dismissed:
So, update the onStart() of the MyDialogFragment to:
override fun onStart() {
super.onStart()
// Making the dialog full screen
dialog?.window?.setLayout(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
)
val root = requireView().findViewById<ConstraintLayout>(R.id.root)
root.setOnClickListener {
dismiss() // Dismiss the dialog
}
val main = requireView().findViewById<ConstraintLayout>(R.id.main_layout)
main.setOnClickListener {
// Consume the event
}
}
ConstraintLayout:
I see your "Dialog" Box contains a lot of different views. For that I made a simple example for you to show how to use Guideline for achieving percentages of Views.
I suggest to learn how this works and rebuild your layout with better constraining on this Guidelines, because now everything is wired up really bad. The idea will be to wire the ConstraintLayout to the Guideline and everything beside that (e.g. TextView etc.) to the ConstraintLayout. (Without Hardcoding!, that's how relative design work).
My example shows a TextView (could be your ConstraintLayout (frame for everything) that has exactly 70% (app:layout_constraintGuide_percent="0.7") vertically and horizontal of the screen. Important is to set the TextViews width and height to 0dp and to attach to the Guideline:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/vertical_guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.7" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/horizontal_guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.7" />
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#2962FF"
app:layout_constraintBottom_toTopOf="#+id/horizontal_guideline"
app:layout_constraintEnd_toStartOf="#+id/vertical_guideline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="TextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
Result:
In addition same for LinearLayout:
You can use android:layout_weight="" to give a percent amount. So 70% means a weight of .70.
Keep in mind to set the width or/and height to 0dp
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="70 percent of screen"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".70" />
</LinearLayout>
Trying to make two login, one for admin, one for user. They share the same layout, but admin has additional TextFields and Buttons to add data to the menu. So when logging in with user credentials, the TextFields and Buttons will be setVisibility(View.GONE), but it's not working at all, in fact it's showing up like setVisibility(View.GONE) was added at all. Any advice would be appreciated.
loginpage.java
DatabaseHelper myDB;
EditText LoginEMail;
EditText LoginPassword;
Button LoginBtn;
LinearLayout linearLayout;
View add_image;
View add_name;
View add_desc;
View add_data;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginpage);
LoginEMail = findViewById(R.id.LoginEMail);
LoginPassword = findViewById(R.id.LoginPassword);
LoginBtn = findViewById(R.id.LoginBtn);
myDB = new DatabaseHelper(this);
linearLayout = findViewById(R.id.linearLayout);
linearLayout = new LinearLayout(this);
add_image = new View(this);
add_name = new View(this);
add_desc = new View(this);
add_data = new View(this);
}
public void Login(View view) {
Intent intent = new Intent(loginpage.this, MenuSelection.class);
if (LoginEMail.getText().toString().equals("admin") && (LoginPassword.getText().toString().equals("admin"))) {
startActivity(intent);
linearLayout.setVisibility(View.VISIBLE);
}
else if(LoginEMail.getText().toString().equals("user") && (LoginPassword.getText().toString().equals("user"))) {
startActivity(intent);
linearLayout.setVisibility(View.GONE);
add_image.setVisibility(View.GONE);
add_name.setVisibility(View.GONE);
add_desc.setVisibility(View.GONE);
add_data.setVisibility(View.GONE);
}
else
Toast.makeText(loginpage.this, "Incorrect E-mail or Password.", Toast.LENGTH_SHORT).show();
}
loginpage.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<EditText
android:id="#+id/LoginEMail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="E-Mail"
android:inputType="textEmailAddress"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.204" />
<EditText
android:id="#+id/LoginPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.338" />
<Button
android:id="#+id/LoginBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Log In"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.602"
android:onClick="Login"/>
</androidx.constraintlayout.widget.ConstraintLayout>
menu_selection.xml
<androidx.constraintlayout.widget.ConstraintLayout
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:gravity="center_horizontal"
android:orientation="vertical">
<ListView
android:id="#+id/menu_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<ImageButton
android:id="#+id/add_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#color/black" />
<EditText
android:id="#+id/add_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Name" />
<EditText
android:id="#+id/add_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Description" />
<Button
android:id="#+id/add_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Instead of add_image = new View(this); you need to associate the variable add_image to the view in your XML layout using the findViewById(R.id.add_image) method. Similarly for the other 3 views that you have initialized this way.
You also need to delete the line linearLayout = new LinearLayout(this);. The previous line setting this variable is correct.
I have made an alert dialog which shows a custom view,
Java Code
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.about_me_alert, null);
CircleImageView imageView = view.findViewById(R.id.image);
Glide.with(view).load("https://example.com/example.jpg").into(imageView);
TextView title = view.findViewById(R.id.title);
title.setText("This is a title");
TextView note = view.findViewById(R.id.note);
note.setText("This is a note");
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(view);
builder.setPositiveButton("Okay", (dialog, which) -> dialog.dismiss()).show();
XML Layout- about_me_alert.xml
<?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"
android:background="?attr/cardbackground"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/image"
android:layout_width="128dp"
android:layout_height="128dp"
android:layout_centerHorizontal="true"
android:layout_margin="8dp" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/image"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:textColor="#color/white"
android:textSize="16sp" />
<TextView
android:id="#+id/note"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_centerHorizontal="true"
android:layout_margin="8dp"
android:textColor="#color/white"
android:textSize="16sp" />
</RelativeLayout>
When alert is shown, it reserves space for the imageview, and two textviews but does not set text or image.
NOTE: This only happens when I make a production apk for release, not while I am testing on my phone.
I would like to ask on how to achieve to wrap a content in alertdialog?
Because the current output of my dialog has an excess white field.
Hope someone can help me to understand this problem thanks.
Here is my activity_dialog.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:minWidth="10dp"
android:minHeight="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context="com.bloxofcode.toggle.MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="50sp"
android:background="#color/colorHeaderDialog"
android:textColor="#android:color/white"
android:text="#string/select_gender"
android:padding="15dp"
android:gravity="center_horizontal|left"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:padding="15dp"
android:text="Sample"
android:id="#+id/editText" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ToggleButton
android:text="ToggleButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/toggleButtonMale"
android:textOn=""
android:textOff=""
android:focusable="false"
android:background="#drawable/check_male"
android:layout_weight="1" />
<ToggleButton
android:text="ToggleButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/toggleButtonFemale"
android:textOn=""
android:textOff=""
android:focusable="false"
android:background="#drawable/check_male"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<Button
android:id="#+id/btnCancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cancel"
android:textSize="20dp"
android:padding="30dp"
android:textColor="#android:color/white"
android:layout_weight="1"/>
<Button
android:id="#+id/btnAccept"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Accept"
android:textSize="20dp"
android:padding="30dp"
android:background="#color/colorDialogOK"
android:textColor="#android:color/white"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Here is some of my implementation in the MainActivity.java:
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
View mView = getLayoutInflater().inflate(R.layout.activity_dialog, null);
.....
mBuilder.setView(mView);
final AlertDialog dialog = mBuilder.create();
dialog.show();
Try Creating a dialog like this. this works perfect and then right away your dialog logic is seperated from you activity logic
public class CustomDialog extends Dialog implements
android.view.View.OnClickListener {
public Activity c;
public CustomDialog d;
public Button yes, no;
public CustomDialog(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog_layout);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//dosomething
}
});
}
}
Hope it is not correct to wrap the content of dialog as it might disrupt the view in tablets and larger devices.
Anyhow, basing upon your requirement.. hope this link works
AlertDialog with custom view: Resize to wrap the view's content
I am trying to add a TextInputLayout with an EditText based on a number that the user selects from a Spinner. I already have the TextInputLayout attributes defined in XML and was hoping to simple just add them programmatically based on the number that the user selects from the spinner:
XML:
<FrameLayout
android:background="#drawable/image_border"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".525">
<Button
android:id="#+id/add_image_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Click to Add Image" />
</FrameLayout>
<LinearLayout
android:orientation="vertical"
android:padding="4dp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".475">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="#+id/create_poll_question_editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true"
android:hint="#string/create_poll_question" />
</android.support.design.widget.TextInputLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/how_many_answers_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/how_many_answers_text"
android:textColor="#color/black"
android:textSize="16sp" />
<Spinner
android:id="#+id/number_of_answers_spinner"
android:layout_width="wrap_content"
android:layout_gravity="bottom"
android:layout_height="24dp"
android:background="#android:drawable/btn_dropdown" />
</LinearLayout>
<!--Want to Add Programatically -->
<android.support.design.widget.TextInputLayout
android:id="#+id/create_poll_answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="#+id/create_poll_answer_editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true"
/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
Here is the code I am currently using, but it does not add dynamically based on the view I already created:
public class YourItemSelectedListener implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String selected = parent.getItemAtPosition(pos).toString();
Toast.makeText(getActivity().getApplicationContext(), selected, Toast.LENGTH_SHORT).show();
for (int i = 0; i < Integer.parseInt(selected); i++) {
ViewGroup layout = (ViewGroup) mRootView.findViewById(R.id.create_poll_linearlayout);
EditText editText = new EditText(getActivity());
editText.setHint("Poll Answer");
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
editText.setLayoutParams(layoutParams);
TextInputLayout newAnswer = new TextInputLayout(getActivity());
newAnswer.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
newAnswer.addView(editText, layoutParams);
layout.addView(newAnswer);
}
}
Easiest way to do such dynamic injections of view is to use ButterKnife library by JakeWharton
http://jakewharton.github.io/butterknife/
You can use it like in your activity's declaration part:
#BindView(R.id.title) TextInputLayout textInputLayout;
Then bind it inside onCreate() like:
ButterKnife.bind(this);
This will roughly be equivalent to inflating and finding the view by id.
Moreover, the library helps to dynamically set drawables,etc. with ease as well
Add
android:id="#+id/create_poll_linearlayout"
to your root LinearLayout.