-1

I just wish to know how can i show or hide edit text preference that i use to add username and password.

can i use such kind of feature here -to enable or disable a button in xml for edit text preference as well

ImageButton ready = (ImageButton) findViewById(R.id.ready);

ready.setVisibility(View.INVISIBLE);

<EditTextPreference
    android:key="password"
    android:title="@string/pref_title_password"
    android:defaultValue="@string/pref_default_password"
    android:selectAllOnFocus="true"
    android:inputType="textPassword"
    android:capitalize="words"
    android:singleLine="true"
    android:maxLines="1" />
tomtom
  • 345
  • 2
  • 15
  • Doing so in xml? tricky. but i could suggest the same in the context code be it; activity, service e.t.c. – D. Sikilai Nov 19 '20 at 10:00
  • can i use something like that in code for edittextpreference ImageButton ready = (ImageButton) findViewById(R.id.ready); mic.setVisibility(View.INVISIBLE); – tomtom Nov 19 '20 at 10:20
  • That is it, if you would like to make it invisible or cover it with another view or disable it or create it programmatically in java after your conditions are satisfied. I think it would only make sense in the context code. – D. Sikilai Nov 19 '20 at 10:26
  • can you help me how to do that .. dont find any referene online ... how to hide – tomtom Nov 19 '20 at 10:52

1 Answers1

0

EditTextPreference is not derived from android.view.View, therefore setVisibilty will not work. Instead do as follows:-

  • get the underlying EditText.
  • call setVisibility from there. Or proceed further by getting the parent view and calling the method from there.

    Compiling our explanation, yield should be as follows:-
    get the preference:-

    EditTextPreference pswd=(EditTextPreference)findPreference("password");
    

    then:-

    pswd.getEditText().setVisibility(View.INVISIBLE);
    

    or generally:-

    ViewParent parent=pswd.getEditText().getParent();
    if(parent instanceof View)
    ((View)parent).setVisibility(View.INVISIBLE);
    else pswd.getEditText().setVisibility(View.INVISIBLE);
    

    Edit:
    The method findPreference is found in android.preference.PreferenceActivity or android.preference.PreferenceFragment. Which means you need to call it from a fragment or activity that is derived from any of them - Your activity should extend any of them.
    I have not tested the above snippets yet but feel free to edit correction or comment.

  • D. Sikilai
    • 467
    • 1
    • 3
    • 17
    • i am getting this error : cannot find symbol method findPreference(String) Any help would be highly appreciated – tomtom Nov 19 '20 at 14:48