Android: Button enabled property is not working - android

i have the String in Edit Text, i want to change change the button state through the string. Please help me out of this problem i am beginner in android.
Here is the code.
String Result = jsonResult.toString();
JSONObject jsonResponse = new JSONObject(Result);
int successValue = jsonResponse.getInt("success");
String messageValue= jsonResponse.getString("message");
String successStringValue = String.valueOf(successValue);
String messageStringValue = String.valueOf(messageValue);
t1.setText(messageStringValue);
String tt1=t1.getText().toString();
if (tt1 != "Appointment is ready."){
b1.setEnabled(true);}
else{
b1.setEnabled(false);}

Change your condition to
if (tt1.equalsIgnoreCase("Appointment is ready.")){
b1.setEnabled(true);
}
else
{
b1.setEnabled(false);
}
use this code for making edittext not editable
<EditText ...
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false">
</EditText>

if(!(tt1.equals("Some String")))
{
//enable button
}
else
{
//disable it
}
Stings are compared by doing ".equals()"

Related

Inputting Array In Multiline Text In Android

I am new to android and am trying to make an app where the user inputs an array in a text box with inputType = textMultiLine. The problem is that I want to make it so that whenever user hits enter, the app takes input of the next array element and not treat the entire text in the textbox as one element. The code is as below :
EditText input = findViewById(R.id.inputtext);
Button show = findViewById(R.id.button);
TextView output = findViewById(R.id.output);
String [] name = new String[3];
for (int i = 0 ; i < 3 ; i++)
{
name[i] = input.getText().toString();
output.setText(name[i]);
}
But whenever i try to take name[1] after hitting enter the app doesnt treat the next line as name[2] but instead treats it as name[1]. For example if type the names john,steve and frank, then i should get an array that is like this :
name[0] = john
name[1] = steve
name[2] = frank
but instead whenever I typejohn,press enter,type steve, press enter and type frank the app treats it as :
name[0] = john
steve
frank
also if i set the output to something like this :
output.setText(name[i] + i)
instead of getting an oupt like this :
john 0
steve 1
frank 2
I get an output like this :
john
steve
frank2
Any and all help would be much appreciated.
Thanks
======================================================================================================================================================
EDIT 1
I tried this code but didn't work:
String name[] = input .getText().toString().split("\\r?\\n");
for (int i = 0 ; i < name.length; i++)
{
output.setText(name[i]);
}
Still get only frank when I input john,steve and frank
If you want to put each line to different array item :
String [] name = input.getText().toString().split("\n");
input.getText().toString() gives you string containing whole EditText content with lines separated by new line - "\n". You need to split this string to get each line.
try below code
String name[] = input .getText().toString().split("\\r?\\n");
String disp="";
for (int i = 0 ; i < name.length; i++)
{
disp += name[i] +"\n";
}
output.setText(disp);
Maybe the following example will be useful:
XML file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<EditText
android:id="#+id/edit_text"
style="#style/Widget.AppCompat.EditText"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginBottom="100dp"
android:inputType="textMultiLine"/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show"/>
<TextView
android:id="#+id/text_view"
style="#style/Widget.AppCompat.EditText"
android:layout_width="300dp"
android:layout_height="wrap_content"/>
</LinearLayout>
Java Code
final EditText input = findViewById(R.id.edit_text);
final Button show = findViewById(R.id.button);
final TextView output = findViewById(R.id.text_view);
show.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
final String inputString = input.getText().toString();
if (!TextUtils.isEmpty(inputString)) {
final String newLine = System.getProperty("line.separator");
final String[] inputText = inputString.split(newLine);
String outputText = "";
for (int i = 0; i < inputText.length; i++) {
outputText += inputText[i];
if (i != inputText.length - 1) {
outputText += newLine;
}
}
output.setText(outputText);
}
}
});
}
You can download de APK here or here the complete source code
(another way:simple!) In the following code it is not necessary to make a split on the input text.
final EditText input = findViewById(R.id.edit_text);
final Button show = findViewById(R.id.button);
final TextView output = findViewById(R.id.text_view);
show.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
final String inputString = input.getText().toString();
if (!TextUtils.isEmpty(inputString)) {
output.setText(inputString);
}
}
});
Note that, your code is wrong, because in for each loop, you override the text was setted in the previous loop.

Get the String value of the EditText which has inputType numberPassword

I would like to get the String value of an EditText that has the attribute inputType: numberPassword, how do I do it?
Code_pin.getText().ToString() always returns a null value.
String str = null;
str = your_edit.getText().toString().trim(); //put your editText value in string
if (!TextUtils.isEmpty(str)) //check for empty
{
// Do your task here
}
First you have to get an Id of the EditText:
EditText et_pwd = (EditText) findViewById(R.id.et_pwd);
your xml file:
<EditText
android:id="#+id/et_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberPassword" />
then you call getText() method:
String text = et_pwd.getText().toString().trim();
if (text.equalsIgnoreCase("")) {
// empty/blank string
}else {
// not blank string
}

Android - Is there a better way to toggle a radio button on page load?

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();
}

Checking for empty edittext android

I have an edittext with input type set to number. I want to check if edittext is empty.
<EditText
android:id="#+id/noOfTranset"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="55dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="Enter number:"
android:inputType="number"
android:textColor="#android:color/black"
android:maxLength="2"/>
Below is the code which I have tried but doesn't checks for empty edittext. What am I missing here? Thanks.
String numTrans = et1.getText().toString();
int transaction = Integer.parseInt(numTrans);
if(numTrans.trim().length() == 0 || numTrans.equals("") || numTrans == null){
// none of the above conditions check for empty edittext
}
Problem is your trying to convert the empty string to an Integer. Integer.parseInt will throw NumberFormatException when the input text is null or empty.
Change your code to parse string to integer only when the input text is not empty
if(!TextUtils.isEmpty(numTrans)){
int transaction = Integer.parseInt(numTrans);
// do your other stuff here
}
How about something like this?
EditText usernameEditText = (EditText) findViewById(R.id.editUsername);
sUsername = usernameEditText.getText().toString();
if (sUsername.matches("")) {
Toast.makeText(this, "You did not enter a username", Toast.LENGTH_SHORT).show();
return;
}
Taken from: Check if EditText is empty.
This is very simple you should check by using trim function like:
String numTrans = et1.getText().toString();
if(!numTrans.trim().length() > 0)
{
int transaction = Integer.parseInt(numTrans);
//code here for empty edittext....
}

Converting EditText to int? (Android)

I am wondering how to convert an EditText input to an int, I have the user input a number, which then divides it by 8.
MainActivity.java:
#SuppressWarnings("unused")
public void calcSpeed(View view)
{
setContentView(R.layout.activity_speed);
final TextView mTextView = (TextView) findViewById(R.id.textView3);
mTextView.setText("You should be getting: " +netSpedCalcd);
}
activity_main.xml:
<EditText
android:id="#+id/editText1"
android:inputType="number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="62dp"
android:ems="10" >
you have to used.
String value= et.getText().toString();
int finalValue=Integer.parseInt(value);
if you have only allow enter number then set EditText property.
android:inputType="number"
if this is helpful then accept otherwise put your comment.
Use Integer.parseInt, and make sure you catch the NumberFormatException that it throws if the input is not an integer.
I'm very sleepy and tired right now but wouldn't this work?:
EditText et = (EditText)findViewById(R.id.editText1);
String sTextFromET = et.getText().toString();
int nIntFromET = new Integer(sTextFromET).intValue();
OR
try
{
int nIntFromET = Integer.parseInt(sTextFromET);
}
catch (NumberFormatException e)
{
// handle the exception
}
Try this,
EditText x = (EditText) findViewById(R.id.editText1);
int n = Integer.parseInt(x.getText().toString());
You can use parseInt with try and catch block
try
{
int myVal= Integer.parseInt(mTextView.getText().toString());
}
catch (NumberFormatException e)
{
// handle the exception
int myVal=0;
}
Or you can create your own tryParse method :
public Integer tryParse(Object obj) {
Integer retVal;
try {
retVal = Integer.parseInt((String) obj);
} catch (NumberFormatException nfe) {
retVal = 0; // or null if that is your preference
}
return retVal;
}
and use it in your code like:
int myVal= tryParse(mTextView.getText().toString());
Note: The following code without try/catch will throw an exception
int myVal= new Integer(mTextView.getText().toString()).intValue();
Or
int myVal= Integer.decode(mTextView.getText().toString()).intValue();
Try the line below to convert editText to integer.
int intVal = Integer.parseInt(mEtValue.getText().toString());
I had the same problem myself. I'm not sure if you got it to work though, but what I had to was:
EditText cypherInput;
cypherInput = (EditText)findViewById(R.id.input_cipherValue);
int cypher = Integer.parseInt(cypherInput.getText().toString());
The third line of code caused the app to crash without using the .getText() before the .toString().
Just for reference, here is my XML:
<EditText
android:id="#+id/input_cipherValue"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
You can use like this
EditText dollar=(EditText) findViewById(R.id.money);
int rupees=Integer.parseInt( dollar.getText().toString());
First, find your EditText in the resource of the android studio by using this code:
EditText value = (EditText) findViewById(R.id.editText1);
Then convert EditText value into a string and then parse the value to an int.
int number = Integer.parseInt(x.getText().toString());
This will work
int total_Parson = Integer.parseInt(etRegularTickets.getText().toString());
int ticket_price=Integer.parseInt(TicketData.get(0).getTicket_price_regular());
total_ticket_amount = ticket_price * total_Parson;
etRegularPrice.setText(""+total_ticket_amount);
In Kotlin, you can do this.
val editText1 = findViewById(R.id.editText)
val intNum = editText1.text.toString().toInt()
In kotlin, there is shortest way thanks to the Extension Function
fun EditText.toInt(): Int {
return this.text.toString().toInt()
}
Use it in your code like below:
mEditText.toInt()

Categories

Resources