My button on clicked is not working in Kotlin Android Studio - android

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()
}
}

Related

Issue with implementing View Binding in Android

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)

NullPointerExceptionm pleas help me

I am new to programming and want to ask for help because I am stuck on this task.
Clicking on the RecyclerView gets this error.
Below I will attach my code and hope for your help and solutions to the problem. Thank you
Error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.skreep.worko, PID: 24044
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.skreep.worko/com.skreep.worko.DetailActivity}: java.lang.NullPointerException: workout_title must not be null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3308)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3457)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2044)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7560)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Caused by: java.lang.NullPointerException: workout_title must not be null
at com.skreep.worko.DetailActivity.onCreate(DetailActivity.kt:32)
at android.app.Activity.performCreate(Activity.java:7894)
at android.app.Activity.performCreate(Activity.java:7881)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3283)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3457) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2044) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:224) 
at android.app.ActivityThread.main(ActivityThread.java:7560) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) 
Workout Adapter
package com.skreep.worko.adapter
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.skreep.worko.DetailActivity
import com.skreep.worko.R
import com.skreep.worko.databinding.ItemWorkoutBinding
import com.skreep.worko.model.WorkoutData
class WorkoutAdapter(var c:Context,var workoutList:ArrayList<WorkoutData>
):RecyclerView.Adapter<WorkoutAdapter.WorkoutViewHolder>()
{
inner class WorkoutViewHolder(var v:ItemWorkoutBinding): RecyclerView.ViewHolder(v.root){}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WorkoutViewHolder {
val inflter = LayoutInflater.from(parent.context)
val v = DataBindingUtil.inflate<ItemWorkoutBinding>(
inflter, R.layout.item_workout,parent,
false)
return WorkoutViewHolder(v)
}
override fun onBindViewHolder(holder: WorkoutViewHolder, position: Int) {
val newList = workoutList[position]
holder.v.isWorkouts = workoutList[position]
holder.v.root.setOnClickListener {
val name = newList.name
val description = newList.description
val fullTime = newList.fullTime
val workoutDesc = newList.workoutDesc
val workoutName = newList.workoutName
val workoutTime = newList.workoutTime
val mIntent = Intent(c,DetailActivity::class.java)
mIntent.putExtra("description",description)
mIntent.putExtra("name",name)
mIntent.putExtra("fullTime",fullTime)
mIntent.putExtra("workoutDesc",workoutDesc)
mIntent.putExtra("workoutName",workoutName)
mIntent.putExtra("workoutTime",workoutTime)
c.startActivity(mIntent)
}
}
override fun getItemCount(): Int {
return workoutList.size
}
}
Detail Activity
package com.skreep.worko
import android.os.Bundle
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.firebase.database.DatabaseReference
import com.skreep.worko.adapter.WorkoutAdapter
import com.skreep.worko.model.WorkoutData
import kotlinx.android.synthetic.main.activity_detail.*
import kotlinx.android.synthetic.main.activity_home.*
import kotlinx.android.synthetic.main.item_workout.*
import kotlinx.android.synthetic.main.item_workout_detail.*
class DetailActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_detail)
val workoutIntent = intent
val name = workoutIntent.getStringExtra("name")
val description = workoutIntent.getStringExtra("description")
val workoutName = workoutIntent.getStringExtra("workoutName")
detail_name.text = name
detail_desc.text = description
workout_title.text = workoutName
}
}
Home Activity
package com.skreep.worko
import android.os.Bundle
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.firebase.database.*
import com.skreep.worko.adapter.WorkoutAdapter
import com.skreep.worko.model.WorkoutData
import kotlinx.android.synthetic.main.activity_detail.*
import kotlinx.android.synthetic.main.activity_home.*
class HomeActivity : BaseActivity() {
lateinit var mDataBase:DatabaseReference
private lateinit var workoutList:ArrayList<WorkoutData>
private lateinit var mAdapter:WorkoutAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
workoutList = ArrayList()
mAdapter = WorkoutAdapter(this, workoutList)
recyclerworkoutList.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
recyclerworkoutList.setHasFixedSize(true)
recyclerworkoutList.adapter = mAdapter
getWorkoutData()
}
private fun getWorkoutData() {
mDataBase = FirebaseDatabase.getInstance().getReference("Workout")
mDataBase.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
if (snapshot.exists()) {
for (userSnapshot in snapshot.children) {
val workout = userSnapshot.getValue(WorkoutData::class.java)
workoutList.add(workout!!)
}
recyclerworkoutList.adapter = mAdapter
}
}
override fun onCancelled(error: DatabaseError) {
Toast.makeText(this#HomeActivity,
error.message, Toast.LENGTH_SHORT).show()
}
})
}
}
XML Activity Detail
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="isData"
type="com.skreep.worko.model.WorkoutData" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/gray"
tools:context=".DetailActivity">
<ImageView
android:id="#+id/back_ic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="20dp"
android:padding="20dp"
android:src="#drawable/ic_back">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="20dp"
android:fontFamily="#font/poppins"
android:padding="20dp"
android:text="#string/app_name"
android:textSize="20sp"></TextView>
<TextView
android:id="#+id/detail_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="120dp"
android:fontFamily="#font/poppins_bold"
android:text="#{isData.name}"
android:textColor="#color/darkblue"
android:textSize="26sp"></TextView>
<TextView
android:id="#+id/detail_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/detail_name"
android:layout_alignLeft="#+id/detail_name"
android:fontFamily="#font/poppins"
android:text="#{isData.workoutDesc}"
android:textColor="#color/lightgray">
</TextView>
<TextView
android:id="#+id/tv_workout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="310dp"
android:fontFamily="#font/poppins_bold"
android:gravity="center"
android:text="Упражнения"
android:textColor="#color/darkblue"
android:textSize="20sp"></TextView>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/workoutListDetail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_workout"
android:orientation="vertical"
android:padding="10dp"
tools:itemCount="1"
/>
</RelativeLayout>
</layout>
Xml activity Home
<?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">
<data>
<variable
name="isWorkouts"
type="com.skreep.worko.model.WorkoutData" />
</data>
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/darkblue"
android:orientation="vertical"
tools:context=".HomeActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:fontFamily="#font/poppins"
android:gravity="center"
android:padding="20dp"
android:text="#string/app_name"
android:textColor="#color/white"
android:textSize="20sp" />
<TextView
android:id="#+id/welcome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/poppins"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:text="#string/welcome"
android:textColor="#color/white"
android:textSize="30sp"
tools:ignore="MissingConstraints" />
<TextView
android:id="#+id/welcome_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/poppins"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:text="#string/welcome_description"
android:textColor="#color/desc_color"
android:textSize="14sp"
app:layout_constraintTop_toBottomOf="#id/welcome" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerworkoutList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal"
android:paddingTop="40dp"
tools:itemCount="1"
tools:listitem="#layout/item_workout" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
</layout>
item_workout xml
<?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">
<data>
<variable
name="isWorkouts"
type="com.skreep.worko.model.WorkoutData" />
</data>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".SplashActivity">
<androidx.cardview.widget.CardView
android:layout_width="250dp"
android:layout_height="350dp"
android:layout_margin="10dp"
android:backgroundTint="#color/gray"
app:cardCornerRadius="10dp"
app:cardElevation="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/watch_ic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:src="#drawable/ic_watch"
app:tint="#color/darkblue"></ImageView>
<TextView
android:id="#+id/tvfulltime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="50dp"
android:layout_marginTop="26dp"
android:fontFamily="#font/poppins"
android:text="#{isWorkouts.fullTime}"
android:textSize="10sp"></TextView>
<TextView
android:id="#+id/tvtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="27dp"
android:layout_marginTop="80dp"
android:fontFamily="#font/poppins_bold"
android:text="#{isWorkouts.name}"
android:textColor="#color/darkblue"
android:textSize="24sp">
</TextView>
<TextView
android:id="#+id/tvdesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvtitle"
android:layout_marginStart="10dp"
android:padding="20dp"
android:text="#{isWorkouts.description}"
android:textColor="#color/lightgray"
android:textSize="12sp"></TextView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="30dp"
android:layout_marginBottom="30dp"
android:src="#drawable/ic_next"
>
</ImageView>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</layout>
item workout detail xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="isWorkout"
type="com.skreep.worko.model.WorkoutData" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".SplashActivity">
<androidx.cardview.widget.CardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="150dp"
android:backgroundTint="#color/gray">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/workout_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:fontFamily="#font/poppins_bold"
android:text="#{isWorkout.workoutName}"
android:textColor="#color/darkblue"
android:textSize="24sp"></TextView>
<TextView
android:id="#+id/workout_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/workout_title"
android:layout_alignLeft="#+id/workout_title"
android:fontFamily="#font/poppins"
android:text="#{isWorkout.workoutName}"
android:textColor="#color/lightgray"
android:textSize="12sp"></TextView>
<TextView
android:id="#+id/workout_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/workout_title"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:text="#{isWorkout.workoutTime}"
android:textColor="#color/darkblue"
android:textSize="12sp">
</TextView>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
</layout>
Check your activity_detail XML file is missing a Textview called workout_title
You have it in a different XML layout file workout_detail xml

How do i show another output below the first?

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

There is no change to the second activity element when intending

I made a simple application using Kotlin. I want to change pages. I create a new Activity and call it from the MainActivity. When I run AVD, then switch pages, the page displayed is empty. There are no elements at all like new Activities.
And this is the intent code for the second Activity:
class MainActivity : AppCompatActivity() {
already_have_account_text_view.setOnClickListener {
Log.d("MainActivity", "Try to login activity")
//launch the login activity
val intent = Intent(this, LoginActivity::class.java)
startActivity(intent)
}
LoginActivity code:
package com.example.kotlinmessanger
import android.os.Bundle
import android.os.PersistableBundle
import androidx.appcompat.app.AppCompatActivity
class LoginActivity: AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
super.onCreate(savedInstanceState, persistentState)
setContentView(R.layout.activity_login)
}
}
And this is the xml of the second Activity:
<?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"
android:padding="20dp"
android:background="#8BC34A"
tools:context=".LoginActivity">
<EditText
android:id="#+id/email_edittext_login"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
android:layout_marginRight="32dp"
android:background="#FFFFFF"
android:ems="10"
android:hint="E-mail"
android:inputType="textEmailAddress"
android:paddingLeft="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/password_edittext_login"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginTop="8dp"
android:background="#FFFFFF"
android:ems="10"
android:paddingLeft="16dp"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="#+id/email_edittext_login"
app:layout_constraintStart_toStartOf="#+id/email_edittext_login"
app:layout_constraintTop_toBottomOf="#+id/email_edittext_login" />
<Button
android:id="#+id/login_button_login"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginTop="8dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="#+id/password_edittext_login"
app:layout_constraintStart_toStartOf="#+id/password_edittext_login"
app:layout_constraintTop_toBottomOf="#+id/password_edittext_login" />
</androidx.constraintlayout.widget.ConstraintLayout>
Please change width of android:layout_width="0dp" 0dp to Any specific width of all the EditText and Button and add some proper width to them

Create multiple sets of plus and minus buttons with TextView on same page of App (Kotlin)

I have two controls that i can increase or decrease an integer. I click on either the first or second set of plus and minus buttons,despite being separate functions, it'll display the Integers on both outputs. For example if I click on the plus and minus button for the integer_number_1, it'll display the numbers on both the integer_number_1 and integer_number_2 outputs/TextView.
How do I fix this??? I've so many different ways and none of them worked.
Here's my xml section:
<?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">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
android:layout_marginBottom="549dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="#+id/decrease_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" />
<TextView
android:id="#+id/integer_number_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="16dp"
android:layout_marginRight="40dp"
android:layout_marginBottom="16dp"
android:inputType="number"
android:text="0"
android:textSize="70sp"
android:textStyle="bold" />
<Button
android:id="#+id/increase_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
</LinearLayout>
<Button
android:id="#+id/decrease_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/integer_number_2"
app:layout_constraintHorizontal_bias="0.513"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout"
app:layout_constraintVertical_bias="0.317" />
<Button
android:id="#+id/increase_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.579"
app:layout_constraintStart_toEndOf="#+id/integer_number_2"
app:layout_constraintTop_toBottomOf="#+id/linearLayout"
app:layout_constraintVertical_bias="0.313" />
<TextView
android:id="#+id/integer_number_2"
android:layout_width="46dp"
android:layout_height="99dp"
android:layout_marginStart="46dp"
android:layout_marginLeft="46dp"
android:inputType="number"
android:text="0"
android:textSize="70sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.426"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout"
app:layout_constraintVertical_bias="0.297" />
</androidx.constraintlayout.widget.ConstraintLayout>
And here's my kt file:
package com.example.plus_and_minus_input
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
increase_1.setOnClickListener { increaseInteger1() }
decrease_1.setOnClickListener { decreaseInteger1() }
increase_2.setOnClickListener { increaseInteger2() }
decrease_2.setOnClickListener { decreaseInteger2() }
}
fun increaseInteger1() {
display(integer_number_1.text.toString().toInt() + 1)
}
fun decreaseInteger1() {
display(integer_number_1.text.toString().toInt() - 1)
}
fun increaseInteger2() {
display(integer_number_2.text.toString().toInt() + 1)
}
fun decreaseInteger2() {
display(integer_number_2.text.toString().toInt() - 1)
}
private fun display(number: Int) {
integer_number_1.setText("$number")
integer_number_2.setText("$number")
}
}
I think you need to separate this following display methods.
private fun display(number: Int) {
integer_number_1.setText("$number")
integer_number_2.setText("$number")
}
to
private fun display_number_1(number: Int) {
integer_number_1.setText("$number")
}
and
private fun display_number_2(number: Int) {
integer_number_2.setText("$number")
}

Categories

Resources