Reputation:
Ii am trying to figure out how to get the information that a user entered into a textview box which contains userpassword and username. My goal is to display the information from the two fields when the user clicks the button Login. I am stuck trying to display the information.
public class Login {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
TextView usernameTextView = (TextView) findViewById(R.id.userloginname);
TextView passwordTextView = (TextView) findViewById(R.id.userpassword);
TextView neighbourView = new TextView(this);
neighbourView.setTag(usernameTextView);
neighbourView.setTag(passwordTextView);
Button button_test;
button_test = (Button) findViewById(R.id.btnLogin);
button_test.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
String username = usernameTextView.toString();
String password = passwordTextView.toString();
// Toast.makeText(getApplicationContext(), "password is :" + password +" " + username + " ", Toast.LENGTH_LONG).show();
// System.out.println(username + "" + password);
Log.i(username + username, password + password);
}
});
// return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_login, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
}
And my XML
<!-- Username Label -->
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#372c24"
android:text="@string/username"/>
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_marginBottom="20dip"
android:singleLine="true"
android:id="@+id/userloginname"
android:inputType="text" />
<!-- Password Label -->
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#372c24"
android:text="@string/password"/>
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:singleLine="true"
android:password="true"
android:inputType="textPassword"
android:id="@+id/userpassword" />
<!-- Login button -->
<Button android:id="@+id/btnLogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="@string/loginButton"/>
Upvotes: 0
Views: 98
Reputation: 261
Here you are converting the TextView to string which is wrong
String username = usernameTextView.toString();
Use this syntax instead of the above to get the text from Textview and converting it to string
String username = usernameTextView.getText().toString();
Upvotes: 0
Reputation: 10687
First of all, a user can't enter text on a TextView. You'll need an EditText for that. You already have EditText in your layout file, so initialize them like so
EditText usernameEditText= (EditText) findViewById(R.id.userloginname);
EditText passwordEditText = (EditText) findViewById(R.id.userpassword);
Then inside your onClick() method
public void onClick(View v) {
String username = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();
Toast.makeText(getApplicationContext(), "password is :" + password +" " + username + " ", Toast.LENGTH_LONG).show();
// System.out.println(username + "" + password);
Log.i(username + username, password + password);
}
Upvotes: 1
Reputation: 1073
usernameTextView
and passwordTextView
are EditText not TextView.
Try to use
String username = usernameTextView.getText().toString();
String password = passwordTextView.getText().toString();
Instead of
String username = usernameTextView.toString();
String password = passwordTextView.toString();
Upvotes: 2