Using EditText in ViewSwitcher doesn't enable it to be editable at first touch
Using EditText in ViewSwitcher doesn't enable it to be editable at first touch
I am using ViewSwitcher
to switch between EditText
and TextView
. When I click the TextView for the first time, it changes to EditText and then I have to click EditText again so that the keyboard appears and I to be able to edit the text.
ViewSwitcher
EditText
TextView
The behavior I want is to be able to edit the text from the first click on the TextView. I don't want the user to click twice each time they want to edit the text.
Is there any way to do that ?
Here is a sample of the code to make it more clear:
XML code
<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="vertical"
tools:context=".Trial">
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/user_name_switcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp">
<TextView
android:id="@+id/username_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="12dp"
android:paddingTop="8dp"
android:text="User Name"
android:textColor="@android:color/black"
android:textSize="18sp" />
<EditText
android:id="@+id/username_et"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="User Name"
android:inputType="textPersonName" />
</ViewSwitcher>
<Button
android:id="@+id/save_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="24dp"
android:layout_marginTop="24dp"
android:padding="12dp"
android:text="save" />
</LinearLayout>
Java code
package com.example.test
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ViewSwitcher;
public class Trial extends AppCompatActivity
TextView userNameTV;
EditText userNameET;
Button saveBtn;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trial);
userNameTV = findViewById(R.id.username_tv);
userNameET = findViewById(R.id.username_et);
saveBtn = findViewById(R.id.save_btn);
saveBtn.setEnabled(false);
userNameTV.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
textViewClicked();
userNameET.setText(userNameTV.getText().toString());
);
saveBtn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
ViewSwitcher switcher = findViewById(R.id.user_name_switcher);
switcher.showNext();
saveBtn.setEnabled(false);
);
public void textViewClicked()
ViewSwitcher switcher = findViewById(R.id.user_name_switcher);
switcher.showNext();
saveBtn.setEnabled(true);
edittext
edittext
edittext
first of all thanks, second of all i tried force focus but sadly it has the same behavior, and regarding the code it is not the main code i just made a sample to make it easier for asking the question and to not bother others with unnecessary parts of code won't affect the question, I use it for edit user profile in my application where the user can see his older data and whenever he wants to edit older data he just has to click the text view that contains older data and then be able to edit and save it.
– M.Moustafa
Aug 22 at 22:12
Yes I see what you are trying to do. as i said, i think this can be simply achieved by using a single
edittext
, at first-time set the old data using setText
example and also set the edittext
as disabled (i.e. not editable) using .setEnabled(false)
then at onClick set it as enabled again, and make sure you didnot set the attribute focusableInTouchMode
to false in xml or code and that's it. give it a try.– Atef Hares
Aug 22 at 22:34
edittext
setText
edittext
.setEnabled(false)
focusableInTouchMode
The first time you touch the layiut, it is a TextView, right? So you would need to find a way to also enable the EditText before ViewSwitcher switches to it. The suggestions by @AtefHares seem legit and you should try it
– esQmo_
Aug 22 at 22:56
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Hi and welcome to StackOverflow, 1st you can force focus on the
edittext
when it is showed this will force showing the keyboard example. 2nd why do you need to use a switcher?! why don't simply use a singleedittext
? you code and layout are very simple and I think the intended behavior can be achieved by using a singleedittext
– Atef Hares
Aug 22 at 21:58