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
}
You can use something like:
val uppercase = userInput.text.toString().toUpperCase()
val lowerCase = uppercase.toLowerCase()
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;}
}
});
You can use .toUpperCase() and .toLowerCase() for your string values e.g.
userInput.text.toString().toUpperCase()
and
userInput.text.toString().toLowerCase()
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
}
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
Related
I have added a delete button in my scientific calculator app, when the button is clicked it will delete the input one after the other.
I use the below code to archive that.
if(data.equals("del")) {
String enteredInput = input.getText().toString();
if(enteredInput.length() > 0) {
enteredInput = enteredInput.substring(0, enteredInput.length()-1);
currentDisplayedInput = enteredInput;
inputToBeParsed = enteredInput;
input.setText(currentDisplayedInput);
}
}
My problem here is that when I'm using trigonometric functions like "sin" the delete button deletes the strings one by one. I want the delete button to delete the "sin" at once.
Please how can I do that.
I have tried doing it this way:
if(data.equals("del")) {
String enteredInput = input.getText().toString();
if (enteredInput == "sin("){
enteredInput = enteredInput.substring(0, enteredInput.length()-4);
currentDisplayedInput = enteredInput;
inputToBeParsed = enteredInput;
input.setText(currentDisplayedInput);
}
else if(enteredInput.length() > 0) {
enteredInput = enteredInput.substring(0, enteredInput.length()-1);
currentDisplayedInput = enteredInput;
inputToBeParsed = enteredInput;
input.setText(currentDisplayedInput);
}
}
But still the problem is not solved.
I made a calculator app and I made a clear Button that clears the TextView.
private TextView _screen;
private String display = "";
private void clear() {
display = "";
currentOperator = "";
result = "";
}
I got this code from Tutorial and set the clear button onClick to onClickClear, so it do that part of the code and it works. Now I have made this code delete only one number at a time and it don't work. What can be done to delete only one number at a time?
public void onClickdel(View v) {
display = "";
}
Below code will delete one char from textView.
String display = textView.getText().toString();
if(!TextUtils.isEmpty(display)) {
display = display.substring(0, display.length() - 1);
textView.setText(display);
}
You are modifying the string and not the textview.
To clear the TextView use:
_screen.setText("");
To remove the last character:
String str = _screen.getText().toString();
if(!str.equals(""))
str = str.substring(0, str.length() - 1);
_screen.setText(str);
String display = textView.getText().toString();
if(!display.isEmpty()) {
textView.setText(display.substring(0, display.length() - 1));
}
My current code fetches a database cursor when the page loads and then pre-fills the form. I am trying to check the proper radio button based whether user gender is "Male" or "Female". My 2 radio buttons is in a RadioGroup;
Java code:
public void fillData(Cursor row) {
sId = (EditText)findViewById(R.id.studentid);
fName = (EditText)findViewById(R.id.firstName);
lName = (EditText)findViewById(R.id.lastName);
gender = (RadioButton)findViewById(R.id.male);
gender2 = (RadioButton)findViewById(R.id.female);
course = (EditText)findViewById(R.id.course);
age = (EditText)findViewById(R.id.age);
address = (EditText)findViewById(R.id.address);
sId.setText(row.getString(0));
fName.setText(row.getString(1));
lName.setText(row.getString(2));
String temp = row.getString(3);
if (temp == "Male") {
gender.toggle();
} else {
gender2.toggle();
}
course.setText(row.getString(4));
age.setText(row.getString(5));
address.setText(row.getString(6));
}
With this code I am only getting the female radio button checked even if temp is "Male". I tested using Toast.maketext
You should compare string with equals method
if ("Male".equals(temp) {
gender.toggle();
} else {
gender2.toggle();
}
You can also use equalsIgnoreCase() in case of string matching this will be a good approach.
if ("Male".equalsIgnoreCase(temp) {
gender.toggle();
} else {
gender2.toggle();
}
Recently I've started to mess around with Android Studio and decided to make an app. I've made the most of my app, but I encounter a little problem. I need to memorize in a variable a number from user input, but I don't know how to that, I've tried solutions found on the internet, even here but I get an error.
Can somebody help me with some ideas or the code edited which I must put in my app ?
This is the java code for the activity:
public class calc_medie_teza extends ActionBarActivity implements View.OnClickListener {
EditText adaug_nota;
static final int READ_BLOCK_SIZE = 100;
TextView afisare;
TextView afisare2;
Button calc;
EditText note_nr;
EditText nota_teza;
int suma;
double medie;
double medieteza;
int nr_note = 0;
int notamedie;
int notateza;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calc_medie_teza);
afisare = (TextView) findViewById(R.id.afisare);
afisare2 = (TextView) findViewById(R.id.afisare2);
calc = (Button) findViewById(R.id.calc);
calc.setOnClickListener(this);
note_nr = (EditText) findViewById(R.id.note_nr);
nota_teza = (EditText) findViewById(R.id.nota_teza);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_calc_medie_teza, menu); // creare meniu
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId(); // meniu
return id == R.id.action_settings || super.onOptionsItemSelected(item);
}
public void onClick(View v) // afisare medie
{
calcul_medie();
}
public void buton_adauga(View v)
{
if(note_nr == )
suma = suma + notamedie;
nr_note = nr_note + 1;
}
public double calcul_medie() // calculul mediei
{
medie = suma / nr_note;
medieteza = ((medie * 3)+ notateza)/4;
return medieteza;
}
Here is a photo with the activity: http://onlypro.ro/img/images/ootllv2f55hnwdgi0xlv.png
Basically the app needs to add the input number to variable when I press the "Adauga nota" [Add grade] button and then I have to insert the "teza" number and when press the "Calculeaza media" [Calculate] the app will then calculate the calcul_medie() and return a decimal number. Between "Introduceti notele aici" [Add grades here] and "Adaugata nota"[Add grade] I have a Number enter text, the same is between "Introduceti teza aici" [Add thesis bere] and "Calculeaza media" [Calculate]. I don't know how to store the number the user puts in and sum it in "notamedie" variable.
I hope you understand my problem.
For any questions you'll have I'll respond as soon as I can.
Thanks in advance !
Well, I think your probably asked how to get the input of the String from the UI and translate them into Integer/BigDecimal. If so, here is some solution:
first, you get get the string from the UI:
String input = note_nr.getText().toString().trim();
change the string input to integer/BigDecimal:
int number1 = Integer.parseInt(input);
BigDecimal number2 = new BigDecimal(input);
Correct me if misunderstood your questions. Thanks.
I did not understand the problem at all. Please clearly define the problem and use english if you can. As fas the I understood there will be a Edittext to which the user will input the value. Gets its value in the activity and then use it.
EditText edittext;
editext = (EditText) findViewById(R.id.editext);
String value = edit text.getText().toString(); // the value that the user inputted in String data format
// Here for addition you will need the value in int or decimal for that
int valueInt = Integer.parse(value); // this will be the value in int type
double valueInt = = Double.parseDouble(value); // this will be the value (user inputted) in double data type ( which you will need if you want to use decimal numbers).
When you press a button just retrieve the appropriate value from the edittext in the following way
edittext = (EditText)layout.findViewById(R.id.txtDescription);
String string = edittext.getText().toString();
to retrieve the text do this
String string = note_nr.getText().toString();
// Converting String to int
int myValue =Integer.parseInt(string);
I tried that one more time, but the IDE says that the variable I used for "string" is never used, but I've used it.
number1= (EditText) findViewById(R.id.number1);
String number_1 = number1.getText().toString();
number2= (EditText) findViewById(R.id.number2);
String number_2= number2.getText().toString();
R3muSGFX, can you post the whole codes?
String number_1 = number1.getText().toString(); this line codes just mean that you initialized a String variable "number_1".
and you will used it when app perform a math function, like you wrote in the beginning:
public double calcul_medie()
{
medie = suma / nr_note;
medieteza = ((medie * 3)+ notateza)/4;
return medieteza;
}
but if you did not use "number_1" at all, IDE will imply you the warning message
:the variable I used for "string" is never used
I have an EditText in android for users to input their AGE. It is set an inputType=phone.
I would like to know if there is a way to check if this EditText is null.
I've already looked at this question: Check if EditText is empty. but it does not address the case where inputType=phone.
These, I've checked already and do not work:
(EditText) findViewByID(R.id.age)).getText().toString() == null
(EditText) findViewByID(R.id.age)).getText().toString() == ""
(EditText) findViewByID(R.id.age)).getText().toString().matches("")
(EditText) findViewByID(R.id.age)).getText().toString().equals("")
(EditText) findViewByID(R.id.age)).getText().toString().equals(null)
(EditText) findViewByID(R.id.age)).getText().toString().trim().length() == 0
(EditText) findViewByID(R.id.age)).getText().toString().trim().equals("")
and isEmpty do not check for blank space.
Thank you for your help.
You can check using the TextUtils class like
TextUtils.isEmpty(ed_text);
or you can check like this:
EditText ed = (EditText) findViewById(R.id.age);
String ed_text = ed.getText().toString().trim();
if(ed_text.isEmpty() || ed_text.length() == 0 || ed_text.equals("") || ed_text == null)
{
//EditText is empty
}
else
{
//EditText is not empty
}
First Method
Use TextUtil library
if(TextUtils.isEmpty(editText.getText().toString())
{
Toast.makeText(this, "plz enter your name ", Toast.LENGTH_SHORT).show();
return;
}
Second Method
private boolean isEmpty(EditText etText)
{
return etText.getText().toString().trim().length() == 0;
}
Add Kotlin getter functions
val EditText.empty get() = text.isEmpty() // it == ""
// and/or
val EditText.blank get() = text.isBlank() // it.trim() == ""
With these, you can just use if (edittext.empty) ... or if (edittext.blank) ...
If you don't want to extend this functionality, the original Kotlin is:
edittext.text.isBlank()
// or
edittext.text.isEmpty()
EditText textAge;
textAge = (EditText)findViewByID(R.id.age);
if (TextUtils.isEmpty(textAge))
{
Toast.makeText(this, "Age Edit text is Empty", Toast.LENGTH_SHORT).show();
//or type here the code you want
}
I use this method for same works:
public boolean checkIsNull(EditText... editTexts){
for (EditText editText: editTexts){
if(editText.getText().length() == 0){
return true;
}
}
return false;
}
Simply do the following
String s = (EditText) findViewByID(R.id.age)).getText().toString();
TextUtils.isEmpty(s);
I found that these tests fail if a user enters a space so I test for a missing hint for empty value
EditText username = (EditText) findViewById(R.id.editTextUserName);
EditText password = (EditText) findViewById(R.id.editTextPassword);
// these hint strings reflect the hints attached to the resources
if (username.getHint().equals("Enter your username") || password.getHint().equals("Enter Your Password")){
// enter your code here
} else {
// alls well
}
editText.length() usually works for me
try this one
if((EditText) findViewByID(R.id.age)).length()==0){
//do whatever when the field is null`
}
Java: Check if EditText is Empty
1) find the EditText
ourEditText = view.findViewById(R.id.edit_text);
2) Get String value of EditText
String ourEditTextString = ourEditText.getText().toString();
3) Remove all spaces
Incase user inputs only blank spaces.
String ourEditTextNoSpaces = ourEditTextString.replaceAll(" ","");
3) Check if empty
boolean isOurEditTextEmpty = ourEditTextNoSpaces.isEmpty();
Try this. This is the only solution I got
String phone;
try{
phone=(EditText) findViewByID(R.id.age)).getText().toString();
}
catch(NumberFormatException e){
Toast.makeText(MainActivity.this, "plz enterphone Number ", Toast.LENGTH_SHORT).show();
return;
}