I am creating a Android application and the fist activity that will open will be a password entry. For know the password has already been set in a variable with the value "admit". I have created a if statement to check what value the user has entered and then compare it to the variable, and if the values match then go to the home screen, however it keeps saying it is the wrong password. Can anybody take a look at my code and advise were i am going wrong.
public class Password extends ActionBarActivity implements View.OnClickListener {
protected String password = "admin";
String getPassword;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_password);
Button passwordButton = (Button) findViewById(passwordbutton);
EditText passwordInput = (EditText) findViewById(R.id.password);
getPassword = (passwordInput.getText().toString());
passwordButton.setOnClickListener(this);
}
public void onClick(View v) {
if (getPassword.equals(password)) {
Intent goHome;
goHome = new Intent(this, home.class);
startActivity(goHome);
} else {
AlertDialog.Builder wrongPasswordBuilder = new AlertDialog.Builder(this);
wrongPasswordBuilder.setTitle(getString(R.string.wrongPasswordTitle));
wrongPasswordBuilder.setMessage(getString(R.string.wrongPasswordTryAgain));
wrongPasswordBuilder.setPositiveButton("ok", null);
AlertDialog dialog = wrongPasswordBuilder.show();
}
}
If you write passwordInput.getText().toString() in onCreate(), it always return empty String. So write these lines in onClick(). Code is written below
public void onClick(View v) {
getPassword = passwordInput.getText().toString()
if (getPassword.equals(password)) {
Intent goHome;
goHome = new Intent(this, home.class);
startActivity(goHome);
} else {
AlertDialog.Builder wrongPasswordBuilder = new AlertDialog.Builder(this);
wrongPasswordBuilder.setTitle(getString(R.string.wrongPasswordTitle));
wrongPasswordBuilder.setMessage(getString(R.string.wrongPasswordTryAgain));
wrongPasswordBuilder.setPositiveButton("ok", null);
AlertDialog dialog = wrongPasswordBuilder.show();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_password);
Button passwordButton = (Button) findViewById(passwordbutton);
EditText passwordInput = (EditText) findViewById(R.id.password);
passwordButton.setOnClickListener(this);
}
public void onClick(View v) {
//Place the getPassword variable in the onClick method. That way it's stored everytime you click it.
getPassword = (passwordInput.getText().toString());
if (getPassword.equals(password)) {
Intent goHome;
goHome = new Intent(this, home.class);
startActivity(goHome);
} else {
AlertDialog.Builder wrongPasswordBuilder = new AlertDialog.Builder(this);
wrongPasswordBuilder.setTitle(getString(R.string.wrongPasswordTitle));
wrongPasswordBuilder.setMessage(getString(R.string.wrongPasswordTryAgain));
wrongPasswordBuilder.setPositiveButton("ok", null);
AlertDialog dialog = wrongPasswordBuilder.show();
}
}
Place getPassword = passwordInput.getText().toString(); within your OnClickListener. Right now your variable getPassword is being set in your onCreate method, so it will always be set to what your default value is for the EditText. Instead, you need to update the variable every time the button is pressed.
Related
So I have a screen where a user enters their name, on button click they are redirected to a menu where it displays their name.
In the menu screen I have a button that takes me to another screen (About Me), on that screen I have a button with an Intent to go back to the Menu Activity.
The issue is that when I click the button with the Intent to go back, the name displays as null instead of what the User entered.
This does not happen when I use the actual android navigation buttons to go to the previous screen, only the Button i created to go back to the Menu Activity.
Launcher Activity where user enters their name
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher_screen);
Button launcherNextBtn = (Button) findViewById(R.id.launcherNextBtn);
EditText launcherVisitorNameEditText = (EditText) findViewById(R.id.launcherVisitorNameEditText);
TextView launcherVisitorNameErrorTextView = (TextView) findViewById(R.id.launcherVisitorNameErrorTextView);
launcherVisitorNameErrorTextView.setText("");
launcherNextBtn.setOnClickListener((View v) -> {
String visitorName = launcherVisitorNameEditText.getText().toString();
if (visitorName.equals("")) {
launcherVisitorNameErrorTextView.setText("Please enter a value");
} else {
launcherVisitorNameErrorTextView.setText("");
Intent goToMenuActivity = new Intent(LauncherScreen.this, MenuScreen.class);
goToMenuActivity.putExtra("visitorName", visitorName);
startActivity(goToMenuActivity);
}
});
Menu activity where it displays the users name
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_screen);
Button menuHomeBtn = (Button) findViewById(R.id.menuHomeBtn);
Button menuAboutMeBtn = (Button) findViewById(R.id.menuAboutMeBtn);
TextView menuNameTextView = (TextView) findViewById(R.id.menuNameTextView);
String visitorName = getIntent().getStringExtra("visitorName");
menuNameTextView.setText("Dear " + visitorName);
menuHomeBtn.setOnClickListener((View v) -> {
Intent goToLauncherActivity = new Intent(MenuScreen.this, LauncherScreen.class);
goToLauncherActivity.putExtra("visitorName", visitorName);
startActivity(goToLauncherActivity);
});
menuAboutMeBtn.setOnClickListener((View v) -> {
Intent goToAboutMeActivity = new Intent(MenuScreen.this, AboutMeScreen.class);
startActivity(goToAboutMeActivity);
});
}
About me activity, when I click the button to back to Menu, i get "Dear null"
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_me_screen);
Button aboutMeBackBtn = (Button) findViewById(R.id.aboutMeBackBtn);
String visitorName = getIntent().getStringExtra("visitorName");
aboutMeBackBtn.setOnClickListener((View v) -> {
Intent goToMenuActivity = new Intent(getApplicationContext(), MenuScreen.class);
startActivity(goToMenuActivity);
});
}
Your problem is in About me activity screen
aboutMeBackBtn.setOnClickListener((View v) -> {
Intent goToMenuActivity = new Intent(getApplicationContext(), MenuScreen.class);
startActivity(goToMenuActivity);
});
This time you are not passing an extra field, so you are getting null
Answer 1
aboutMeBackBtn.setOnClickListener((View v) -> {
onBackPressed();
});
Answer 2
aboutMeBackBtn.setOnClickListener((View v) -> {
Intent goToMenuActivity = new Intent(getApplicationContext(), MenuScreen.class);
goToMenuActivity.putExtra("visitorName", visitorName)
startActivity(goToMenuActivity);
});
I'm currently trying to create an activity, which should be creating a new TextView on my main activity, everytime a Button is clicked on the first activity. However, instead of creating a new TextView everytime the button is clicked, it just changes the values of the first created TextView, so that there is always only one TextView. Is there a way to make it so that my first activity will not only create one single textview?
Here's the code from my "NewSubjectActivity":
**public class NewSubjectActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_subject);
Button SaveBtn = findViewById(R.id.SaveBtn);
nsaSaveBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Click();
}
});
}
protected void Click(){
Intent intent = new Intent(NewSubjectActivity.this, MainActivity.class);
Boolean createnewTextView = true;
intent.putExtra("createnewTextView",createnewTextView);
startActivity(intent);
}
}
And here's the (relevant) code from my MainActivity:
protected void ReceiveValue (){
//getting Extras
Intent nsareceivedvalues = getIntent();
boolean createTextView = false;
createTextView = nsareceivedvalues.getExtras().getBoolean("createnewTextView");
//declaring fixed Views
final LinearLayout mainLinearLayout = (LinearLayout)findViewById(R.id.mainLinearLayout);
//Params for TextView
RelativeLayout.LayoutParams Params = new RelativeLayout.LayoutParams(1000, 200);
Params.setMargins(0, 10, 0, 10);
while(createTextView) {
//creating a TextView
TextView newsubject = new TextView(MainActivity.this);
//applying values to the TextView
newsubject.setLayoutParams(Params);
newsubject.setGravity(CENTER);
newsubject.setBackgroundColor(Color.GRAY);
mainLinearLayout.addView(newsubject);
createTextView = false;
}
}
As I said, this only create one text view, everytime I press the button on my "NewSubjectActivity" I think this might be, because the previous text view is not saved and the MainActivity is reset everytime I switch between the activities.
Every help and advise is much appreciated <3
Maybe when you are going back to your main activity you should call to onBackPressed() function, so it will reset nothing at all. Here's the example in kotlin
val back_button:Button = findViewById(R.id.button2)
back_button.setOnClickListener { v -> run {
//Change activity
onBackPressed()
} }
in Java should be:
Button back_button = findViewById(R.id.button2);
back_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Change activity
onBackPressed()
}
});
Let's hope it works!
I'm using Firebase RealTime database, and using retrieving data, I'm asking about if I want to add If method on OnClick() for the button and get the ActionView link from real time database, and if there's no link in the database give a toast message that "There's no link available ATM"
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.res_screen);
String data0= getIntent().getStringExtra("Name");
String data1= getIntent().getStringExtra("Address");
String data2= getIntent().getStringExtra("Phone1");
String data3= getIntent().getStringExtra("Phone2");
String data4= getIntent().getStringExtra("Phone3");
String data5= getIntent().getStringExtra("Offer");
final String data6= getIntent().getStringExtra("Facebook");
final String data7= getIntent().getStringExtra("Menu");
final String data8 = getIntent().getStringExtra("Menu");
TextView nm = findViewById(R.id.name);
TextView ad = findViewById(R.id.address);
TextView ph1 = findViewById(R.id.phone1);
TextView ph2 = findViewById(R.id.phone2);
TextView ph3 = findViewById(R.id.phone3);
TextView off = findViewById(R.id.closeoffer);
ImageView fb = findViewById(R.id.facebookbut);
Button menu = findViewById(R.id.menubut);
nm.setText(data0);
ad.setText(data1);
ph1.setText(data2);
ph2.setText(data3);
ph3.setText(data4);
off.setText(data5);
fb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(data6));
startActivity(intent);
}
});
menu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(getIntent().getStringExtra("Menu")));
startActivity(i);
}
});
}
Maybe am a little confuse because my bad language.
I want to add if else into OnClick().
Mean.. if this button have link to visit ACTION_VIEW, then go. But if haven't link from realtime database, then give a Toast message "Sorry there's no link available at this moment"
Maybe you need to do this :
fb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//data6 may be having link to fb ,here we check if it is not null
if(data6!=null )
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(data6));
startActivity(intent);
}else{
//toast a message
Toast.makeText(Yourclass.this, "Sorry there's no link available at this moment", Toast.LENGTH_SHORT).show();
}
});
I am making a log-in system on Android. And I want the register Button to be unclickable when it has been clicked. I am using this code:
final Button register = (Button) findViewById(R.id.register);
register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
register.setEnabled(false);
Intent register = new Intent(getApplicationContext(), register.class);
startActivity(register);
}
});
This is working great, but I want the Button to remain unclickable even when the application or phone has been restarted. Does anyone know a way to make the Button unclickable permanently even when the application has been shut down?
As I already said in the comments section something like this may work:
public class MyActivity extends Activity {
private static final String KEY_IS_BUTTON_CLICKABLE = "key_clickable";
#Override
public void onCreate(Bundle savedInstanceState) {
...
final Button register = (Button) findViewById(R.id.register);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean isClickable = sharedPreferences.getBoolean(KEY_IS_BUTTON_CLICKABLE, true);
register.setEnabled(isClickable);
if(isClickable) {
register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
register.setEnabled(false);
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit()
.putBoolean(KEY_IS_BUTTON_CLICKABLE, false);
Intent register = new Intent(getApplicationContext(), register.class);
startActivity(register);
}
});
}
}
...
}
In this case you could take a pessimistic approach and disable the button in the layout (by default) with android:clickable="false" and enable it in the condition where registration is required.
I've fingers on my teeths with Java and Android. Normaly i code with Python, it easier and easier !
Java never let me do anything, or only in a unique method that i never know :(
So the only code working is copy-paste and it's hard to customize...
So, i've a simple editable text and a button, when i click on it, i go to an url. The button works : i can open my webview.
But i want to make the url with the editText and i can't :(
See that :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Add Click listeners for all buttons
final EditText Login = (EditText) findViewById(R.id.entry);
View firstButton = findViewById(R.id.ok);
firstButton.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()){
case R.id.ok:
Intent j = new Intent(this, Webscreen.class);
j.putExtra(com.tatai.froyo.Webscreen.URL,
"http://192.168.1.12/index2.php");
startActivity(j);
break;
}
So, how can i have the login edittext to make the url like http://192.168.1.12/index2.php?log=toto
... where toto may is get with the login edittext.
Impossible to read a global var on the onclick, i'm lost ! :(
i trid that on the onclick listner :
String logDetail = Login.getText().toString();
but it can't see Login, it's out ! : undeclared
on your code, TRy this,
public void onClick(View v)
{
switch(v.getId())
{
case R.id.ok:
String logDetail = Login.getText().toString();
Intent j = new Intent(this, Webscreen.class);
j.putExtra(com.tatai.froyo.Webscreen.URL,"http://192.168.1.12/index2.php?log="+logDetail);
startActivity(j);
break;
}
EDIT:
EditText Login;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Add Click listeners for all buttons
Login = (EditText) findViewById(R.id.entry);
View firstButton = findViewById(R.id.ok);
firstButton.setOnClickListener(this);
}
public void onClick(View v)
{
switch(v.getId())
{
case R.id.ok:
String logDetail = Login.getText().toString();
Intent j = new Intent(this, Webscreen.class);
j.putExtra(com.tatai.froyo.Webscreen.URL,"http://192.168.1.12/index2.php?log="+logDetail);
startActivity(j);
break;
}