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>
Related
I am using a constraint layout in my XML file. I have a view like in the example. Imageview and textview. I want both of these to have the same action after clicking. How can I group the two together and give them an id?
xml :
<ImageView
android:id="#+id/menu_drawerLogout"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="20dp"
android:layout_marginTop="8dp"
android:src="#drawable/ic_menu_exit"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view6" />
<TextView
android:id="#+id/menu_drawerLogout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="16dp"
android:layout_marginTop="17dp"
android:text="#string/sign_out_text"/>
You can't have 2 components with the same id in an XML layout resource file.
Method 1
If you want both to have the same action set a common onClickListener to both like,
xml
<ImageView
android:id="#+id/imageView"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="20dp"
android:layout_marginTop="8dp"
android:src="#drawable/ic_menu_exit"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view6" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="16dp"
android:layout_marginTop="17dp"
android:text="#string/sign_out_text"/>
inside onCreate method
ImageView imageView = findViewById(R.id.imageView);
TextView textView = findViewById(R.id.textView);
View.OnClickListener myListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
// action
}
});
imageView.setOnClickListener(myListener);
textView.setOnClickListener(myListener);
Method 2
You can put both the views in a container and then set a onClickListener to the container
xml
<LinearLayout
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="20dp"
android:layout_marginTop="8dp"
android:src="#drawable/ic_menu_exit"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view6" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="16dp"
android:layout_marginTop="17dp"
android:text="#string/sign_out_text"/>
</LinearLayout>
inside onCreate
LinearLayout layout = findViewById(R.id.container);
View.OnClickListener myListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
// action
}
});
layout.setOnClickListener(myListener);
Consider the code below:
<TextView
android:id="#+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginTop="12dp" />
<ImageView
android:id="#+id/location"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginStart="33dp"
android:layout_marginTop="5dp"
app:srcCompat="#drawable/openLoc" />
You can but it is not recommended.
Set ids in a way that elements can be easily identifiable by ID like android:id="#+id/txtLocation" android:id="#+id/imgLocation" it makes it easy to identify element type just by reading ID. You can make it even easier by appending layout name in beginning like android:id="#+id/profileTxtLocation". Now this will help you while coding as autocomplete feature will assist you. Just type layout name you will get the list of all layout elements, then you will type the kind of element you get the list of all asked elements(es: textViews) in layout.
My layout was inside a constraintlayout and in this case I used the constraintlayout group component.
xml :
<ImageView
android:id="#+id/img_logout"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="20dp"
android:layout_marginTop="8dp"
android:src="#drawable/ic_menu_exit"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view6" />
<TextView
android:id="#+id/menu_drawerLogout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="16dp"
android:layout_marginTop="17dp"
android:text="#string/sign_out_text"
android:textColor="#color/black_choice"
android:textSize="15sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/img_logout"
app:layout_constraintTop_toBottomOf="#+id/img_star" />
<androidx.constraintlayout.widget.Group
android:id="#+id/logout_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:constraint_referenced_ids="img_logout,menu_drawerLogout" />
Then I created the Group.setAllOnClickListener field in MainActivity :
private fun Group.setAllOnClickListener(listener: View.OnClickListener?) {
referencedIds.forEach { id ->
rootView.findViewById<View>(id).setOnClickListener(listener)
}
}
and
logoutGroup.setAllOnClickListener()
logOutDialog()
}
I'm trying to display a custom dialog but it's not showing up but making the background transparent. The weirdest thing is that when i change just the custom dialog xml it shows up without problems so i think it could be the xml but i've been looking for a while and i can't find the problem.
This is the fragment code:
class DeleteConfirmationFragment : Fragment() {
private lateinit var binding: FragmentDeleteConfirmationBinding
lateinit var dialog : Dialog
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_delete_confirmation, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding = FragmentDeleteConfirmationBinding.bind(view)
dialog = context?.let { Dialog(it) }!!
binding.btnNo.setOnClickListener {
openDeletedAccountDialog()
}
}
private fun openDeletedAccountDialog() {
dialog.setContentView(R.layout.dialog_deleted_account) // if i change this xml file for other it
//works properly so i think it could be an android bug..
dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
dialog.show()
}
}
This is the xml that isnt working:
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp">
<androidx.cardview.widget.CardView
android:id="#+id/cardViewDeletedAccount"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardCornerRadius="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tvDeletedAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:fontFamily="#font/poppins_semibold"
android:text="Cuenta eliminada"
android:textColor="#color/black"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:fontFamily="#font/poppins_semibold"
android:gravity="center_horizontal"
android:padding="10dp"
android:text="Tu cuenta se ha eliminado correctamente. ¡Esperamos volver a verte pronto!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvDeletedAccount"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
Display using custom xml which isnt working:
Other custom dialog xml code that is working properly:
<?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="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp">
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/poppins_bold"
android:gravity="center"
android:text="¡Tu friendzone ha crecido!"
android:textColor="#color/white"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="#+id/cardViewFriendzone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.884" />
<androidx.cardview.widget.CardView
android:id="#+id/cardViewFriendzone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:id="#+id/cardView2"
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="100dp"
app:cardElevation="20dp"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.47"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/tvNotificationImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
app:srcCompat="#drawable/pedra" />
</androidx.cardview.widget.CardView>
<TextView
android:id="#+id/tvDialogName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:fontFamily="#font/poppins_semibold"
android:text="María González"
android:textColor="#color/black"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:fontFamily="#font/poppins_semibold"
android:padding="10dp"
android:text="Soy una chica aventurera, amistosa y muy curiosa. Mi pasión son los animales, en especial los perros."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvDialogName"
app:layout_constraintVertical_bias="0.45" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
If i change just the xml in the open deleted dialog function like this it shows up the other dialog correctly:
private fun openDeletedAccountDialog() {
dialog.setContentView(R.layout.dialog_friendzone_layout) // <-- change made here
dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
dialog.show()
}
Display using other custom dialog xml:
Any idea of what is happening?
I had the same problem too and it was because I was using 0dp in ConstrintLayout in order to using match_constraint size but it's not working in dialogs.
you just need to change android:layout_width="0dp" of your CardView to android:layout_width="match_parent".
I want to make it possible to make the PlayerView in landscape mode full screen but I can't make it work. So far, I have tried to set the playerView layout params programmatically when configuration changes to landscape mode, but it still isn't working.
Layout that I have created
<androidx.constraintlayout.motion.widget.MotionLayout 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/player_motion_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_collapseMode="parallax"
app:layoutDescription="#xml/vod_player_scene"
app:viewToDetectTouch="#id/top_player_container">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/top_player_container"
android:layout_width="match_parent"
android:layout_height="280dp"
android:background="#272727"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.exoplayer2.ui.PlayerView
android:id="#+id/playerView"
android:layout_width="match_parent"
android:layout_height="280dp"
android:focusable="true"
app:controller_layout_id="#layout/exo_playback_control_view_vod"
app:fastforward_increment="10000"
app:hide_on_touch="true"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:resize_mode="fixed_width"
app:rewind_increment="10000"
app:show_timeout="2000" />
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="64dp"
android:layout_height="64dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="#id/playerView"
app:layout_constraintEnd_toEndOf="#id/playerView"
app:layout_constraintStart_toStartOf="#id/playerView"
app:layout_constraintTop_toTopOf="#id/playerView" />
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/image_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:alpha="0"
app:layout_constraintBottom_toBottomOf="#id/top_player_container"
app:layout_constraintEnd_toStartOf="#id/image_clear"
app:layout_constraintTop_toTopOf="#id/top_player_container"
app:srcCompat="#drawable/ic_play_arrow_32dp"
app:tint="#color/white" />
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/image_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:alpha="0"
android:background="?attr/selectableItemBackgroundBorderless"
app:layout_constraintBottom_toBottomOf="#id/top_player_container"
app:layout_constraintEnd_toEndOf="#id/top_player_container"
app:layout_constraintTop_toTopOf="#id/top_player_container"
app:srcCompat="#drawable/ic_clear_32dp"
app:tint="#color/white" />
<com.google.android.material.textview.MaterialTextView
android:id="#+id/video_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:ellipsize="end"
android:fontFamily="#font/montserrat_semi_bold"
android:maxLines="1"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="#id/top_player_container"
app:layout_constraintEnd_toStartOf="#id/image_play"
app:layout_constraintStart_toEndOf="#id/playerView"
app:layout_constraintTop_toTopOf="#id/top_player_container"
tools:text="Blade Runner" />
<FrameLayout
android:id="#+id/recyclerview_container"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#color/video_club_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/top_player_container" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerview_front"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#color/video_club_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/top_player_container" />
<ProgressBar
android:id="#+id/recyclerView_progressView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="#id/recyclerview_front"
app:layout_constraintStart_toStartOf="#id/recyclerview_front"
app:layout_constraintTop_toTopOf="#id/recyclerview_front" />
</androidx.constraintlayout.motion.widget.MotionLayout>
And programatically, I am doing it this way (see below code)
#Override
public void onConfigurationChanged(#NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int newOrientation = newConfig.orientation;
if (newOrientation == Configuration.ORIENTATION_LANDSCAPE) {
MotionLayout.LayoutParams params = (MotionLayout.LayoutParams)
getPlayerView().getLayoutParams();
params.width = params.MATCH_PARENT;
params.height = params.MATCH_PARENT;
getPlayerView().setLayoutParams(params);
} else {
}
}
Also in Manifest.xml I have declared activity as
<activity
android:name=".main.MainActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode"
android:label="#string/app_name"
android:theme="#style/MainTheme"
android:windowSoftInputMode="adjustPan" />
Let me know if you need anything else to share with you.
Thanks in advance.
To be able to change any of the views that have MotionLayout as parent, we should work with MotionScene.
What I did to fix this problem, I created two functions, one to make playerView width and height to MATCH_PARENT and the other to reset it to previous state (when screen orientation back to portrait).
private void setPlayerToMatchHeight() {
ConstraintSet expandedSet = getMotionLayout().getConstraintSet(R.id.expanded);
ConstraintSet.Constraint topPlayerContainerSet = expandedSet.getConstraint(R.id.top_player_container);
topPlayerContainerSet.layout.mHeight = ViewGroup.LayoutParams.MATCH_PARENT;
}
private void setPlayerTo16x9dimensionRatio() {
ConstraintSet expandedSet = getMotionLayout().getConstraintSet(R.id.expanded);
ConstraintSet.Constraint topPlayerContainerSet = expandedSet.getConstraint(R.id.top_player_container);
topPlayerContainerSet.layout.mHeight = 0;
topPlayerContainerSet.layout.dimensionRatio = "16:9";
}
And then, in onConfigurationChanged, based on newOrientation, I call these functions respectively.
I have created a gridlayout with width=3 and height =2. i placed 6 views in each grid element with ids as view1,view2,view3,view4,view5 and view 6. now how to place buttons on each view programmatically?
View child1,child2,child3,child4,child5,child6;
child1=(View)view.findViewById(R.id.view1);
child2=(View)view.findViewById(R.id.view2);
child3=(View)view.findViewById(R.id.view3);
child4=(View)view.findViewById(R.id.view4);
child5=(View)view.findViewById(R.id.view5);
child6=(View)view.findViewById(R.id.view6);
and finally to place dynamic buttons i used the code as below.
child1 = this.getActivity().getLayoutInflater().inflate(R.layout.video,null);
video = (Button)child1.findViewById(R.id.button);
this continues for child2,3 etc.video is an xml layout with a button id as button.
the layout is as follows
<?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"
android:weightSum="1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#484848"
android:layout_weight=".1">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imb1"
android:layout_weight=".07"
android:layout_marginBottom="7.5dp"
android:textColor="#ffffff"
android:src="#drawable/back"
android:adjustViewBounds="false"
android:background="#484848" />
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center_vertical"
android:background="#484848"
android:src="#drawable/medilearn1"
android:layout_weight=".15"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Dashboard"
android:id="#+id/textView3"
android:layout_gravity="center"
android:textColor="#fbfbfb"
android:textAlignment="viewStart"
android:layout_marginLeft="20dp"
android:layout_weight="0.67" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Restart"
android:id="#+id/button3"
android:layout_weight=".07"
android:background="#d86018"
android:textColor="#ffffff"
android:layout_marginTop="7.5dp"
android:layout_marginBottom="7.5dp"
android:layout_marginRight="7.5dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d8d8d8"
android:layout_weight=".9">
<SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/searchView"
android:background="#ffffff"
android:queryHint="search"
android:layout_gravity="center_horizontal" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:background="#d8d8d8">
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="3"
android:rowCount="2"
android:id="#+id/gridView">
<View
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_columnWeight="1"
android:id="#+id/view1" />
<View
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_columnWeight="1"
android:id="#+id/view2" />
<View
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_columnWeight="1"
android:id="#+id/view3" />
<View
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_columnWeight="1"
android:id="#+id/view4" />
<View
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_columnWeight="1"
android:id="#+id/view5" />
<View
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_columnWeight="1"
android:id="#+id/view6" />
</GridLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#484848"
android:layout_weight=".1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Version:1.0"
android:id="#+id/textView6"
android:layout_gravity="center"
android:textColor="#fbfbfb"
android:textAlignment="viewStart"
android:layout_marginLeft="20dp" />
</LinearLayout>
</LinearLayout>
This is my layout.
I found the solution. I declared linearlayout in each grid with hight and width as my wish and called child with addView().so,
LinearLayout lay1=(LinearLayout)view.findViewById(R.id.layout1);
and then
lay1.addView(child1);
You can add a Button to your view like this:
Button btn = new Button();
btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btn.setId(1);
btn.setText("ButtonText");
yourView.addView(btn);
If you want it to fill your whole view, replace LayoutParams.WRAP_CONTENT with LayoutParams.MATCH_PARENT.
just set adapter for your GridView
MyGridAdapter csAdapter = new MyGridAdapter();
csGridView.setAdapter(csAdapter);
csGridView.setOnItemClickListener(gridItemClicked);
and adapter code is
public AdapterView.OnItemClickListener gridItemClicked = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//your code
}
};
Update:
Based on the layout you posted there are two options you can pursue:
Create the layout in your xml file: you could just replace the View tags with Button tags if your layout is static and will always require those buttons.
Create the layout dynamically: for this you don't need the View tags inside your layout as those will be created programmatically, so you can remove those. Then do the following for every Button you want to create (I'm assuming the layout containing the buttons is called video.xml):
View child = this.getActivity().getLayoutInflater().inflate(R.layout.video,null);
GridLayout.LayoutParams params = new GridLayout.LayoutParams();
// set the row number this child is located in
params.rowSpec = GridLayout.spec(rowNum, 1);
// set the column number this child is located in
params.columnSpec = GridLayout.spec(colNum, 1);
child.setLayoutParams(params);
grid.addView(child);
Also, please note that a View cannot have a child View, only layouts extending ViewGroup (e.g. FrameLayout, GridLayout, ...) can have child Views!
The best way to inflate the View in your case is the following code:
GridLayout grid = view.findViewById(R.id.grid); // get the grid layout
// ...
child1 = this.getActivity().getLayoutInflater().inflate(R.layout.video, grid, true);
video = (Button)child1.findViewById(R.id.button);
By providing the parent (grid), the LayoutInflator will take care of properly inflating the requested resource into the view hierarchy, the true attaches the inflated layout to the root layout.
Also check the API docs for this:
inflate(int resource, ViewGroup root, boolean attachToRoot):
Inflate a new view hierarchy from the specified xml resource.
im using this metod LINK for generated a recyclerview with GridLayoutManager to autospan columns but i fail to make a square dummy layout for holderview, My intention is to imitate a gallery using recyclerview,
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#color/white"
android:orientation="vertical"
android:padding="1dp">
<ImageView
android:id="#+id/txt"
android:layout_width="match_parent"
android:layout_height="100dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_gravity="center_horizontal|bottom"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<TextView
android:id="#+id/card_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ellipsize="end"
android:singleLine="true"
android:text="folder"
android:textSize="12dp"
android:textStyle="bold"/>
</FrameLayout>
Check out this answer, but basically you can create a custom SquareRelativeLayout and then override the onMeasure method so that it's always square like this:
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Set a square layout.
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}