the 1st image
so this the 1st data, then when i try to input another data
2nd pic
how to show another input under the 1st input data?
here's my 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="match_parent"
android:orientation="vertical"
tools:context=".MainActivity5">
<TextView
android:id="#+id/textView15"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/nama_matkul" />
<Spinner
android:id="#+id/nama_matkul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="#array/pilih_matkul" />
<TextView
android:id="#+id/textView16"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Nilai" />
<EditText
android:id="#+id/fill_nilai"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="#string/fill_nilai"
android:importantForAutofill="no"
android:hint="#string/edtx"/>
<Button
android:id="#+id/dang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/dang" />
<TextView
android:id="#+id/tampilkan"
android:layout_width="match_parent"
android:layout_height="379dp"
android:inputType="textMultiLine"
android:minLines="2"
android:maxLines="9"/>
and my kt
class MainActivity5 : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main5)
val nama_matkul= findViewById<Spinner>(R.id.nama_matkul)
val fill_nilai = findViewById<EditText>(R.id.fill_nilai)
val dang = findViewById<Button>(R.id.dang)
val tampilkan = findViewById<TextView>(R.id.tampilkan)
dang.setOnClickListener {
tampilkan.text= "mata kuliah :"+nama_matkul.selectedItem.toString()+"\n Nilai :"+fill_nilai.text
}
}
}
and sorry for poor english
Related
I am facing a problem which is my button ID is well-configured and in my main activity, I am calling the button using the correct ID too, but after I run the project on the emulator, the button is not working after I clicked on it for multiple times.
Can anyone help me to solve the problem?
This is my xml file:
<?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:background="#color/bgColor"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginBottom="20dp"
android:text="Mobile Quiz Application"
android:textColor="#color/white"
android:textSize="30sp"
android:textStyle="bold" />
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
app:cardCornerRadius="10dp"
android:background="#color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="10dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="Welcome To The Quiz App!"
android:textColor="#color/txtTitle"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="Please enter your name before starting the quiz."
android:textColor="#color/txtDesc"
android:textSize="15sp" />
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp">
<androidx.appcompat.widget.AppCompatEditText
android:id="#+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Your name......"
android:inputType="textCapWords"
android:textColor="#color/txtTitle"
android:textColorHint="#color/txtDesc" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="#+id/btn_Start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="Continue"
android:textStyle="bold">
</Button>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
This is my main file:
package com.example.quizapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btnStart = findViewById<Button>(R.id.btn_Start)
val nameInput = findViewById<EditText>(R.id.etName)
btnStart.setOnClickListener {
Toast.makeText(this#MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
}
}
}
findViewById is an old method.
i would recommend you to use with ViewBinding.
click here to documentation
You can use viewBinding
add line to build.gradle
buildFeatures{
viewBinding true
}
and update your activity like this
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.btnStart.setOnClickListener{
Toast.makeText(this#MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
}
}
I am Trying to Achieve the Exposed Drop-Down Menu in Android I have tried this one but can't identify where I went wrong.
DropDown_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.textview.MaterialTextView
android:id="#+id/textViewFeelings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="TextView"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold" />
</androidx.appcompat.widget.LinearLayoutCompat>
BottomSheetDialogConnectWifiFragment.kt
class BottomSheetDialogConnectWifiFragment : BottomSheetDialogFragment() {
private lateinit var _binding: FragmentBottomSheetDialogConnectWifiBinding
private val items = listOf("Material", "Design", "Components", "Android")
private lateinit var ActivityContext: Context
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onAttach(context: Context) {
ActivityContext = context
super.onAttach(context)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentBottomSheetDialogConnectWifiBinding.inflate(inflater, container, false)
return _binding.root
//
// val arrayAdapter = ArrayAdapter(_binding.root.context, R.layout.dropdown_item, items)
// _binding.autoCompleteTextFieldSsidType.setAdapter(arrayAdapter)
_binding.autoCompleteTextFieldSsidType.apply {
setAdapter(
ArrayAdapter(
ActivityContext,
R.layout.dropdown_item,
items
)
)
}
}
companion object {
const val TAG = "CustomBottomSheetDialogFragment"
}
}
fragment_bottom_sheet_dialog_connect_wifi
<?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:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context=".Fragments.BottomSheetDialogConnectWifiFragment">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<androidx.cardview.widget.CardView
android:layout_width="100dp"
android:layout_height="2dp"
android:layout_centerHorizontal="true"
android:elevation="10dp"
android:foregroundGravity="center_vertical"
app:cardBackgroundColor="#color/black"
app:cardCornerRadius="100dp">
<!--YOUR CONTENT-->
</androidx.cardview.widget.CardView>
<com.google.android.material.textview.MaterialTextView
android:id="#+id/idTVCourseTracks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Add Network"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="15sp" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/tilSsid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/idTVCourseTracks"
android:keyboardNavigationCluster="true">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter The SSID" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textFieldSsidType"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tilSsid"
android:layout_marginTop="12dp">
<AutoCompleteTextView
android:id="#+id/autoCompleteTextFieldSsidType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Enter The SSID"
android:inputType="none" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textFieldPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textFieldSsidType"
android:layout_marginTop="12dp">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:id="#+id/idBtnDismiss"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textFieldPassword"
android:layout_marginTop="12dp"
android:text="Connect"
android:textAllCaps="false"
android:theme="#style/ShapeAppearanceOverlay.Material3.Button" />
</RelativeLayout>
</ScrollView>
</com.google.android.material.card.MaterialCardView>
I Created An bottomSheet in Android/kotlin and trying to solve this cant understood where i missing some thing to
i have try with .Apply{}Method And also with the normally but both of them not worked for me
I am trying to implement view binding to one of my activity. The issue is I am not able to access the views and it shows unresolved reference.
Now when I am using tools:viewBindingIgnore="true"
then there are no issues with the views and I am able to access them but then I am getting unresolved symbol for the activity class.
activity_single_movie.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:visibility="gone"/>
<TextView
android:id="#+id/text_error"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Connection gone!!"
android:textColor="#color/white"
android:visibility="gone" />
<ScrollView
app:layout_constraintTop_toTopOf= "parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/movie_poster"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:layout_gravity="center"
android:scaleType="fitCenter"
android:src="#drawable/placeholder_image"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="8dp" >
<TextView
android:id="#+id/movie_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="movie"
android:textStyle="bold"
android:textSize="18sp" />
<TextView
android:id="#+id/movie_tagline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sub title"
android:textStyle="bold"
android:textSize="14sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="movie info"
android:textStyle="bold"
android:textSize="14sp" />
//Layout for Release Date
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="Release Date: "
android:textSize="12sp"
style="bold" />
<TextView
android:id="#+id/movie_release_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="2019"
android:textSize="12sp"
style="bold" />
</LinearLayout>
//Layout for Rating
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="Rating: "
android:textSize="12sp"
style="bold" />
<TextView
android:id="#+id/movie_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="5/10"
android:textSize="12sp"
style="bold" />
</LinearLayout>
//Layout for Runtime
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="Runtime: "
android:textSize="12sp"
style="bold" />
<TextView
android:id="#+id/movie_runtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="2hrs 13mins"
android:textSize="12sp"
style="bold" />
</LinearLayout>
//Layout for Budget
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="Budget: "
android:textSize="12sp"
style="bold" />
<TextView
android:id="#+id/movie_budget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="$550Million"
android:textSize="12sp"
style="bold" />
</LinearLayout>
//Layout for revenue
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="Budget: "
android:textSize="12sp"
style="bold" />
<TextView
android:id="#+id/movie_revenue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="$550Million"
android:textSize="12sp"
style="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
In SingleMovie.kt
package com.example.movieapp.ui.single_movie_details
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import com.bumptech.glide.Glide
import com.example.movieapp.R
import com.example.movieapp.data.api.POSTER_BASE_URL
import com.example.movieapp.data.api.TheMovieDBClient
import com.example.movieapp.data.api.TheMovieDBInterface
import com.example.movieapp.data.repository.NetworkState
import com.example.movieapp.data.vo.MovieDetails
import com.example.movieapp.databinding.ActivitySingleMovieBinding
import java.text.NumberFormat
import java.util.*
class SingleMovie : AppCompatActivity() {
private lateinit var viewModel: SingleMovieViewModel
private lateinit var movieRepository: MovieDetailsRepository
private lateinit var binding: ActivitySingleMovieBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivitySingleMovieBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
val movieId: Int = intent.getIntExtra("id", 1)
val apiService: TheMovieDBInterface = TheMovieDBClient.getClient()
movieRepository = MovieDetailsRepository(apiService)
viewModel = getViewModel(movieId)
viewModel.movieDetails.observe(this, Observer {
bindUi(it)
})
viewModel.movieDetailsNetworkState.observe(this, Observer {
binding.progress_bar.setVisibility(if (it == NetworkState.LOADING) View.VISIBLE else View.GONE)
binding.text_error.setVisibility( if(it == NetworkState.ERROR) View.VISIBLE else View.GONE)
})
}
fun bindUi(it: MovieDetails){
binding.movie_title.setText(it.title)
binding.movie_tagline.setText(it.tagline)
binding.movie_release_date.setText(it.releaseDate)
binding.movie_rating.setText(it.voteAverage.toString())
binding.movie_runtime.setText(it.runtime.toString() + "minutes")
val formatCurrency: NumberFormat = NumberFormat.getCurrencyInstance(Locale.US)
binding.movie_budget.setText(formatCurrency.format(it.budget))
binding.movie_revenue.setText(formatCurrency.format(it.revenue))
//for poster of movie
val moviePosterURL: String = POSTER_BASE_URL + it.posterPath
Glide.with(this)
.load(moviePosterURL)
.into(binding.movie_poster)
}
private fun getViewModel(movieId: Int): SingleMovieViewModel {
return ViewModelProviders.of(this, object: ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return SingleMovieViewModel(movieRepository, movieId) as T
}
}) [SingleMovieViewModel::class.java]
}
}
Errors while using tools:viewBindingIgnore="true"
Unresolved reference: ActivitySingleMovieBinding
Errors without using tools:viewBindingIgnore="true"
Unresolved reference: progress_bar
and similar for all the views
View Binding converts underscores to camelCase, the correct syntax is binding.progressBar
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:visibility="gone"/>
binding.progressBar.setVisibility(if (it == NetworkState.LOADING) View.VISIBLE else View.GONE)
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 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")
}
}
})
}