-1

I have a requirement like when user click on edit text only it should be in the editable mode other wise non editable mode. And after editing the text need to remove the cursor and the updated text should be visible to Edit text. Please share me any idea.

How I implemented is: my edittext xml

    <EditText
                android:layout_weight="1"
                android:id="@+id/namevalue"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:fontFamily="@font/robotobold"
                android:text="ABC"
                android:focusable="false"
                android:cursorVisible="false"
                android:background="@android:color/transparent"
                android:textSize="14sp"
                android:textColor="@color/colortextw"
                />

And My Java code

nameval.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        nameval.setCursorVisible(true);
        nameval.setFocusable(true);
        nameval.setFocusableInTouchMode(true);
        nameval.requestFocus();
    }
});            

But the problem is, the cursor is not moving from the edit text after editing and when I move to the next page and came back to the same page the updated edit text is not showing in the edit text

Osama Rizwan
  • 615
  • 1
  • 7
  • 19
Lahari Areti
  • 627
  • 2
  • 10
  • 30
  • The default behaviour of Edittext is same as what you want. You don't need to do anything else other than to just include it in your layout.xml – s_o_m_m_y_e_e Sep 25 '20 at 06:50
  • Here the updated edit text is not showing in edit text . when I again came to that class – Lahari Areti Sep 25 '20 at 06:53
  • https://stackoverflow.com/questions/5056734/android-force-edittext-to-remove-focus this may help you – Maithili Joshi Sep 25 '20 at 06:54
  • why not saving your ediitext value using `onSavedInstanceState` ? – Wini Sep 25 '20 at 06:54
  • What you mean by coming to the activity again? Are you restarting the app? Or from another activity? – s_o_m_m_y_e_e Sep 25 '20 at 06:57
  • It might be anything i may close the app or move to another activity. if any thing is editted it should be saved automatically and display – Lahari Areti Sep 25 '20 at 06:58
  • @Wini whenever the text changes i need to save it . It means I need to write the edittext watcher and save that string in onSavedInstance – Lahari Areti Sep 25 '20 at 07:02
  • If you are restarting the app, it would remove the text of the editText as the activity's onDestroy() is called which would destroy the activity. And if you have another activity in your app(which is on top of this activity in stack) and you press the back button of your device, you should be able to see the text in edittext. Kindly refer here for details on activity lifecycle - [Activity Lifecycle](https://developer.android.com/guide/components/activities/activity-lifecycle) – s_o_m_m_y_e_e Sep 25 '20 at 07:05
  • @Wini I Stored the edit text value to shared prefernece when ever there the edit text is edited. And when I close or moving to next activity and came back I am getting from shared pref and setting to edit text – Lahari Areti Sep 25 '20 at 07:27
  • @LahariAreti are you now facing some problem?you got what you want? – Wini Sep 25 '20 at 07:28
  • 1
    @Wini I got what I want . Thanks – Lahari Areti Sep 25 '20 at 07:30

1 Answers1

0

I didn't get much from your requirement, but if want to enable the EditText after clicking on it and the after writing something to it you want to cursor focus to next edit text then billow snippets code will help you.

Example :

public class DemoActivity1 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_demo);

    EditText editTextName = findViewById(R.id.etx_name);
    EditText editTextEmail = findViewById(R.id.etx_email);
    EditText editTextMobile = findViewById(R.id.etx_mob);


    editTextName.setFocusable(false);
    editTextName.setClickable(true);

    editTextEmail.setFocusable(false);
    editTextEmail.setClickable(true);


    editTextMobile.setFocusable(false);
    editTextMobile.setClickable(true);

    editTextName.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            editTextName.setClickable(true);
            editTextName.setEnabled(true);
            editTextName.setFocusableInTouchMode(true);


        }
    });
    editTextEmail.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            editTextEmail.setFocusable(true);
            editTextName.setFocusable(false);
            editTextName.setClickable(true);
        }
    });

    editTextName.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    editTextEmail.requestFocus();
                }
            }, 1000);

        }
    });

    editTextEmail.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    editTextEmail.setFocusable(false);
                    editTextEmail.setClickable(true);
                    editTextMobile.requestFocus();
                }
            }, 1000);

        }
    });

}

}

Sniffer
  • 1,495
  • 2
  • 14
  • 30