How can access a VIEW in Android - java

I am working with Platform View in Flutter and I want to return a VIEW in my getView() constructor.
I have an .xml file with the View group that I want to return to in this. My question is how to return this since my constructor returns a VIEW.
That is the sample code:
internal class SBNativeView(context: Context, id: Int, creationParams: Map<String?, Any?>?) : PlatformView, AppCompatActivity() {
private val textView: TextView
override fun getView(): View {
return textView
}
override fun dispose() {}
init {
textView = TextView(context)
textView.textSize = 72f
textView.setBackgroundColor(Color.rgb(255, 255, 255))
textView.text = "Rendered on a native Android view (id: $id)"
}
}
I need return this view
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/loginConstraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textlayout_login_user_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="48dp"
android:layout_marginEnd="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/edittext_login_user_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="UserID"
android:imeOptions="actionDone"
android:inputType="textVisiblePassword"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textlayout_login_nickname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
app:layout_constraintTop_toBottomOf="#+id/textlayout_login_user_id">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/edittext_login_nickname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Nickname"
android:imeOptions="actionDone"
android:inputType="textVisiblePassword"/>
</com.google.android.material.textfield.TextInputLayout>
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/button_login_connect"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="match_parent"
android:layout_height="44dp"
android:layout_marginStart="16dp"
android:layout_marginTop="44dp"
android:layout_marginEnd="16dp"
android:background="#774df2"
android:stateListAnimator="#null"
android:text="Connect"
app:layout_constraintTop_toBottomOf="#+id/textlayout_login_nickname"/>
</androidx.constraintlayout.widget.ConstraintLayout>
How Can I return this view in getView()?

Related

Hello ,my app crashes every time I invoke Spinner which exists in a Fragment called "Firstfragment"

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//This Spinner is not located in the MainActivity but in A fragment
Spinner s = (Spinner) findViewById(R.id.spinner33);
s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String msupplier=s.getSelectedItem().toString();
Log.e("Selected item : ", msupplier);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
//Main MainActivity
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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"
android:id="#+id/drawer">
<include
layout="#layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginTop="104dp"
app:headerLayout="#layout/nav_header"
app:menu="#menu/nav_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
//first 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:id="#+id/Fragment1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2196F3"
tools:context=".MainActivity">
<ImageView
android:id="#+id/imageView4"
android:layout_width="match_parent"
android:layout_height="773dp"
android:src="#mipmap/background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/teal_700"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="#+id/watBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="true"
android:drawableTop="#drawable/ic_baseline_looks_one_24"
android:editable="true"
android:enabled="true"
android:focusable="auto"
android:gravity="center"
android:includeFontPadding="true"
android:linksClickable="true"
android:onClick="WattMthd"
android:text="#string/WAT"
app:backgroundTint="#android:color/holo_blue_dark"
tools:layout_editor_absoluteX="35dp"
tools:layout_editor_absoluteY="129dp" />
<Button
android:id="#+id/ampBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableTop="#drawable/ic_baseline_looks_two_24"
android:editable="true"
android:gravity="center"
android:onClick="ampMthd"
android:text="#string/AMP"
android:textColorLink="#FFFFFF"
app:backgroundTint="#android:color/darker_gray" />
<Button
android:id="#+id/voltBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableTop="#drawable/ic_baseline_looks_3_24"
android:editable="true"
android:gravity="center"
android:onClick="voltMthd"
android:text="فولت"
app:backgroundTint="#android:color/holo_blue_dark" />
<Button
android:id="#+id/kWbtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableTop="#drawable/ic_baseline_looks_4_24"
android:editable="true"
android:gravity="center"
android:onClick="kWMthd"
android:text="كيلو"
app:backgroundTint="#android:color/holo_blue_dark" />
</LinearLayout>
<Spinner
android:id="#+id/spinner33"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:dropDownWidth="match_parent"
android:entries="#array/SpinerConvertFrom"
android:foreground="#drawable/ic_baseline_arrow_drop_down_circle_24"
android:foregroundGravity="left|center"
android:gravity="center"
android:textAlignment="center"
android:visibility="visible"
app:flow_verticalAlign="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/spinner33">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/Text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/AMP"
android:inputType="numberDecimal"
android:selectAllOnFocus="true"
android:textAlignment="center" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textInputLayout3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textInputLayout">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/Volt"
android:inputType="numberDecimal"
android:selectAllOnFocus="true"
android:textAlignment="center" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
If you are calling the spinner initialization from the fragment but it exists in the main activity it will crash, you need to call it in the activity, or move it to the xml of the framgnet.
activity_main belongs to MainActivity. So there is a fragment_first which belongs to FirstFragment. The spinner you are trying to initialize belongs to fragment_first, so you have to use it inside FirstFragment or move it (spinner) to activity_main
Hi bro inside you activity_main.xml you didn't define spinner, so you can't get view which you didn't create in your xml file, you can get it from your fragment, or you must move Spinner to activity_main.xml.

Custom Dialog not showing with certain XML

I'm trying to display a custom dialog but it's not showing up but making the background transparent. The weirdest thing is that when i change just the custom dialog xml it shows up without problems so i think it could be the xml but i've been looking for a while and i can't find the problem.
This is the fragment code:
class DeleteConfirmationFragment : Fragment() {
private lateinit var binding: FragmentDeleteConfirmationBinding
lateinit var dialog : Dialog
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_delete_confirmation, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding = FragmentDeleteConfirmationBinding.bind(view)
dialog = context?.let { Dialog(it) }!!
binding.btnNo.setOnClickListener {
openDeletedAccountDialog()
}
}
private fun openDeletedAccountDialog() {
dialog.setContentView(R.layout.dialog_deleted_account) // if i change this xml file for other it
//works properly so i think it could be an android bug..
dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
dialog.show()
}
}
This is the xml that isnt working:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp">
<androidx.cardview.widget.CardView
android:id="#+id/cardViewDeletedAccount"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardCornerRadius="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tvDeletedAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:fontFamily="#font/poppins_semibold"
android:text="Cuenta eliminada"
android:textColor="#color/black"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:fontFamily="#font/poppins_semibold"
android:gravity="center_horizontal"
android:padding="10dp"
android:text="Tu cuenta se ha eliminado correctamente. ¡Esperamos volver a verte pronto!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvDeletedAccount"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
Display using custom xml which isnt working:
Other custom dialog xml code that is working properly:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp">
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/poppins_bold"
android:gravity="center"
android:text="¡Tu friendzone ha crecido!"
android:textColor="#color/white"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="#+id/cardViewFriendzone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.884" />
<androidx.cardview.widget.CardView
android:id="#+id/cardViewFriendzone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:id="#+id/cardView2"
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="100dp"
app:cardElevation="20dp"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.47"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/tvNotificationImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
app:srcCompat="#drawable/pedra" />
</androidx.cardview.widget.CardView>
<TextView
android:id="#+id/tvDialogName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:fontFamily="#font/poppins_semibold"
android:text="María González"
android:textColor="#color/black"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:fontFamily="#font/poppins_semibold"
android:padding="10dp"
android:text="Soy una chica aventurera, amistosa y muy curiosa. Mi pasión son los animales, en especial los perros."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvDialogName"
app:layout_constraintVertical_bias="0.45" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
If i change just the xml in the open deleted dialog function like this it shows up the other dialog correctly:
private fun openDeletedAccountDialog() {
dialog.setContentView(R.layout.dialog_friendzone_layout) // <-- change made here
dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
dialog.show()
}
Display using other custom dialog xml:
Any idea of what is happening?
I had the same problem too and it was because I was using 0dp in ConstrintLayout in order to using match_constraint size but it's not working in dialogs.
you just need to change android:layout_width="0dp" of your CardView to android:layout_width="match_parent".

Cardview width not matching parent [duplicate]

This question already has answers here:
RecyclerView items don't fill width
(3 answers)
Recycler view showing single item
(8 answers)
Closed 2 years ago.
I need to create a Recyclerview. For that I have created an cardview model to be displayed in a recyclerview. Even though i have given match parent to the layouts, the width of cardview is not fitting the parent. Its width is showing approximately up to half of the screen(not matching parent) Do anyone can support?.please advise better way to do this.
Attached the xml code below
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
app:cardCornerRadius="15dp"
android:layout_height="wrap_content"
android:foreground="#drawable/border_creatives"
android:layout_marginHorizontal="6dp"
android:layout_marginVertical="4dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/dficon"
app:layout_constraintBottom_toTopOf="#id/guideline19"
app:layout_constraintStart_toStartOf="#id/guideline23"
app:layout_constraintTop_toBottomOf="#id/guideline18"
app:layout_constraintEnd_toStartOf="#id/guideline50"/>
<TextView
android:id="#+id/dname"
android:layout_width="0dp"
android:layout_height="0dp"
android:fontFamily="#font/amiko_semibold"
android:textColor="#color/black"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#+id/guideline24"
app:layout_constraintEnd_toStartOf="#+id/ddownbutton"
app:layout_constraintStart_toStartOf="#+id/guideline50"
app:layout_constraintTop_toTopOf="#+id/guideline18" />
<TextView
android:id="#+id/ddate"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:fontFamily="#font/amiko_semibold"
android:textColor="#color/black"
android:textSize="13sp"
app:layout_constraintBottom_toTopOf="#+id/guideline19"
app:layout_constraintEnd_toStartOf="#id/barrier7"
app:layout_constraintStart_toStartOf="#id/guideline50"
app:layout_constraintTop_toTopOf="#+id/guideline24" />
<TextView
android:id="#+id/dfileextension"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="30dp"
android:fontFamily="#font/amiko_semibold"
android:textColor="#color/black"
android:textSize="13sp"
app:layout_constraintBottom_toTopOf="#+id/guideline19"
app:layout_constraintStart_toEndOf="#id/barrier7"
app:layout_constraintTop_toTopOf="#+id/guideline24" />
<ImageButton
android:id="#+id/ddownbutton"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/mins_customselector"
android:clickable="true"
android:src="#drawable/filedownload_icon"
app:layout_constraintBottom_toTopOf="#+id/guideline19"
app:layout_constraintEnd_toStartOf="#+id/guideline22"
app:layout_constraintTop_toTopOf="#+id/guideline18"
android:focusable="true" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.10" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.90" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.8275" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.97" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.045" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline24"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.57" />
<androidx.constraintlayout.widget.Barrier
android:id="#+id/barrier7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="right"
app:constraint_referenced_ids="ddate" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline50"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.20" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
main layout having recyclerview
<?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=".Download_Activity">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="#+id/toolbar"
app:layout_constraintStart_toStartOf="#+id/guideline49"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="#id/toolbar">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="start"
android:text="#string/downloads"
android:textColor="#FCFFFD"
android:textSize="30sp"
app:fontFamily="#font/carter_one"/>
</com.google.android.material.textfield.TextInputLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/drecyclerdownloads"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/toolbar" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline49"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.20" />
</androidx.constraintlayout.widget.ConstraintLayout>
Adapter class
public class DownloadsAdapter extends RecyclerView.Adapter<Downloadsviewholder> {
Download_Activity download_activity;
ArrayList<Downloadsmodel> downloadsmodels;
public DownloadsAdapter(Download_Activity download_activity, ArrayList<Downloadsmodel> downloadsmodels) {
this.download_activity = download_activity;
this.downloadsmodels = downloadsmodels;
}
#NonNull
#Override
public Downloadsviewholder onCreateViewHolder(#NonNull ViewGroup parent, int i) {
LayoutInflater layoutInflater = LayoutInflater.from(download_activity.getBaseContext());
View view = layoutInflater.inflate(R.layout.downloads_elements,null,false);
return new Downloadsviewholder(view);
}
#Override
public void onBindViewHolder(#NonNull final Downloadsviewholder downloadsviewholder, final int i) {
downloadsviewholder.dName.setText(downloadsmodels.get(i).getName());
downloadsviewholder.dUploaddate.setText(downloadsmodels.get(i).getUploaddate());
downloadsviewholder.dExtension.setText(downloadsmodels.get(i).getFileextension());
Picasso.get().load(downloadsmodels.get(i).getIcon()).into(downloadsviewholder.dIcon);
downloadsviewholder.dButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (ConnectionCheck.checkconnetion(download_activity.getBaseContext())) {
dowloadFile(downloadsviewholder.dName.getContext(), downloadsmodels.get(i).getName(), downloadsmodels.get(i).getFileextension()
, DIRECTORY_DOWNLOADS, downloadsmodels.get(i).getLink());
} else {
Toast.makeText(download_activity.getBaseContext(),"Check your internet connection", Toast.LENGTH_SHORT).show();
}
}
});
}
public void dowloadFile (Context context,String filename, String fileextension, String destinationdirectory, String url){
DownloadManager downloadManager=(DownloadManager) context.getSystemService(context.DOWNLOAD_SERVICE);
Uri uri=Uri.parse(url);
DownloadManager.Request request=new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalFilesDir(context, destinationdirectory, filename+"."+fileextension);
downloadManager.enqueue(request);
}
#Override
public int getItemCount() {
return downloadsmodels.size();
}
}
Viewholder
public class Downloadsviewholder extends RecyclerView.ViewHolder {
TextView dName;TextView dLink;TextView dUploaddate; TextView dExtension;
ImageView dIcon;
ImageButton dButton;
public Downloadsviewholder(#NonNull View itemView) {
super(itemView);
dName = itemView.findViewById(R.id.dname);
dUploaddate=itemView.findViewById(R.id.ddate);
dExtension=itemView.findViewById(R.id.dfileextension);
dButton=itemView.findViewById(R.id.ddownbutton);
dIcon=itemView.findViewById(R.id.dficon);
}
}

Views in RecyclerView still has height of 0 even when displayed

I am confused because in my RecyclerView, some items has their height = 0 (even when they are correctly displayed and visible) and some have their normal height.
How is that possible ?
If I monitor the height with addOnGlobalLayoutListener, 90% of item has their correct height calculated, and 10% still have 0.
Any idea of how android works for this ?
My code is in Kotlin but it doesn’t matter.
init {
var heightItemView = 0
var currentY = itemView.y
var oldY = currentY
itemView.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
if (itemView.height > 0) {
heightItemView = itemView.height
itemView.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
I also tried with measuredHeight().
My onCreateViewHolder() from the adapter:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PairMarketAdapter.ViewHolder {
val v = android.view.LayoutInflater.from(parent.context)
.inflate(R.layout.item_pair_market, parent, false)
val holder = PairMarketAdapter.ViewHolder(v)
holder.itemView.setOnClickListener {
listener.onItemAction(holder.adapterPosition)
}
return holder
}
XML of the fragment:
<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:id="#+id/lCoordinatorRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvPairMarket"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingTop="#dimen/spacing_normal"
tools:listitem="#layout/item_pair_market"
/>
</LinearLayout>
And the item XML:
<LinearLayout
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="?selectableItemBackground"
android:paddingBottom="#dimen/spacing_normal"
android:paddingTop="#dimen/spacing_small"
android:paddingEnd="#dimen/spacing_normal"
android:paddingStart="#dimen/spacing_normal"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/spacing_small"
android:layout_weight="1.5"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="#+id/tvSymbol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:textAllCaps="true"
android:textColor="?android:attr/textColorPrimary"
android:textSize="#dimen/font_large"
android:textStyle="bold"
tools:text="USD"
/>
<TextView
android:id="#+id/tvSymbolBase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/spacing_tiny"
android:maxLines="1"
android:textAllCaps="true"
android:textColor="?android:attr/textColorSecondary"
android:textSize="#dimen/font_large"
android:textStyle="bold"
tools:text="BTC"
/>
<View
android:id="#+id/vIsSelected"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_gravity="center"
android:layout_marginStart="#dimen/spacing_small"
android:background="#drawable/circle_green_dot"
android:visibility="gone"
/>
</LinearLayout>
<TextView
android:id="#+id/tvExchange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/spacing_tiny"
android:maxLines="1"
android:textColor="?android:attr/textColorSecondary"
android:textSize="#dimen/font_normal"
tools:text="1. Bitfinex"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:id="#+id/tvPercent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:textColor="?android:attr/textColorPrimary"
android:textSize="#dimen/font_large"
tools:text="55.5%"
/>
</LinearLayout>
</LinearLayout>
itemView.measuredHeight android will try to measure your view before display try using itemView.height inside addOnGlobalLayoutListener

How to collapse a view to the bottom of the screen in android using coordinator layout?

Can someone help me make a Collapsable Toolbar go to the bottom without exiting from the screen?
Initial view:
After scrolling down:
This is what I've tried, but the toolbar scrolls always to the top and dissapears:
<?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:id="#+id/search_place_root_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.location.SearchPlaceActivity">
<android.support.constraint.ConstraintLayout
android:id="#+id/search_place_header"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/home_background"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="#+id/search_place_et"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="25dp"
android:layout_marginStart="57dp"
android:layout_marginTop="8dp"
android:background="#e3e3e3"
android:padding="10dp"
android:textColor="#color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/search_place_back_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
app:layout_constraintBottom_toBottomOf="#+id/search_place_et"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/search_place_et"
app:srcCompat="#drawable/back" />
</android.support.constraint.ConstraintLayout>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/search_place_header"
android:foregroundGravity="bottom">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.constraint.ConstraintLayout
android:id="#+id/search_place_results_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/list_search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.constraint.ConstraintLayout
android:layout_width="0dp"
android:layout_height="122dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/list_search">
<ImageView
android:id="#+id/imageView22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:src="#drawable/location_pin"
app:layout_constraintBottom_toBottomOf="#id/search_place_use_my_current_loc_tv"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#id/search_place_use_my_current_loc_tv" />
<TextView
android:id="#+id/search_place_use_my_current_loc_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="34dp"
android:fontFamily="#font/opensanssemibold"
android:letterSpacing="0.02"
android:lineSpacingExtra="12sp"
android:text="#string/use_my_current_location"
android:textColor="#000000"
android:textSize="15sp"
app:layout_constraintStart_toEndOf="#+id/imageView22"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
</android.support.constraint.ConstraintLayout>
How can I make the transition for my whole content to go to the bottom and shrink a little bit?
Xml
<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="match_parent"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="info.androidhive.bottomsheet.MainActivity"
tools:showIn="#layout/activity_main">
<-Your Views->
</LinearLayout>
Java
BottomSheetBehavior sheetBehavior = BottomSheetBehavior.from(layoutBottomSheet);
/**
* bottom sheet state change listener
* we are changing button text when sheet changed state
* */
sheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onStateChanged(#NonNull View bottomSheet, int newState) {
switch (newState) {
case BottomSheetBehavior.STATE_HIDDEN:
break;
case BottomSheetBehavior.STATE_EXPANDED: {
btnBottomSheet.setText("Close Sheet");
}
break;
case BottomSheetBehavior.STATE_COLLAPSED: {
btnBottomSheet.setText("Expand Sheet");
}
break;
case BottomSheetBehavior.STATE_DRAGGING:
break;
case BottomSheetBehavior.STATE_SETTLING:
break;
}
}
#Override
public void onSlide(#NonNull View bottomSheet, float slideOffset) {
}
});
See more details

Categories