Custom Toast inflater must be initialized - kotlin

i have this fun
private fun testToast(){
val inflater:LayoutInflater
val view = inflater.inflate(R.layout.custom_toast_message)
val toast = Toast(this.context)
toast.setGravity(Gravity.TOP,0,70)
toast.duration = Toast.LENGTH_LONG
toast.view =view
toast.show()
}
the main idea its to change the background color of the toast without getting a square toast
in this code i get an error in inflater.inflate
(inflater must be initialized)
this is the test xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom_toast_container"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#color/background_blue"
android:padding="16dp"
android:weightSum="1"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_toasticon"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"/>
<TextView
android:id="#+id/TextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_gravity="center_vertical"
android:layout_weight="0.8"
android:textColor="#color/gauge_red"
android:fontFamily="#font/roboto"
android:text="Prueba prueba PRUEBA"
/>
</LinearLayout>
con someone help me or annother way to do a toast with custom color.
change the color of a toast with rounded corners

You declared the inflater variable but never assigned anything to it (using =). You can’t use a variable’s value if you haven’t assigned anything to it yet.
If this function is in an Activity, you can delete the line that declares val inflater and replace inflater. with layoutInflater. to use the property of the Activity. If it’s in a Fragment, use requireActivity().layoutInflator..
By the way, the latest version of Android has deprecated setting a custom view for your toast. On Android R and later if you set a view on your Toast, it won’t show. They want you to use SnackBars instead. There might be a third party library available that could help create something more similar to Toasts.

you can make a drawable resource to achieve it
toast_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/teal_200" />
<corners android:radius="48dp" />
</shape>
and use this drawable for background of toast view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom_toast_container"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#drawable/toast_background"
android:orientation="horizontal"
android:padding="16dp"
android:weightSum="1">
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:src="#drawable/ic_launcher_background" />
<TextView
android:id="#+id/TextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.8"
android:text="Prueba prueba PRUEBA"
android:textColor="#color/purple_700"
android:textSize="14sp" />
</LinearLayout>
the result

Related

How to hide the FloatingActionButton that is part of the Activity in the Fragment in Kotlin?

In my 'HomeActivity', I have a FloatingActionButton and four Fragments. I want the FloatingActionButton to be shown only in the 'fragment1' and 'fragment2' make hide it in the 'fragment3' and 'fragment4'. I tried but it didn't work.
Following is what I have in the xml file of the 'HomeActivity'
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/bottom_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="16dp"
android:background="#android:color/transparent"
app:menu="#menu/bottom_nav_menu" />
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_hide_category"
app:layout_anchor="#id/bottom_appbar"/>
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp"
app:defaultNavHost="true"
app:navGraph="#navigation/mobile_navigation"
app:layout_anchor="#id/bottom_appbar"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
I added the below code in the 'HomeActivity'
fun hideFabOne(){
binding.fabOne.hide()
}
and below code in the onCreateView of the fragments where I want to hide the FAB. But, it didn't work.
HomeActivity().hideFabOne()
Looks like you are creating new instance of activity , try
(requireActivity() as HomeActivity).hideFabOne()
*Add for comment
You can modify the hideFabOne() something like
fun hideFabOne(){
binding.fabOne.visiblity = if( binding.fabOne.isVisible ) View.VISIBLE else View.GONE
}

Image not appear using splash screen react native

i want to put image in splash screen react native but it didnt work. im using this library https://github.com/crazycodeboy/react-native-splash-screen
i put some text in it and it works fine. but the image doesnt appear at all. i tried it in android studio it works fine but when react native rendering image not appear at all
Image : png 32 bit
Here is it the code
<?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"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical"
android:background="#color/white"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
app:srcCompat="#drawable/splash" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="7dp"
android:text="Tes"
android:textColor="#4972D1"
android:textSize="24sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="7dp"
android:text="Tes"
android:textColor="#759fff"
android:textSize="24sp" />
</LinearLayout>
Here is it in android studio
Here is it when i launch the app
According to the official docs, you should add scaleType property to your ImageView.
android:scaleType="centerCrop"
If it doesn't works please add your manifest file for further help.
You also need to add in the image in Splash.xml and then re-build the project.
Also add the images in drawable folder.

Recyclerview multiple viewtype causes first row item expanded unexpectedly

I want to make something like this
so I am using a recyclerview which has two viewtype ( for the first item I am using one kind of viewholder and rest of item I am using another viewholder).
But on my device, it is showing like this
For the first viewholder I am using this layout file
<LinearLayout 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">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/attach_circle_imageview"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#drawable/attach_photo"
app:civ_border_color="#607D8A"
app:civ_border_width="1dp"
android:layout_marginLeft="14dp"
android:layout_marginBottom="14dp"/>
and for others, I am using this layout file
<android.support.v7.widget.CardView
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginLeft="14dp"
android:layout_marginBottom="14dp"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="10dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/gallary_iv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/white_bg_iv"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/pic_photo_number_bg"
app:layout_constraintBottom_toBottomOf="#+id/gallary_iv"
app:layout_constraintEnd_toEndOf="#+id/gallary_iv"
app:layout_constraintStart_toStartOf="#+id/gallary_iv"
app:layout_constraintTop_toTopOf="#+id/gallary_iv" />
<TextView
android:id="#+id/pic_count_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textSize="16sp"
android:textColor="#506876"
app:layout_constraintTop_toTopOf="#+id/white_bg_iv"
app:layout_constraintBottom_toBottomOf="#+id/white_bg_iv"
app:layout_constraintStart_toStartOf="#+id/white_bg_iv"
app:layout_constraintEnd_toEndOf="#id/white_bg_iv"/>
</android.support.constraint.ConstraintLayout>
In the first-row pictures height is expanded unexpectedly, how can I solve this problem?
set the first viewholder layout
android:layout_width="match_parent"
android:layout_height="match_parent"
change to
android:layout_width="90dp"
android:layout_height="90dp"

Using android DataBinding library how to pass parameters to binding events

I followed examples from Android Developers Binding Events and implementing step-by-step. That working fine. But I want to send parameters from adapter to handlers, how can achieve this using data binding handlers
I got the answer.
In xml for onclick use lambda expression
layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="movie"
type="embitel.com.databindingexample.helper.Movie" />
<variable
name="handler"
type="embitel.com.databindingexample.helper.MyHandlers" />
</data>
<android.support.v7.widget.CardView
android:id="#+id/cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="4dp"
android:onClick="#{(view)->handler.onItemClicked(view,movie)}"
app:cardBackgroundColor="#android:color/white"
app:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="128dp"
android:scaleType="centerCrop"
app:error="#{#drawable/ic_launcher}"
app:imageUrl="#{movie.imageUrl}" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#{movie.title}" />
</LinearLayout>
</android.support.v7.widget.CardView>
then create handler class as,
public class MyHandlers {
public void onItemClicked(View v, Movie movie) {
Context context = v.getContext();
context.startActivity(DetailActivity.buildIntent(context, movie));
}
}
then you need to set handler where that xml is iflated as,
binding.setHandler(new MyHandlers());
you can also put handler method in any class. In that case you have to set that class name as handler.

android, how to place a scroll view in the middle of the screen:

I'm trying to create a a screen layout of the following though:
A line with some TextView in it
A scroll view where the user actions will add or remove View from
A buttons line.
I have no problem with 1. or 3. But 2. gives me troubles. My scheme is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainX"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/labels"
android:layout_weight="1">
<!--Some text view of various sizes -->
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/dataScroll">
<LinearLayout
android:id="#+id/dataShow"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/buttons"
android:layout_weight="1">
<!--some buttons-->
</LinearLayout>
</LinearLayout>
When I run the program, the buttons line is in the middle of the screen and When I add element to the screen (using the addView on the dataShow layout), the frist one is added without a problem but after that I can't tell what is happening.
Thanks
I'm not exactly sure what you are trying to do.
It seems like you want to dynamically add items to the dataShow layout. Instead of using a ScrollView containing a LinearLayout, you should be using a ListView (vertical scrolling) or Gallery (horizontal scrolling) for your dataShow layout. This will provide the scroll functionality and allow you to add items dynamically to the list/gallery.
Try using a RelativeLayout, then align the header at the top, and the footer at the bottom. Set the ScrollView paddingBottom.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="text"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/header"
android:layout_above="#+id/footer"
android:paddingBottom="100dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="center main content"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"/>
</ScrollView>
<RelativeLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#color/colorPrimary"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"/>
</RelativeLayout>
</RelativeLayout>