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.
Related
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
i have created a app with 3 edit text and and 3 buttons,
Edittext:
- id
- name
- date
Button:
submit.
check.
delete.
name to store name of user,
date to store date,
submit to store data in sqlit database,
check to retrive data,
delete to delete the specfied id in database,
id is auto incremented it is used when deleting.
my problem here is if i not enter anything in the edit text it takes null values
and display as shown in below pic instead of taking null i want to show message "PLEASE ENTER DATA"
and whenever i delete data next id value must decrease by 1 please help me.
Before entering the data to database use should check EditTexts are null or not in on click method and if null print toast messages like below
idEditText = (EditText) findViewById(R.id.editText_id);
nameEditText = (EditText) findViewById(R.id.editText_Name);
dateEditText = (EditText) findViewById(R.id.editText_Date);
String id = idEditText.getText().toString().trim();
String name = nameEditText.getText().toString().();
String date = dateEditText.getText().toString().();
if(id.length() == 0 || name.length() == 0 || date.length())
{
Toast.makeText(this, "Please fill all details",Toast.LENGTH_SHORT).show();
}
Try this
idEditText =(EditText) findViewById(R.id.editText_id);
nameEditText =(EditText)findViewById(R.id.editText_Name);
dateEditText =(EditText)findViewById(R.id.editText_Date);
private boolean checkEmptyFields() {
boolean check = false
String id = idEditText.getText().toString().trim();
String name = nameEditText.getText().toString().trim();
String date = dateEditText.getText().toString().trim();
if (id.isEmpty()) {
idEditText.setError("Invalid Input");
check = true;
} else {
dEditText.setError(null);
check = false;
}
if (name.isEmpty()) {
nameEditText.setError("Invalid Input");
check = true;
} else {
nameEditText.setError(null);
check = false;
}
if (date.isEmpty()) {
dateEditText.setError("Invalid Input");
check = true;
} else {
dateEditText.setError(null);
check = false;
}
return check;
}
if (!checkEmptyFields())
{
//Add Data to DB
}
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();
}
I am working on app where i use Urdu Custom Keyboard its work fine but the problem is that when i type any-word e.g. (سلام), cursor become not works at mid character for example cut/copy/paste or deleting (ا) character from the mid from word are not work.
i uses rough technique just appending characters but is also work fine.
For taping any alphabetic
private void addText(View v) {
// String b = "";
// b = (String) v.getTag();
// urdu_word.setText(b);
if (isEdit == true) {
String b = "";
b = (String) v.getTag();
if (b != null) {
Log.i("buttonsOnclick", b);
// adding text in Edittext
mEt.append(b);
}
}
}
For back button tapping
private void isBack(View v) {
if (isEdit == true) {
CharSequence cc = mEt.getText();
if (cc != null && cc.length() > 0) {
{
mEt.setText("");
mEt.append(cc.subSequence(0, cc.length() - 1));
}
}
}
}
Here the screenshot clear my problem to you people
I used a lot of library and code from github but don't catch good idea
1) Keyboard-1
2) Keyboard-2
3) Keyboard-3
4) Keyboard-4
i checked all these keyboard and more from libs, have same cursor issue, how to manage fully my custom keyboard by deleting character from mid and copy my written text copy paste like normal keyboard with EditText, thanks in advance all of you :)
Thanks God i solved my issue using simple logic.
For back button
private void isBack(View v) {
// char[] tempChar = null;
if ((mEt.getText().toString().length() > 0)) {
int temp = mEt.getSelectionEnd() - 1;
if (temp >= 0) {
mEt.setText((mEt.getText().toString()
.substring(0, mEt.getSelectionEnd() - 1).concat(mEt
.getText()
.toString()
.substring(mEt.getSelectionEnd(),
mEt.getText().length()))));
mEt.setSelection(temp);
}
}
}
For adding any character
private void addText(View v) {
int temp = mEt.getSelectionEnd();
if (temp >= 0) {
String b = "";
b = (String) v.getTag();
mEt.setText((mEt.getText().toString()
.substring(0, mEt.getSelectionEnd()) + b.concat(mEt
.getText().toString()
.substring(mEt.getSelectionEnd(), mEt.getText().length()))));
mEt.setSelection(temp + 1);
}
}
for copy paste i added few lines code to EditText
<EditText
android:id="#+id/xEt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#drawable/edittextshape"
android:ems="10"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="top"
android:imeOptions="actionDone"
android:padding="15dp"
android:singleLine="false"
android:visibility="visible" />
I have a string (length 3-8) assigned to a variable (text). I want to check whether the 2nd and 3rd characters are NOT numeric (a letter or symbol or space..or anything other than numbers).
Elementary way to do this could be:
if(((text.charAt(1)-'0')>=0)&&(text.charAt(1)-'0')<10))||((text.charAt(2)-'0')>=0)&&(text.charAt(2)-'0')<10)))
{
//do nothing, since this means 2nd and/or 3rd characters in the string are numeric
}
else
{
// Your condition is met
}
You could also use REGEX's , if your checking is still more complicated.
Here is Another way to achieve this:
boolean isNumeric = true;
String test = "testing";
char second = test.charAt(1);
char third = test.charAt(2);
try {
Integer.parseInt(String.valueOf(second));
Integer.parseInt(String.valueOf(third));
} catch(NumberFormatException e) {
isNumeric = false;
}
System.out.println("Contains Number in 2nd and 3rd or both position: " + isNumeric);
You might make use of the String.IndexOf(String) method, like:
String digits = "0123456789";
String s2 = text.substring(2,3);
String s3 = text.substring(3,4);
boolean valid = (digits.indexOf(s2) > -1) && (digits.indexOf(s3) > -1);