How to place cursor to certain EditText box? - android

My XML contains Five EditText box and One Button. My cursor is now pointed to the First EditText box. How can I click a button to place the cursor automaticly to Third EditText box.
Thank you!

on your button's onClick() put..
thirdEditText.requestFocus();
Something like,
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
thirdEditText.requestFocus();
}
});

editText3.requestFocus();
add in onClick method of button.

Use requestFocus() method to gain focus.
Or put < requestFocus/> in your XML layout.

this is the code:
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
edittext.setFocusableInTouchMode(true);
edittext.requestFocus();
}
});

try
EditText editText = (EditText) findViewById(R.id.textId);
editText.requestFocus();

just add <requestFocus/> tag in the EditText like:
<EditText
android:id="#+id/editText"
android:layout_width="320dp"
android:layout_height="wrap_content"
android:layout_below="#id/label"
android:inputType="numberDecimal"
android:textSize="25dp" >
<requestFocus />
</EditText>

Related

Non-responding EditText

In my activity , an EditText is invisible by default and once a button is clicked it becomes visible.
I managed to do that but once the EditText is visible , I can't input text to it.In other words, the soft keyboard never appears ,the writing cursor never appears , and the hint inside it never goes and it's like "frozen".
I tried the following but didn't solve my problem
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editsearch.setVisibility(View.VISIBLE);
editsearch.setEnabled(true);
editsearch.setFocusable(true);
}
});
and this is my xml
<EditText
android:id="#+id/search"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#color/colorAccent"
android:drawableEnd="#drawable/ic_search"
android:drawablePadding="3dp"
android:layout_alignParentTop="true"
android:drawableRight="#drawable/ic_search"
android:visibility="invisible"/>
Try adding editsearch.requestFocus() method to your code after the line editsearch.setFocusable(true).
I found the solution.
I use relative layout and below my Edittext , there is a listview. I found that the Edittext was behind the listview so I brought it to front.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editsearch.setVisibility(View.VISIBLE);
editsearch.requestFocus();
editsearch.bringToFront();
editsearch.invalidate();
}
});

Setting onClickListener to editText

Hi im trying to add on click listener to editText so i can disable the softkeyboard when user clicks on edittext using this code below, how to do that?
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);
First it needs to be focusable...
<EditText
...
android:inputType="none"
android:focusable="false"
... />
You have to implement it in your code and than just add this to get an click listener...
myEditText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// hide the keyboard
// show own keyboard or buttons
}
});
try it and set OnClickListener
<androidx.appcompat.widget.AppCompatEditText
android:id="#+id/edt"
android:inputType="none"
android:focusable="false"
android:editable="false"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
The easiest and straight forward way you can set it is by editing the xml file as follows:
android:onClick="onClickMyEditText"
and define the same method in Activity class:
public void onClickMyEditText(View view) {
//your code here
}

EditText onClick not working in fragment

I have set the onClick property of an EditText which is located in a fragment:
<EditText android:id="#+id/edittext1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:inputType="none" android:maxLines="1"
android:singleLine="true" android:layout_marginTop="16dp"
android:focusable="false"
android:longClickable="false"
android:clickable="true"
android:onClick="doSomething"
android:cursorVisible="false"
android:editable="false">
Then in the fragment class I have:
public void doSomething(View view) {
//show dialogfragment...
}
But the method doSomething is grayed out and I get the warning 'Method doSomething is never used'.
Note: This code was originally in an activity and was working fine.
Is there another way to handle onClick in fragments?
first initialize an EditText instance in the top of your fragment
EditText et;
then
in your onCreateView()
add:
etEmployee=(EditText)rootView.findViewById(R.id.edittext1);
then implement your onClickListener()
et.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//what ever you need to do goes here
}
});
Note: in fragments you have to refer to the inflatedView to be able to access your editText in this case rootView.
also the code you are using doesn't work becouse here you are using fragments and using onClick attribute in your xml would only make you able to use it in the MainActivity that contain your fragment.
hope this will help.
If you add android:onClick="doSomething" to your fragment then method will be invoked in your activity but not in Activity. If you want the callback in your fragment add through pragmatically.
edittext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Your code.
}
});
You can always try to add a OnClickListener to the EditText if it does not matter to you in which way the function gets called.
edittext1 = (EditText) view.findViewById(R.id.edittext1);
edittext1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
doSomething();
}
});
In your layout XML try specifying a tools:context="com.mypackage.path.MyFragment". Don't know if it'll fix it for 'onClick', but it has fixed similar issues for me in the past.
This goes in the top most 'ViewGroup' in your layout file, example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.mypackage.path.MyFragment" />

How to change input type Number to Text of EditText in android

I have a requirement in my project,that by default the input type of the Edittext is Number.How we can give an option to the User to change the input type Number to Text/Alphabets.
Thanks
Tiru
You could use a toggle button to switch between number to text and vice versa.
in your layout xml:
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="InputType"
android:textOff="Text"
android:textOn="Number" />
Activity class:
EditText ed;
ToggleButton edtb;
//flag : used in the added code
static boolean flag = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed = (EditText) findViewById(R.id.editText1);
edtb = (ToggleButton) findViewById(R.id.toggleButton1);
edtb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
ed.setInputType(InputType.TYPE_CLASS_TEXT);
}
else
{
ed.setInputType(InputType.TYPE_CLASS_NUMBER);
}
}
});
/* **Edited** Adding a way to achieve the same using EditText only. */
ed.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(flag)
{
ed.setInputType(InputType.TYPE_CLASS_TEXT);
flag=false;
}
else
{
ed.setInputType(InputType.TYPE_CLASS_NUMBER);
flag=true;
}
}
});
}
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number|text"
/>
ToggleButton can help you. Create listener and asociate number type with one state and text with other, when user click the button, the input text will change.
First put in xml :
android:inputType="number"
change the input type Number to Text/Alphabets dynamically like.
edittext.setInputType(InputType.TYPE_CLASS_TEXT);
if you want only number in EditText
you can use - android:inputType="number"
android:digits="0,1,2,3,4,5,6,7,8,9"
also use android:numeric.
You can use android:inputType=text and write your own validation code to accept number/single decimal after number etc.

Programmatically adding EditText box

I have the following text with me.
"I drink tea and coffee". Now the requirement is to change this text to "I drink EditText AND EditText"....Here the EditText is a edit box where in the user can enter answers once it is clicked. I need to make this change pro grammatically.
Any suggestions on this, as to how this can be achieved????
You could use the following code in button's click event handler:
String str = "I drink tea and coffee";
String editTextString = editText.getText().ToString();
str.replace("coffee", editTextString);
str.replace("tea", editTextString);
You can ctreate activity structure in your layout xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I drink " />
<Button
android:id="#+id/firstAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EditText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" and " />
<Button
android:id="#+id/secondAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EditText" />
</LinearLayout>
And set listners to your buttons
private String[] answers = { "tea", "coffee", "juice", "compote" };
...
Button firstAnswer = (Button) findViewById(R.id.firstAnswer);
firstAnswer.setOnClickListener(new OnClickListener() {
private int position = 0;
#Override
public void onClick(View view) {
((Button) view).setText(answers[position]);
position++;
if (position == answers.length)
position = 0;
}
});
By using getText()
Example
Button mButton;
EditText mEdit;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton = (Button)findViewById(R.id.button);
mEdit = (EditText)findViewById(R.id.edittext);
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
Log.v("EditText", mEdit.getText().toString());
}
});
}
after that
mEdit.setText("Tea");
EditText et=(EditText)findViewByID(R.id.yourId);
Button bt=(Button)findViewById(R.id.yourBtId);
bt.setOnClickListener(
new View.setOnClickListener()
{
public void onClick(View v)
{
et.setText("You Drink EditText");
}
});
Put These code in onCreate() method
Use ViewSwitcher. This widget displays one of two views it contains:
<ViewSwitcher
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</ViewSwitcher>
And then, when button is pressed, switch it and load text from EditText to TextView or vice versa:
editText.setText(textView.getText());
viewswitcher.showNext();
or
textView.setText(editText.getText());
viewswitcher.showPrevious();
Refer to this for details.
You can use 4 textboxes with texts "I drink" ,"tea" , " and ", "coffee" respectively.
On the ontouch event of the 2nd and 4th textbox, you can display a textbox or edittext and edit the text. On a button click you can get the texts from the textboxes and display it again.
Just Use this : yourTextField.setText("..."+yourEditText.getText().toString());

Categories

Resources