how to check letter inside word[hangman] - android

i want to my button can check if the letter is inside the word for example
word: Hai
user pres btnA
result : _a_
user pres btnH
result : Ha_
this what i do at kotlin Android studio
fun guessTry(click : View){
val guessW = txtKata.toString()
txtKata.text = null
if (click == btnA){
val a = btnA.text
if(a in secretWord.toLowerCase() || a in secretWord.toUpperCase() ){
corecGuess.add(guessW)
wordSecret()
}
}
}
word: Hai
user pres btnA
result : _a_
user pres btnH
result : Ha_

I think you can try to change the if function like this
fun guessTry(click : View){
val guessW = txtKata.toString()
txtKata.text = null
if (click == btnA){
val a = btnA.text
if(secretWord.toLowerCase().contains(a) || secretWord.toUpperCase().contains(a) ){
corecGuess.add(guessW)
wordSecret()
}
}
}

Related

Decrease function in Kotlin

Hey, I have been trying to add decrease function in my android button counter app. There are three buttons(clear, increase, decrease) in my app. Increase is for increasing the counter, decrease is for decreasing the counter and clear is for clearing the textview. i am new here in android. So help me to write the code in kotlin
button?.setOnClickListener(object :View.OnClickListener {
override fun onClick(v: View?) {
numTimeClicked += 1
textview?.append("\n The button got tapped $numTimeClicked time")
if (numTimeClicked != 1){
textview?.append("s")
}
}
})
clear?.setOnClickListener(object :View.OnClickListener{
override fun onClick(v: View?) {
if (numTimeClicked <= 100){
textview?.text = ""
}
}
})
Decrease?.setOnClickListener(object :View.OnClickListener{
override fun onClick(v: View){
if (numTimeClicked >= 1 ){
numTimeClicked -= 1
textview?.append(numTimeClicked.toString())
}
}
})
}
private var userInput:EditText? = null
private var button:Button? = null
private var textview:TextView? = null
private var clear:Button? = null
private var Decrease:Button? = null
private var numTimeClicked = 0
}
fun decreasenumber(var numTimeClicked : Int
) : Int{
numTimeClicked--
return numTimeClicked
}
just replace this line
textview?.append(numTimeClicked.toString())
to
textview?.text = "" + numTimeClicked
append behaves like this: 3 -> 32 -> 321, I think your purpose is to replace not append

Disabling button functionality if any fields are empty in Kotlin

Are Name and Age null if there are no inputs in the EditText field when the App runs? Also, I can't seem to get the conditional syntax down. Any tips? I'm new to Kotlin.
val button = findViewById<Button>(R.id.button4)
button.setOnClickListener {
when{
Name.val==null && Age.val==null && gender.val=="" -> button.isEnabled=false
}
val intent = Intent(this, fourth::class.java)
startActivity(intent)
}

How to click a button multiple times and change modes in an android app

I am new to Android Development. I need to build a temperature converter app which would convert Celcius to Farenheit and Farenheit to Celcius. I have used an editText for user input. There are two buttons. One button is to convert the input and the other one is the mode button which would toggle between the two modes of conversion. When I launch the app the mode is in celcius to farenheit by default. By clicking on mode button I can change the mode to farenheit to celcius scale. The problem is that when I again click on the mode button it does not return to the celcius to farenheit conversion mode. I don't know how to do it. Can someone help me in this regard?
I have set the function getset() for the convert button and function mode() for the mode button.
fun getSet(view: View)
{
val convert = findViewById<Button>(R.id.button)
convert.setOnClickListener {
if(editText.length()==0)
{
editText.setError("Enter a Value")
}
else
{
val editxt = findViewById<EditText>(R.id.editText)
val msg = editxt.text.toString()
val txtview = findViewById<TextView>(R.id.textView2).apply {
val cel = msg.toDouble()
val far = (cel*1.8)+32
text = "Result: " + far.toString()
}
}
}
}
fun mode(view: View)
{
val convert = findViewById<Button>(R.id.button)
val heading = findViewById<TextView>(R.id.textView).apply {
val caption = "Farenheit to Celcius"
text = caption
}
convert.setOnClickListener {
if(editText.length()==0)
{
editText.setError("Enter a Value")
}
else
{
val editxt = findViewById<EditText>(R.id.editText)
val msg = editxt.text.toString()
val txtview = findViewById<TextView>(R.id.textView2).apply {
val far = msg.toDouble()
val cel = (far-32)*0.5555555556
text = "Result: " + cel.toString()
}
}
}
}
You need to store the "mode" in a global variable.
create a global variable
var isModeCelsius: Boolean = true
Now inside your onCreate() method in your activity, under the setContentView(R.layout.your_layout_name) line enter the below code.
//Initialize edittext and button
val convert = findViewById<Button>(R.id.button)
val heading = findViewById<TextView>(R.id.textView)
val modeButton = findViewById<Button>(R.id.id_of_button)
val editxt = findViewById<EditText>(R.id.editText)
val showResultTextView = findViewById<TextView>(R.id.textView2)
//You only need to assign the click listener once
modeButton.setOnClickListener {
if (isModeCelsius) {
isModeCelsius = false
} else {
isModeCelsius = true
}
//Or you can simply use
//isModeCelsius=!isModeCelsius
}
convert.setOnClickListener {
val msg = editxt.text.toString()
if(isModeCelsius){
val far = msg.toDouble()
val cel = (far-32)*0.5555555556
showResultTextView.text = "Result: " + cel.toString()
}else{
val cel = msg.toDouble()
val far = (cel*1.8)+32
showResultTextView.text = "Result: " + far.toString()
}
}

How do I get the last one click button player1 or player2

class MainActivity : AppCompatActivity() {
enum class PLAYINGPLAYER {
FIRST_PLAYER,
SECOND_PLAYER
}
var playingplayer: PLAYINGPLAYER? = null
var buttonClicked:ArrayList<Int> = ArrayList()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
playingplayer = PLAYINGPLAYER.FIRST_PLAYER
}
fun imgButtonClicked(v: View) {
val btnselected = v as Button
var optionNumber = 0
when (btnselected.id) {
R.id.btn1 -> optionNumber = 1
R.id.btn2 -> optionNumber = 2
R.id.btn3 -> optionNumber = 3
R.id.btn4 -> optionNumber = 4
R.id.btn5 -> optionNumber = 5
R.id.btn6 -> optionNumber = 6
}
action(optionNumber, btnselected)
}
var button1Click = ArrayList<Button>()
var button2Click = ArrayList<Button>()
fun action(optionNumber: Int, btnselected: Button) {
if (playingplayer == PLAYINGPLAYER.FIRST_PLAYER) {
btnselected.setBackgroundColor(Color.GREEN)
buttonClicked.add(optionNumber)
btnselected.isEnabled = false
button1Click.add(btnselected)
playingplayer = PLAYINGPLAYER.SECOND_PLAYER
}
else if (playingplayer == PLAYINGPLAYER.SECOND_PLAYER) {
btnselected.setBackgroundColor(Color.RED)
buttonClicked.add(optionNumber)
btnselected.isEnabled = false
button2Click.add(btnselected)
playingplayer = PLAYINGPLAYER.FIRST_PLAYER
}
imagebtnState()
}
fun imagebtnState() {
val btnLast = buttonClicked.get(buttonClicked.size - 1)
if (buttonClicked.contains(1) && buttonClicked.contains(2)) {
if ( btnLast == ??? player1 ) {
imgbBtnBackground.setImageResource(R.drawable.rca)
}
}
else if (buttonClicked.contains(1) && buttonClicked.contains(2)) {
if ( btnLast == ??? player2 ) {
imgbBtnBackground.setImageResource(R.drawable.wac)
}
}
}
}
I have two players and several buttons. Player1 clicks on a button and player2 clicks on another button. When the players press several specific ?, I want to change the background of imageButton.
In (btnLast = ???) what I have put to get last one click in (buttonClicked.contains(1) && buttonClicked.contains(2)) is player1 or player2.
Assuming you are only ever going to have two players create a boolean called var playerOneLastClicked = true and if player one clicks set true, else if player two clicks set false
if (buttonClicked.contains(1) && buttonClicked.contains(2)) {
if (playerOneLastClicked) {
imgbBtnBackground.setImageResource(R.drawable.rca)
} else {
imgbBtnBackground.setImageResource(R.drawable.wac)
}
}
Since imagebtnState() is called after each click it doesn't really matter about checking who clicked the last in the arrayList as the method does the check to see whether or not the specified buttons have been clicked anyway
I think that's that what you were asking for?

Check if EditText is empty while calculating numbers

I'm trying to build an age calculator application. When I didn't enter a number in one of the EditTexts and click on calculate Button, my app is crashed! "App has stopped" and click the photo
I tried to set yearbirth.isEmpty() || yearbirth.equals("") || yearbirth == null , but nothing worked.
CODE:-
class MainActivity : AppCompatActivity() {
var yearage:Int?= null
var monthage:Int?= null
var dayage:Int?= null
#SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
buGetAge.setOnClickListener({
val year= Calendar.getInstance().get(Calendar.YEAR)
val month= Calendar.getInstance().get(Calendar.MONTH)
val day= Calendar.getInstance().get(Calendar.DAY_OF_MONTH)
var yearage = year - Integer.parseInt(yearbirth.text.toString()) -1
val monthage:Int?
val dayage:Int?
if (yearbirth== null) {
Toast.makeText(this, "You have to enter your year berth", Toast.LENGTH_LONG).show()
} else {
if (month < Integer.parseInt(monthbirth.text.toString())){
monthage=(Integer.parseInt(monthbirth.text.toString())- month- 12)* -1
}else if (month > Integer.parseInt(monthbirth.text.toString())) {
yearage=year- Integer.parseInt(yearbirth.text.toString())
monthage= (Integer.parseInt(monthbirth.text.toString())- month)* -1
}else {
yearage=year- Integer.parseInt(yearbirth.text.toString())
monthage=0
}
if (day < Integer.parseInt(daybirth.text.toString())){
dayage= (Integer.parseInt(daybirth.text.toString())- day- 30)* -1
}else if (day > Integer.parseInt(daybirth.text.toString())){
dayage= day- Integer.parseInt(daybirth.text.toString())
}else {
dayage=0
}
val a= (yearage* 12)+ monthage
val b= (a * 30)+ dayage
val ageinyears= "$yearage years"
val ageinmonths= "$a months"
val ageindays= "$b days"
txtshow.text = "Your age is $yearage years, $monthage months and $dayage days"
txtshow2.text = "\nYOUR AGE:\nin years:\n\nin months:\n\nin days:"
txtshow3.text = "\n\n$ageinyears \n\n$ageinmonths \n\n $ageindays "
this.yearage=yearage
this.monthage=monthage
this.dayage=dayage
}
})
}
}
Verify that yearbirth.text.toString() is not empty before attempting to parse it into an Integer (and also if it's a valid integer.)
To achieve so, put the line you showed in your code:
var yearage = year - Integer.parseInt(yearbirth.text.toString()) -1
Inside an if verifying that the input is valid, like so:
if(yearbirth.text.toString().matches(Regex("\\d+")))
var yearage = year - Integer.parseInt(yearbirth.text.toString()) -1
You can check for empty Input like this:
newString = Integer.toString(yearbirth).trim();
if (newString.matches("")) {
Toast.makeText(this, "EditText is empty", Toast.LENGTH_SHORT).show();
return;
}

Categories

Resources