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);
Related
I am testing my knowledge of android studio and seem to be running into a problem I am trying to make an app that takes the value of a edittext and puts that value into a textview when you click a button. After that it brings you to a page with a back button on it and when I click that it brings me to the first page. But when I try to click the first button it doesn't work anymore.
Code:
public class TestApp extends AppCompatActivity {
private EditText ET;
private Button Add;
private TextView TV;
public String Array[] = new String[9999999];
private Button GoBack;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_app);
ET = (EditText) findViewById(R.id.ET);
Add = (Button) findViewById(R.id.Add);
Add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String getC = ET.getText().toString();
setContentView(R.layout.value);
TV = (TextView) findViewById(R.id.TV);
GoBack = (Button) findViewById(R.id.GoBack);
if (getC.length() > 0){
for (int i = 0; i < Array.length; i++){
if (Array[i] == null){
Array[i] = getC;
TV.setText(Reminders[i]);
}
}
}
GoBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.activity_calender_for_glasses);
}
});
}
});
}
}
thank you for your help.
If not necessary, don't modify the current content view. If you want multiple screens, you have to create multiple activities or fragments...
Moreover :
at the beginning, you inflate R.layout.activity_test_app
when you click on goBack button (1), you inflate R.layout.activity_calender_for_glasses and it's not the same as the first screen
(1) be careful to coding rules: https://source.android.com/setup/contribute/code-style
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.
I have an EditText box that appears and the hint in it is actually a question that needs to be answered. Once the user enters the value and hits next, how can I make a different EditText box take the originals place. I want to do this so there is a new hint(question) and I can manipulate that input differently than with the first input.
In my example, my first EditText is enter_one and I want it to change to enter_two after the next_button is clicked.
Thank you for your help!
final EditText getInput = (EditText)findViewById(R.id.enter_key_one);
Button clickButton = (Button) findViewById(R.id.next_button);
clickButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String newValue = getInput.getText().toString().trim();
int input = Integer.parseInt(newValue);
System.out.println("Your value is " +input);
}
});
Create two edit text in XML layout, and align both in same position, one over another. Then declare the android:visibility="gone" for edittext enter_two.
Then onClick of next should be:
final EditText getInput = (EditText)findViewById(R.id.enter_key_one);
final EditText getInput_two = (EditText)findViewById(R.id.enter_key_two);
Button clickButton = (Button) findViewById(R.id.next_button);
clickButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String newValue = getInput.getText().toString().trim();
int input = Integer.parseInt(newValue);
System.out.println("Your value is " +input);
getInput_two.setVisibility(View.VISIBLE);
getInput.setVisibility(View.GONE);
}
});
You can just change the hint and clear the EditText :
final EditText getInput = (EditText)findViewById(R.id.enter_key_one);
Button clickButton = (Button) findViewById(R.id.next_button);
clickButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getInput.setHint(newQuestion);
getInput.setText("");
}
});
Hi I can't seem to build this.. It states that it cannot resolve the OnClickListener. The onClick action is performed the back button that goes to the main activity.
Button bnCompute = (Button) this.findViewById(R.id.bnCompute);
bnCompute.setOnClickListener(new View.OnClickListener());
{
#Override
public void onClick (View view){
Toast.makeText(MainActivity.this, "You Compute All!", Toast.LENGTH_LONG).show();
EditText etBeauty = (EditText) MainActivity.this.findViewById(R.id.etBeauty);
EditText etBody = (EditText) MainActivity.this.findViewById(R.id.etBody);
EditText etIntelligence = (EditText) MainActivity.this.findViewById(R.id.etIntelligence);
int total = Integer.parseInt(String.valueOf(etBeauty.getText())) + Integer.parseInt(String.valueOf(etBody.getText()))
+ Integer.parseInt(String.valueOf(etIntelligence.getText()));
Intent actSummary = new Intent(MainActivity.this, Score.class);
actSummary.putExtra("total", Integer.toString(total));
MainActivity.this.startActivity(actSummary);
}
}
You have implemented onClickoutside the scope of listener.
It should be something like this below :
Button button = (Button) findViewById(R.id.button1);
//Your mistake is on this line.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
}
});
I have followed a tutorial for button creation and this is the code I have for the button:
public class MainActivity extends ActionBarActivity {
private final static String EXTRA_MESSAGE = "com.example.jamie.lifewithasd.MESSAGE";
public void Parents(View v) {
Button button=(Button) v;
startActivity(new Intent(getApplicationContext(),ParentPage.class));
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
I have no idea if I can create all my buttons the same way or not.
If you want to create button pragmatically then check out this link for creating button pragmatically.
http://www.mysamplecode.com/2011/10/android-programmatically-generate.html
If you want to open another activity from current then you need intent.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(CurrentActivity.this, ActivityTwo.class);
startActivity(i);
}
});
Check out these links
http://www.vogella.com/tutorials/AndroidIntent/article.html
http://www.mkyong.com/android/android-button-example/