Comparing drawables imageButtons in android - android

I have 6 image buttons with drawables, I'm trying to compare them using getDrawable().
Here's my xml code
'''
<
?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=".TicTacToe">
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="25dp"
android:layout_marginTop="147dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="14dp"
android:onClick="change"
app:layout_constraintBottom_toTopOf="#+id/imageButton2"
app:layout_constraintEnd_toStartOf="#+id/imageButton01"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/blank" />
<ImageButton
android:id="#+id/imageButton01"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="147dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="14dp"
android:onClick="change"
app:layout_constraintBottom_toTopOf="#+id/imageButton02"
app:layout_constraintEnd_toStartOf="#+id/imageButton001"
app:layout_constraintStart_toEndOf="#+id/imageButton1"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/blank" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="24dp"
android:layout_marginEnd="6dp"
android:layout_marginBottom="10dp"
android:onClick="change"
app:layout_constraintBottom_toTopOf="#+id/imageButton3"
app:layout_constraintEnd_toStartOf="#+id/imageButton02"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageButton1"
app:srcCompat="#drawable/blank" />
<ImageButton
android:id="#+id/imageButton001"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="148dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="13dp"
android:onClick="change"
app:layout_constraintBottom_toTopOf="#+id/imageButton002"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageButton01"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/blank" />
<ImageButton
android:id="#+id/imageButton02"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="7dp"
android:layout_marginBottom="10dp"
android:onClick="change"
app:layout_constraintBottom_toTopOf="#+id/imageButton03"
app:layout_constraintEnd_toStartOf="#+id/imageButton002"
app:layout_constraintStart_toEndOf="#+id/imageButton2"
app:layout_constraintTop_toBottomOf="#+id/imageButton01"
app:srcCompat="#drawable/blank" />
<ImageButton
android:id="#+id/imageButton002"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="14dp"
android:layout_marginBottom="10dp"
android:onClick="change"
app:layout_constraintBottom_toTopOf="#+id/imageButton003"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageButton02"
app:layout_constraintTop_toBottomOf="#+id/imageButton001"
app:srcCompat="#drawable/blank" />
<ImageButton
android:id="#+id/imageButton3"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="24dp"
android:layout_marginEnd="6dp"
android:layout_marginBottom="200dp"
android:onClick="change"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/imageButton03"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageButton2"
app:srcCompat="#drawable/blank" />
<ImageButton
android:id="#+id/imageButton03"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="7dp"
android:layout_marginBottom="200dp"
android:onClick="change"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/imageButton003"
app:layout_constraintStart_toEndOf="#+id/imageButton3"
app:layout_constraintTop_toBottomOf="#+id/imageButton02"
app:srcCompat="#drawable/blank" />
<ImageButton
android:id="#+id/imageButton003"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="14dp"
android:layout_marginBottom="200dp"
android:onClick="change"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageButton03"
app:layout_constraintTop_toBottomOf="#+id/imageButton002"
app:srcCompat="#drawable/blank" />
</androidx.constraintlayout.widget.ConstraintLayout
'''
kotlin code
'''
class TicTacToe : AppCompatActivity() {
var spot1: ImageButton? =null
var spot01: ImageButton? =null
var spot001: ImageButton? =null
var spot2: ImageButton? =null
var spot02: ImageButton? =null
var spot002: ImageButton? =null
var spot3: ImageButton? =null
var spot03: ImageButton? =null
var spot003: ImageButton? =null
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_tic_tac_toe)
spot1 = findViewById<ImageButton>(R.id.imageButton1)
spot01 = findViewById<ImageButton>(R.id.imageButton01)
spot001 = findViewById<ImageButton>(R.id.imageButton001)
spot2 = findViewById<ImageButton>(R.id.imageButton2)
spot02 = findViewById<ImageButton>(R.id.imageButton02)
spot002 = findViewById<ImageButton>(R.id.imageButton002)
spot3 = findViewById<ImageButton>(R.id.imageButton3)
spot03 = findViewById<ImageButton>(R.id.imageButton03)
spot003 = findViewById<ImageButton>(R.id.imageButton003)
}
val photos: IntArray= intArrayOf(R.drawable.x, R.drawable.o, R.drawable.blank)
var turn = 1
var winState = false
fun change(view: View){
var spot = findViewById<ImageButton>(view.getId()) //as Button
if(turn == 1) {
spot.setImageResource(photos[0])
turn = turn * -1
}
else{
spot.setImageResource(photos[1])
turn = turn * -1
}
if(spot1?.getDrawable() == spot01?.getDrawable() &&
spot1?.getDrawable() == spot001?.getDrawable() &&
spot1?.getDrawable() != photos[2].toDrawable())
{
winState = true;
Toast.makeText(getApplicationContext(),"Winner",Toast.LENGTH_SHORT).show();
}
}
}
'''

Drawable is an Object, and as such needs to be compared using the equals() method. Anyway, I am not sure that it will work either, because I am not sure of how a Drawable implements that method, so it might not be suitable for your case.
Also, you are using your view to check the state of your business logic, and that's not ideal at all. The best solution would be to have a Map or a matrix (let's say, an array with a dimension of [3][3]), where you store the actual value of the single spots, and then you reconcile the drawables with the values in the matrix.
I know that this would affect everything in your code and you have to heavely change it, so a quickier solution could be to use android:tag on your ImageButtons. You can specify any Object on it, so you could also use and Integer and compare it, always using the equals() method. This means you can rely on the tag for your logic.

Related

LuckyWheel not showing on the android studio design

Im new to Kotlin and android studio.
I was trying to use this LuckyWheel from https://github.com/mmoamenn/LuckyWheel_Android
and try to make the wheel spin and rotate with the items (ex dice1, dice2, and so on)
but i'm not sure what i'm doing wrong.
here is the MainActivity.kt
class MainActivity : AppCompatActivity() {
var luckyWheel1: LuckyWheel? = findViewById(R.id.wheel1)
var dice1 = WheelItem(Color.LTGRAY, BitmapFactory.decodeResource(resources, R.drawable.dice_1))
var dice2 = WheelItem(Color.BLUE, BitmapFactory.decodeResource(resources, R.drawable.dice_2))
var dice3 = WheelItem(Color.BLACK, BitmapFactory.decodeResource(resources, R.drawable.dice_3))
var dice4 = WheelItem(Color.GRAY, BitmapFactory.decodeResource(resources, R.drawable.dice_4))
var dice5 = WheelItem(Color.RED, BitmapFactory.decodeResource(resources, R.drawable.dice_5))
var dice6 = WheelItem(Color.BLACK, BitmapFactory.decodeResource(resources, R.drawable.dice_6))
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val wheelItems = mutableListOf(dice1, dice2, dice3, dice4, dice5, dice6)
luckyWheel1?.addWheelItems(wheelItems)
}
}
and here is my activity_main.xml
<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="350dp" android:layout_gravity="center_vertical" tools:context=".MainActivity">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_end="206dp"/>
<com.bluehomestudio.luckywheel.LuckyWheel
android:id="#+id/wheel1"
android:layout_width="150dp"
android:layout_height="150dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:background_color="#color/white" />
<Button
android:id="#+id/buttonRoll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Roll"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/tie" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/winner"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="#id/guideline1"
app:layout_constraintTop_toTopOf="parent"/>
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/guideline1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:srcCompat="#drawable/winner" />
</androidx.constraintlayout.widget.ConstraintLayout>
I'm not sure what i'm missing or doing something wrong.. please help!
I think I understand that you need Luckywheel with the items in there.
not sure if I have put those "dices" in the wheel using bitmap?
not sure if I need some kind of canvas?
please let me know.
Thank you.

Unresolved reference to constraintLayout variable in Kotlin code in android app

I am following this tutorial to create an android app with space animation stuff. In the step titled "Transition Manager" you're instructed to add the following code to your MainActivity.kt file:
`
constraintSet1.clone(constraintLayout) //1
constraintSet2.clone(this, R.layout.activity_main) //2
departButton.setOnClickListener { //3
//apply the transition
TransitionManager.beginDelayedTransition(constraintLayout) //4
val constraint = if (!isOffscreen) constraintSet1 else constraintSet2
isOffscreen = !isOffscreen
constraint.applyTo(constraintLayout) //5
}
`
When this code is added the variable "constraintLayout" throws up an unresolved reference error
I think constraintLayout is supposed to be pointing to something in the xml and something is failing to make that happen, I just don't know what.
The kotlin code after adding the above block is:
`
package com.raywenderlich.android.razegalactic
import android.os.Bundle
import android.support.constraint.*
import android.support.v7.app.AppCompatActivity
import android.transition.TransitionManager
import kotlinx.android.synthetic.main.keyframe1.*
/**
* Main Screen
*/
class MainActivity : AppCompatActivity() {
private val constraintSet1 = ConstraintSet()
private val constraintSet2 = ConstraintSet()
private var isOffscreen = true
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.keyframe1)
switch1.setOnCheckedChangeListener { _, isChecked ->
switch1.setText(if (isChecked) R.string.round_trip else R.string.one_way)
constraintSet1.clone(constraintLayout) //1
constraintSet2.clone(this, R.layout.activity_main) //2
departButton.setOnClickListener { //3
//apply the transition
TransitionManager.beginDelayedTransition(constraintLayout) //4
val constraint = if (!isOffscreen) constraintSet1 else constraintSet2
isOffscreen = !isOffscreen
constraint.applyTo(constraintLayout) //5
}
}
}
}
`
The main xml is as follows:
`
<android.support.constraint.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:id="#+id/ConstraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="#+id/flightsIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/rocket_icon"
app:layout_constraintBottom_toBottomOf="#+id/spaceStationIcon"
app:layout_constraintEnd_toStartOf="#+id/roverIcon"
app:layout_constraintStart_toEndOf="#+id/spaceStationIcon"
app:layout_constraintTop_toTopOf="#+id/spaceStationIcon" />
<ImageView
android:id="#+id/spaceStationIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="15dp"
android:src="#drawable/space_station_icon"
app:layout_constraintEnd_toStartOf="#+id/flightsIcon"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/roverIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/rover_icon"
app:layout_constraintBottom_toBottomOf="#+id/flightsIcon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/flightsIcon"
app:layout_constraintTop_toTopOf="#+id/flightsIcon" />
<TextView
android:id="#+id/spaceStationLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="#string/space_stations"
app:layout_constraintEnd_toEndOf="#+id/spaceStationIcon"
app:layout_constraintStart_toStartOf="#+id/spaceStationIcon"
app:layout_constraintTop_toBottomOf="#+id/spaceStationIcon" />
<TextView
android:id="#+id/textView1"
android:layout_width="124dp"
android:layout_height="98dp"
android:layout_marginEnd="40dp"
android:background="#color/colorPrimary"
android:gravity="center"
android:paddingEnd="20dp"
android:text="#string/dca"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="#+id/doubleArrowsIcon"
app:layout_constraintEnd_toEndOf="#+id/doubleArrowsIcon"
app:layout_constraintTop_toTopOf="#+id/doubleArrowsIcon" />
<TextView
android:id="#+id/roverLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="#string/rovers"
app:layout_constraintEnd_toEndOf="#+id/roverIcon"
app:layout_constraintStart_toStartOf="#+id/roverIcon"
app:layout_constraintTop_toBottomOf="#+id/roverIcon" />
<TextView
android:id="#+id/textView2"
android:layout_width="124dp"
android:layout_height="98dp"
android:layout_marginStart="40dp"
android:background="#color/colorPrimary"
android:gravity="center"
android:paddingStart="20dp"
android:text="#string/mars"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="#+id/doubleArrowsIcon"
app:layout_constraintStart_toStartOf="#+id/doubleArrowsIcon"
app:layout_constraintTop_toTopOf="#+id/doubleArrowsIcon" />
<TextView
android:id="#+id/flightsLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="#string/flights"
app:layout_constraintEnd_toEndOf="#+id/flightsIcon"
app:layout_constraintStart_toStartOf="#+id/flightsIcon"
app:layout_constraintTop_toBottomOf="#+id/flightsIcon" />
<ImageView
android:id="#+id/doubleArrowsIcon"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginBottom="40dp"
android:src="#drawable/double_arrows"
app:layout_constraintBottom_toTopOf="#+id/guideline1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Switch
android:id="#+id/switch1"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="200dp"
android:background="#color/colorAccent"
android:checked="false"
android:padding="8dp"
android:switchPadding="24dp"
android:text="#string/one_way"
android:textColor="#android:color/white"
app:layout_constraintStart_toStartOf="#+id/guideline2"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#color/colorAccent"
android:padding="8dp"
android:text="#string/traveller"
android:textColor="#android:color/white"
app:layout_constraintStart_toStartOf="#+id/guideline2"
app:layout_constraintTop_toBottomOf="#+id/switch1" />
<ImageView
android:id="#+id/rocketIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="8dp"
android:src="#drawable/rocket_icon"
app:layout_constraintCircle="#id/galaxyIcon"
app:layout_constraintCircleAngle="270"
app:layout_constraintCircleRadius="100dp" />
<ImageView
android:id="#+id/galaxyIcon"
android:layout_width="90dp"
android:layout_height="90dp"
android:src="#drawable/galaxy"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.495" />
<Button
android:id="#+id/departButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/depart"
android:textColor="#android:color/white"
app:backgroundTint="#color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_editor_absoluteX="158dp" />
<android.support.constraint.Guideline
android:id="#+id/guideline1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="0dp" />
<android.support.constraint.Guideline
android:id="#+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintGuide_percent="1" />
</android.support.constraint.ConstraintLayout>
`
can anyone tell me what I might be missing or how this can be fixed?
Thanks in advance.
You're suppose to reference a view from the xml before you can use it.
Do this, also if the name of the layout the MainActivity is suppose to use is main.xml, it should be the one your setContentView use not some other layout.
lateinit var cosntraintLayout: ConstraintLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main) // <- main.xml
cosntraintLayout = findViewById<ConstraintLayout>(R.id.ConstraintLayout) // <- add this line
...
...
}
switch1 and departButton will also throw the same error, but I'll leave it for you to figure it out based on the code I provided.

Setting setOnClickListener on a Button in a View Stub

I'm creating a View Stub for my login screen, however I have a question. When I try to set an setOnClickListener on my login button in my viewstub_login.kt file, the button action won't go through. Do I need to change my extension on my Kotlin file, or do I need to put my setOnClickListener in the onStart() function? Thank you!
viewstub_login.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"
android:background="#BF090909">
<androidx.cardview.widget.CardView
android:id="#+id/loginCardView"
android:layout_width="0dp"
android:layout_height="300dp"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
app:cardBackgroundColor="#color/main_purple"
app:cardCornerRadius="8dp"
app:cardElevation="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/logoLoginImageView"
android:layout_width="90dp"
android:layout_height="35dp"
android:layout_marginTop="12dp"
android:contentDescription="#string/login_logo_image"
android:scaleType="fitXY"
android:src="#drawable/skedaddle_services_"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/emailTextInputLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="24dp"
android:textColorHint="#color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/logoLoginImageView">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/emailInputTextView"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#drawable/textview_underlinr"
android:drawableStart="#drawable/outline_mail_white_24"
android:drawablePadding="5dp"
android:hint="#string/e_mail"
android:textColor="#color/white"
android:textColorHint="#color/white" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/passwordTextInputLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="24dp"
android:hint="#string/password"
android:textColorHint="#color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/emailTextInputLayout"
app:passwordToggleEnabled="true"
app:passwordToggleTint="#color/white">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/passwordTextInputView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/textview_underlinr"
android:drawableStart="#drawable/outline_lock_white_24"
android:drawablePadding="5dp"
android:hint="#string/password"
android:inputType="textPassword"
android:textColor="#color/white"
android:textColorHint="#color/white" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="#+id/forgotPasswordButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:background="#android:color/transparent"
android:text="#string/forgot_password"
android:textAlignment="textEnd"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="12sp"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/passwordTextInputLayout" />
<Button
android:id="#+id/cancelButton"
android:layout_width="120dp"
android:layout_height="48dp"
android:background="#color/black"
android:text="#android:string/cancel"
android:textAllCaps="false"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/secondLoginButton"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/forgotPasswordButton" />
<Button
android:id="#+id/secondLoginButton"
android:layout_width="120dp"
android:layout_height="48dp"
android:layout_marginStart="32dp"
android:background="#color/main_teal"
android:text="#string/log_in"
android:textAllCaps="false"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="#+id/cancelButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/cancelButton"
app:layout_constraintTop_toTopOf="#+id/cancelButton"
tools:ignore="TextContrastCheck" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
viewstub_login.kt
class LoginViewStub : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.viewstub_login)
val emailTextView = findViewById<TextInputEditText>(R.id.emailInputTextView)
val passwordTextView = findViewById<TextInputEditText>(R.id.passwordTextInputView)
val forgetPasswordButton = findViewById<Button>(R.id.forgotPasswordButton)
val loginButton = findViewById<Button>(R.id.secondLoginButton)
val cancelButton = findViewById<Button>(R.id.cancelButton)
val emailText = emailTextView.text
val passwordText = passwordTextView.text
loginButton.setOnClickListener {
if (emailText.isEmpty(emailTextView.text) || passwordText.isEmpty(passwordTextView.text)) {
val dialogBuilder = AlertDialog.Builder(this)
dialogBuilder.setMessage("Please fill out all fields.").setCancelable(false).setPositiveButton("Okay", DialogInterface.OnClickListener {
dialog, id -> finish()
}).setNegativeButton("Cancel", DialogInterface.OnClickListener {
dialog, id -> dialog.cancel()
})
val alert = dialogBuilder.create()
alert.show()
}
}
cancelButton.setOnClickListener {
}
forgetPasswordButton.setOnClickListener {
}
}
}
private fun Boolean.isEmpty(str: CharSequence?): Boolean {
return str == null || str.length == 0
}
click listener is correct, but inside the block, your code is not correct
because emailText and passwordText is not null, as you have defined above, thus the if condition returning false and your code just going over it

Reset integers in a TextView to 0 (Kotlin)

So I got the increment and decrement buttons work, but I need to reset the second set to 0 using the clear button. If you push it, it'll show you a 0 as a string value, but it won't reset the output. For example if I we're to increment the integer values up to 6 and then push the clear button, it'll show 0 in the TextView but once I push the increment button again it'll go straight up to 7 and not 1.
What am I missing?
KT
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() }
clear_button.setOnClickListener {
integer_number_2.text ="0"
}
}
private var integer1 = 0
private var integer2 = 0
fun increaseInteger1() {
//display_number_1(integer_number_1.text.toString().toInt() + 1)
integer1 = (integer1 + 1).coerceAtMost(10)
display_number_1(integer1)
}
fun decreaseInteger1() {
//display_number_1(integer_number_1.text.toString().toInt() - 1)
integer1 = (integer1 - 1).coerceAtLeast(0)
display_number_1(integer1)
}
fun increaseInteger2() {
//display_number_2(integer_number_2.text.toString().toInt() + 1)
integer2 = (integer2 + 1).coerceAtMost(10)
display_number_2(integer2)
}
fun decreaseInteger2() {
//display_number_2(integer_number_2.text.toString().toInt() - 1)
integer2 = (integer2 - 1).coerceAtLeast(0)
display_number_2(integer2)
}
private fun display_number_1(number: Int) {
integer_number_1.setText("$number")
}
private fun display_number_2(number: Int) {
integer_number_2.setText("$number")
}
}
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"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="309dp"
android:layout_height="118dp"
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" />
<Button
android:id="#+id/clear_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/integer_number_2" />
</androidx.constraintlayout.widget.ConstraintLayout>
You need to reset the value of the global int var also:
clear_button.setOnClickListener {
integer_number_2.text ="0"
integer2 = 0
}
It's bad practise to name your variables with underscores in kotlin/java. Use camel case instead tvIntegerNumberTwo and probably assign it the initials of the xml element type to better recognise what it is.

Android Kotlin pass value from textfields to numberPicker

So I have these values (Long and Double) in three textfields. They represent time, pace, and distance.
Now, to prevent user error I want to make them choose the values using numberPickers. I want to replace those textfields with those pickers.
This is what I have:
val time = timeTxtField as TextView
val distance = distanceTxtField as TextView
val pace = paceTxtField as TextView
var resetRunningBtn = clearBtn
val pickerMinutes = numberPicker as NumberPicker
val pickerSeconds = numberPickerSeconds as NumberPicker
pickerMinutes.minValue = 0
pickerMinutes.maxValue = 59
pickerMinutes.wrapSelectorWheel = false
pickerSeconds.minValue = 0
pickerSeconds.maxValue = 60
pickerSeconds.wrapSelectorWheel = false
calculateBtn.setOnClickListener {
when {
time.text.isEmpty() && (distance.text.isNotEmpty() && pace.text.isNotEmpty()) ->
calculatePace(null, distance.text.toString().toDouble(), pace.text.toString())
distance.text.isEmpty() && (time.text.isNotEmpty() && pace.text.isNotEmpty()) ->
calculatePace(time.text.toString(), null, pace.text.toString())
pace.text.isEmpty() && (time.text.isNotEmpty() && distance.text.isNotEmpty()) ->
calculatePace(time.text.toString(), distance.text.toString().toDouble(), null)
else -> {
Toast.makeText(this, "Please check fields",
Toast.LENGTH_SHORT).show()
}
}
}
As you can see I have the pickers set and all that. I tried to call something like .value but it didn't work.
This is my layout, so you can have a bit of understanding of the concept.
Any tips? :)
Thanks
EDIT XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.reecreate.woderator2.Controller.RunningCalculatorActivity">
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Running Pace Calculator"
android:textSize="24sp"
android:textStyle="bold"
android:inputType="numberDecimal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/distanceTxtField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="28dp"
android:ems="10"
android:hint="distance..."
android:inputType="number|numberDecimal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/radioType" />
<EditText
android:id="#+id/timeTxtField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="time mm:ss"
android:inputType="time"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/distanceTxtField" />
<EditText
android:id="#+id/paceTxtField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="pace mm:ss 10:21"
android:inputType="time"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/timeTxtField" />
<RadioGroup
android:id="#+id/radioType"
android:layout_width="280dp"
android:layout_height="56dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView5">
<RadioButton
android:id="#+id/milesDistanceRadioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="140dp"
android:layout_marginTop="16dp"
android:text="Miles"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toRightOf="#id/kmDistanceRadioBtn"
app:layout_constraintTop_toBottomOf="#id/textView5" />
<RadioButton
android:id="#+id/kmDistanceRadioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:text="KM"
app:layout_constraintEnd_toStartOf="#+id/milesRadioBtn"
app:layout_constraintHorizontal_bias="0.694"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RadioGroup>
<RadioGroup
android:id="#+id/paceRadioGroup"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.294"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/paceTxtField">
<RadioButton
android:id="#+id/milesPaceRadioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="140dp"
android:layout_marginTop="16dp"
android:text="MILES PACE"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toRightOf="#id/kmDistanceRadioBtn"
app:layout_constraintTop_toTopOf="parent" />
<RadioButton
android:id="#+id/kmPaceRadioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:text="KM PACE"
app:layout_constraintEnd_toStartOf="#+id/milesDistanceRadioBtn"
app:layout_constraintHorizontal_bias="0.694"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RadioGroup>
<Button
android:id="#+id/calculateBtn"
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/colorAccent"
android:text="Calculate"
android:textColor="#android:color/background_light"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/result"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:background="#drawable/border"
android:hint="Result"
android:textAlignment="center"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/paceRadioGroup" />
<Button
android:id="#+id/clearBtn"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:background="#color/colorAccent"
android:text="CLEAR"
android:textColor="#android:color/background_light"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/result" />
<NumberPicker
android:id="#+id/numberPicker"
android:layout_width="64dp"
android:layout_height="119dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.407"
app:layout_constraintTop_toBottomOf="#+id/radioType" />
<NumberPicker
android:id="#+id/numberPickerSeconds"
android:layout_width="64dp"
android:layout_height="119dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/numberPicker" />
EDIT VALUE LISTENER
I added this, but not doing anything
var pickedValue: Int = pickerMinutes.value
time.setText(Integer.toString(pickedValue))
................
when {
pickerMinutes.isClickable -> time.setText(Integer.toString(pickedValue))
}
numberPicker.setOnValueChangedListener { pickerMinutes, oldVal, newVal ->
//store values in variable for use later
}
A few general Kotlin things to note: I see you are setting variables and casting the the number pickers. You don't need to do this. In Kotlin, with the correct import, something like: kotlinx.android.synthetic.main.my_layout_file.*, you can simply call the xml by its id without casting, like so: numberPicker. That goes for your other items as well, such as the TextViews.
The other thing is that you shouldn't have to call Integer.toString(pickedValue). You can just call pickedValue.toString().
Lastly, since I am unable to see exactly how you are testing the app, my suggestion would be to do the following:
Create variables to store the picker values:
var numPickerVal = 0;
var secondsPickerVal = 0;
Then set a change listener for each of the pickers:
numberPicker.setOnValueChangedListener { picker, oldVal, newVal ->
numPickerVal = newVal
}
numberPickerSeconds.setOnValueChangedListener { picker, oldVal, newVal ->
secondsPickerVal = newVal
}

Categories

Resources