Sadiq Md Asif
Sadiq Md Asif

Reputation: 902

EditText.getText().toString() is Empty at onCreate() after setting data with ViewModel

I want to focus to the end of the editText when i get back to the activity from another activity. I Pass viewName with Intent to filter which View should be focused. I am using ViewModel - Data Binding Model class to set data to the views

Problem I can get the values from intent and view also focused but I can not get the size of text to set the cursor to the end of the text in the editText. Though Values are set to each views , i can see and do other stuff with the values. Only problem is getText().toString() at the beginning to set focus at last.

bindingName.viewName.getText().toString() is always empty thus cursor stays at the beginning.

This problem occurs if i call the requestFocus in the onCreate() or onResume() state. but Works fine if I put this to some method which executes a bit later.

Java Step1.class

public class Step1 extends Activity implements AdapterView.OnItemSelectedListener, View.OnClickListener {
    Step1Binding step1Binding;
   ....
    SubmittedModel submittedModel;
    boolean isTerritorySet = false;
    boolean isCeNameSet = false;
    boolean isDistributorSet = false;
    boolean isPSRSet = false;
    boolean isInEditMode = false;
    String viewName;

    private ArrayList<ArrayList<String>> areaList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        step1Binding = DataBindingUtil.setContentView(this, R.layout.layout_step1);
       .....
        submittedModel = new SubmittedModel();
        submittedModel.setUser_id(UserInfo.getInstance().getUserInfo(this).getUserId());
        .....

        if (getIntent().hasExtra("value")) {
            submittedModel = (SubmittedModel) getIntent().getSerializableExtra("value");
            step1Binding.setSubmitData(submittedModel);
        }
        if (getIntent().hasExtra("edit")) {
            isInEditMode = getIntent().getBooleanExtra("edit", false);
            if (isInEditMode) {
                step1Binding.backBtn.setVisibility(View.VISIBLE);
            }
        }
        if (getIntent().hasExtra("viewName")){
            viewName =getIntent().getStringExtra("viewName");
            requestFocusOnSelectedView();  //Method to select views
        }

        .....
    }

    .......

    void requestFocusOnSelectedView() {
        //request focus
        if (isInEditMode & viewName != null) {
            switch (viewName) {
                case ViewName.territory:
                    step1Binding.territoryList.requestFocus();
                    break;
                case ViewName.ceName:
                    step1Binding.ceNameList.requestFocus();
                    break;
                case ViewName.distributor:
                    step1Binding.distributorList.requestFocus();
                    break;
                case ViewName.psr:
                    step1Binding.psrNameList.requestFocus();
                    break;
                case ViewName.subRoute:
                    step1Binding.subRoute.requestFocus();
                    step1Binding.subRoute.setSelection(step1Binding.subRoute.getText().length());
                    break;
                case ViewName.outlet:
                    step1Binding.outletName.requestFocus();
                    step1Binding.outletName.setSelection(step1Binding.outletName.getText().length());
                    break;
                case ViewName.retailerName:
                    step1Binding.retailerName.requestFocus();
                    step1Binding.retailerName.setSelection(step1Binding.retailerName.getText().length());
                    break;
                case ViewName.retailerMobile:
                    step1Binding.retailerMobile.requestFocus();
                    step1Binding.retailerMobile.setSelection(step1Binding.retailerMobile.getText().length());
                    break;
                case ViewName.address:
                    step1Binding.address.requestFocus();
                    step1Binding.address.setSelection(step1Binding.address.getText().length());
                    break;


            }
        }
    }

}

XML + DATA Model : Code works fine

Upvotes: 2

Views: 583

Answers (2)

George Mount
George Mount

Reputation: 20926

After setting all your data, but before requesting focus, call

binding.executePendingBindings();

This will force the View values to be set.

Upvotes: 2

beeb
beeb

Reputation: 1615

Try this:

view.post(new Runnable() {
    public void run() {
        theView2Focus.requestFocus();
    }
});

Your problem is that you have to wait until the UI is created. That is exactly what you detected.

Upvotes: 1

Related Questions