0

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.

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);

    }
}
esQmo_
  • 1,464
  • 3
  • 18
  • 43
M.Moustafa
  • 31
  • 1
  • 7
  • Hi and welcome to StackOverflow, 1st you can force focus on the `edittext` when it is showed this will force showing the keyboard [example](https://stackoverflow.com/questions/8991522/how-can-i-set-the-focus-and-display-the-keyboard-on-my-edittext-programmatical). 2nd why do you need to use a switcher?! why don't simply use a single `edittext`? you code and layout are very simple and I think the intended behavior can be achieved by using a single `edittext` – Atef Hares Aug 22 '18 at 21:58
  • 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 '18 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](https://stackoverflow.com/questions/4590957/how-to-set-text-in-an-edittext) 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 '18 at 22:34
  • 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 '18 at 22:56

0 Answers0