Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add focus programmatically in Android?

Tags:

java

android

I've two EditText views (UserName and Password), I want to focus on 1st edittext if both fields are empty and it should focus on Password field if username already entered. I have tried every solution so far but nothing seems to work.

edtPassword.requestFocus(); 
edtPassword.setFocusableInTouchMode(true);
edtPassword.getParent().requestChildFocus(edtPassword,edtPassword);
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

Fragment Class:

public class ReloginpopupFragment extends DialogFragment {



@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(STYLE_NO_TITLE, R.style.DialogTheme);

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.popup_relogin, container);
    unbinder = ButterKnife.bind(this, view);
    return view;
}

@Override
public void onResume() {
    super.onResume();
    KeyboardHelper.showSoftKeyboard(getContext(), edtUserName);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    if (sharedPreferenceManager.getCurrentUser().getUserID() != null) {
        edtUserName.setText(sharedPreferenceManager.getCurrentUser().getUserID());
        edtPassword.requestFocus();

    }
    setListener();
    PackageManager packageManager = getContext().getApplicationContext().getPackageManager();
    String packageName = getContext().getApplicationContext().getPackageName();
    try {
        String myVersionName = packageManager.getPackageInfo(packageName, 0).versionName; txtAppVersion.setText("App Version "+myVersionName);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }

}

}

like image 332
AqsaSarwar Avatar asked Jan 30 '26 18:01

AqsaSarwar


1 Answers

You can focus on editText field by these methods

Programatically:

edittext.requestFocus();

Through xml:

<EditText...>
    <requestFocus />
enter code here
</EditText>

But the main problem is in your activity lifecycle where you're overriding onResume Method

@Override
public void onResume() {
    super.onResume();
    KeyboardHelper.showSoftKeyboard(getContext(), edtUserName);
}

You can replace your logic from onViewCreated() to onResume()

  @Override
    public void onResume () {
        super.onResume();
        if (sharedPreferenceManager.getCurrentUser().getUserID() != null) {

            edtUserName.setText(sharedPreferenceManager.getCurrentUser().getUserID());
            edtPassword.requestFocus();
            KeyboardHelper.showSoftKeyboard(getContext(), edtPassword);

        } else {
            KeyboardHelper.showSoftKeyboard(getContext(), edtUserName);
        }

    }
like image 102
Hamza Khan Avatar answered Feb 02 '26 09:02

Hamza Khan