I have a simple app with 2 edittext boxes. When input is typed into the 1st box one set of calculations are performed. If input is put into the other instead, a different set of calc's occurs.
So, if a number is typed into the first box etBox1, and the user leaves the box, the data from the first box is used to calculate a reesult and put it into the second box. If a number is typed into the second box etBox2, the data is used to calculate a value for the first box.
I tried:
final EditText etBox1 = (EditText) findViewById(R.id.etBox1)
final EditText etBox2 = (EditText) findViewById(R.id.etBox2)
etBox1.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
#override
public void onFocusChange(View v, boolean lostfocus)
{
if (lostFocus == true)
{ //do my calculations....}
This fires when the focus given to etBox1 instead of waiting for the box to loose focus. This crashes the app because the user hasn't had the chance to input a number into the box. Any ideas why this behaves as a "hasFocus" instead of a "lostFocus"? There is no documentation available on lostFocus at Android's site.
In your question, you have this.
final EditText etBox1 = (EditText) findViewById(R.id.etBox1);
final EditText etBox1 = (EditText) findViewById(R.id.etBox1);
You are using the same ID when mapping the text boxes. I guess it should look like this:
final EditText etBox1 = (EditText) findViewById(R.id.etBox1);
final EditText etBox2 = (EditText) findViewById(R.id.etBox2);
Not to mention that you should also get a compilation error with your code, as you define etBox1 two times...
(Unless this is only a typo in your question, and your code actually looks different...)
According to http://developer.android.com/reference/android/view/View.OnFocusChangeListener.html
onFocusChange is defined as public abstract void onFocusChange (View v, boolean hasFocus), so your boolean called lostFocus is named backwards and so confusing you, I'd recommend to change it to something like hasFocus.
now you should see that for if statement is the wrong way round, you should be checking if == false
Related
The app crashes when the button is selected which uses the values converted from edit texts. Tried multiple ways to move the part edittext = R.ids .. to try and make sure the edittexts picks new values after the oncreate first runs.
Think the calculation part causes the crash because its trying to perform a calculation with stored values from the edit text when the value is false from the first time the edit text gets the R.ids... in the onCreate method.
needed hide/display editText based off a radio button setonCheckedChangeListener in the onCreate method. So edittext = R.ids .. set in this method, the app does not crash at runtime like it would if I moved the edittext = R.ids .. to the testFunction method.
EditText editTextValue;
EditText editTextValue2;
double amount;
protected void onCreate(){...
//Get edittext field parameters
editTextValue = (EditText) findViewById(R.id.editText_weight_kg);
//listener to switch editTexts on which radio button selected in units group
unitsRG.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.imperial) {
editTextValue2.setVisibility(View.VISIBLE);
editTextValue.setVisibility(View.GONE);
}
void testFunction(View view){
String stringValue = editTextValue..getText().toString();
//check value as long as its not empty for the edit text , save it
if (editTextValue.getText().length() > 0) {
amount = Integer.parseInt(stringValue);
Log.e("MainActivity", " " + amount);
}
}
but now when I run the app I get this error in the long cat
enter image description here
E/ClipboardServiceEx﹕ clipEx is android.sec.clipboard.ClipboardExManager#1f70b420
E/ClipboardServiceEx﹕ clipEx has text data : false
here is the xml for one of the edit texts
<EditText
android:id="#+id/editText_weight_lb"
style="#style/EditTextViewStyle"
android:visibility="visible"/>
In the editTextStyle , I set the textCursorDrawable to null to try and have different colors for the pointer and underline colors. Not sure if this could also be affecting the editTextView storing the value
<item name="android:textCursorDrawable">#null</item>
I also tried setting edittext = R.ids in the testfunction and in the onCreate method. See if the editTexts would store the values the user enters rather than keeping the empty values when onCreate initially run.
I still got the same clipEx has text data:false error after trying this.
I searched the logcat error "clipEx has text data: false" and found something regarding samsung memory leaks.
https://github.com/square/leakcanary/issues/133
I am using a samsung galaxy for testing. I feel the issue is more with where I'm setting the edittexts to the R.ids thats causing the issue.
I saw the post for checking to make sure valid value entered for edittext.
Issue with empty EditText
How to Check whether a value is entered in editexts before submitting?
will add the check after finding out why values are not getting stored/ still remaining false.
Thanks
well I tried a different approach to implement the method.
I placed a button in the OnCreate method to define the event handlers against the buttons:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button calculate = (Button)findViewById(R.id.calculate);
calculate.setOnClickListener(new View.OnClickListener(){
String stringValue = editTextValue.getText().toString();
//check value as long as its not empty for the edit text , save it
if (editTextValue.getText().length() > 0) {
amount = Integer.parseInt(stringValue);
Log.e("MainActivity", " " + amount);
}
.....
.....
}
}
By using the button method in the OnCreate, when I ran the app, errors would actually come up on the Integer.parseInt() method call. Turns out that even though the editTexts that I was entering text for did not have text values, the other editTexts still had strings for the text, so this would cause the app to crash.
<EditText...
android:text="kg"/>
I took out the text values. It worked again.
I also took out this line in the style sheet for the editText. This was to change the editText border color, cursor color, or line.
<item name="android:textCursorDrawable">#null</item>
I tried the public void testFunction() approach which I had used before, the app works, but the clipEx has text data : false continues to show up.
But the app works now with either the Button method in onCreate or as a public void testFunction() approach.
I want to append text to an android EditText view but I want that text to not be present in the popup editor. To be clear I want to put units in the EditText. So for example "10 gallons" but when the popup editor is displayed I only want to see and edit "10". Then when the value is returned I want the " gallons" appended back on to the view.
Is this possible in an automatic way or do I have to track onTouch() events and have a listener for the keyboard and manually append the units again?
I believe the onFocusChange method for EditText views would detect when a user is editing the text field. Try something like this..
EditText et = (EditText) findViewById(R.id.editText);
et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if (b) {
((EditText)view).setText(String.valueOf(value));
} else {
value = Integer.valueOf(((EditText)view).getText().toString());
((EditText)view).setText(value + " gallons");
}
}
});
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.
In my activity I have the following views
TextView player1;
TextView player2;
TextView player3;
TextView player4;
EditText player1name;
EditText player2name;
EditText player3name;
EditText player4name;
Each of the TextView's has the onclick listener applied to it. and so fires the OnClick function.
When we get to the onClick this is what i am currently doing:
#Override
public void onClick(View v) {
//the v variable is the clicked textview, in this case "player1"
//hide the textview and show the resultant edittext
v.setVisibility(View.GONE);
player1name.setVisibility(View.VISIBLE);
//set focus on edit text and when focus is lost hide it and set the textview text
player1name.requestFocus();
imm.showSoftInput(player1name, InputMethodManager.SHOW_FORCED);
player1name.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View y, boolean x) {
v.setVisibility(View.VISIBLE);
player1name.setVisibility(View.GONE);
imm.hideSoftInputFromWindow(player1name.getWindowToken(), 0);
String name = player1name.getText().toString();
if (name.equals("")) {
v.setText("Player Name1");
} else {
v.setText(name);
}
}
});
}
However with this solution I will need to duplicate this code and change the view names for player2 - player2name, player3 - player3name etc
i can obviously grab the clicked TextView via v, however what i cant seem to do is grab its corresponding EditText.
i had thought of doing this:
View test = v + "name";
//then i replace all references to player1name with the test variable
but it doesnt work it wants me to convert View test; into a string
any suggestions?
EDIT: made it easier to understand my question
View test = v + "name";
will give a compile error. Because "v" is not a string type. and also even if it was String, test is not. This line is pretty wrong.
There a few options to achieve what you want,
You can use hashmap
Declare a global field for hashmap
private final HashMap<Integer,EditText> map = new HashMap<Integer,EditText>();
and in onCreate method put your textview id as key, and put your edittext variables in value.
player1name = (EditText) findViewById(R.id.player1name);
map.put(R.id.textView1, player1name);
// for the rest
in onClick method
EditText e = map.get(v.getId());
Then replace them with "e"
e.requestFocus(); //example
Will you please state your problem clearly? Currently, your language is very ambiguous and I can not figure out, exactly what are you looking for. It will help us to know your problem and in turn solve it.
So, today I decided to try out Android, so please understand that I am a beginner in it.
What I want to achieve right now is to have a EditText, and a set of buttons to be used to enter data into the EditText.
What I've done currently is stick a set of button widgets in the XML layout, and I use this code to make the buttons insert stuff into the EditText:
final EditText inputline = (EditText) findViewById(R.id.textentry);
final Button my_button = (Button) findViewById(R.id.my_btn);
my_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
inputline.append("a");
}
});
This kind of works, but I need help with a few issues:
it always appends the character at the end of the string, not at the current cursor position
similarly, when I call inputline.selectAll() and press my button, it inserts the text at the end of the string again; whereas I want it to delete the text first (as it's selected) and then insert the character
it seems tedious to write all that code for each of the buttons I have. Is there a better way to do this altogether?
Thanks for your help!
I have now pretty much solved by replacing inputline.append("a"); etc. with my custom function, lineInsert(), which you can see below.
public void lineInsert(CharSequence text) {
final EditText inputline = (EditText) findViewById(R.id.textentry);
int start = inputline.getSelectionStart();
int end = inputline.getSelectionEnd();
inputline.getText().replace(Math.min(start,end), Math.max(start,end), text, 0, text.length());
inputline.setSelection(inputline.getSelectionEnd());
}
This has the same behavior as the soft keyboard.