Settext when you touch on edittex - android

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.

Related

How to manipulate the contents of a button without using id; Android?

I'm new in android world and I have a problem, well, I'm making a project very simple it is about an activity where i have a button and an EditText. The button has an event onClick in XML.
My problem is it: I need the button value and send this value to
EditText but my button don't have a id. Help me I don't know how manipulate a element if it dont have a id.
XML Code:
<View
android:layout_height="#dimen/cell_height"
android:background="#color/red"/>
<Button
android:layout_marginLeft="#dimen/button_margin"
android:layout_gravity="center_vertical"
android:text="#string/hex_red"
android:textColor="#color/red"
android:onClick="copy"/>`
Java code:
public void copy(View boton){
EditText txtSelected = (EditText)this.findViewById(R.id.txtColor)
String color = boton; <-- here need the button value
txtSelected.setText(color);
}
I need your help, Thanks
you can say boton.getText().toString()
Modify your copy() function like this:
public void copy(View boton) {
EditText txtSelected = (EditText)this.findViewById(R.id.txtColor)
Button btn = (Button) boton; // << key point.
String color = btn.getText().toString();
txtSelected.setText(color);
}`
public void onClickBtn(View view) {
EditText txtSelected = (EditText)this.findViewById(R.id.txtColor)
Button btn = (Button)(view);
String value = (String) btn.getText();
txtSelected.setText(value);
txtSelected.setSelection(value.length()); // cursor will be at the end of the text
}

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

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.

Android onClick function for an edittext, How do I call the edittext?

I have an edittext form that can get filled, I want to clear it upon an onClick of a text, so I put an onClick that goes to my clear function in main.
I however, dont know how to properly target the edittext that I want. I want to clear TWO of them.
public void clear(View v) {
#+id/toptext.setText("");
}
This is the XML for that specific text.
<TextView
android:id="#+id/toptext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#303030"
android:text="#string/toptext"
android:textAppearance="?
android:attr/textAppearanceLarge"
android:textColor="#33B5E5"
android:textSize="50sp"
android:onClick="clear" />
Create instances of your EditTexts, using the id of the EditTexts in your xml layout. Then use setText on them and make them blank.
public void clear(View v) {
EditText et = (EditText)findViewById(R.id.theIdOfYourEditText);
EditText et2 = (EditText)findViewById(R.id.theIdOfYourOtherEditText);
et.setText("");
et2.setText("");
}
Edit for some reason the method above didn't work. This was the end solution:
// Put one of these in your onCreate method:
TextView tt = (TextView)findViewById(R.id.toptext);
tt.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
EditText et = (EditText)findViewById(R.id.theIdOfYourEditText);
EditText et2 = (EditText)findViewById(R.id.theIdOfYourOtherEditText);
et.setText("");
et2.setText("");
return true;
}
});
// or
TextView tt = (TextView)findViewById(R.id.toptext);
tt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText et = (EditText)findViewById(R.id.theIdOfYourEditText);
EditText et2 = (EditText)findViewById(R.id.theIdOfYourOtherEditText);
et.setText("");
et2.setText("");
}
});
Try this:
TextView tv = (TextView) findViewById(R.id.toptext);
tv.setText("Whatever you want!");
You can use this code:
public void clear(View v) {
if (v.getId() == R.id.toptext) {
((EditText) v).setText(null);
}
}
Similarly, add another if condition for another EditText.
First, get reference to your TextView by calling findViewById:
TextView textView = (TextView) findViewById(R.id.toptext);
Then use it to clear:
textView.setText("");
I think instead of setting "", u should use null as.
TextView textView = (TextView) findViewById(R.id.toptext);
textView.setText(null);
Use the param passed to the onClick() (v)
public void clear(View v)
{
((TextView)v).setText("");
}
Assuming you want to clear the text in the TextView that you have your onClick attached to. The View passed into clear() is the View that was clicked so you just cast it to the appropriate View (TextView) then you can use it as normal.
So this will clear the text on whichever TextView was clicked.

Android Move cursor from one EditText to another one?

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.

Categories

Resources