I need to limit one EditText at 1 line only when the EditText has the focus but it doesn't do anything when it has the focus, only if it has the focus and I close the application works. I don't know why it's happen and I try some ways to solve it but I can't. This is my code:
if(myText.isFocused()){
myText.setSingleLine(true);
myText.setMaxLines(1);
myText.setLines(1);
}
Edited:
After dicussion, here is what I came to understand:
when the property of the editText is put in the onCreate method the Activity breaks.
Main object: create editText with the property singleLine=true programmatically.
Try setting onFocusChangeListener on your edittext.
myText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus){
myText.setSingleLine(true);
myText.setMaxLines(1);
myText.setLines(1);
}
}
});
Your code won't listen to all focus changes. It will just check it once. Hope this helps.
For keeping edittext singleLine irrespective of focus put this in onCreate after setContentView:
myText = (EditText) findViewById(R.id.ed);
myText.setSingleLine(true);
<EditText
android:id="#+id/edt_email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLength="Enter Your text limit like 10"
android:padding="8dp"
android:singleLine="true"
android:textSize="15dp" />
EDITED
Add the parameter id android:id="#+id/first_layout" for the xml layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/first_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
then in the onCreate or anywhere:
public class Test extends Activity {
private RelativeLayout mRlayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);
mRlayout = (RelativeLayout) findViewById(R.id.first_layout); // use the id here
EditText myEditText = getEditText();
mRlayout.addView(myEditText);
}
private EditText getEditText(){
EditText lEditText= new EditText(Test.this); // Pass it an Activity or Context
lEditText.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
lEditText.setSingleLine(true);
return lEditText;
}
}
when ever you want to create an editText, call getEditText(), Provided you also define all the other necessary properties for the editText
Related
I am trying to clear the text in my edit text field but for some reason it is not working.
I have declared an edit text field in the xml of my view:
<EditText
android:id="#+id/enterGlucoseLevel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/mySimpleXYPlot"
android:layout_centerHorizontal="true"
android:ems="10"
android:inputType="numberDecimal" />
I have used the attribute onClick to link my button to the enterGlucoseAction() method in my MainActivity class:
<Button
android:id="#+id/enterGlucoseButton"
android:clickable="true"
android:onClick= "enterGlucoseAction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/enterGlucoseLevel"
android:layout_centerHorizontal="true"
android:text="#string/enterGlucoseLvlButton" />
Here is that method:
public void enterGlucoseAction(View v) {
glucoseField.setText("");
Toast.makeText(v.getContext(), "You clicked the button", Toast.LENGTH_SHORT).show();
}
I get the toast pop up but my edit text field is not being cleared.
Here is my on create and instance variables in case the problem lies there.
private XYPlot plot;
private Button addGlucoseLevel;
private EditText glucoseField;
private XYSeries currentSeries;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addGlucoseLevel = (Button) findViewById(R.id.enterGlucoseButton);
glucoseField = (EditText) findViewById(R.id.enterGlucoseLevel);
currentSeries = new SimpleXYSeries(Arrays.asList(new Number[] { 7200, 8, 54000, 2, 64800, 4 }),
SimpleXYSeries.ArrayFormat.XY_VALS_INTERLEAVED,
"Glucose Level");
//Create an series of numbers to plot.
createGraph(currentSeries);
}
Thanks in advance.
And sorry, I know this has been asked before but I can't find a solution to my problem specifically.
To clear your EditText which accepts numbers use clear() method:
glucoseField.getText().clear();
You can try setting it this way:
((EditText)findViewById(R.id.enterGlucoseLevel)).getText().clear();
Alternatively you can use getText().clear(); just replace glucoseField.setText(""); with glucoseField.getText().clear();
You should use
setText("\b");
As we can use escape sequence
((EditText)findViewById(R.id.yourEditTextId)).getText().clear();
anything else crashed.
I have a simple LinearLayout that consists of a TextView and an EditText. The behaviour that I'd like to achieve is to be able to click on the EditText and handle it like normal, but treat the encompassing LinearLayout as a button that launches a new activity.
So for example, if the user clicks the space around the button in the view, a new activity is launched. If the user clicks on the EditText, then the keyboard appears and the user can populate the EditText.
Here is the simple onClickListener for the layout, which simply states that it has been clicked:
LinearLayout test = (LinearLayout)findViewById(R.id.linearLayout1);
test.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
System.out.println("layout clicked");
}
});
And the EditText has an OnFocusChangeListener that will simply state when it has gotten focus:
#Override
public void onFocusChange(View v, boolean hasFocus) {
System.out.println("EditText clicked");
}
Results:
-When the user clicks on the layout, the result "layout clicked" is correct
-When the user clicks on the edittext, the result is "layout clicked" followed by "EditText clicked", which is not correct. I'd like to ignore the linear layout's onClick event for this case.
Try creating a FrameLayout that contains a LinearLayout containing the TextView and place the EditText above the LinearLayout. This way you will not need to change anything about the listeners.
So it would be like this:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</LinearLayout>
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="200dp" />
</FrameLayout>
Note: use margins to adjust the position of the EditText
I guess something like this should work, although it isn't the cleanest solution.
Declare a runnable that should be executed when the layout is clicked.
final Handler handler = new Handler();
Runnable layoutPressed = new Runnable() {
public void run() {
// LAYOUT CLICKED
}
};
Then start this runnable in your layout onClickListener.
LinearLayout test = (LinearLayout)findViewById(R.id.linearLayout1);
test.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
handler.postDelayed(layoutPressed, 100);
}
});
Cancel the runnable in onFocusChange.
#Override
public void onFocusChange(View v, boolean hasFocus) {
handler.removeCallbacks(layoutPressed);
// EditText clicked
}
So whenever you click the editText, the onClick of your layout will get called first, but the actions will get cancelled when onFocusChange of the editText is called.
When you click the layout, onClick will get called and will execute its actions with a 100 msec delay.
You might have to modify the delay of the runnable.
How settext when you touch the edittext? example When you touch the edittext this delete the text automatically
<EditText
android:id="#+id/valor"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_alignLeft="#+id/button"
android:layout_below="#+id/tipo"
android:ems="10"
android:inputType="numberDecimal"
android:text="#string/value"
android:textColor="#ffffff"
android:textSize="24sp" />
The initial value is 0 but when I touch the edittext I need what the value are ""
You need to set the android:onClick attribute. Suppose you do this:
android:onClick="deleteText"
Then you'd need to implement the method like this:
public void deleteText(View view) {
// view will reference to your TextView
TextView tv = (TextView) view;
tv.setText("");
}
This would erase the text in it at that time.
Just use the hint property. It displays text in grey until the user starts entering their own text.
You can add a click listener programmatically.
final EditText textBox = (EditText) findViewById(R.id.valor);
textBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textBox.setText("");
}
});
Try this.
final EditText editText = (ExitText)findViewById(R.id.valor);
exitText.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
editText.setText("");
}
});
Use hint rather than doing like so, Just use this line in your EditText of your xml file:
android:hint="Write Hint"
You can use this concept of selector and when onFocus removes the hint.
I've coded professionally for many many years in other languages, but I'm pretty new at Android and I'm learning a lot from sites like stack overflow.
After hours of searching I'm struggling with trying to get a reference to an EditText in a Button click method. Both the EditText and Button are part of the same Activity.
Here's a cut down part of my activity_my.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:id="#+id/my_layout"
tools:context=".MyActivity" >
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords"
android:singleLine="true"
android:id="#+id/name" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/name"
android:text="#string/save"
android:id="#+id/save" />
</RelativeLayout>
And a cut down part of my MyActivity.java:
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutInflater().inflate(R.layout.activity_my, null));
findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
saveClick(view);
}
});
}
public void saveClick(View view)
{
EditText editText = (EditText) view.findViewById(R.id.name); // this is always null
if (editText != null)
{
String name = editText.getText().toString();
}
}
}
I think it's because I'm trying to use the findViewById method on the view parameter. I remember reading somewhere that the parameter to a button click event is the Button itself, not the layout. If that's correct how do I get a reference to the layout in order to use the findViewById method? Or is there something else wrong?
I've seen examples where the onClick event wraps an anonymous method where you can successfully use the findViewById because (I think) you are essentially nested in the onCreate event. However, I'd like to keep my button click event as a separate (non-anonymous) method.
Thanks for any help in advance.
Change this
EditText editText = (EditText) view.findViewById(R.id.name);
to
EditText editText = (EditText)findViewById(R.id.name);
and you can simply use
setContentView(R.layout.activity_my);
directly.
I guess EditText belongs to activity_my.xml
Also there is no need to initialize edittext everytime on button click
EditText editText;// declare as instance variable
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
editText = (EditText) view.findViewById(R.id.name)
public void saveClick(View view)
{
EditText editText = (EditText) view.findViewById(R.id.name); // this is always null
if (editText != null)
{
String name = editText.getText().toString();
}
}
that returns a valid View (an object != null) only if the EditText is inside the Button. You should look for the EditText in the View's hierarchy
use this
EditText editText = (EditText) findViewById(R.id.name);
The View.findViewById() method looks for child views.
In your case, the findViewById call is in the onClickListener of your edittext view. This view does not have a child, it is simple an edittext. That's why you're getting null.
I know these type of question asked many time .but still nobody gave perfect answer for that.
I have question :
I want to move from EditText1 ** to another **EditText2 .
I had already detect to editText1 but how to move cursor to editText2.?
In short I had to move my cursor position from one editText1 to another EditText2 directly.
I faced this type of issue and found the solution as below.
Here I have two editText, if I press "a", my cursor will move to next step. I used below code for doing it.
final EditText editText = (EditText) findViewById(R.id.editText1);
editText.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v , int keyCode , KeyEvent event) {
EditText editText2 = (EditText) findViewById(R.id.editText2);
// TODO Auto-generated method stub
if (keyCode == event.KEYCODE_A) {
Selection.setSelection((Editable) editText2.getText(),editText.getSelectionStart());
editText2.requestFocus();
}
return true;
}
});
Let me know if you are facing any error regarding this.
For this, all you need to do is...add below two properties to your EditText tag in xml, except the last EditText(In case, you add it to the last EditText also, then the cursor control will again go to the first EditText when you press enter/next from the keypad)
<EditText
.
.
android:singleLine="true"
android:imeOptions="actionNext"
.
.
/>
Hope this helps
Here is a working example, hope this helps.
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Some string of text"
/>
<EditText
android:id="#android:id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Some string of text"
/>
<Button
android:id="#android:id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
/>
</LinearLayout>
Class:
public class Example extends Activity {
TextView text1;
TextView text2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text1 = (EditText) findViewById(android.R.id.text1);
text2 = (EditText) findViewById(android.R.id.text2);
Button button = (Button) findViewById(android.R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Selection.setSelection((Editable) text2.getText(), text1.getSelectionStart());
text2.requestFocus();
}
});
}
}
Set onKeyListener to detect the key pressed on every key pressed checked your condition and when your condition will be fulfilled set edittext property edittext2.requestFocus();
I have tested all the previous code segments, and find all are working fine. But I find just calling "requestFocus()" with the proper edittext object is also working. As per as ques asked, the ans can be:
edittext2.requestFocus();
which is working fine for me. Please correct me if I am wrong.