Remove Text from the edit text when edit text is focused - android

I want to remove the text when the edit text is focused programmatically, when i try to do that the fragment class throws null pointer exception.
The design which i am using is Material Text Input Layout, when i set the hint in the text input layout, the hint goes upward in the box which is around the TextInputLayout and i don't want that. i just want the box,
So i set the text as an hint in the EditTextField in the XML file and i want to remove the text when user click in the EditText or when the EditText is focused.
Here is the Screen Recorded for better understanding of the issue
https://www.youtube.com/watch?v=6EgBymObb4A
Here is the layout XML File
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".screens.fragments.login_fragment"
android:background="#color/black"
android:focusable="true"
android:focusableInTouchMode="true">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<TextView
android:id="#+id/txt_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textAllCaps="false"
android:fontFamily="#font/raleway_semi_bold"
android:textColor="#color/white"
android:textSize="32sp"
android:letterSpacing="0.1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toTopOf="#id/txt_login_des"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="#+id/txt_login_des"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Your Credintials to get access"
android:textColor="#9CFFFFFF"
android:fontFamily="#font/raleway_medium"
android:textSize="12sp"
app:layout_constraintTop_toBottomOf="#id/txt_login"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="15dp"
android:layout_marginStart="20dp"/>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/user_id_feild"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginTop="30dp"
app:boxCornerRadiusBottomEnd="8dp"
app:boxCornerRadiusBottomStart="8dp"
app:boxCornerRadiusTopEnd="8dp"
app:boxCornerRadiusTopStart="8dp"
app:boxStrokeColor="#color/white"
app:boxStrokeErrorColor="#FF0000"
app:boxStrokeWidth="1dp"
android:layout_marginBottom="20dp"
app:layout_constraintTop_toBottomOf="#id/txt_login_des"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="#id/password_feild">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/LoginUserIDField"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusedByDefault="false"
android:text="User ID"
android:textColor="#74FFFFFF"
android:drawableStart="#drawable/custom_user_edit_txt_input"
android:drawablePadding="10dp"
android:padding="10dp"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/password_feild"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="40dp"
app:boxCornerRadiusBottomEnd="8dp"
app:boxCornerRadiusBottomStart="8dp"
app:boxCornerRadiusTopEnd="8dp"
app:boxCornerRadiusTopStart="8dp"
app:boxStrokeColor="#color/white"
app:boxStrokeErrorColor="#FF0000"
app:boxStrokeWidth="1dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="#id/btn_signIn">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/UserLoginPassword"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="•••••••••••••"
android:drawableStart="#drawable/custom_pass_txt_input"
android:drawablePadding="10dp"
android:textColor="#74FFFFFF"
android:padding="10dp"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:id="#+id/btn_signIn"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_marginBottom="10dp"
android:backgroundTint="#color/white"
android:text="Sign In"
android:textAllCaps="false"
android:textColor="#color/black"
app:rippleColor="#color/black"
app:strokeColor="#color/white"
app:strokeWidth="1dp"
app:layout_constraintBottom_toTopOf="#id/btn_register"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<com.google.android.material.button.MaterialButton
android:id="#+id/btn_register"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_marginBottom="50dp"
android:text="Register"
android:textAllCaps="false"
android:textColor="#color/white"
app:rippleColor="#color/black"
app:strokeColor="#color/white"
app:strokeWidth="1dp"
app:layout_constraintBottom_toTopOf="#id/bottom_fab_btns_lyt"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<LinearLayout
android:id="#+id/bottom_fab_btns_lyt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent">
<com.google.android.material.button.MaterialButton
android:id="#+id/btn_facebook"
style="#style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
android:layout_width="50dp"
android:layout_height="50dp"
app:strokeWidth="1dp"
app:strokeColor="#color/white"
app:icon="#drawable/icons8_facebook"
app:iconTintMode="add"
app:iconSize="24dp"
app:iconTint="#000000"
app:iconPadding="0dp"
android:background="#drawable/fab_background"
android:backgroundTintMode="add"
app:backgroundTintMode="add"
app:rippleColor="#color/white"/>
<com.google.android.material.button.MaterialButton
android:id="#+id/btn_google"
style="#style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
android:layout_width="50dp"
android:layout_height="50dp"
app:strokeWidth="1dp"
app:strokeColor="#color/white"
app:icon="#drawable/google_cute_icon"
app:iconTintMode="add"
app:iconSize="24dp"
app:iconTint="#000000"
app:iconPadding="0dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:background="#drawable/fab_background"
android:backgroundTintMode="add"
app:backgroundTintMode="add"
app:rippleColor="#color/white"/>
<com.google.android.material.button.MaterialButton
android:id="#+id/btn_twitter"
style="#style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
android:layout_width="50dp"
android:layout_height="50dp"
app:strokeWidth="1dp"
app:strokeColor="#color/white"
app:icon="#drawable/google_cute_icon"
app:iconTintMode="add"
app:iconSize="24dp"
app:iconTint="#000000"
app:iconPadding="0dp"
android:background="#drawable/fab_background"
android:backgroundTintMode="add"
app:backgroundTintMode="add"
app:rippleColor="#color/white"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Fragment Class
package com.ak_applications.kottry.screens.fragments
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.AnimatedVectorDrawable
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import androidx.fragment.app.Fragment
import android.view.View
import android.view.ViewGroup
import com.ak_applications.kottry.R
import com.ak_applications.kottry.databinding.FragmentLoginFragmentBinding
class login_fragment : Fragment(R.layout.fragment_login_fragment) {
private var _binding: FragmentLoginFragmentBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentLoginFragmentBinding.inflate(inflater, container, false)
val view = binding.root
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
override fun onDestroy() {
super.onDestroy()
_binding = null
}
}
I tried
if(binding.LoginUserFeild.isFocused)
{
binding.LoginUserFeild.setText("")
binding.LoginUserFeild.setTextColor(Color.parseColor("#ffffff"))
}
also
if(binding.LoginUserFeild.isFocused)
{
binding.userLoginfeild.setHint("") // which is TextInputLayout.
}
also I have used the scroll layout as parent layout for the fragment but as you can see in the screen recording when user click on the EditText Feild the layout goes to top but user scroll down while the EditText selected the layout won't go all the way to up, layout missing somthing i don't know.
I am new to android please help me here.

binding.LoginUserFeild.setOnFocusChangeListener { _, hasFocus ->
if (hasFocus) {
binding.LoginUserFeild.setText("")
}
}

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!

Need help working out the logic in the code: Kotlin

When you enter some value in the "product price" field and select one of the packages (after clicking on the calculate button), the amount indicated in the packages themselves should be added, and the result of this should be displayed in the TextView, when you click on the "round" button, the resulting value should be rounded ... I'm lost and don't know how to implement this, I tried and searched for information on all kinds of sites, but there is very little information, maybe knowledgeable people will :(
program
Kotlin code:
package com.example.myapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.RadioButton
import android.widget.TextView
class MainActivity : AppCompatActivity() {
private lateinit var radioButton: RadioButton
private lateinit var radioButton2: RadioButton
private lateinit var radioButton3: RadioButton
private lateinit var textView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
radioButton = findViewById(R.id.radioButton)
radioButton2 = findViewById(R.id.radioButton2)
radioButton3 = findViewById(R.id.radioButton3)
}
fun SetOnClick(view: View){
}
}
Xml code :
<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"
tools:context=".MainActivity">
<Button
android:id="#+id/button"
android:layout_width="204dp"
android:layout_height="61dp"
android:layout_marginStart="149dp"
android:layout_marginEnd="168dp"
android:layout_marginBottom="228dp"
android:text="Рассчитать"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Switch
android:id="#+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="298dp"
android:layout_marginEnd="25dp"
android:layout_marginBottom="315dp"
android:minHeight="48dp"
android:text="Округлить"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textInputLayout"
android:layout_width="409dp"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:layout_marginTop="63dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Цена продукта" />
</com.google.android.material.textfield.TextInputLayout>
<RadioButton
android:id="#+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="35dp"
android:layout_marginTop="83dp"
android:layout_marginEnd="188dp"
android:text="Начальный пакет (+5%)"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textInputLayout" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="35dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="165dp"
android:text="Продвинутый пакет (+10%)"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/radioButton" />
<RadioButton
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="35dp"
android:layout_marginTop="6dp"
android:layout_marginEnd="196dp"
android:text="Элитный пакет (+25%)"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/radioButton2" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="157dp"
android:layout_marginTop="68dp"
android:layout_marginEnd="196dp"
android:layout_marginBottom="141dp"
android:text="TextView"
android:textSize="60sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.395"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button"
app:layout_constraintVertical_bias="0.475" />
</androidx.constraintlayout.widget.ConstraintLayout>
You are using RadioButton without any RadioGroup which is the reason why you are able to select multiple RadioButton at a time. Check this documentation and this tutorial on how to work with radio buttons.
Regarding your Business Logic on how rounding will happen you can use the MathsUtil class to perform your rounding off operation.

Android: Dialog box is too narrow

So whenever I click a button in order to show my dialog box, it displays the dialog box like this, its too narrow.Sorry new account image of un-intended results here.please help me fix this, I want it to show like in the designer here ( again sorry for external links).
Here is the xml code for the dialog fragment:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<TextView
android:layout_width="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="20dp"
tools:text="Enter new member details"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="#+id/instructions_text"
android:layout_height="wrap_content"/>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:id="#+id/name_layout"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="20dp"
android:hint="#string/name"
app:layout_constraintTop_toBottomOf="#+id/instructions_text"
app:layout_constraintEnd_toEndOf="parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:id="#+id/name"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:id="#+id/surname_layout"
app:layout_constraintStart_toStartOf="parent"
android:hint="#string/surname"
android:layout_marginTop="20dp"
app:layout_constraintTop_toBottomOf="#+id/name_layout"
app:layout_constraintEnd_toEndOf="parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:id="#+id/surname"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:id="#+id/dob_layout"
app:layout_constraintStart_toStartOf="parent"
android:hint="#string/birth_date"
android:layout_marginTop="20dp"
app:layout_constraintTop_toBottomOf="#+id/surname_layout"
app:layout_constraintEnd_toEndOf="parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:id="#+id/date_of_birth"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
<Button
android:layout_width="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
android:id="#+id/add_button"
android:layout_marginTop="10dp"
android:text="#string/add"
app:layout_constraintTop_toBottomOf="#id/dob_layout"
android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
app:layout_constraintStart_toStartOf="parent"
android:id="#+id/cancel_button"
android:layout_marginTop="10dp"
android:text="#string/cancel"
app:layout_constraintTop_toBottomOf="#id/dob_layout"
android:layout_height="wrap_content"/>
</androidx.constraintlayout.widget.ConstraintLayout>
I found a somewhat similar post from 8 years ago, a "solution was to set min-width and min-height , but I don't want to hard-code values in for such kind of an issue, I did try that and it worked but it looked weird on another device I tested. Isn't there a more official or better solution to that? Thanks for any input. Oh also here is my code for the dialog box inflator:
class NewMemberDialogue: DialogFragment() {
private lateinit var binding: DialogLayoutBinding
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = DialogLayoutBinding.inflate(layoutInflater,container,false)
binding.cancelButton.setOnClickListener { dismiss() }
return binding.root
}
}
"Dialogs are sized by their content. You can add padding and margins on the outer layout to consume more space, but that will not redistribute the views inside."
Custom dialog too small

Fragment Not Showing in the Activity

I have tried everything but i am not able to find the bug in the code what was it, i want to show the fragment in the activity but the fragment is not showing, What i have tried is
followed instruction as per https://developer.android.com/guide/fragments/create#kotlin, result = not worked.
Watched and followed 6 to 10 youtube tutorials, result = not worked.
tried to find the solution on the google, result = tried and checked everything as per every single article but not worked.
Tried different layouts in the XML layout file like as Fragment, FrameLayout, <androidx.fragment.app.FragmentContainerView>
i was learning the java for android dvelopment but when i know that the primary language for android development is kotlin then i just move to the kotlin.
So i am very new to kotlin please tell me what is the problem why the fragment is not working.
MainActivity
package com.ak_applications.kottry.screens
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import com.ak_applications.kottry.R
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btnGo: Button = findViewById(R.id.btn_go)
btnGo.setOnClickListener {
startActivity(Intent(this, login_signup::class.java))
}
}
}
MainActiviy.Layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".screens.MainActivity"
android:background="#drawable/blue_and_purple">
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:id="#+id/btn_go"
android:layout_width="220dp"
android:layout_height="60dp"
app:strokeColor="#color/white"
app:strokeWidth="1dp"
android:text="GO"
android:textColor="#color/white"
android:textSize="24sp"
app:elevation="34dp"
app:rippleColor="#color/white"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Class in which i want to show the fragment.
Layout
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<FrameLayout
android:id="#+id/frag_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"/>
</LinearLayout>
Class
package com.ak_applications.kottry.screens
import android.os.Bundle
import android.os.PersistableBundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.commit
import com.ak_applications.kottry.R
import com.ak_applications.kottry.screens.fragments.login_fragment
class login_signup: AppCompatActivity(R.layout.login_signup_layout) {
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
super.onCreate(savedInstanceState, persistentState)
if (savedInstanceState == null) {
supportFragmentManager.commit {
setReorderingAllowed(true)
add(R.id.frag_container, login_fragment())
}
}
}
}
The Fragment that i want to show
Fragment Layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".screens.fragments.login_fragment"
android:background="#color/black">
<TextView
android:id="#+id/txt_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textAllCaps="false"
android:fontFamily="#font/raleway_semi_bold"
android:textColor="#color/white"
android:textSize="32sp"
android:letterSpacing="0.1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="#+id/txt_login_des"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Your Credintials to get access"
android:textColor="#9CFFFFFF"
android:fontFamily="#font/raleway_medium"
android:textSize="18sp"
app:layout_constraintTop_toBottomOf="#id/txt_login"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="15dp"
android:layout_marginStart="20dp"/>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/user_id_feild"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginTop="100dp"
app:boxStrokeColor="#color/white"
app:boxStrokeErrorColor="#FF0000"
app:boxStrokeWidth="1dp"
app:boxCornerRadiusTopStart="8dp"
app:boxCornerRadiusBottomEnd="8dp"
app:boxCornerRadiusBottomStart="8dp"
app:boxCornerRadiusTopEnd="8dp"
app:startIconDrawable="#drawable/user_active_state"
app:layout_constraintTop_toBottomOf="#id/txt_login_des"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="#id/password_feild"
app:layout_constraintVertical_bias="0">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Email or User name or Phone Number"
android:textColorHint="#74FFFFFF"
android:padding="10dp"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/password_feild"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginTop="30dp"
app:boxStrokeColor="#color/white"
app:boxStrokeErrorColor="#FF0000"
app:boxStrokeWidth="1dp"
app:boxCornerRadiusTopStart="8dp"
app:boxCornerRadiusBottomEnd="8dp"
app:boxCornerRadiusBottomStart="8dp"
app:boxCornerRadiusTopEnd="8dp"
app:startIconDrawable="#drawable/lock_unlock_anime"
app:layout_constraintTop_toBottomOf="#id/user_id_feild"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toTopOf="#id/btn_signIn"
app:layout_constraintVertical_bias="1">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="•••••••••••••"
android:textColorHint="#74FFFFFF"
android:padding="10dp"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:id="#+id/btn_signIn"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="250dp"
android:layout_height="60dp"
android:text="Sign In"
android:backgroundTint="#color/white"
android:textAllCaps="false"
android:textColor="#color/black"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#id/password_feild"
app:layout_constraintBottom_toTopOf="#id/fbtn_google"
app:strokeColor="#color/white"
app:strokeWidth="1dp"
app:rippleColor="#color/black"/>
<com.google.android.material.button.MaterialButton
android:id="#+id/btn_register"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="250dp"
android:layout_height="60dp"
android:text="Register"
android:textAllCaps="false"
android:textColor="#color/white"
app:layout_constraintBottom_toTopOf="#id/fbtn_google"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintVertical_bias="0"
android:layout_marginTop="10dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btn_signIn"
app:rippleColor="#color/black"
app:strokeColor="#color/white"
app:strokeWidth="1dp" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fbtn_google"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#6F000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:elevation="35dp"
app:borderWidth="0dp"
android:layout_marginBottom="50dp"
android:src="#drawable/google_cute_icon"
android:tintMode="add"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fbtn_facebook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/black"
android:background="#drawable/icons8_facebook"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toLeftOf="#id/fbtn_google"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="1"
android:layout_marginEnd="15dp"
app:elevation="35dp"
app:borderWidth="0dp"
android:layout_marginBottom="50dp"
android:src="#drawable/icons8_facebook"
android:tintMode="add"
app:maxImageSize="30dp"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fbtn_twitter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#6F000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toRightOf="#id/fbtn_google"
app:layout_constraintHorizontal_bias="0"
android:layout_marginStart="15dp"
app:elevation="35dp"
app:borderWidth="0dp"
android:layout_marginBottom="50dp"
android:src="#drawable/google_cute_icon"
android:tintMode="add"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Fragment Class
package com.ak_applications.kottry.screens.fragments
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.View
import com.ak_applications.kottry.R
class login_fragment : Fragment(R.layout.fragment_login_fragment) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
}
Plase help me out i don't understand what is the issue with my code.
In login_signup activity, change the onCreate() from
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?)
to
override fun onCreate(savedInstanceState: Bundle?)
Not really sure what the problem might be, but I'll give a couple of possible solutions.
1st:
Try using a FragmentContainerView to contain your fragment. Something like this:
<androidx.fragment.app.FragmentContainerView
android:id="#+id/frag_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="PATH TO YOUR FRAGMENT">
In the name tag you can out the path to the fragment that you want to inflate first.
The path should something like com.ak_applications.kottry.login_fragment.
2nd:
You can try a different way to use the supportFragmentManager. I use it like this:
supportFragmentManager.beginTransaction().replace(R.id.frag_container, login_fragment()).commit()
Just give it a go on any of the two.

constraintLayout keyframe animation on fragment

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.

Categories

Resources