How to show user entered EditText value in a android dialogue box - java

my modified sample of my work basically below i have 2 editboxes i want the values of the both to be shown in an alertbox, rest of the code works fine, the alertbox does show up but without the entered values.
public class Main extends Activity {
EditText username = null;
EditText password = null;
Button login;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText usern = (EditText) findViewById(R.id.username);
EditText pass = (EditText) findViewById(R.id.password);
Button login = (Button) findViewById(R.id.login);
String usernm = usern.getText().toString();
String passnm = pass.getText().toString();
final Builder alert = new AlertDialog.Builder(this)
.setTitle("SHOW FIELDS")
.setMessage("USERNAME:" + usernm + " PASSWORD:" + passnm)
.setNegativeButton("CLOSE", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
closeContextMenu();
}
});
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alert.show();
}
});

you will need to get EditText's value on button so show in AlertDialog as :
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
usernm = usern.getText().toString();
passnm = pass.getText().toString();
alert.show();
}
and declare both usernm and passnm at class level
EDIT :
or best way create a method to show alert and call it on button click as:
public void showalert(){
usernm = usern.getText().toString();
passnm = pass.getText().toString();
alert = new AlertDialog.Builder(Main.this)
.setTitle("SHOW FIELDS")
.setMessage("USERNAME:" + usernm + " PASSWORD:" + passnm)
.setNegativeButton("CLOSE", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
closeContextMenu();
}
});
alert.show();
}
and call this method on button click as:
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showalert();
}:
});
and declare all variables at class level by which you can access it in whole class as :
public class Main extends Activity {
EditText usern ;
EditText pass ;
Button login;
String usernm ,passnm; //<<< Declare here
Builder alert;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
usern = (EditText) findViewById(R.id.username);
pass = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showalert();
}
});
}

Related

Always shows Login successful, regardless of input. [Android Studio][JAVA]

I am learning Android studio, I am trying to create a basic functional Signup and Login page. But regardless of input in Login page it always shows true in if statement (Login Successful), Please tell help me address why is it happening?
Signup Page Code:
String Username,Email,Password;
Button btn;
EditText uname,email,pw;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uname = (EditText) findViewById(R.id.uname);
email = (EditText) findViewById(R.id.email);
pw = (EditText) findViewById(R.id.pw);
btn = (Button) findViewById(R.id.signupbtn);
Username = uname.getText().toString();
Email = email.getText().toString();
Password = pw.getText().toString();
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(SignUpActivity.this, Signin.class);
intent.putExtra("USER", Username);
intent.putExtra("PASS", Password);
startActivity(intent);
}
});
Sign In page Code:
String Signuser, Signpw;
EditText USERNAME, PASSWORD;
TextView showusername;
Button signinbtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signin);
String SavedUser = getIntent().getStringExtra("USER");
String SavedPass = getIntent().getStringExtra("PASS");
USERNAME = (EditText)findViewById(R.id.Userenter);
PASSWORD = (EditText)findViewById(R.id.passenter);
signinbtn = (Button)findViewById(R.id.signinbtn);
showusername = (TextView)findViewById(R.id.ShowUser);
showusername.setText(SavedUser);
Signuser = USERNAME.getText().toString();
Signpw = PASSWORD.getText().toString();
signinbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (Signuser.equals(SavedUser) && Signpw.equals(SavedPass)) {
Toast.makeText(Signin.this, "Login Successfull", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(Signin.this, "NOPE! DOSENT WORK!", Toast.LENGTH_SHORT).show();
}
}
});

How to delete individual elements from textview on Android studio

I am doing this project, can anyone help me to delete individual elements from the text view?enter image description here
here's i implemented the 'button' to insert and 'button2' to delete, and here's the code down bellow
public class MainActivity extends Activity {
private EditText editText;
private Button button;
private TextView textView;
private static final String TAG = "MainActivity";
// To hold all data in different mode : Portrait and landscape
private final String text_context = "TX";
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate: in");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)findViewById(R.id.editText);
button = (Button)findViewById(R.id.button);
textView = (TextView)findViewById((R.id.textView));
textView.setText(""); // make it no text at runtime, but text at view
textView.setMovementMethod(new ScrollingMovementMethod()); // make it scrolling
editText.setText("");
final View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
String result = editText.getText().toString();
result += "\n";
textView.append(result);
editText.setText(""); // text on EDITTEXT will disappear as soon as we click the button
}
};
final View.OnClickListener onClickListener1 = new View.OnClickListener() {
#Override
public void onClick(View view) {
}
};
if(editText != null)
button.setOnClickListener(onClickListener);
Log.d(TAG, "onCreate:out");
}
}
Please help me TO IMPLEMENT DELETE ACTION VIA BUTTON2....
Right now your onclick listeners are generically listening for clicks on any view.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Handle button events
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Handle button2 events
}
});
Or better yet implement View.OnClickListener() on your activity
Use regex
String input = editText.getText().toString();
String regex = "\\b" + input + "\\b";
String tvText = textView.getText().toString();
tvText = tvText.replaceAll(regex, "");
textView.setText(tvText);
Put this code in
final View.OnClickListener onClickListener1 = new View.OnClickListener() {
#Override
public void onClick(View view) {
//code goes here
}
};
if(editText != null)
// add this onclick to your button2
button2.setOnClickListener(onClickListener1);
Log.d(TAG, "onCreate:out");
}

How do I know which button does this method correspond to?

I am very new to Android Studio, and I have been looking at the official tutorial and found out this code:
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user taps the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
However, I don't see how that sendMessage() method is linked to the button we made in the tutorial. Which line mentions that this method corresponds to the button, which has a name button_send and a value of Send?
In your layout file android:onClick="sendMessage" attribute should be there on which you want to call this function whenever tapped.
You don't need to link button to this method by yourself, android:onClick attribute does it for you. If you want to link that button to this function by yourself then you will have to give an id to that button using android:id="#+id/button_send" and then link in this way:
Button send = (Button) findViewById(R.id.button_send);
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sendMessage(v);
}
});
In XML add this attribute "android:onClick="${methodName}" in Button element.
OR:
You can create button object and refference to UI.
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Refference UI
Button button = findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
sendMessage(view);
}
});
}
/** Called when the user taps the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}

java.util.HashMap cannot be cast to com.kosalgeek.genasync12.AsyncResponse

this is main activity java class
public class MainActivity extends AppCompatActivity implements AsyncResponse,View.OnClickListener {
EditText etusername,etpassword;
Button btnlogin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etusername = (EditText) findViewById(R.id.etusername);
etpassword = (EditText) findViewById(R.id.etpassword);
btnlogin = (Button) findViewById(R.id.btnlogin);
btnlogin.setOnClickListener(this);
}
#Override
public void processFinish(String result) {
Toast.makeText(this,result,Toast.LENGTH_LONG).show();
}
#Override
public void onClick(View v) {
HashMap postData = new HashMap();
postData.put("txtUsername", etusername.getText().toString());
postData.put("txtPassword", etpassword.getText().toString() );
PostResponseAsyncTask task = new PostResponseAsyncTask(this, (AsyncResponse) postData);
task.execute("http://10.0.3.2/client/login.php");
}
}
in xml file i have three fields.. username password and login button.
after clicking button , it says app currently stopped!
im using android phone t run my app!
I went through the library which u provided,U need to Change it Like this :
public class MainActivity extends AppCompatActivity implements AsyncResponse,View.OnClickListener {
EditText etusername,etpassword;
Button btnlogin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etusername = (EditText) findViewById(R.id.etusername);
etpassword = (EditText) findViewById(R.id.etpassword);
btnlogin = (Button) findViewById(R.id.btnlogin);
btnlogin.setOnClickListener(this);
}
#Override
public void processFinish(String result) {
Toast.makeText(this,result,Toast.LENGTH_LONG).show();
}
#Override
public void onClick(View v) {
HashMap postData = new HashMap();
postData.put("txtUsername", etusername.getText().toString());
postData.put("txtPassword", etpassword.getText().toString() );
PostResponseAsyncTask task = new PostResponseAsyncTask(this,postData,this);
task.execute("http://10.0.3.2/client/login.php");
}
}

Output string variable value from EditText into a message box

I am trying to input a number into a EditText, then output it in a message/AlertDialog after click a button. So far I have coded up what I think should work to output it, but for some reason, it doesn't. At the moment, the only output I recieve from the message box, is the text that I specified: "saved". There is no value from the variable being displayed.
Hopefully someone will be able to see what I am doing wrong and find a solution.
Thanks
Code below:
Button saveBtn1 = (Button) findViewById(R.id.btnSave1);
saveBtn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText inputTxt1 = (EditText) findViewById(R.id.yourPhoneNum);
String phoneNum1 = inputTxt1.getText().toString();
savenum1(phoneNum1);
}
});
public void savenum1(String phoneNum1) {
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage("Saved" + phoneNum1);
//dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();
dlgAlert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss the dialog
}
});
dlgAlert.create().show();
}
Create a private field EditText inputTxt1;
Before Button saveBtn1 = (Button) findViewById(R.id.btnSave1);
get the EditText id: inputTxt1 = (EditText) findViewById(R.id.yourPhoneNum);
At saveBtn1.setOnClickListener
get the phoneNum1: String phoneNum1 = inputTxt1.getText().toString();
Call savenum1 with phoneNum1: savenum1(phoneNum1);
Remove from savenum1:
EditText inputTxt1 = (EditText) findViewById(R.id.yourPhoneNum);
String phoneNum1 = (String) inputTxt1.getText().toString();
Code after correction:
public class SettingsScreen extends Activity {
private EditText inputTxt1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_settings);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
EditText inputTxt1 = (EditText) findViewById(R.id.yourPhoneNum);
Button saveBtn1 = (Button) findViewById(R.id.btnSave1);
saveBtn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String phoneNum1 = inputTxt1.getText().toString();
savenum1(phoneNum1);
}
});
}
public void savenum1(String phoneNum1) {
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage("Saved" + phoneNum1);
//dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss the dialog
}
});
dlgAlert.create().show();
}

Categories