I want to build a pager adapter. I created the adapter and setup it into a view pager, but nothing is showing on screen. Please help me with this issue. My pager view is still white... Here is the code:
Adapter class
class WelcomeViewPagerAdapter(private val context: Context?,private val listOfCards:ArrayList<String>) : PagerAdapter() {
override fun isViewFromObject(view: View, `object`: Any): Boolean {
return view === `object` as LinearLayout
}
override fun getCount(): Int {
return listOfCards.size
}
override fun instantiateItem(container: ViewGroup, position: Int): Any {
val view = LayoutInflater.from(this.context).inflate(R.layout.welcome_view_pager_view, container, false)
view.titleTextView.text = listOfCards[position]
view.descriptionTextView.text = listOfCards[position]
container.addView(view)
return view
}
override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
container.removeView(`object` as LinearLayout)
}
}
Fragment class
val listOfMessages = ArrayList<String>()
listOfMessages.add("Lorem ipsum dolor sit amet, consec")
listOfMessages.add("Lorem ipsum dolor sit amet, consec")
listOfMessages.add("Lorem ipsum dolor sit amet, consec")
listOfMessages.add("Lorem ipsum dolor sit amet, consec")
welcomeViewPager.adapter = WelcomeViewPagerAdapter(context, listOfMessages)
welcome_view_pager_view.xml code from 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:orientation="vertical"
android:background="#FFF"
android:layout_height="wrap_content">
<TextView
android:id="@+id/titleTextView"
android:layout_width="match_parent"
android:textAlignment="center"
android:layout_height="wrap_content"
android:text="Welcome to Food Point"
android:textSize="26sp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:fontFamily="@font/open_sans_semibold"
android:textColor="#343434" />
<TextView
android:id="@+id/descriptionTextView"
android:layout_width="match_parent"
android:paddingTop="15dp"
android:textAlignment="center"
android:layout_height="wrap_content"
android:text="Please give access your camera so that we can scan and provide you that what\n is inside the food"
android:textSize="15sp"
android:paddingStart="60dp"
android:paddingEnd="60dp"
android:paddingBottom="15dp"
android:fontFamily="@font/open_sans_semibold"
android:textColor="#343434" />
fragment xml
<LinearLayout 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"
android:orientation="vertical"
android:background="#f8f8f8"
tools:context=".screens.WelcomeFragment">
<com.makeramen.roundedimageview.RoundedImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/imageView1"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/bicycle"
android:scaleType="fitCenter"
android:layout_gravity="center"
android:layout_marginTop="40dp"
app:riv_corner_radius="100dp"
app:riv_mutate_background="true"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Let's get started"
android:fontFamily="@font/open_sans_bold"
android:textColor="#343434"
android:textAlignment="center"
android:layout_marginTop="24dp"
android:textSize="24sp"/>
<androidx.viewpager.widget.ViewPager
android:id="@+id/welcomeViewPager"
android:layout_width="match_parent"
android:layout_height="170dp"
android:layout_marginTop="44dp" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/welcomePagerDots"
android:layout_width="80dp"
android:layout_height="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/welcomeViewPager"
app:tabBackground="@drawable/view_pager_tab_selector"
app:tabGravity="center"
android:backgroundTint="#ffffff"
app:tabIndicatorHeight="0dp"
app:tabPaddingEnd="10dp"
app:tabPaddingStart="10dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="40dp"
android:layout_marginStart="40dp"
android:background="@drawable/rounded_bg_50"
android:backgroundTint="#3061D7"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:text="Create account"
android:fontFamily="@font/open_sans_light"
android:textColor="#fff"
android:textSize="17sp"
android:textAllCaps="false"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="40dp"
android:layout_marginStart="40dp"
android:background="@drawable/rounded_bg_50"
android:backgroundTint="#fff"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:layout_marginTop="15dp"
android:text="Login"
android:fontFamily="@font/open_sans_light"
android:textColor="#343434"
android:textSize="17sp"
android:textAllCaps="false"/>
Some text, Some text,Some textSome text, Some text,Some textSome text, Some text,Some textSome text, Some text,Some textSome text, Some text,Some textSome text, Some text,Some textSome text, Some
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_welcome, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val listOfMessages = ArrayList<String>()
listOfMessages.add("Lorem ipsum dolor sit amet, consec")
listOfMessages.add("Lorem ipsum dolor sit amet, consec")
listOfMessages.add("Lorem ipsum dolor sit amet, consec")
listOfMessages.add("Lorem ipsum dolor sit amet, consec")
welcomeViewPager.adapter = WelcomeViewPagerAdapter(context, listOfMessages)
welcomePagerDots.setupWithViewPager(welcomeViewPager)
welcomeViewPager.invalidate()
for (i in 0 until welcomePagerDots.tabCount) {
val tab = (welcomePagerDots.getChildAt(0) as ViewGroup).getChildAt(i)
(tab.layoutParams as ViewGroup.MarginLayoutParams).setMargins(0, 0, 30, 0);
tab.requestLayout()
}
}