Making a ViewStub Overlay an Activity - android

I'm trying to create a Login ViewStub that will overlay the Welcome Activity when customers select the login button. However, I'm running into a problem. My stub will open when clicking login, but the buttons on my Welcome Activity won't dim. Also, do I need to make my background view of my stub wrap content so it can cover the whole screen? Or should I use a different technique from a stub to accomplish what I’m trying to do? Thank you!
Screenshots of what I'm trying to accomplish
activity_welcome.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:background="#drawable/gradient_background"
tools:context=".Activities.Authentication.WelcomeActivity">
<ImageView
android:id="#+id/logoImageView"
android:layout_width="175dp"
android:layout_height="75dp"
android:layout_marginTop="60dp"
android:contentDescription="#string/logo_image"
android:scaleType="fitXY"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/skedaddle_services_" />
<TextView
android:id="#+id/findQualityServicesTextView"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
android:text="#string/finding_quality_service_providers_near_you_in_just_seconds"
android:textAlignment="center"
android:textColor="#color/white"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/logoImageView" />
<TextView
android:id="#+id/guestOrLogInTextView"
android:layout_width="314dp"
android:layout_height="38dp"
android:layout_marginTop="8dp"
android:text="#string/continue_as_a_guest_to_look_around_the_app_log_in_or_create_an_account_to_start_requesting_services"
android:textAlignment="center"
android:textColor="#color/white"
android:textSize="14sp"
android:textStyle="bold|italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/findQualityServicesTextView" />
<ViewStub
android:id="#+id/loginStub"
android:layout_width="360dp"
android:layout_height="620dp"
android:layout="#layout/viewstub_login"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/loginButton"
android:layout_width="0dp"
android:layout_height="48dp"
android:background="#drawable/first_login_rounded_button"
android:text="#string/log_in"
android:textAllCaps="false"
android:textColor="#color/white"
app:layout_constraintBottom_toTopOf="#+id/signUpButton"
app:layout_constraintEnd_toEndOf="#+id/signUpButton"
app:layout_constraintStart_toStartOf="#+id/signUpButton"
app:layout_constraintTop_toBottomOf="#+id/guestOrLogInTextView"
app:layout_constraintVertical_bias="0.13"
app:layout_constraintVertical_chainStyle="packed" />
<Button
android:id="#+id/signUpButton"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginTop="8dp"
android:background="#drawable/first_signup_rounded_button"
android:text="#string/sign_up"
android:textAllCaps="false"
android:textColor="#color/white"
app:layout_constraintBottom_toTopOf="#+id/continueAsGuestButton"
app:layout_constraintEnd_toEndOf="#+id/continueAsGuestButton"
app:layout_constraintStart_toStartOf="#+id/continueAsGuestButton"
app:layout_constraintTop_toBottomOf="#+id/loginButton" />
<Button
android:id="#+id/continueAsGuestButton"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="24dp"
android:background="#android:color/transparent"
android:text="#string/continue_as_guest"
android:textAllCaps="false"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/signUpButton"
tools:ignore="TextContrastCheck" />
</androidx.constraintlayout.widget.ConstraintLayout>
viewstub_login.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#BF090909">
</androidx.constraintlayout.widget.ConstraintLayout>
WelcomeActivty.kt
val loginButton = findViewById<Button>(R.id.loginButton)
loginButton.setOnClickListener {
val loginStub = findViewById<ViewStub>(R.id.loginStub)
loginStub.inflate()
}

Got it, I had to set the alpha of each button.
val loginButton = findViewById<Button>(R.id.loginButton)
val signUpButton = findViewById<Button>(R.id.signUpButton)
val guestButton = findViewById<Button>(R.id.continueAsGuestButton)
loginButton.setOnClickListener {
val loginStub = findViewById<ViewStub>(R.id.loginStub)
loginStub.inflate()
loginButton.alpha = 0.25f
loginButton.isEnabled = false
signUpButton.alpha = 0.25f
signUpButton.isEnabled = false
guestButton.alpha = 0.25f
guestButton.isEnabled = false
}

Related

Recyclerview Layout cant match parent

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!

Custom Android CardView Dialog Preview different

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())
}

Problem with layout constructing in Android

I have a layout for my recyclerview item:
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:orientation="vertical">
<ImageView
android:id="#+id/iv_hotel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_launcher_background" />
<TextView
android:id="#+id/tv_hotel_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/iv_hotel"
tools:text="Belleclaire Hotel" />
<include
android:id="#+id/include"
layout="#layout/include_stars_set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="#+id/tv_hotel_name"
app:layout_constraintTop_toBottomOf="#+id/tv_hotel_name" />
<TextView
android:id="#+id/tv_hotel_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="#+id/include"
app:layout_constraintTop_toBottomOf="#+id/include"
tools:text="250 West 77th Street, Manhattan" />
<TextView
android:id="#+id/tv_distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="#+id/tv_hotel_address"
app:layout_constraintTop_toBottomOf="#+id/tv_hotel_address"
tools:text="100.0" />
<TextView
android:id="#+id/tv_suites_available_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/label_suites_availability"
app:layout_constraintStart_toStartOf="#+id/tv_distance"
app:layout_constraintTop_toBottomOf="#+id/tv_distance" />
<TextView
android:id="#+id/tv_suites_available"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintStart_toEndOf="#+id/tv_suites_available_label"
app:layout_constraintTop_toTopOf="#+id/tv_suites_available_label" />
<ProgressBar
android:id="#+id/progress_hotel_image"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="#+id/iv_hotel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/iv_hotel" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
It seems like this:
enter image description here
However after building I have this view:
enter image description here
As you can see, I see only a half of my progressbar after I'm trying to dowload an image to my iv_hotel with a help of Glide.
How can I solve this problem? I'm a bit bad at xml guidelines understanding, so I'm asking for a help(
Use placeholder to show default image while a request is in progress
Glide.with(context)
.load(url)
.placeholder(R.drawable.placeholder)
.into(view);
if (!imageUrl.isNullOrEmpty()) {
imageView?.let { iv ->
Glide.with(iv.context)
.load(AppUtility.IMAGE_THUMB_URL.plus(imageUrl))
.apply(options)
.into(iv)
}
} else {
imageView?.setImageDrawable(error)
// set here your error img
}

Set View Gone but still take up space

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=

how to fix the alteration of visibility inside a recyclerview's viewholder layout

I have a recyclerview using the following xml as a list item for each of its views
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.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="match_parent"
android:layout_height="wrap_content"
app:cardElevation="2dp">
<android.support.constraint.ConstraintLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/item_question_name"
style="?android:attr/textAppearanceLarge"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/app_margin"
app:layout_constraintBottom_toTopOf="#+id/item_question_question"
app:layout_constraintEnd_toStartOf="#+id/item_question_needs_sync"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="This is the name" />
<android.support.v7.widget.AppCompatImageView android:id="#+id/item_question_needs_sync"
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="#+id/item_question_name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/item_question_name"
app:srcCompat="#drawable/ic_sync" />
<TextView android:id="#+id/item_question_question"
style="?android:attr/textAppearanceMedium"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/app_margin"
app:layout_constraintBottom_toTopOf="#+id/item_question_type"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#+id/item_question_name"
app:layout_constraintTop_toBottomOf="#+id/item_question_name"
tools:text="this is the question" />
<TextView android:id="#+id/item_question_type"
style="?android:attr/textAppearanceMedium"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/app_margin"
android:layout_marginTop="#dimen/app_margin"
app:layout_constraintBottom_toTopOf="#+id/item_question_separator"
app:layout_constraintEnd_toStartOf="#+id/item_question_info"
app:layout_constraintHorizontal_weight="5"
app:layout_constraintStart_toStartOf="#+id/item_question_question"
app:layout_constraintTop_toBottomOf="#+id/item_question_question"
tools:text="Four faces which are awesome" />
<TextView android:id="#+id/item_question_info"
style="?android:attr/textAppearanceSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="#+id/item_question_type"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_weight="3"
app:layout_constraintStart_toEndOf="#+id/item_question_type"
app:layout_constraintTop_toTopOf="#+id/item_question_type"
tools:text="answered ALL the questions" />
<View android:id="#+id/item_question_separator"
android:layout_width="0dp"
android:layout_height="2dp"
android:background="#color/colorAccent"
app:layout_constraintBottom_toTopOf="#+id/item_question_buttons"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/item_question_info" />
<android.support.constraint.ConstraintLayout android:id="#+id/item_question_buttons"
android:layout_width="match_parent"
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/item_question_separator">
<Button android:id="#+id/item_question_delete"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/item_question_button_delete"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/item_question_edit"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button android:id="#+id/item_question_edit"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/item_question_button_edit"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintBottom_toBottomOf="#+id/item_question_delete"
app:layout_constraintEnd_toStartOf="#+id/item_question_view"
app:layout_constraintStart_toEndOf="#+id/item_question_delete"
app:layout_constraintTop_toTopOf="#+id/item_question_delete" />
<Button android:id="#+id/item_question_view"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/item_question_button_view"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintBottom_toBottomOf="#+id/item_question_delete"
app:layout_constraintEnd_toStartOf="#+id/item_question_start"
app:layout_constraintStart_toEndOf="#+id/item_question_edit"
app:layout_constraintTop_toTopOf="#+id/item_question_delete" />
<Button android:id="#+id/item_question_start"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/item_question_button_start"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintBottom_toBottomOf="#+id/item_question_delete"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/item_question_view"
app:layout_constraintTop_toTopOf="#+id/item_question_delete" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
my problem is that when the user clicks on the view I have the viewholder toggling whether the #+id/item_question_buttons will be shown or not like so:
void bind_view(Info info, boolean selected) {
//load the data from info
.....
// _buttons is a View object where I stored the constraint layout with the buttons
if (selected) {
_buttons.setVisibility(VISIBLE);
} else {
_buttons.setVisibility(GONE);
}
}
my problem is that every other time (i.e. the 2nd 4th 6th etc) the user toggles the visibility of the _buttons View the view expands to triple the normal size (which is the same as the max height a button can have).
I solved it by using android:maxLines="1" but I was wandering , why does this happen? and is there a better way to fix this?
thanks in advance for any help you can provide

Categories

Resources