I have recently came across to this video Keyframe Animations with ConstraintLayout and ConstraintSet , it seems pretty cool . so Implemented this way following the steps showed in a blog post . There is just one difference though between the blog and my use case . I am working with fragment . I thought theoretically implementation supposed to be as it is . Here is my work around for keyframe animation on two constrainSet()
ConstrainLayout initial (point A so to speak)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_root">
<ImageView
android:id="#+id/imageView"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:src="#android:drawable/ic_menu_camera"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#+id/textView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.687" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:gravity="center_vertical"
android:padding="10dp"
android:text="#string/missing_permission"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView"
app:layout_constraintVertical_bias="0.476" />
<Button
android:id="#+id/setting_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="76dp"
android:text="#string/settings"
app:layout_constraintBottom_toTopOf="#+id/imageView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="#+id/imageView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.511" />
</android.support.constraint.ConstraintLayout>
ConstrainLayout initial (point B the destination , the final look of the layout)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="#+id/fragment_root">
<ImageView
android:id="#+id/imageView"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="136dp"
android:src="#android:drawable/ic_menu_camera"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:gravity="center_vertical"
android:padding="10dp"
android:text="#string/missing_permission"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="#+id/imageView"
app:layout_constraintEnd_toEndOf="#+id/imageView"
app:layout_constraintStart_toStartOf="#+id/imageView"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintVertical_bias="0.858" />
<Button
android:id="#+id/setting_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="164dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="#string/settings"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/imageView"
app:layout_constraintStart_toStartOf="#+id/imageView" />
</android.support.constraint.ConstraintLayout>
Here is my fragment with keyframe transition code
var goToSettings: () -> Unit = {}
private lateinit var root: ConstraintLayout
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
= inflater.inflate(R.layout.nocamera_permission_fragment_alt, container, false).also {
root = it.findViewById(R.id.fragment_root)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) =
super.onViewCreated(view, savedInstanceState).run {
setting_button.setOnClickListener { goToSettings() }
val constraintSet = ConstraintSet()
constraintSet.clone(activity, R.layout.nocamera_permission_fragment)
val transition = ChangeBounds().apply {
interpolator = OvershootInterpolator()
duration = 1000
}
TransitionManager.beginDelayedTransition(root, transition)
constraintSet.applyTo(root)
}
I has no effect on the layout , app renders the final layout R.layout.nocamera_permission_fragment after run . Does anybody have any idea ?
You are applying transition too soon (View is not even attached to window yet and had no layout pass), so delayed transition is discarded. Try using post instead of run.
Related
Im trying to make 2 recyclerview. And 2 of it using the same layout with Match Parent width. But the first Recyclerview display it properly but the second one doesnt.
My first Recyclerview 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="wrap_content"
android:background="#color/white"
android:gravity="center_vertical"
android:padding="10dp">
<ImageView
android:id="#+id/ivProduct"
android:layout_width="95dp"
android:layout_height="95dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription"
tools:src="#drawable/ic_image" />
<TextView
android:id="#+id/tvTitleProduct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:fontFamily="#font/sen_bold"
android:textColor="#color/black"
android:textSize="15sp"
app:layout_constraintStart_toEndOf="#id/ivProduct"
app:layout_constraintTop_toTopOf="parent"
tools:text="Microsoft Office 2021 Pro Plus" />
<TextView
android:id="#+id/tvPriceProduct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="11dp"
android:layout_marginTop="8dp"
android:fontFamily="#font/sen"
android:textColor="#color/black"
app:layout_constraintStart_toEndOf="#id/ivProduct"
app:layout_constraintTop_toBottomOf="#id/tvTitleProduct"
tools:text="Rp. 35.000,00" />
<com.google.android.material.button.MaterialButton
android:id="#+id/btnEditDisplayProduct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="8dp"
android:backgroundTint="#color/second_color"
android:fontFamily="sans-serif"
android:text="Edit"
android:textColor="#color/white"
android:textSize="12sp"
android:textStyle="bold"
app:cornerRadius="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#id/ivProduct"
app:layout_constraintTop_toBottomOf="#id/tvPriceProduct"
tools:ignore="HardcodedText" />
<com.google.android.material.button.MaterialButton
android:id="#+id/btnPreviewDisplayProduct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:backgroundTint="#color/second_color"
android:fontFamily="sans-serif"
android:text="Preview"
android:textColor="#color/white"
android:textSize="11sp"
android:textStyle="bold"
app:cornerRadius="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#id/btnEditDisplayProduct"
app:layout_constraintTop_toBottomOf="#id/tvPriceProduct"
tools:ignore="HardcodedText" />
</androidx.constraintlayout.widget.ConstraintLayout>
And here is my second 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="wrap_content"
android:padding="10dp"
android:layout_marginTop="10dp"
android:background="#color/white">
<TextView
android:id="#+id/tvOwner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:fontFamily="#font/roboto"
android:textColor="#color/black"
android:textSize="14sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Buyyer\t: " />
<TextView
android:id="#+id/tvDevice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
android:fontFamily="#font/roboto"
android:textColor="#color/black"
android:textSize="14sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/tvOwner"
tools:text="Device\t: " />
<TextView
android:id="#+id/tvType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
android:fontFamily="#font/roboto"
android:textColor="#color/black"
android:textSize="14sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/tvDevice"
tools:text="Tipe\t\t\t: " />
<TextView
app:layout_constraintHorizontal_weight="1"
android:id="#+id/tvToken"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
android:fontFamily="#font/roboto"
android:textColor="#color/black"
android:textSize="14sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/tvType"
tools:text="Token\t\t:" />
</androidx.constraintlayout.widget.ConstraintLayout>
And here is my oncreateviewholder code :
class ViewHolder(val binding : LicenseRecyclerviewAdapterBinding) :
RecyclerView.ViewHolder(binding.root)
override fun onCreateViewHolder(parent : ViewGroup, viewType : Int) : ViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val binding = LicenseRecyclerviewAdapterBinding.inflate(layoutInflater)
return ViewHolder(binding)
}
I tried to use dummyview but it still doesnt work
My fragment code :
override fun onViewCreated(view : View, savedInstanceState : Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding = FragmentDaftarLisensiBinding.bind(view)
val listLicense = mutableListOf<License>()
val adapter = LicenseListAdapter(listLicense)
binding.rvLicense.adapter = adapter
binding.rvLicense.layoutManager = LinearLayoutManager(requireContext())
binding.rvLicense.setHasFixedSize(true)
viewModel.getLicenseData().observe(viewLifecycleOwner, Observer {
it.forEach { license ->
listLicense.add(license)
adapter.notifyItemInserted(listLicense.size - 1)
}
})
}
If you're talking about how the widths of the ViewHolders are wrong, this:
LicenseRecyclerviewAdapterBinding.inflate(layoutInflater)
needs to be this:
LicenseRecyclerviewAdapterBinding.inflate(layoutInflater, parent, false)
You need to pass the parent View in so it do stuff like work out what match_parent means in terms of size - it needs a parent to match to when you're inflating it! The false just means it doesn't actually attach it to the parent.
This is a pretty standard call when you're inflating layouts, like when you inflate a Fragment's view in onCreateView. It's pretty rare that you don't pass the parent/container so it can work out sizing and stuff!
I want to display a custom dialog using a cardView to display some information. The preview looks exactly like it should and is previewed in Android Studio the following:
The quick_preview.xml xml for this is
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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/SensorOverView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:minWidth="300dp"
android:minHeight="300dp"
android:visibility="visible"
app:cardCornerRadius="20dp"
app:cardElevation="10dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="160dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/scrollView2">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/subTitleLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toTopOf="#+id/titleLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="#+id/btnClose"
android:layout_width="72dp"
android:layout_height="24dp"
android:layout_weight="1"
android:background="#color/background_light_elevation_1"
android:text="Button"
android:textColor="#78909C"
app:icon="#drawable/ic_baseline_close_24"
app:iconTint="#color/app_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/txtSubtitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_weight="1"
android:text="Lorem Ipsum"
android:textSize="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/btnClose"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/titleLayout"
android:layout_width="0dp"
android:layout_height="45dp"
app:layout_constraintBottom_toTopOf="#+id/viewContent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/subTitleLayout">
<TextView
android:id="#+id/txtTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_weight="1"
android:text="Lorem ipsum"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/rejectOrAddLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/viewContent">
<Button
android:id="#+id/btnReject"
android:layout_width="72dp"
android:layout_height="56dp"
android:background="#FF5454"
android:text="Button"
app:icon="#drawable/ic_baseline_close_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/btnAdd"
app:layout_constraintStart_toEndOf="#+id/txtInfo"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btnAdd"
android:layout_width="72dp"
android:layout_height="56dp"
android:layout_weight="1"
android:background="#00D7A0"
android:text="Button"
app:icon="#drawable/ic_baseline_check_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/btnReject"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/txtInfo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_weight="1"
android:maxHeight="56dp"
android:text="Select to delete/add"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/btnReject"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/viewContent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toTopOf="#+id/rejectOrAddLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/titleLayout"
tools:listitem="#layout/sensor_content_item" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
The dialog uses a custom class QuickPreviewDialog.kt
class QuickPreviewDialog(context: Context) : Dialog(context) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
this.requestWindowFeature(Window.FEATURE_NO_TITLE)
this.setCancelable(false);
this.setContentView(R.layout.sensor_quick_preview);
this.getWindow()?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
}
}
and is invoked by:
val dialog = QuickPreviewDialog(this)
dialog.show()
But the result is the following and I don't know what's wrong:
To achieve what you want without changing too much code,a simple solution i would suggest you to use is putting a LinearLayout as your cardView's parent
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
<androidx.cardview.widget.CardView
android:id="#+id/SensorOverView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:minWidth="300dp"
android:minHeight="300dp"
android:visibility="visible"
app:cardCornerRadius="20dp"
app:cardElevation="10dp">
//Rest of your view
</androidx.cardview.widget.CardView>
</LinearLayout>
And just like that you see your dialog's size as same as you want it to be
Android dialogs tend to have their own ideas about how large they should be. We will address the dialog size, but first you will need to change how the minimum height and width is specified in the CardView.
Change
android:minWidth="300dp"
android:minHeight="300dp"
to
app:minWidth="300dp"
app:minHeight="300dp"
If you just make this change, you should see your entire layout crammed into the small dialog window. This is the correct way to specify minimum sizes for CardViews.
You will have to programmatically adjust the dialog window's size by doing something like the following to override the defaults:
val padding = 160
val dialog = QuickPreviewDialog(this)
dialog.show()
// Set the window's size only after dialog.show()
val widthHeight = getScreenSize()
dialog.window?.setLayout(widthHeight.first - padding, widthHeight.second - padding)
private fun getScreenSize() =
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
Pair(
displayMetrics.widthPixels,
displayMetrics.heightPixels
)
} else {
val bounds = windowManager.currentWindowMetrics.bounds
Pair(bounds.width(), bounds.height())
}
The items in my RecyclerView Grid are stretched automatically if the left item is shorter and the right item is taller or vice versa (2 grid spans) because I want to use layout_height="wrap_content". How to keep that left item from being stretched automatically? I still want to use the GridLayout style because in my case it is not suitable if using StaggeredGridLayout (because it is more suitable for photo galleries).
<androidx.coordinatorlayout.widget.CoordinatorLayout
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="#color/very_light_gray"
android:paddingBottom="61dp"
tools:context="Fragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
...
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_explore_buyer_outlet"
android:layout_width="0dp"
android:layout_height="0dp"
android:clipToPadding="false"
android:overScrollMode="ifContentScrolls"
android:paddingEnd="#dimen/_20sdp"
android:paddingBottom="28dp"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:spanCount="2"
tools:ignore="RtlSymmetry"
tools:itemCount="18"
tools:listitem="#layout/item_grid" />
</androidx.constraintlayout.widget.ConstraintLayout
</androidx.coordinatorlayout.widget.CoordinatorLayout>
item_grid.xml
<androidx.cardview.widget.CardView
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="177dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/_14sdp"
android:layout_marginTop="#dimen/_14sdp"
android:layout_marginEnd="#dimen/_minus18sdp"
android:layout_marginBottom="-7dp"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="14dp"
app:cardElevation="1dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/iv_item_outlet_buyer_photo"
android:layout_width="match_parent"
android:layout_height="112dp"
android:importantForAccessibility="no"
android:scaleType="centerCrop"
android:src="#drawable/outlet10"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tv_item_outlet_buyer_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="9dp"
android:background="#drawable/outlet_buyer_status_open_label"
android:fontFamily="#font/notosansjp_medium"
android:gravity="center"
android:includeFontPadding="false"
android:paddingHorizontal="8dp"
android:paddingTop="3dp"
android:paddingBottom="3.75dp"
android:textColor="#color/white"
android:textSize="10sp"
app:layout_constraintBottom_toBottomOf="#+id/iv_item_outlet_buyer_photo"
app:layout_constraintEnd_toEndOf="parent"
tools:ignore="SmallSp"
tools:text="Open" />
<TextView
android:id="#+id/tv_item_outlet_buyer_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10.5dp"
android:fontFamily="#font/notosansjp_bold"
android:includeFontPadding="false"
android:lineSpacingExtra="1.5dp"
android:paddingStart="15dp"
android:paddingEnd="#dimen/_17sdp"
android:paddingBottom="2dp"
android:textColor="#color/dark_gray"
android:textSize="#dimen/_10ssp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/iv_item_outlet_buyer_photo"
tools:text="Guttenburg, Alazka" />
<ImageView
android:id="#+id/iv_item_outlet_buyer_dotted_line"
android:layout_width="0dp"
android:layout_height="5dp"
android:layout_marginTop="5dp"
android:importantForAccessibility="no"
android:layerType="software"
android:paddingStart="16dp"
android:paddingEnd="15dp"
android:src="#drawable/dotted_shape"
app:layout_constraintEnd_toEndOf="#+id/tv_item_outlet_buyer_name"
app:layout_constraintStart_toStartOf="#+id/tv_item_outlet_buyer_name"
app:layout_constraintTop_toBottomOf="#+id/tv_item_outlet_buyer_name" />
<TextView
android:id="#+id/tv_item_outlet_buyer_category"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="4.5dp"
android:layout_marginBottom="14dp"
android:fontFamily="#font/notosansjp_regular"
android:includeFontPadding="false"
android:lineSpacingExtra="1.5dp"
android:paddingHorizontal="15dp"
android:textColor="#color/dark_gray"
android:textSize="11sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/iv_item_outlet_buyer_dotted_line"
tools:ignore="SmallSp"
tools:text="Food ∙ Drink ∙ Snack" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
I want make my GridLayout like this (edited in Figma):
But I got like this if last textView have constraintBottom_toBottomOf(parent):
And, I got like this if last textView not have constraintBottom_toBottomOf(parent):
What should I do?
You just need to make sure your TextView's layout_height is wrap_content, and give that the black background. The containing layout should be transparent, so when it resizes to fit the grid, the extra space is just "empty". The TextView shouldn't resize with it (and neither should anything else in the item's layout), so the black area stays the same - the extra height is unused.
Here's a quick example
MainFragment.kt
class MainFragment : Fragment(R.layout.fragment_main) {
lateinit var binding: FragmentMainBinding
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// fragment_main.xml has a RecyclerView in it with "recycler" for its ID
binding = FragmentMainBinding.bind(view)
with(binding) {
recycler.layoutManager = GridLayoutManager(requireContext(), 2)
recycler.adapter = Adapterino(requireContext())
}
}
private class Adapterino(val context: Context) : RecyclerView.Adapter<Adapterino.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
ItemViewBinding.inflate(LayoutInflater.from(context)).run(::ViewHolder)
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
// just generating random amounts of text to get some variation
holder.binding.textView.text =
generateSequence { "cats" }.take(Random.nextInt(5, 20)).joinToString(" ")
}
override fun getItemCount(): Int = 20
private class ViewHolder(val binding: ItemViewBinding) : RecyclerView.ViewHolder(binding.root)
}
}
item_view.xml
<?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="wrap_content"
android:orientation="vertical"
android:padding="8dp"
>
<View
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#color/black"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="240dp"
android:src="#mipmap/ic_launcher"
/>
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black"
android:textColor="#color/white"
android:textSize="20sp"
/>
</LinearLayout>
I'm currently developing an Android app in Kotlin in which I use a BottomSheetDialog Fragment. Whenever the dialog pops up, the rest of the screen is dimmed. Can I somehow disable this? I don't want to click the screen behind the fragment, I just want it to show up undimmed.
Thanks in advance:
XMl of the Fragment:
<?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:background="#color/dark_green"
>
<TextView
android:id="#+id/Title"
android:layout_width="304dp"
android:layout_height="50dp"
android:layout_marginStart="50dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="50dp"
android:layout_marginBottom="20dp"
android:lineSpacingExtra="8sp"
android:textAlignment="center"
android:textColor="#CCDEEE"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.571"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/Snippet"
android:layout_width="304dp"
android:layout_height="50dp"
android:layout_marginStart="50dp"
android:layout_marginTop="90dp"
android:layout_marginEnd="50dp"
android:layout_marginBottom="20dp"
android:lineSpacingExtra="8sp"
android:textAlignment="center"
android:textColor="#CCDEEE"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.571"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/Position"
android:layout_width="304dp"
android:layout_height="70dp"
android:layout_marginStart="50dp"
android:layout_marginTop="160dp"
android:layout_marginEnd="50dp"
android:layout_marginBottom="60dp"
android:lineSpacingExtra="8sp"
android:textAlignment="center"
android:textColor="#CCDEEE"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.571"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here is a picture of the app so you can see what I mean:
https://imgur.com/u6MYYKW
I suppose you are using Android Jetpack's Navigation Component, in case you aren't, maybe you should consider using it. You can take a look at this excellent video by Chet Haase of the Android Developers Team where it is explained.
Now, going to the specifics of your question, if you followed the steps in the video, you can use any of the two options that I indicate below in the code:
package com.example.myapp
import ...
class MyBottomSheetDialog : BottomSheetDialogFragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
//Option 1:
dialog?.window?.setDimAmount(0F)
//Option 2:
dialog?.window?.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
return inflater.inflate(R.layout.my_dialog, container, false)
}
}
Here is my Code that is used in RecyleViewAdapater!
val yearOfMemory = SimpleDateFormat("yyyy").format(memory.timeOfMemory)
// if year not show before, show it.
if (!yearSet.contains(yearOfMemory)) {
holder.yearOfMemories.visibility = View.VISIBLE
holder.yearOfMemories.text = yearOfMemory
yearSet.add(yearOfMemory)
}
Here is my ReccleView XML.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:layout_editor_absoluteY="81dp">
<TextView
android:id="#+id/month_and_day_of_memory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/content_of_memory"
app:layout_constraintHorizontal_bias="0.8"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/content_of_memory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/year_of_group_memory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:textSize="36sp"
android:visibility="gone"
app:layout_constraintBottom_toTopOf="#+id/month_and_day_of_memory"
app:layout_constraintEnd_toStartOf="#+id/month_and_day_of_memory"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Like this, I already have set TextView(#+id/year_of_group_memory) Gone, but it still takes up space at the second item,and it don't take up at the third item.
I have tried all kinds. So, how can I fix it.?
I turn on the layout bounds.
So,how can cut off the below height,now?
Is your code in a RecyclerView adapter? If so, you must explicitly hide it due to the view recycling mechanism. Always have explicit if-else statements when hiding/showing views.
val yearOfMemory = SimpleDateFormat("yyyy").format(memory.timeOfMemory)
if (!yearSet.contains(yearOfMemory)) {
holder.yearOfMemories.visibility = View.VISIBLE
holder.yearOfMemories.text = yearOfMemory
yearSet.add(yearOfMemory)
} else {
holder.yearOfMemories.visibility = View.GONE
}
Also try replacing your viewholder contents with the following xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/year_of_group_memory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:textSize="36sp"
android:lines="2"
tools:text="2017"
/>
<TextView
android:id="#+id/month_and_day_of_memory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/year_of_group_memory"
android:layout_alignBottom="#id/year_of_group_memory"
android:layout_margin="8dp"
android:textSize="18sp"
tools:text="03-11"
/>
<TextView
android:id="#+id/content_of_memory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/month_and_day_of_memory"
android:layout_alignTop="#id/month_and_day_of_memory"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:textSize="18sp"
tools:text="第一次接吻"
/>
</RelativeLayout>
If you use this, you have to set the height of yearOfMemories to 0 or wrap_content accordingly.
Try this:
<TextView
android:id="#+id/year_of_group_memory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
you hide this TextView but youre putting android:textSize="36sp" that it shouldn't have and other margins and app:layout_constraintBottom_toTopOf=