if statement in Android Studio - java

I'm new to Android development, I am currently trying to see if a value entered is equal to a value. Here I am seeing if the user input equals to 5, they are currently set as strings as to is the text field.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_uk_postage);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button home = (Button) findViewById (R.id.btnHome);
Button calculate = (Button) findViewById (R.id.btnCalculate);
home.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(ukPostage.this, MainActivity.class));
}
});
EditText lengthInput = (EditText) findViewById(R.id.editTextLength);
final String lengths = lengthInput.getText().toString();
final TextView amount = (TextView) findViewById (R.id.txtAmount);
calculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
if (lengths.equals("5")) {
amount.setVisibility(View.VISIBLE);
}
}
});
The text goes to visible if i click the button without the if statement there however won't once I write the if statement.
Thanks in advance.

if (lengths.equals("5")) {
String is immutable and once you reference it you need to reference it again with your edit text to reflect the latest value.
Instead of the above code you need to reference the lengths back with the lengthInput value each time you click the calculate button.
calculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
lengths = lengthInput.getText().toString();
if (lengths.equals("5")) {
amount.setVisibility(View.VISIBLE);
}
}
});
IMPORTANT: You need to set the lengths as a global variable instead of being a final variable since it can only be initialized once.

Related

ParseInt Syntax

I'm having a problem with the parseInt method in Android sdk. So far I have one simple edit text and one button in my program. I now want to save the number in a new Variable in my program on button click, so what are the steps I need to take to do so?
So far my code looks like this:
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#override
public void onClick(View v) {
int val = Integer.parseInt(EditText.getText().toString());
}
// Perform action on click
});
As per your code snippiest,
You can initialize **EditText* variable like that
final EditText editText= (EditText) findViewById(R.id.edittext);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int val = Integer.parseInt(editText.getText().toString());
}
// Perform action on click
}
Try to use above solution, I hope it will work
If you want to save a variable accessible from all your app, you can use the shared preferences.
Example :
// To save your int
final SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("App", Context.MODE_PRIVATE);
final SharedPreferences.Editor sharedPreferencesEditor = sharedPreferences.edit();
final EditText editText = (EditText) findViewById(R.id.edittext);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int value = Integer.parseInt(editText.getText().toString());
sharedPreferencesEditor.putInt("YourValue", value);
sharedPreferencesEditor.commit();
}
});
// To retreive your int
sharedPreferences.getInt("YourValue", 0);

How to make a button change a label [duplicate]

This question already has answers here:
Change button text and action - android development
(2 answers)
Closed 6 years ago.
I am new to java, and I am making a small little game that says "yes" or "no" when the user presses a button. I am not sure how to code to make the label change based on the user pushing the button. The code is below. Any other problems that you see with the code, I am interested in discovering those also.
public class FirstProject extends AppCompatActivity {
TextView answerTextView;
EditText name1Txt;
EditText name2Txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
answerTextView = (TextView) findViewById(R.id.answerLbl);
name1Txt = (EditText) findViewById(R.id.nameoneTxt);
name2Txt = (EditText) findViewById(R.id.name2Txt);
Button compBtn = (Button) findViewById(R.id.compBtn);
compBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
//where I want to make the button react
}
});
}
Every EditText has a method you have to call
EditText#setText(...);
Example:
name1Txt = (EditText) findViewById(R.id.nameoneTxt);
name2Txt = (EditText) findViewById(R.id.name2Txt);
Button compBtn = (Button) findViewById(R.id.compBtn);
compBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
//where I want to make the button react
name1Txt.setText("Hello...");
name2Txt.setText("...world");
}
});

Getting a NullExceptionPointer on my setOnClickListener()

I'm getting a NullException error on my Button setOnClickListener() method. I'm a bit flabbergasted as to why this is happening. Please bear in my mind that this is my first Android app, and I assume that there are bound to be some runtime errors (there are no compilation errors).
btnNaira.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dollarToNaira();
}
});
The dollarToNaira() method can be found here:
public void dollarToNaira() {
try {
dollarAmt = txtAmount.getText().toString();
nairaAmt = (Double.parseDouble(dollarAmt) * 199.00);
lblOutput.setText(dollarAmt + "U.S. Dollars = " + nairaAmt + " Nigerian Naira.");
}catch(Exception ex) {
message = "Please enter a valid numerical amount";
lblOutput.setText(message);
}finally {
txtAmount.requestFocus();
txtAmount.selectAll();
}
}
Please tell me what I am doing wrong I would like to know what causes the setOnClickListener method to return NULL. Thanks!
EDIT: this is the entire onCreate() method, I don't believe that I made an error:
protected void onCreate(Bundle savedInstanceState) {
txtAmount = (EditText) findViewById(R.id.txtAmount);
btnNaira = (Button) findViewById(R.id.btnNaira);
btnCFA = (Button) findViewById(R.id.btnCFA);
lblOutput = (TextView) findViewById(R.id.lblOutput);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//set up event listener on btnNaira and btnCFA
btnNaira.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dollarToNaira();
}
});
btnCFA.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dollarToCFA();
}
});
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
there is no problem in your code.
i guess you've made a mistake in initializing buttons and text views.
1- make sure that you define your listener inside a method, like onCreate(),
i think you had defined that in class body.
2- i guess that you had defined your button or text views,
but you did not initialized them , so they are null.
here is an example of initializing a Button
Button btnDoSomething = (Button)findViewById(R.id.idOfButton);
now you can setOnClickListener for it .
You should write in this way
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtAmount = (EditText) findViewById(R.id.txtAmount);
btnNaira = (Button) findViewById(R.id.btnNaira);
btnCFA = (Button) findViewById(R.id.btnCFA);
lblOutput = (TextView) findViewById(R.id.lblOutput);
Make sure you call setContentView(R.layout.activity_main); before initialize your button View.
Hope it helps.

How to reference button for onClickListener?

I am trying to create an Android app, and i want to create a on click listener, here is what I have so far.
public void amazonListener() {
amazonButton = (Button) findViewById(R.id.amazonButton);
}
As you see, i am in the very early stages, but where I first referenced amazonButton (before the = sign) button, it turns into red text and it says Cannot resolve symbol 'amazonButton'. Also, I have referenced this method in the onCreate method
This is how you'd go about creating a button and setting a click listener to it.
public class MainActivity extends YouTubeFailureRecoveryActivity {
Button amazonButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
amazonButton = (Button) findViewById(R.id.amazonButton);
amazonButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Define what should happen when the button is clicked.
}
});
}
}
You can also put the initializations in a single method and call that method, like you've tried :
public class MainActivity extends YouTubeFailureRecoveryActivity {
Button amazonButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeViews();
}
private void initializeViews() {
//Make sure you've declared the buttons before you initialize them.
amazonButton = (Button) findViewById(R.id.amazonButton);
amazonButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Define what should happen when the button is clicked.
}
});
// Add more Views initialization here ...
....
}
}
You need to give the type of the variable when you declare it.
Button amazonButton = (Button) findViewById(R.id.amazonButton);
The alternative is to declare it (but not initialize it) outside of any method and then initialize it later.
Button amazonButton;
/* ... */
private void amazonListener() {
amazonButton = (Button) findViewById(R.id.amazonButton);
}

How to make android edit text uneditable when switch is off?

I am brand new to android development and I am having difficulties trying to make my editText uneditable after a switch is set to off. The problem is that The switch function doesn't work. Even after it is set to off, I can still edit the text.
here is my code :
public void onCreate(Bundle savedInstanceState) {
this.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
final View switch1 = (Switch) findViewById(R.id.editSwitch);
final EditText mEdit = (EditText) findViewById(R.id.bioTxt);
switch1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (switch1.getContext().toString().equals("On")) {
mEdit.setEnabled(true);
}
else if (switch1.getContext().toString().equals("Off")) {
EditText mEdit = (EditText) findViewById(R.id.bioTxt);
mEdit.setEnabled(false);
}
}
});
};
}
Switch has a function isChecked() which returns true if the Switch is in the 'On' position. Therefore your onClick method can just be:
public void onClick(View v) {
mEdit.setEnabled(switch1.isChecked());
}
switch1.getContext().toString()
What are you trying to do here? getContext on a view returns the activity that owns it. It does not return anything that will turn into "On" or "Off". You need to call some other function. I'm not sure which, as it looks like its a custom view.

Categories