I'm new to android. I've an activity which consists of radio buttons, and on pressing each button it opens another activity which consists of EditText andTextView. The number of EditText and TextViews may vary according to each button press. I want to read data from all EditText and process it(like adding etc). The input to EditText is only integers.
For example on pressing a button an activity gets created with 5 EditText views. I want to add all the numbers that are entered to each EditText.
How can I do this?
You can get the input from edittext like
int a = Integer.parseInt(edittext.getText().toString());
Do this for all edittext you have in your currently activity. And then perform further action as needed in those integers.
You can set the output to textview like
textview.setText(someVar);
What you will have to do is get all the edittext value convert it to integer and add it.
Example would be:
lets say you have 3 edittexts.
private editText1,editText2,editText3;
private Button sum;
//on button click event do this
sum.setOnClickListener(view.OnClickListener(){
public void onClick(View v){
int a = Integer.parseInt(editText1.getText().toString());
int b = Integer.parseInt(editText1.getText().toString());
int c = Integer.parseInt(editText1.getText().toString());
//add these numbers
int totalSum = inta+intb+inc;
//Show the value in toast
Toast.makeText(getApplicationContext(),String.valueOf(totalSum),Toast.LENGTH_LONG),show();
}
});
Related
So I have 6 edit texts and a button as shown below:
My question is how do I use the input from the EditTexts (which I have stored in content_main.xml) to do mathematical operations like calculating an average which I want to show up in a toast when the calculate button is pressed. I have already written some code in the MainActivity.java file that brings up a toast when the calculate button is pressed (also in content_main.xml), I just need to figure out how to use the inputs from the EditTexts in the toast.
EditText myText // = findViewById...
String text = myText.getText().toString();
What you should do first is to give each of its elements ID to also recognize from the Activity.
Then you should use the click event of the button
//Here it is referring to the id that gave his element in its layout
Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
And finally, like the button, get their input values EditText
//Here it is referring to the id that gave his element in its layout
EditText text = (EditText)findViewById(R.id.editText01);
And in order to do math, parse the string value remaining on a double (double for decimals can give the exact calculation if you want something, if you want to be an int approximately)
try{
Double value = Double.parseDouble(text);
}catch(NumberFormatException e){
//Message for error parse
}
I have multiple strings in my java file.
I want to create a Next button that navigate through string content on button click one by one in a single EditText.
How do I do this?
Do you want to achieve something like the following image .
add android:imeOptions="actionNext" in your EditText (in layout.xml)
If you mean to highlight the words one after another, you can do something like this:
public int index = 0; //word that is highlighted at this moment
Button button = (Button) findViewById(R.id.nextButton);
EditText editTextView = (EditText)findViewById(R.id.myTextView);
button.setOnClickListener(this);
and then in the onClick method:
String textInEditText = editTextView.getText().toString();
String[] words = textInEditText.split(' ');
String toBeHighlighted = words[index];
toBeHighlighted = "<font color='red'>"+toBeHighlighted+"</font>");
textInEditText.replace(words[index], toBeHighlighted);
editTextView.setText(Html.fromHtml(textInEditText));
index++; // move to next word
I am working on Android application in which I want to make my textfield editable and clickable. It has multiple TextFields and EditTexts on my screen. I have "EDIT" TextField for which I want to make it clickable and after clicking I want to make other field editable and enable. Without clicking edit Textfield all of them should not be enable.
My code snippet is given below:
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
fName = (TextView) findViewById(R.id.fnametxt);
lName = (TextView) findViewById(R.id.lastnameTxt);
mailText = (TextView) findViewById(R.id.mailTxt);
mobileText = (TextView) findViewById(R.id.mobileTxt);
dobText = (TextView) findViewById(R.id.dobTxt);
fName.setText(currentUserFirstName);
lName.setText(currentUserLastName);
dobText.setText("");
mobileText.setText(currentUserContactNumber);
mailText.setText(currentUserEmail);
}
You can't convert TextView to EditText, but you can rather use setEnabled property for EditText
You can use editText.setEnabled(true); to make the EditText editable.
Say you are having two edittexts as follows. And on entering data in first edittext, you need to make edittext2 editable, then you can do this:
EditText edittext1, edittext2;
//findViewByIds for both views
editText1.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if(s.length() != 0)
editText2.setEnabled(true);
else
editText2.setEnabled(false);
}
});
Hope this gives a clue how to use it.
EDIT:
Don't get confused between TextField, Button, EditText.
In Android, simple read only field is TextView. Editable textbox is called EditText, and Button is plain Button.
So as per what you are saying, you want tomake EditTexts editable upon clicking of a Button.
Use this:
EditText edittext1, edittext2;
Button button;
//findViewByIds for all views.
buton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
editText1.setEnabled(true);
editText2.setEnabled(true);
}
});
In my opinion, if you want to edit your textfield, then its better to go with EditText. The reason is as follows
The TextView's editable param does make it editable (with some restrictions).
If you set
android:editable="true"
you can access the TextView via the D-pad, or you could add
android:focusableInTouchMode="true"
to be able to gain focus on touch.
The problem is you cannot modify the existing text, and you cannot move the cursor. The text you write just gets added before the existing text.
I'm developing an app in Android and need to implement a common app function- accepting numeric input from the user via a EditText. After typing in this value, I want the user to be able to be select a 'go' button. A result should then appear below the first TextView. What should I use to display the result (TextView etc?) and how can I go about implementing this process. Thanks
You are new to this so First check d link http://www.tutorialspoint.com/android/android_event_handling.htm .
Here You have to define 1 TextView , 1 EditText and 1 Button
like
Button _go = (Button) findViewById(R.id._btngo);
than
_go.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView _getdata= (TextView) findViewById(R.id._tvresult);
EditText _result= (EditText) findViewById(R.id._etgetinput);
// get the input value and store as a string
String result = _result.getText().toString();
// display the result in textview
getdata.setText(result);
}
});
Hope you will find the solution .. :)
I am developing an application for Android.
My application contains ten buttons, to which I have set an onclicklistener() method.
The ten buttons contains the digits 0-9.
Now, if I click any two or three buttons among the ten buttons, the corresponding digits must be entered into edit text and it must be shown in the edittext box.
I am able to display the single digit if I click on any of the buttons, but if I click on another button, then the previous value disappears and the new value is shown.
But what I want is this: no matter how many buttons I click, that no. of digits will appear in the edittext box.
Please can anyone explain to me the code, or give me a hint so that it can be made in a simpler way.
Using Shared Preferences:
I think, you may used Shared Preferences when you button was click, get value from Edittext and put on shared preferences. After click next button get that shared preferences value. You may used each button click put on value shared preferences.
Go to this problem, which is help you to solve: >> SharedPreference problem in android
Using Intent:
May be used this code on button click event:
Bundle extras = getIntent().getExtras();
String value1 = extras.getString("Value1");
String value2 = extras.getString("Value2");
if (value1 != null && value2 != null) {
EditText text1 = (EditText) findViewById(R.id.EditText01);
EditText text2 = (EditText) findViewById(R.id.EditText02);
text1.setText(value1);
text2.setText(value2);
}
Other useful resources:
Get Value of a Edit Text field
http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-edittext-controls/
http://www.java2s.com/Code/Android/UI/GetvaluefromEditText.htm
http://geekswithblogs.net/bosuch/archive/2011/01/17/android---passing-data-between-activities.aspx
You are using editText.setText("");
Instead you must use editText.append();
You can take a public static String variable and concat the new value to previous and set it to EditText
Use below code
public class AsActivity extends Activity {
/** Called when the activity is first created. */
Button b1,b2,b3;
EditText et;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button3);
et=(EditText)findViewById(R.id.editText1);
}
public void onclick(View v){
switch(v.getId()){
case R.id.button1:
et.append("1");
break;
case R.id.button2:
et.append("2");
break;
case R.id.button3:
et.append("3");
break;
}
}
}
here i have use property of button you can use switch statement as shown above in ur onclicklistener
In all 0-9 buttons onclick event you can write following code.
editText.setText((editText.getText().toString) +""+ nevText);
When you click on button then above code set newText with previous text in edittext box.
You should use the EditText append() method which appends data to the EditText.
So each time a new button is clicked just use :
myEdtiText.append(str);