Android Move cursor from one EditText to another one? - android

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.

Related

Android EditText .setText("") not working

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.

Create editText with the property singleLine=true programmatically

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

Settext when you touch on edittex

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.

FindByView to get an EditText in a Button click method returns null

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.

How to display cursor at the end of text in TextView?

I wanted to display blinking cursor at the end of the text in TextView .
I tried by android:cursorVisible="true" in TextView But no go .
Even i tried text.setCursorVisible(true); Doesn't work .
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cursorVisible="true"
android:textCursorDrawable="#null" />
Does any one know any solution for it ?
First of all you should use EditText in place of TextView for taking input. If still the cursor doesn't blink, set the android:cursorVisible="true"attribute in xml file, it should make the cursor blink. If your cursor is not visible in edit text, that's also a reason one can't see the cursor blinking. Set android:textCursorDrawable="#null". This should solve your problem
<EditText
android:id="#+id/editext1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textCursorDrawable="#null"
android:cursorVisible="true">
</EditText>
In your activity class, add this code as well.
EditText input = (EditText)findViewById(R.id.edittext1);
input.setSelection(input.getText().length());
There is a solution for this.
I had to do this when I was making a terminal app and I used a simple runnable put a cursor at the end and make it blink.
I made 3 class variables:
private boolean displayCursor;
private boolean cursorOn;
private String terminalText;
private TextView terminal; // The TextView Object
terminalText keeps track of the text to be displayed.
Created a class method that runs the runnable the first time
private void runCursorThread() {
Runnable runnable = new Runnable() {
public void run() {
if (displayCursor) {
if (cursorOn) {
terminal.setText(terminalText);
} else {
terminal.setText(terminalText + '_');
}
cursorOn = !cursorOn;
}
terminal.postDelayed(this, 400);
}
};
runnable.run();
}
And initiated the variables and called runCursorThread() in onCreate()
cursorOn = false;
displayCursor = true;
runCursorThread();
I think you should go for EditText. You can set its background and make it appears like TextView with below code.
Step 1
<EditText
android:id="#+id/edtText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent" >
</EditText>
Step 2
EditText edt = (EditText) findViewById(R.id.edtText);
edt.setSelection(edt.getText().length());
Output
Finally Fixed this Using the EditText as per #Chintan Rathod advice.
<EditText
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"/> //reference to #Chintan Rathod.
Code
EditText text=(EditText) findViewById(R.id.text);
text.setText("hello");
text.setSelection(text.getText().length()); // reference to #Umer Farooq code.

Categories

Resources