i tried to make my first converter in android studio with java but 2 functions setText and getText are not working
package com.example.unitconvertor;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity<editTextTextPersonName> extends AppCompatActivity {
private Button button;
private editTextTextPersonName editTextTextPersonName;
private View editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "value saved", Toast.LENGTH_SHORT).show();
String s = editText.getText().toString();
int inch = Integer.parseInt(s);
double cm = 2.54 * inch;
editText.setText( "the value is" + cm);
}
});
}
}
i hope that i am clear
thankyou
Try this-
public class MainActivity extends AppCompatActivity {
private Button button;
private EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
editText = findViewById(R.id.your_id);//ADD YOUR EDIT TEXT ID HERE
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "value saved", Toast.LENGTH_SHORT).show();
String s = editText.getText().toString();
int inch = Integer.parseInt(s);
double cm = 2.54 * inch;
editText.setText( "the value is" + cm);
}
});
}
}
The error is that your "edittext" is declared as view, correct it like this : private EditText edittext;
then, retrieve it by id like this in your onCreate method edittext = findViewById(R.id.your_id);
I am trying to work with two buttons on one screen and I want to have two different actions occur when each button is clicked. This is my code and I am getting the error message onClick view is already defined on the method titles of both onClick methods. Any help is greatly appreciated.
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity2 extends AppCompatActivity implements View.OnClickListener {
private Button button3;
private Button button2;
private EditText editText;
private EditText editText9;
private EditText editText10;
private EditText editText11;
private EditText editText12;
private EditText editText13;
private EditText editText14;
private TextView textView;
private TextView textView22;
private View view;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2_main);
Intent intent = getIntent();
button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(this);
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(this);
editText = (EditText) findViewById(R.id.editText);
editText9 = (EditText) findViewById(R.id.editText9);
editText10 = (EditText) findViewById(R.id.editText10);
editText11 = (EditText) findViewById(R.id.editText11);
editText12 = (EditText) findViewById(R.id.editText12);
editText13 = (EditText) findViewById(R.id.editText13);
editText14 = (EditText) findViewById(R.id.editText14);
textView = (TextView) findViewById(R.id.textView);
textView22 = (TextView) findViewById(R.id.textView22);
}
public void onClick (View view) {
this.view = view;
if (view.getId() == R.id.button3) {
Intent intent = new Intent(MainActivity2.this, MainActivity.class);
MainActivity2.this.startActivity(intent);
}
}
public void onClick (View view){
if (view.getId() == R.id.button2) {
String value8 = editText.getText().toString();
String value9 = editText9.getText().toString();
String value10 = editText10.getText().toString();
String value11 = editText11.getText().toString();
String value12 = editText12.getText().toString();
String value13 = editText13.getText().toString();
String value14 = editText14.getText().toString();
}
}
}
You have two onClick methods with the same parameters so it doesn't know which to use. Combine the two and it should work.
I am trying to get values from shared preferences and it is returning null. The shared preference value is set from an asynctask. I tested it from inside the asynctask, printing the stord value in a toast and it worked, but problem is when I try the retrive the value outside the asynctask by clicking on the buttonRegister, it display null. There is no error shown into the logcat.
The code of the Activity:
package com.example.mohalogin;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Properties;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class RegisterActivity extends Activity {
private EditText editTextName;
private EditText editTextUsername;
private EditText editTextPassword;
private EditText editTextEmail;
private Button buttonRegister;
private Button buttonLogin;
//private Button gmail,yahoo;
Context context;
private SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "MyPreferences" ;
public static final String RegisResult = "result";
private static final String REGISTER_URL = "xxxxxxxxxx"; //fake data
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
editTextName = (EditText) findViewById(R.id.editTextName);
editTextUsername = (EditText) findViewById(R.id.editTextUserName);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
buttonRegister = (Button) findViewById(R.id.buttonRegister);
buttonLogin = (Button) findViewById(R.id.buttonLogin);
buttonRegister.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0){
registerUser();
sharedpreferences=getApplicationContext().getSharedPreferences("MyPrefs", getApplicationContext().MODE_PRIVATE);
buttonLogin.setText(sharedpreferences.getString("result","")); //testing by printing the value in the button
}
});
/*buttonLogin.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0){
startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
}
});*/
}
private void registerUser() {
String name = editTextName.getText().toString().trim().toLowerCase();
String username = editTextUsername.getText().toString().trim().toLowerCase();
String password = editTextPassword.getText().toString().trim().toLowerCase();
String email = editTextEmail.getText().toString().trim().toLowerCase();
register(name,username,password,email);
}
private void register(String name, String username, String password, String email) {
class RegisterUser extends AsyncTask<String, Void, String> {
ProgressDialog loading;
RegisterUserClass ruc = new RegisterUserClass();
private Context context2;
#Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(RegisterActivity.this, "Please Wait","Registering new user", true, false);
}
#Override
protected String doInBackground(String... params) {
HashMap<String, String> data = new HashMap<String,String>();
data.put("name",params[0]);
data.put("username",params[1]);
data.put("password",params[2]);
data.put("email",params[3]);
String result = ruc.sendPostRequest(REGISTER_URL,data);
return result;
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
sharedpreferences = context2.getSharedPreferences(MyPREFERENCES, context2.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(RegisResult, s);
editor.commit();
Toast.makeText(getApplicationContext(),sharedpreferences.getString("result",""), Toast.LENGTH_LONG).show();
}
public RegisterUser(Context context2)
{
this.context2=context2;
}
}
RegisterUser ru = new RegisterUser(getApplicationContext());
ru.execute(name, username, password, email);
}
}
Your problem is with the call to getSharedPreferences(String, int) -
You basically use two separate SharePreference instances, since you are passing a different name (String) for the SharedPreferences each time -
When you store the value, you are using
getSharedPreferences("MyPreferences", context2.MODE_PRIVATE);
And when you try to obtain the value, you are using
getSharedPreferences("MyPrefs", getApplicationContext().MODE_PRIVATE);
Modify your code to use the same SharedPreference name and it should work.
When you assign part of the code to be executed by the AsyncTask, it is actually executed in a new thread, other than the Ui thread. Actually in the onClick of the ButtonRegister, the registerUser() and the following two lines, run in parallel mode not sequentially.
If you want to make sure that saving in SharedPreferences is completed successfully, you can read it again and set the button text. Alter the OnPostExecute of your AsyncTask this way:
#Override
protected void onPostExecute(String s)
{
super.onPostExecute(s);
loading.dismiss();
.
.
.
buttonLogin.setText(sharedpreferences.getString("result",""));
}
You are accessing the value at the wrong place.
Try this code instead :)
package com.example.mohalogin;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Properties;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class RegisterActivity extends Activity {
private EditText editTextName;
private EditText editTextUsername;
private EditText editTextPassword;
private EditText editTextEmail;
private Button buttonRegister;
private Button buttonLogin;
//private Button gmail,yahoo;
Context context;
private SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "MyPreferences" ;
public static final String RegisResult = "result";
private static final String REGISTER_URL = "xxxxxxxxxx"; //fake data
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
sharedpreferences=getApplicationContext().getSharedPreferences("MyPrefs", getApplicationContext().MODE_PRIVATE);
editTextName = (EditText) findViewById(R.id.editTextName);
editTextUsername = (EditText) findViewById(R.id.editTextUserName);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
buttonRegister = (Button) findViewById(R.id.buttonRegister);
buttonLogin = (Button) findViewById(R.id.buttonLogin);
buttonRegister.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0){
registerUser();
}
});
/*buttonLogin.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0){
startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
}
});*/
}
private void registerUser() {
String name = editTextName.getText().toString().trim().toLowerCase();
String username = editTextUsername.getText().toString().trim().toLowerCase();
String password = editTextPassword.getText().toString().trim().toLowerCase();
String email = editTextEmail.getText().toString().trim().toLowerCase();
register(name,username,password,email);
}
private void register(String name, String username, String password, String email) {
class RegisterUser extends AsyncTask<String, Void, String> {
ProgressDialog loading;
RegisterUserClass ruc = new RegisterUserClass();
private Context context2;
#Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(RegisterActivity.this, "Please Wait","Registering new user", true, false);
}
#Override
protected String doInBackground(String... params) {
HashMap<String, String> data = new HashMap<String,String>();
data.put("name",params[0]);
data.put("username",params[1]);
data.put("password",params[2]);
data.put("email",params[3]);
String result = ruc.sendPostRequest(REGISTER_URL,data);
return result;
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute();
loading.dismiss();
sharedpreferences = context2.getSharedPreferences(MyPREFERENCES, context2.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(RegisResult, s);
editor.commit();
Toast.makeText(getApplicationContext(),sharedpreferences.getString("result",""), Toast.LENGTH_LONG).show();
buttonLogin.setText(sharedpreferences.getString(RegisResult,"")); //testing by printing the value in the button
}
public RegisterUser(Context context2)
{
this.context2=context2;
}
}
RegisterUser ru = new RegisterUser(getApplicationContext());
ru.execute(name, username, password, email);
}
}
When you try to retrieve the value outside the asynctask, it display null because the asynctask didn't finish. Whatever you need to do with the data you get it in the executing of the asyncTask, you need to do it inside of onPostExecute, it is the only place where you can be sure the asyncTask already finish.
Re-instantiate your Shared-preference after the commit.
Hi i have bit problem in login actually i"m trying to tesing login by checking username from databse and edit text value but its not working . the following is my codes .
i'm bit confussed about to get the EditTextvalue
package com.example.employeemanager;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Login extends Activity {
SQLiteDatabase db;
EditText unameedt;
String unam;
String pass;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
Button logbtn = (Button) findViewById(R.id.loginbtn);
Button signup = (Button) findViewById(R.id.signup);
EditText unameedt = (EditText) findViewById(R.id.editText1);
EditText passedt = (EditText) findViewById(R.id.editText2);
db = openOrCreateDatabase("Employeemanager", MODE_PRIVATE, null);
unam = unameedt.getText().toString();
pass = passedt.getText().toString();
logbtn.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
Cursor c = db.rawQuery(
"SELECT username, password from Employee", null);
if (c != null) {
if (c.moveToFirst()) {
do {
String uname = c.getString(c
.getColumnIndex("username"));
String upass = c.getString(c
.getColumnIndex("password"));
if (uname.equals(unam)) {
StartActivity(new Intent(Login.this, Home.class));
}
} while (c.moveToNext());
}
}
}
});
signup.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
startActivity(new Intent(Login.this, Emp_signup.class));
}
});
}
}
Put your
unam = unameedt.getText().toString();
pass=passedt.getText().toString();
into butonClick event
logbtn.setOnClickListener(new OnClickListener()
{
unam = unameedt.getText().toString();
pass=passedt.getText().toString();
});
Its simple but looks like a big thing while start learning Android. What you doing now is you got the value in onCreate
unam = unameedt.getText().toString();
pass = passedt.getText().toString();
So, now the unameand pass is empty because while onCreate there is no values set to the corresponding EditText.
In OnClick Method you comparing the Value from Edittext and from database
if (uname.equals(unam)) {//if(usernamefromDatabase.equals(emptyValue))
StartActivity(new Intent(Login.this, Home.class));
}
Thats where the problem is.
What you want to do now is get the unam and pass values in onClick
unam = unameedt.getText().toString();
pass = passedt.getText().toString();
if (uname.equals(unam)) {//if(usernamefromDatabase.equals(valueyouentered))
StartActivity(new Intent(Login.this, Home.class));
}
It should work now.
package com.example.app123;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText input;
EditText output;
Button one;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = (EditText) findViewById(R.id.editText1);
output = (EditText) findViewById(R.id.editText2);
one = (Button) findViewById(R.id.button1);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (v == one) {
Context context1 = getApplicationContext();
CharSequence text1 = "Please enter a valid number";
int duration = Toast.LENGTH_SHORT;
final Toast toast = Toast.makeText(context1, text1,
duration);
toast.show();
int inputValue = Integer.parseInt(input.getText()
.toString());
int value = inputValue * inputValue;
output.setText(value);
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it ispresent.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
hi remove this condition in your button click code if(v==one) only use this like
input=(EditText)findViewById(R.id.editText1);
output = (EditText) findViewById(R.id.editText2);
one = (Button) findViewById(R.id.button1);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Context context1 = getApplicationContext();
CharSequence text1 = "Please enter a valid number";
int duration = Toast.LENGTH_SHORT;
final Toast toast = Toast.makeText(context1, text1,
duration);
toast.show();
int inputValue = Integer.parseInt(input.getText()
.toString());
int value = inputValue * inputValue;
output.setText(value);
}
});
i hope it's work for you.
If you have multiple buttons:
Button b1=(Button)findviewById(R.id.b1);
Button b2=(Button)findviewById(R.id.b2);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch(v.getid())
case R.id.b1:
CharSequence text1 = "Please enter a valid number";
int duration = Toast.LENGTH_SHORT;
final Toast toast = Toast.makeText(YourActivity.this, text1,
duration);
toast.show();
int inputValue = Integer.parseInt(input.getText()
.toString());
int value = inputValue * inputValue;
output.setText(value);
break;
case R.id.b1:
break;
}
});