I am trying to use ViewModel for making API calls, But when I cick the button to send the phone number for OTP the app breaks and their is not information in the logcat too.
Code of LoginActivity.kt
`
class LoginActivity : AppCompatActivity() {
private lateinit var binding: ActivityLoginBinding
private lateinit var viewModel: LoginViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityLoginBinding.inflate(layoutInflater)
setContentView(binding.root)
viewModel = LoginViewModelFactory(LoginService.create()).create(LoginViewModel::class.java)
binding.loginViewModel = viewModel
binding.lifecycleOwner = this
binding.continueButton.setOnClickListener {
viewModel.onLogin()
}
viewModel.login.observe(this){
gotoOTP()
}
}
fun gotoOTP(){
var intent = Intent(this#LoginActivity, OTPLoginVerification::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_TASK_ON_HOME
this.startActivity(intent)
this.finish()
}
}
`
code for LoginViewModel.kt
`
class LoginViewModel(var loginService: LoginService) : ViewModel(), Observable {
val phoneNumber: MutableLiveData<String> = MutableLiveData()
val login = MutableLiveData<Unit>()
fun onLogin(){
loginService.getOTP(LoginRequestModel(phone_no = phoneNumber.value.toString()))
.enqueue(object: Callback<LoginResponseModel>{
override fun onResponse(call: Call<LoginResponseModel>, response: Response<LoginResponseModel>) {
if(response.isSuccessful) {
if (response.body()!!.result) login.postValue(Unit)
else print("Error")
}
else{
print("Error")
}
}
override fun onFailure(call: Call<LoginResponseModel>, t: Throwable) {
print("Error")
}
})
}
override fun addOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {
}
override fun removeOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {
}
}
`
code for LoginViewModelFactory.kt
`
class LoginViewModelFactory(private val loginService: LoginService): ViewModelProvider.Factory{
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if(modelClass.isAssignableFrom(LoginViewModel::class.java)){
return LoginViewModel(loginService) as T
}
else throw IllegalArgumentException("Error Unknown ViewModel Type")
}
}
And this is the service Interface
interface LoginService {
#POST("getOTP")
fun getOTP(#Body model: LoginRequestModel) : Call<LoginResponseModel>
companion object{
val BASE_URL = "*********"
fun create(): LoginService{
val client = OkHttpClient.Builder()
client.connectTimeout(2, TimeUnit.MINUTES)
client.readTimeout(2, TimeUnit.MINUTES)
client.writeTimeout(2, TimeUnit.MINUTES)
val gson = GsonBuilder()
.serializeNulls()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.setLenient()
.create()
val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(client.build())
.build()
return retrofit.create(LoginService::class.java)
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="LoginViewModel"
type="com.example.sample.viewmodels.LoginViewModel" />
</data>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Auth.LoginActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.card.MaterialCardView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="#id/g_h_1"
app:cardBackgroundColor="#color/peach_dark"
style="#style/CustomCardViewStyle">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="fitXY"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintDimensionRatio="1.2:1"
android:src="#drawable/loginimage" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="fitCenter"
android:src="#drawable/logo"
app:layout_constraintDimensionRatio="3:1"
app:layout_constraintTop_toTopOf="#id/g_h_2"
app:layout_constraintStart_toStartOf="#id/g_v_2"
app:layout_constraintEnd_toEndOf="#id/g_v_3"
app:layout_constraintBottom_toBottomOf="#id/g_h_3" />
<TextView
android:id="#+id/company_highlight"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/company_highlight"
android:layout_marginTop="10dp"
app:layout_constraintStart_toStartOf="#id/g_v_1"
app:layout_constraintEnd_toEndOf="#id/g_v_4"
app:layout_constraintTop_toTopOf="#id/g_h_3"
android:textAlignment="center"
android:textSize="20dp"
android:fontFamily="#font/metropolisbold"
android:textColor="#color/t_dark" />
<TextView
android:id="#+id/login_textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/login_text"
android:layout_marginTop="20dp"
app:layout_constraintStart_toStartOf="#id/g_v_1"
app:layout_constraintEnd_toEndOf="#id/g_v_4"
app:layout_constraintTop_toBottomOf="#id/company_highlight"
android:textAlignment="center"
android:textSize="17dp"
android:fontFamily="#font/metropolissemibold" />
<androidx.gridlayout.widget.GridLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/gridview_mobilenumber"
app:layout_constraintStart_toStartOf="#id/g_v_1"
app:layout_constraintEnd_toEndOf="#id/g_v_4"
app:layout_constraintTop_toBottomOf="#+id/login_textview"
android:layout_marginTop="25dp"
app:columnCount="2">
<com.google.android.material.card.MaterialCardView
android:id="#+id/country_flag_cardview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_column="0"
android:padding="10dp"
app:layout_columnWeight="0.4"
app:cardCornerRadius="10dp"
app:layout_rowWeight="1"
android:layout_marginLeft="3dp"
app:strokeWidth="1dp"
app:strokeColor="#color/t_light">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/country_flag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:padding="5dp"
android:src="#drawable/indianflag" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/country_flag"
android:layout_centerVertical="true"
android:src="#drawable/dropdown" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="#+id/mobile_number_cardview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_column="1"
android:padding="10dp"
android:layout_marginLeft="10dp"
app:layout_columnWeight="20"
app:layout_rowWeight="1"
app:cardCornerRadius="10dp"
app:strokeWidth="1dp"
app:strokeColor="#color/t_light">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/country_code_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:fontFamily="#font/metropolissemibold"
android:text="#string/country_code"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dp"
android:textColor="#color/t_dark"
android:textSize="22dp" />
<EditText
android:id="#+id/mobile_number_edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/country_code_textview"
android:background="#null"
android:fontFamily="#font/metropolislight"
android:hint="#string/enter_mobile"
android:text="#{LoginViewModel.phoneNumber}"
android:inputType="phone"
android:maxLength="10"
android:textColor="#color/t_dark"
android:textColorHint="#color/t_light"
android:textSize="15dp" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
</androidx.gridlayout.widget.GridLayout>
<TextView
android:id="#+id/new_user_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New User"
android:textSize="15dp"
android:fontFamily="#font/metropolisbold"
app:layout_constraintEnd_toEndOf="#id/g_v_4"
app:layout_constraintTop_toBottomOf="#id/gridview_mobilenumber"
android:textColor="#color/peach_700"
android:layout_marginTop="5dp" />
<com.google.android.material.button.MaterialButton
android:id="#+id/continue_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="#id/g_v_1"
app:layout_constraintEnd_toEndOf="#id/g_v_4"
app:layout_constraintTop_toBottomOf="#id/new_user_textview"
android:text="Continue"
android:fontFamily="#font/metropolissemibold"
android:textAllCaps="false"
android:textSize="17dp"
app:cornerRadius="10dp"
android:layout_marginTop="20dp" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/g_h_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.45" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/g_h_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.47" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/g_h_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.54" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/g_v_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.10" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/g_v_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.30" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/g_v_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.70" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/g_v_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.90" />
</android.constraintlayout.widget.ConstraintLayout>
</ScrollView>
To the best of my knowledge I tried to make changes but nothing worked.
Any help would be great, As I am unable to find the place where I am making mistake.
Thanks in Advance.
Related
I trying to practice some android lessons that contains recyclerview topic, currently I succeed to implement all api and response but there's one problem with my item_article_preview.xml.
The title and description of news doesn't shows in emulator like it's in design preview
After running the app
here's my xml row
<?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="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:orientation="horizontal">
<ImageView
android:id="#+id/ivArticleImage"
android:layout_width="0dp"
android:layout_height="90dp"
android:layout_weight="0.8"
android:scaleType="centerCrop" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.2"
android:orientation="vertical">
<TextView
android:id="#+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:ellipsize="end"
android:maxLines="2"
android:textColor="#color/black"
android:textSize="15sp"
android:textStyle="bold"
tools:text="TITLE" />
<TextView
android:id="#+id/tvDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:ellipsize="end"
android:maxLines="3"
android:textColor="#color/black"
android:text="DESCRIPTION" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/tvSource"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="SOURCE"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/tvPublishedAt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="PUBLISHED AT"
android:textColor="#android:color/black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvSource" />
</LinearLayout>
I tried other row design but it didn't show the title and descreption at all
<?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="8dp">
<ImageView
android:id="#+id/ivArticleImage"
android:layout_width="160dp"
android:layout_height="90dp"
android:scaleType="centerCrop"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tvSource"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="SOURCE"
android:textColor="#android:color/black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/ivArticleImage" />
<TextView
android:id="#+id/tvTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:ellipsize="end"
android:maxLines="3"
android:text="TITLE"
android:textColor="#android:color/black"
android:textSize="15sp"
android:textStyle="bold"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toRightOf="#+id/ivArticleImage"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tvDescription"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:ellipsize="end"
android:maxLines="5"
android:text="DESCRIPTION"
android:textColor="#android:color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/ivArticleImage"
app:layout_constraintTop_toBottomOf="#+id/tvTitle" />
<TextView
android:id="#+id/tvPublishedAt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="PUBLISHED AT"
android:textColor="#android:color/black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvSource" />
</androidx.constraintlayout.widget.ConstraintLayout>
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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvBreakingNews"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="#+id/paginationProgressBar"
style="?attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:background="#android:color/transparent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
my adapter class
private const val TAG = "NewsAdapter"
class NewsAdapter : RecyclerView.Adapter<NewsAdapter.ArticleViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ArticleViewHolder {
val binding = ItemRowBinding.inflate(LayoutInflater.from(parent.context))
return ArticleViewHolder(binding)
}
override fun onBindViewHolder(holder: ArticleViewHolder, position: Int) {
val article = differ.currentList[position]
// holder.bind(article)
holder.binding.apply {
Glide.with(this.root).load(article.urlToImage).into(ivArticleImage)
tvSource.text = article.source.name
tvTitle.text = article.title
tvDescription.text = article.description
tvPublishedAt.text = article.publishedAt
this#NewsAdapter.setOnItemClickListener {
onItemClickListener?.let {
it(article)
}
}
}
}
override fun getItemCount(): Int {
return differ.currentList.size
}
inner class ArticleViewHolder( val binding: ItemRowBinding) :
RecyclerView.ViewHolder(binding.root) {
private var onItemClickListener: ((Article) -> Unit)? = null
private fun setOnItemClickListener(listener: (Article) -> Unit) {
onItemClickListener = listener
}
private val diffCallBack = object : DiffUtil.ItemCallback<Article>() {
override fun areItemsTheSame(oldItem: Article, newItem: Article): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: Article, newItem: Article): Boolean {
return oldItem == newItem
}
}
val differ = AsyncListDiffer(this, diffCallBack)
}
ACTIVITY:
val list : MutableList<Item> = mutableListOf()
list.add(Item("TITLE", "DESCRITION", "ESPN", "2022-08-02T14:32:47Z"))
val rv = findViewById<RecyclerView>(R.id.rvBreakingNews)
rv.layoutManager = LinearLayoutManager(applicationContext)
rv.adapter = RvAdapter(list)
ADAPTER
class RvAdapter(private var list : List<Item>) : RecyclerView.Adapter<RvAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.title.setText(list[position].getTitle())
holder.desc.setText(list[position].getDescription())
holder.source.setText(list[position].getSource())
holder.published.setText(list[position].getPublished())
}
// return the number of the items in the list
override fun getItemCount(): Int {
return list.size
}
// Holds the views for adding it to image and text
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val title : TextView = itemView.findViewById(R.id.tvTitle)
val desc : TextView = itemView.findViewById(R.id.tvDescription)
val source : TextView = itemView.findViewById(R.id.tvSource)
val published : TextView = itemView.findViewById(R.id.tvPublishedAt)
val imageView : ImageView = itemView.findViewById(R.id.ivArticleImage)
}
}
ACTIVITY XML (SAME AS YOURS):
<?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="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="#+id/paginationProgressBar"
style="?attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:background="#android:color/transparent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
ITEM XML(SAME AS YOURS):
<?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="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:orientation="horizontal">
<ImageView
android:id="#+id/ivArticleImage"
android:layout_width="0dp"
android:layout_height="90dp"
android:layout_weight="0.8"
android:background="#android:drawable/sym_def_app_icon"
android:scaleType="centerCrop" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.2"
android:orientation="vertical">
<TextView
android:id="#+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:ellipsize="end"
android:maxLines="2"
android:textColor="#color/black"
android:textSize="15sp"
android:textStyle="bold"
tools:text="TITLE" />
<TextView
android:id="#+id/tvDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:ellipsize="end"
android:maxLines="3"
android:textColor="#color/black"
android:text="DESCRIPTION" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/tvSource"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="SOURCE"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/tvPublishedAt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="PUBLISHED AT"
android:textColor="#android:color/black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvSource" />
</LinearLayout>
I found the problem causes this spilled of screen, here in this line
val binding = ItemRowBinding.inflate(LayoutInflater.from(parent.context))
you just inflated the inflater of a parent but there are two parameters is missing,
the viewGroup which it "parent and attachToRoot = false
edit your onCreateViewHolder like this
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ArticleViewHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = ItemRowBinding.inflate(inflater,parent,false)
return ArticleViewHolder(binding)
}
I have this home button in top bar:
val activity: ImageButton =findViewById(R.id.activity)
activity.setOnClickListener{
startActivity(Intent(this, ActivitySearch::class.java))
}
Instead of going to a new page, I want to be able to just put a "to top" intent, I have tried to replace:
startActivity(Intent(this, ActivitySearch::class.java))
with
mRecyclerView.smoothScrollToPosition(0);
But this does not work, any ideas? I do have a RecyclerView in use so half way down, I want to be able to click the above button and it goes straight to the top.
Total code:
private val apiService by lazy {
ApiClient.create()
}
private var taskData : ArrayList<TaskResponse> = arrayListOf();
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val activity: ImageButton =findViewById(R.id.activity)
activity.setOnClickListener{
startActivity(Intent(this, ActivitySearch::class.java))
}
val searchbutton:ImageButton=findViewById(R.id.searchbutton)
searchbutton.setOnClickListener{
startActivity(Intent(this, ActivitySearch::class.java))
}
val saves:ImageButton=findViewById(R.id.saves)
saves.setOnClickListener{
startActivity(Intent(this, ActivitySaves::class.java))
}
val settings:ImageButton=findViewById(R.id.settings)
settings.setOnClickListener{
startActivity(Intent(this, ActivitySettings::class.java))
}
loadData();
}
private fun loadData(){
var data = apiService.getMainData();
data.enqueue(object : Callback<ArrayList<TaskResponse>> {
override fun onResponse(
call: Call<ArrayList<TaskResponse>>,
response: Response<ArrayList<TaskResponse>>
) {
if (response.code() == 200) {
taskData = response.body()!!;
displaydata();
}
}
override fun onFailure(call: Call<ArrayList<TaskResponse>>, t: Throwable) {
Log.d("-----------------", t.toString())
}
})
}
private fun displaydata(){
var recyclerView = findViewById<RecyclerView>(R.id.mainList);
var adapter = TaskAdapter(this, taskData);
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = adapter
val swipeRefreshLayout: SwipeRefreshLayout = findViewById(R.id.swipeRefreshLayout)
// load data on swipe refresh.
swipeRefreshLayout.setOnRefreshListener { loadData() }
// Disable the refreshing after data load
swipeRefreshLayout.isRefreshing = false
}
And what i'm using for activity page:
<?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=".MainActivity">
<View
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:background="#6A6A6A"
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/searchbutton"
app:layout_constraintVertical_bias="0.01" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="46dp"
android:gravity="center"
android:text="#string/w2d"
android:textColor="#color/black"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.047"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.01" />
<ImageButton
android:id="#+id/activity"
android:layout_width="45dp"
android:layout_height="46dp"
android:layout_gravity="center"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:background="#FFFFFF"
android:contentDescription="#string/activities"
android:src="#drawable/ic_home"
android:tint="#00B0F0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.107"
app:layout_constraintStart_toEndOf="#+id/textView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.01" />
<ImageButton
android:id="#+id/searchbutton"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:background="#FFFFFF"
android:contentDescription="#string/activities"
android:src="#drawable/ic_search"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.154"
app:layout_constraintStart_toEndOf="#+id/activity"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.01" />
<ImageButton
android:id="#+id/saves"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:background="#FFFFFF"
android:contentDescription="#string/activities"
android:src="#drawable/ic_saves"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.242"
app:layout_constraintStart_toEndOf="#+id/searchbutton"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.01" />
<ImageButton
android:id="#+id/settings"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:background="#FFFFFF"
android:contentDescription="#string/activities"
android:src="#drawable/ic_setting"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.673"
app:layout_constraintStart_toEndOf="#+id/saves"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.01" />
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00FFFFFF"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view"
app:layout_constraintHorizontal_bias="0.0">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/mainList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F0F2F5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view"
app:layout_constraintVertical_bias="0"
tools:layout_editor_absoluteX="0dp" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
This should work for going to the top of the recyclerView.
mRecyclerView.smoothScrollToPosition(0);
or
mRecyclerView.scrollToPosition(0);
When your list changes in recyclerView adapter, you should use that code. Make sure other parts of the code. This should work.
Using a comments answer from #cactustictacs, I edited my code to this and it worked:
val activity: ImageButton =findViewById(R.id.activity)
val mRecyclerView = findViewById<RecyclerView>(R.id.mainList)
activity.setOnClickListener{
mRecyclerView.smoothScrollToPosition(0)
}
I'm trying to create a Two-Way Data binding connection with a LiveData object of a modal class. But when I tries to read the values of user it turns out to be null. Here is my code -
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val viewModal: MainActivityViewModal = ViewModelProvider(
this,
MainActivityViewModalFactory()
).get(MainActivityViewModal::class.java)
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(
this,
R.layout.activity_main
).apply {
this.lifecycleOwner = this#MainActivity
this.viewModal = viewModal
}
}}
Here is the Layout file -
<data>
<variable
name="viewModal"
type="com.weaponx.databindingexample.MainActivityViewModal" />
</data>
<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">
<ImageView
android:id="#+id/ivLogo"
android:layout_width="100dp"
android:layout_height="100dp"
android:contentDescription="#string/logo"
android:tint="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#android:drawable/ic_menu_myplaces" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/clBody"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/ivLogo">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.10" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/tilUserName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/edit_text_bg"
android:hint="#string/user_name"
android:padding="5dp"
app:layout_constraintEnd_toStartOf="#id/guideline2"
app:layout_constraintStart_toEndOf="#id/guideline1"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/tietUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:inputType="text"
android:text="#={viewModal.user.userName}" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/tilPassword"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#drawable/edit_text_bg"
android:hint="#string/password"
android:padding="5dp"
app:layout_constraintEnd_toStartOf="#id/guideline2"
app:layout_constraintStart_toEndOf="#id/guideline1"
app:layout_constraintTop_toBottomOf="#id/tilUserName">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/tietPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:inputType="textPassword"
android:text="#={viewModal.user.password}" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:background="#drawable/button_bg"
android:text="#string/login"
android:onClick="#{() -> viewModal.login()}"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:textSize="18sp"
app:layout_constraintEnd_toStartOf="#id/guideline2"
app:layout_constraintStart_toEndOf="#id/guideline1"
app:layout_constraintTop_toBottomOf="#id/tilPassword" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.90" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Here is the ViewModal
class MainActivityViewModal : ViewModel() {
var user = MutableLiveData<User>()
fun login() {
println("Login Success ${user.value}")
}}
And here is the Modal Class
class User() {
var userName: String = ""
var password: String = ""
override fun toString(): String {
return "User(userName='$userName', password='$password')"
}}
Please tell me any solution or a good way around to this problem
In the MainActivityViewModal define user as just User type rather as MutableLiveData.
I am building an android app which has follow and unfollow feature. I have 3 pages inside ViewPager in main home. Feed consist of post of the users based on interest.
For example, user A follows user B, that posts the article in 3 different interest food, travel and tech. In this case, user A will be able to see his all the post in the 3 different places. What I want is that if user A who follows B unfollow the user B from any of the page it will just get removed from all the pages and refresh the feed.
I want to observe that follow and unfollow event based on the user action and refresh the feed.
How to build observer for this pattern. I am open to use anything expect BroadcastReceiver.
feed_item.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:id="#+id/parent_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:foreground="?android:attr/selectableItemBackgroundBorderless"
app:cardCornerRadius="4dp"
app:cardElevation="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:orientation="horizontal">
<com.facebook.drawee.view.SimpleDraweeView
android:id="#+id/profilePicture"
android:layout_width="40dp"
android:layout_height="40dp"
fresco:roundedCornerRadius="#dimen/feed_rect_card_radius" />
<LinearLayout
android:id="#+id/linearlayout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:maxLines="1"
android:textColor="#color/darkTextColor" />
<TextView
android:id="#+id/data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:fontFamily="#font/noto_sans_bold"
android:textColor="#747474"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
<com.google.android.material.button.MaterialButton
android:id="#+id/followBtn"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|center"
android:layout_marginEnd="32dp"
android:fontFamily="#font/noto_sans_bold"
android:gravity="center"
android:text="#string/follow"
android:textColor="#color/green"
android:textSize="10sp"
app:rippleColor="#color/green"
app:strokeColor="#color/green" />
<ImageView
android:id="#+id/pop_up_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|top"
android:layout_marginTop="4dp"
android:contentDescription="#null"
android:padding="2dp"
android:src="#drawable/ic_more_dark" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingStart="4dp"
android:paddingTop="8dp"
android:paddingEnd="4dp"
android:paddingBottom="8dp">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp">
<com.facebook.drawee.view.SimpleDraweeView
android:id="#+id/image"
android:layout_width="124dp"
android:layout_height="80dp"
fresco:roundedCornerRadius="#dimen/feed_rect_card_radius" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="4dp"
android:contentDescription="#null"
android:src="#drawable/ic_play_circle_feature" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="#dimen/item_offset">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/post_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:ellipsize="end"
android:fontFamily="#font/noto_sans_bold"
android:gravity="start|top"
android:maxLines="2"
android:textColor="#color/darkTextColor"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="#+id/post_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:fontFamily="#font/noto_sans"
android:gravity="start"
android:lineSpacingExtra="4dp"
android:maxLines="2"
android:padding="2dp"
android:textSize="12sp"
android:visibility="gone" />
</LinearLayout>
<LinearLayout
android:id="#+id/categoryLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal" />
<FrameLayout
android:id="#+id/whatsAppShare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:foreground="?attr/selectableItemBackground"
android:padding="4dp">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:contentDescription="#null"
android:src="#drawable/ic_whatsapp_fill" />
</FrameLayout>
</FrameLayout>
</LinearLayout>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
FeedAdapter
class FeedAdapter(private val mContext: Context, val fg: FragmentManager, val nodes: ArrayList<Feed.Item>, val tagId: Int) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
var showLoader: Boolean = false
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType) {
ViewTypes.FEED -> FeedItemVH(LayoutInflater.from(mContext).inflate(R.layout.feed_item, parent, false))
else -> FooterViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_footer, parent, false))
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder) {
is FeedItemVH -> feedItemView(holder, nodes[position].fragments().feedItem())
else -> {
if (showLoader) (holder as FooterViewHolder).itemView.footerLoader.visibility = View.VISIBLE
else (holder as FooterViewHolder).itemView.footerLoader.visibility = View.GONE
}
}
}
private fun feedItemView(holder: FeedItemVH, feed: FeedItem) {
holder.followBtn.setOnClickListener {
//API call to follow/unfollow user and broadcast the result.
}
}
override fun getItemCount(): Int {
return when (nodes.size) {
0 -> 0
else -> nodes.size + 1
}
}
override fun getItemViewType(position: Int): Int {
return when {
position != 0 && position == itemCount - 1 -> ViewTypes.LOADER
else -> {
ViewTypes.FEED
}
}
}
fun showLoader(status: Boolean) {
showLoader = status
Handler().post { this.notifyDataSetChanged() }
}
interface ViewTypes {
companion object {
const val FEED = 1
const val LOADER = 2
}
}
}
Any type of help would be appreciated. Thanks!
I am using a RecyclerView with LinearLayoutManager to fetch Data that I get from my API using Retrofit.
The problem is whenever I try to scroll down it goes to the first position.
Some answers on Github & Here, suggested decreasing the image' resolution and avoid using focusable objects but none of these solutions worked for me.
This is my RecyclerView declaration
<android.support.v7.widget.RecyclerView
app:layout_constraintTop_toBottomOf="#id/btn_rech"
app:layout_constraintStart_toStartOf="parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="50dp"
android:descendantFocusability="beforeDescendants"
android:id="#+id/recyler_view_voiture_occasion">
</android.support.v7.widget.RecyclerView>
This is the CardView item that I use to bind data
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="340dp"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:ignore="MissingConstraints"
app:cardCornerRadius="10sp"
android:elevation="15dp"
android:background="#color/colorGrey"
app:cardBackgroundColor="#color/colorLightGrey"
android:layout_margin="7dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="170dp"
android:background="#color/colorWhite"
android:scaleType="centerCrop"
android:id="#+id/annonce_image"
/>
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:text="#string/general"
android:layout_below="#+id/annonce_image"
android:layout_alignStart="#id/annonce_image"
android:layout_marginStart="10dp"
android:textAppearance="#android:style/TextAppearance.Material.Widget.Toolbar.Title"
android:id="#+id/annonce_info"
/>
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#android:style/TextAppearance.Material.Widget.Toolbar.Subtitle"
android:padding="2dp"
android:layout_marginStart="10dp"
android:id="#+id/annonce_price_info"
android:layout_below="#+id/annonce_info"
android:text="this is a sample text"
/>
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#ffffff"
android:outlineProvider="#color/colorPrimaryDark"
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:weightSum="90"
android:background="#drawable/item_edit_versement"
android:layout_below="#id/annonce_price_info"
android:layout_marginHorizontal="10dp"
android:padding="5dp"
android:id="#+id/infos_holder"
>
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/annonce_km"
android:layout_weight="30"
android:textAlignment="center"
android:text="#string/prix"
android:textSize="14sp"
android:fontFamily="sans-serif-medium"
/>
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/annonce_annee"
android:textAlignment="center"
android:layout_weight="30"
android:text="#string/prix"
android:textSize="14sp"
android:fontFamily="sans-serif-medium"
/>
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_weight="30"
android:id="#+id/annonce_fuel"
android:textAlignment="center"
android:layout_height="wrap_content"
android:text="#string/Fuel"
android:textSize="14sp"
android:fontFamily="sans-serif-medium"
/>
</android.support.v7.widget.LinearLayoutCompat>
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_marginTop="12dp"
android:weightSum="100"
android:gravity="center"
android:layout_below="#id/infos_holder"
android:layout_height="wrap_content">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/item_edit_versement"
android:layout_marginHorizontal="10dp"
android:layout_weight="40"
android:textStyle="bold"
android:textColor="#color/colorPrimaryDark"
android:textSize="14sp"
android:text="Faire un offre"
/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/item_edit_versement"
android:backgroundTint="#424242"
android:drawableStart="#drawable/phone"
android:paddingStart="15dp"
android:text="Call owner"
android:textColor="#color/colorWhite"
android:textStyle="bold"
android:textSize="15sp"
android:layout_marginHorizontal="10dp"
android:layout_weight="35"
/>
</android.support.v7.widget.LinearLayoutCompat>
</RelativeLayout>
</android.support.v7.widget.CardView>
This is my part of Code where I get the data from DB and adapt it into the RV
fun prepareRecyclerView(v : View, id : String){
val layout = LinearLayoutManager(v.context)
layout.orientation = LinearLayoutManager.VERTICAL
val adapter = v.findViewById<RecyclerView>(R.id.recyler_view_voiture_occasion)
adapter.layoutManager = layout
val annonceAdapter = OccasionCarsAdapter(v.context,annonceList )
adapter.adapter = annonceAdapter
//initLineaire(v , R.id.recyler_view_voiture_occasion, LinearLayoutManager.VERTICAL ,annonceAdapter as RecyclerView.Adapter<RecyclerView.ViewHolder>)
}
fun getAnnonceList(id: String, maView: View, filters: VehiculeRechFilters?){
val service = prepareService()
val requestCall = service.GetOccasionAnnouncement(id,
filters!!.minPrix, filters.maxPrix, filters.minAnnee,
filters.minAnnee, filters.maxKm, filters.codeVersion)
requestCall.enqueue(object : Callback<List<VehiculeOccasion>> {
override fun onFailure(call: Call<List<VehiculeOccasion>>, t: Throwable) {
Log.e("Call response" , "Can't get the data" , t.cause)
}
override fun onResponse(call: Call<List<VehiculeOccasion>>, response: Response<List<VehiculeOccasion>>) {
if(response.isSuccessful){
Toast.makeText(this#CustomOccasionFragment.context,response.body().toString(), Toast.LENGTH_LONG).show()
for (e in response.body()!!){
annonceList.add(e)
}
prepareRecyclerView(maView!! ,idUser)
}
else {
Log.i("response assert" , "couldn't get the data correctly")
}
}
})
}