user11054683
user11054683

Reputation:

Convert EditText text to uppercase and lowercase , when clicked on button?

What i am trying to do is take input from the user and on button click event i want to display that EditText input on TextView. On button click listener, textView should display the input string in All caps and then clicking the same button it should convert that string into lowercase an show it on TextView. How can I achieve this?

This is what i have tried.

var userInput = findViewById<EditText>(R.id.editText)
var caseButton = findViewById<Button>(R.id.upperLowerButton)
var caseView = findViewById<TextView>(R.id.textUpperLower)

caseButton.setOnClickListener {
    caseView.text =  userInput.text
}

Upvotes: 0

Views: 2189

Answers (5)

Geek Tanmoy
Geek Tanmoy

Reputation: 860

In Kotlin as toUperCase() and toLowerCase() are deprecated we can use uppercase() instead of toUpperCase() and lowercase() instead of toLowerCase().

For example,

val lower="abc"
Log.e("TAG", "Uppercase: ${lower.uppercase()}" ) //ABC
val upper="ABC"
Log.e("TAG", "Lowercase: ${upper.lowercase()}" ) //abc

Upvotes: 0

Akanshi Srivastava
Akanshi Srivastava

Reputation: 1500

Using methods - toUpperCase() and toLowerCase() can easily solve this problem.

button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String h =  textView.getText().toString();
                    if (isCaps) {
                        textView.setText(h.toUpperCase());
                        isCaps = false;
                    }
                       else {
                           textView.setText(h.toLowerCase());
                       isCaps = true;}
                    }
    
            });

Upvotes: 1

Jakir Hossain
Jakir Hossain

Reputation: 3930

You can try like following.

var isUpperCase = false;
var txt = userInput.text.toString()
caseButton.setOnClickListener {
      if(isUpperCase){
         txt = txt.toLowerCase()
         isUpperCase = false
      }else{
          txt = txt.toUpperCase()
          isUpperCase = true
      }
       caseView.text =  txt
}

Upvotes: 0

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363845

You can use something like:

val uppercase = userInput.text.toString().toUpperCase()
val lowerCase = uppercase.toLowerCase()

Upvotes: 3

majid ghafouri
majid ghafouri

Reputation: 869

You can use .toUpperCase() and .toLowerCase() for your string values e.g.

userInput.text.toString().toUpperCase() 

and

userInput.text.toString().toLowerCase()

Upvotes: 0

Related Questions