Android Studio - How I can save a button value? - java

I like to save and disable a button, after I use the button (the code line below):
btn1.setEnabled(false);
I already know that I must use SharedPreferences, but I still need help with the code. I have already tried a lot but without success. Thats my Code.
public class Pass extends AppCompatActivity implements View.OnClickListener {
private Button btn1;
private EditText text1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pass);
btn1 = (Button) findViewById(R.id.button);
btn1.setOnClickListener(this);
text1 = (EditText) findViewById(R.id.editText);
text1.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
}
public void onClick (View view){
if (text1.getText().toString().equals("Pass)){
AlertDialog ad = new AlertDialog.Builder(this).create();
ad.setMessage("Super");
ad.show();
Intent intent = new Intent(this,Popup.class);
startActivity(intent);
btn1.setEnabled(false);
}else{
String message = "Leider falsch";
Toast.makeText(this,message, Toast.LENGTH_LONG).show();
}
}
}
Thanks for help

public class Pass extends AppCompatActivity implements View.OnClickListener
{
private Button btn1;
private EditText text1;
private SharedPreferences sharefPref;
private SharedPreferences.Editor editor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pass);
btn1 = (Button) findViewById(R.id.button);
btn1.setOnClickListener(this);
text1 = (EditText) findViewById(R.id.editText);
text1.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
editor = sharedPref.edit();
}
public void onClick (View view){
if(sharedPref.getBoolean("YourKey"),true)
{
editor.putBoolean("YourKey",false);
editor.commit();
if (text1.getText().toString().equals("Pass)){
AlertDialog ad = new AlertDialog.Builder(this).create();
ad.setMessage("Super");
ad.show();
Intent intent = new Intent(this,Popup.class);
startActivity(intent);
}else{
String message = "Leider falsch";
Toast.makeText(this,message, Toast.LENGTH_LONG).show();
}
}
}
}

Related

Database is saved on click but not going to the next class when clicked

public class JobSignup extends AppCompatActivity {
private Button btn;
private ImageView imageview;
private static final String IMAGE_DIRECTORY = "/demonuts";
private int GALLERY = 1, CAMERA = 2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_job_signup);
FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference myRef = database.getReferenceFromUrl("https://rozgarinepal100.firebaseio.com/");
final TextView userName = (TextView) findViewById(R.id.userName);
final EditText writeName = (EditText) findViewById(R.id.writeName);
ImageButton sendDataToFirebase = (ImageButton) findViewById(R.id.sendDataToFirebase);
final TextView dateOfBirth = (TextView) findViewById(R.id.dateOfBirth);
final EditText enterDateOfBirth = (EditText) findViewById(R.id.enterDateOfBirth);
if ( writeName.getText().toString().length() == 0)
{
writeName.setError("Username Compulsory");
}
// if( TextUtils.isEmpty(writeName.getText())) {
// /**
// * You can Toast a message here that the Username is Empty
// **/
//
// writeName.setError("First name is required!");
//
// }
sendDataToFirebase.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
myRef.child("0").child(userName.getText().toString()).setValue(writeName.getText().toString());
//myRef.child("NAME:").setValue("Kathmandu University");
myRef.child("0").child(dateOfBirth.getText().toString()).setValue(enterDateOfBirth.getText().toString());
}
});
}
public void onClick(View v) {
Intent i = new Intent(this, HomeActivity.class);
startActivity(i);
}
}
My code is running fine and database is also saved on click but on click it is not going to the next class using intent.Here in the onclick listener function i have added link for database when the button is clicked on android.Database is running fine on click operation but i am not able to go the next class which i have created.I have the only problem to go the next class when the same button is clicked.I am trying to enable the database and goto the next class using same button.
Try this, It will work...
sendDataToFirebase.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
myRef.child("0").child(userName.getText().toString()).setValue(writeName.getText().toString());
//myRef.child("NAME:").setValue("Kathmandu University");
myRef.child("0").child(dateOfBirth.getText().toString()).setValue(enterDateOfBirth.getText().toString());
Intent i = new Intent(JobSignup.this, HomeActivity.class);
startActivity(i);
}
});

Java- Android Toast Message doesn't show me me the message

I'am new to Android Studio, here is my problem, i used some other solutions but they were not useful. i don't know what is the problem with that.
Code is given below,
public class SignUpActivity extends AppCompatActivity {
EditText etName;
EditText etEmail;
EditText etPass;
RadioButton radioButton1;
RadioButton radioButton2;
RadioGroup radioGroup;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
Button sgButton = findViewById(R.id.sgButton);
etEmail = findViewById(R.id.input_Email);
etPass = findViewById(R.id.input_password);
etName = findViewById(R.id.inputName);
radioButton1 = findViewById(R.id.f);
radioButton2 = findViewById(R.id.m);
radioGroup = findViewById(R.id.radioGroup);
sgButton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
final String name = etName.getText().toString();
final String email = etEmail.getText().toString();
final String pass = etPass.getText().toString();
Toast toast = Toast.makeText(SignUpActivity.this,
"Crunch you felt it", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_HORIZONTAL,0,0);
toast.show(); // here is the Code for Toast.
}
}
);
}
}
Any help will be appreciated.

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);
}
}

Passing EditText integer to TextView via Intent app crashes after clicking (button) three

I am trying just to pass the int user input to the next class and print it, see that it works before continuing on using it or something else.
Home.java
start and exit button
public class Home extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button one = (Button) findViewById(R.id.b1);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goToSecondActivity();
}
});
Button two = (Button) findViewById(R.id.b2);
two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
private void goToSecondActivity() {
Intent i = new Intent(this, SelectNumberOfPlayers.class);
startActivity(i);
}
}
SelectNumberOfPlayers.java
Taking only the numbers from the input and passing it to StartGame.class
public class SelectNumberOfPlayers extends AppCompatActivity {
EditText numberOfPlayers;
Button three;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.enter_number_of_players);
three = (Button) findViewById(R.id.button3);
three.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
String txt = numberOfPlayers.getText().toString();
Intent i = new Intent(getApplicationContext(), StartGame.class);
i.putExtra("players", txt);
startActivity(i);
}
});
}
}
StartGame.java
Receiving Int and printing to TextView
public class StartGame extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_game);
Intent i = getIntent();
TextView numOfPlayersVal = (TextView) findViewById(R.id.txt2);
numOfPlayersVal.setText(i.getStringExtra("Player number"));
}
}
Where is the error happening? I've set the input keyboard to take only numbers
1. you forgot to initialize youe editext first initialize it in your SelectNumberOfPlayers Activity
numberOfPlayers = (EditText) findViewById(R.id.numberOfPlayers);
2.
the key must be same to pass and receive data with intent
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_game);
Intent i = getIntent();
TextView numOfPlayersVal = (TextView) findViewById(R.id.txt2);
numOfPlayersVal.setText(i.getStringExtra("players"));
}

Trouble with Intent in Android Studio

I have been trying to get a string to go from one java class to another for a project of mine. The code I have been experimenting with is not working. When I press the button, I know it opens the other Java class because it creates the other layout, but it doesn't show the string. Please help me.
First Java Class:
public class MainActivity extends AppCompatActivity {
private Button button;
Context context;
private EditText editText;
String number = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
editText = (EditText) findViewById(R.id.editText);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (editText.getText().toString() != null) {
String value = "value";
Intent secondscreenIntent = new Intent(context, SecondScreenJ.class);
secondscreenIntent.putExtra("Number", editText.getText().toString());
startActivity(secondscreenIntent);
}
}
});
}
}
Second Java Class:
public class SecondScreenJ extends Activity {
String number = null;
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondscreen);
textView = (TextView) findViewById(R.id.textView);
Bundle extras = getIntent().getExtras();
if (extras != null){
number = extras.getString("number");
}
textView.setText(number);
}
}
you are putting "Number" as key but, in your second activity you are trying to retrieve "number"
so change number to Number and it will work.
Its case Sensitive.
Don't make your keys hard-coded in that way.
Just declare public static variable in MainActivity and use it from SecondScreenJ
public class MainActivity extends AppCompatActivity {
private Button button;
Context context;
private EditText editText;
public static String NUMBER_KEY = "Number";
String number = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
editText = (EditText) findViewById(R.id.editText);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (editText.getText().toString() != null) {
String value = "value";
Intent secondscreenIntent = new Intent(context, SecondScreenJ.class);
secondscreenIntent.putExtra(NUMBER_KEY , editText.getText().toString());
startActivity(secondscreenIntent);
}
}
});
}
}
Second Java Class:
public class SecondScreenJ extends Activity {
String number = null;
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondscreen);
textView = (TextView) findViewById(R.id.textView);
Bundle extras = getIntent().getExtras();
if (extras != null){
number = extras.getString(MainActivity.NUMBER_KEY);
}
textView.setText(number);
}
}
Beware when using the key for putting and getting the extras. They're case sensitive. Replace "number" with "Number" in your second activity.

Categories