my code in android studio (i think it is in (toString)) [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I have a problem in my code in android studio, I created it to say "Hello" when the person "abc" writes but that didn't work . can u plz help me. here is my codes
final Button butt=(Button)findViewById(R.id.butt);
butt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final EditText frag =(EditText)findViewById(R.id.frag);
final TextView hello=(TextView)findViewById(R.id.hello);
String verb =frag.getText().toString();
if (verb=="abc"){
hello.setText("Hello");

Because the condition in if block returns false
Use this code instead.
final Button butt = (Button) findViewById(R.id.butt);
butt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final EditText frag = (EditText) findViewById(R.id.frag);
final TextView hello = (TextView) findViewById(R.id.hello);
String verb = frag.getText().toString();
if ("abc".equals(verb)) {
hello.setText("Hello");
}
}
}

When comparing string, always use .equals method
Example:
String str1 = "yourstring1";
String str2 = "yourstring2";
if(str1.equals(str2))//return false
{...}

Related

simple login "if" condition but it only halfe work android studio java [duplicate]

This question already has answers here:
What is an off-by-one error and how do I fix it?
(6 answers)
Closed 1 year ago.
Hello I am new to android java codding I created a "login activity" and "Users class"
and my condition is to add a number of users and pass to the "Users class" and check if the password given is correct then log in,
however, it works correctly when I put the right password but if the password is wrong the app just crashes and the if-else condition does not run.
public class MainActivity extends AppCompatActivity {
private EditText Name;
private EditText Password;
private TextView Loginmsg;
public Button Btlogin;
public static int Counter;
public static ArrayList <User> mUsers;
public static int Tester;
private String nameHolder;
private String passHolder;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Name = findViewById(R.id.etpsudo);
Password = findViewById(R.id.etpaswword);
Loginmsg = findViewById(R.id.txtlog);
Btlogin = findViewById(R.id.btnlogin);
Tester=0;
Btlogin.setOnClickListener(new View.OnClickListener() {
#SuppressLint("SetTextI18n")
#Override
public void onClick(View v) {
nameHolder=Name.getText().toString();
passHolder=Password.getText().toString();
for(int i=0; i<=mUsers.size();i++){
if((nameHolder.equals(mUsers.get(i).getmNom()))&&(passHolder.equals(mUsers.get(i).getmPass()))){
Counter=i;
i=mUsers.size()+1;
Tester=1;
}
}
if (Tester==1){
Intent drawless = new Intent(MainActivity.this,newacc.class);
startActivity(drawless);
}
else{
Loginmsg.setText("eroor");
}
}
});
mUsers = new ArrayList<>();
mUsers.add(new User("amine","12345"));
mUsers.add(new User("bouhali","4523"));
mUsers.add(new User("khawla","ae12"));
}
}
You have a problem in you for loop
Look carefully at
for(int i=0; i<=mUsers.size();i++).
ArrayLists are index starting at 0. This mean that the last element is less than the total number, not equal to the total number.
Also I agree with Dave, use a break rather than modifying the value of i to end the loop, it is much clearer.

how set variable text in text view [duplicate]

This question already has answers here:
how to resolve java.lang.string cannot be cast to java.lang.integer android with xml parser
(2 answers)
Converting a string to an integer on Android
(14 answers)
Closed 5 years ago.
In the text view, I'll put a variable value of the string as text.
The error I'm facing is:
FATAL EXCEPTION: main Process: app.com.application, PID: 14336 Java. Lang. ClassCastException: Java. Lang. String cannot cast to Java. Lang. Integer atapp.com.application.activity.PayActivity$1.onCheckedChanged(PayActivity.java:41)
Also, this is my code for this part:
public class PayActivity extends AppCompatActivity {
Button button;
RadioGroup radioGroup;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pay);
button = findViewById(R.id.btnPay);
radioGroup = findViewById(R.id.radioPay);
textView = findViewById(R.id.pay);
final String[] TextShow ={
"مبلغ این بسته 2500 تومان است",
"مبلغ این بسته 7000 تومان است",
"مبلغ این بسته 10000 تومان است",
"مبلغ این بسته 20000 تومان است"
};
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, #IdRes int checkedId) {
int selectedId = radioGroup.getCheckedRadioButtonId();
RadioButton radioButton = findViewById(selectedId);
final int ArticleTag = (int) radioButton.getTag();
textView.setText(TextShow[ArticleTag]);
Toast.makeText(PayActivity.this,TextShow[ArticleTag] , Toast.LENGTH_SHORT).show();
}
});
}
}
Also, tags include "0" ,"1", ,"2" and "3".
Can you guide me where is my problem?
Thanks
You can't cast a string containing an integer value to an integer value:
(int) radioButton.getTag()
You have to explicitly convert to a string (since getTag() returns an Object), then parse:
Integer.parseInt(radioButton.getTag().toString())

How to disable a button in onCreate method with if statment [duplicate]

This question already has answers here:
How to disable an Android button?
(13 answers)
Closed 5 years ago.
I have a problem, I want to disable a button in onCreate method, please share the way of disabling any button at runtime in onCreate method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_requests_interface);
Intent intent = new Intent(this, AdminPopup.class);
startActivity(intent);
String fName = getIntent().getStringExtra("fName");
TextView tfName = (TextView)findViewById(R.id.fName);
tfName.setText(fName);
String vuEmail = getIntent().getStringExtra("VUEmail");
TextView tEmail = (TextView)findViewById(R.id.vuEmail);
tEmail.setText(vuEmail);
EditText vuEmailTest = (EditText)findViewById(R.id.vuEmail);
String email = vuEmailTest.getText().toString();
String str = email.substring(0,2);
if(str.equals("bc")){
String str2 = email.substring(3,9);
boolean digitsOnly = TextUtils.isDigitsOnly(str2);
if (digitsOnly){
Button accButton = (Button)findViewById(R.id.accButton);
}
}
else{
Button accButton = (Button)findViewById(R.id.accButton);
}
}
Try this:
Button accButton = (Button) findViewById(R.id.accButton);
accButton.setEnabled(false);
Note that in your posted code, you are setting the button with findViewbyId(), which should be findViewById() (the By needs to be capitalized).
Button button =(Button) findViewById(R.id.buttonid);
button.setVisibility(View.GONE);
Use android:enabled="false" in xml or accButton.setEnabled(false) in code
Also, it's better to check is numeric by this method:
public static boolean isNumeric(String str) {
try {
double d = Double.parseDouble(str);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}
Do this:
Button b = (Button) findViewById(R.id.mybutton);
b.setEnabled(false);

Android Unfortunately app has stopped? [duplicate]

This question already has answers here:
java.lang.IllegalStateException : Could not find a method onClick handler with id_button
(4 answers)
Closed 8 years ago.
I have to make an app where the user inputs and then the AI responds but when I input some text and press send it gives me the message "Unfortunately, app has stopped".
Here is my code:
Here is the code for sending:
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Button"
android:id="#+id/Send_btn"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="Zdenka" />...
Here is the start of the .java file:
EditText Text, OdgBox;
String odg;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
EditText Text = (EditText) findViewById(R.id.Txt); //User input
Button Btn = (Button) findViewById(R.id.Send_btn); //Send button
TextView Output = (TextView) findViewById(R.id.TextView); //AI output
}...
And the last part of the java file:
...
public void Zdenka (TextView Output, EditText Text, String odg) {
String Text1 = Text.toString().toLowerCase();
if (Text1 == "živjo") {
odg = "Živjo";
}
else if (Text1 == "zivjo") {
odg = "oj";
}
else{ odg = "Ne razumem."; }
Output.setText(odg);
Thanks for the help!
What I can see from your code is: You need to change your method of onClick:
You will need to replace
public void Zdenka (TextView Output, EditText Text, String odg) {...}
with
public void Zdenka (View v) {...}
Hope it helps.
And after you do this, make sure to use equals or equalsIgnoreCase for comparing String as Squonk said. == will compare objects, not the actual String.

Storing editText to variables, Android Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I posted this question before, however i didnt explain myself good. I will have the other question removed. Here is my current situation:
I have a regular xml page with a textView which when clicked opens a popup dialog. This dialog contains 2 editText. Currently my code (OnClick – Done button) gets the value of both edit texts and puts them into the single TextView. However when i open the pop-up again, instead of the two strings being listed in its own editText (Where each string was originally inputted) the combined string which was stored in the text view appears in one edit text. The issue is that although i’m getting the strings from 2 different editText’s and storing them into one textView. I cannot get each string back individually. I understand that i may have to store the string from each editText into variables and then i can use the variables to show the strings combined in the textView (and the editText – when i open the popup dialog again) How would i go about this? Thank for your help
The code:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
TextView showPopUpButton;
EditText getInput;
EditText getInput2;
String myvalue = "";
String myvalue2 = "";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showPopUpButton = (TextView) findViewById(R.id.buttonShowPopUp);
showPopUpButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showPopUp3(); }
});
}
private void showPopUp3() {
AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
helpBuilder.setTitle("Enter PU Builder");
LayoutInflater inflater = getLayoutInflater();
View checkboxLayout = inflater.inflate(R.layout.popuplayout, null);
helpBuilder.setView(checkboxLayout);
getInput = (EditText) checkboxLayout.findViewById(R.id.editText1);
getInput2 = (EditText) checkboxLayout.findViewById(R.id.editText2);
getInput.setText(myvalue);
getInput2.setText(myvalue2);
helpBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which)
{
myvalue = getInput.getText().toString();
showPopUpButton.setText(myvalue + ", " + myvalue2);
}
});
AlertDialog helpDialog = helpBuilder.create();
helpDialog.show();
}
}
First of all you need string to save your EditText value in it declare it like this
public class MainActivity extends Activity {
/** Called when the activity is first created. */
TextView showPopUpButton; //NEW
EditText getInput; //NEW
EditText getInput2; //NEW
// declare string to save the dialog edittext
String myValue = "" ;
then you need to show the last value of dialog in the EditText so try this :
private void showPopUp3() {
AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
helpBuilder.setTitle("Enter PU Builder");
LayoutInflater inflater = getLayoutInflater();
View checkboxLayout = inflater.inflate(R.layout.popuplayout, null);
getInput = (EditText) checkboxLayout.findViewById(R.id.editText1); //MISTAKE
getInput2 = (EditText) checkboxLayout.findViewById(R.id.editText2); //MISTAKE
getInput.setText(showPopUpButton.getText()); //New to keep the text in the editText when done is pressed
getInput2.setText(getInput2.getText()); //New test
// here set the my value to edit text , note firs time will be empty
getInput.setText(myValue)
and last thing when you click in done button in dialog you need to save the EditText value like that :
#Override
public void onClick(DialogInterface dialog, int which){
//showPopUpButton.setText(getInput.getText() + ", " + getInput2.getText());//NEW
//showPopUpButton.setText(value) ;
// save the edit text value into myvalue string
myvalue = getInput.getText().toString();
}
});
feed me back
This is fairly straightforward.
Create the variables to placehold your strings.
String inputText;
Where applicable, get and set.
inputText = editText.getText().toString();
textView.setText(inputText);
Did I understand this correctly? Is this what you are trying to accomplish?

Categories

Resources