logo
down
shadow

Dialog Builder in PreferenceFragment


Dialog Builder in PreferenceFragment

Content Index :

Dialog Builder in PreferenceFragment
Tag : android , By : S Hall
Date : November 23 2020, 01:01 AM

seems to work fine So I decided that I would just implement an extended ListPreference which deals with creating the confirmation dialog and voila, It now works perfectly.
public class ThemeListPreference extends ListPreference {

    private int mClickedDialogEntryIndex;

    private Context mContext;

    public ThemeListPreference(Context ctxt) {
        this(ctxt, null);
    }

    public ThemeListPreference(Context ctxt, AttributeSet attrs) {
        super(ctxt, attrs);

        mContext = ctxt;

        setNegativeButtonText(ctxt.getString(R.string.no));
    }

    @Override
    protected void onPrepareDialogBuilder(Builder builder) {
        if (getEntries() == null || getEntries() == null) {
            throw new IllegalStateException(
                    "ListPreference requires an entries array and an entryValues array.");
        }

        mClickedDialogEntryIndex = findIndexOfValue(getValue());
        builder.setSingleChoiceItems(getEntries(), mClickedDialogEntryIndex,
                new DialogInterface.OnClickListener() {
                    public void onClick(final DialogInterface dialog, final int which) {
                        // Save preference and close dialog
                        mClickedDialogEntryIndex = which;

                        ThemeListPreference.this.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                        dialog.dismiss();

                        new AlertDialog.Builder(mContext)
                                .setIcon(android.R.drawable.ic_dialog_alert)
                                .setTitle(R.string.confirm_restart_title)
                                .setMessage(R.string.confirm_restart_message)
                                .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                        Utils.changeTheme((Activity) mContext);
                                    }

                                })
                                .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }

                                })
                                .show();
                    }
                });

        builder.setPositiveButton(null, null);
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {

        CharSequence[] mEntryValues = getEntryValues();

        if (positiveResult && mClickedDialogEntryIndex >= 0 && mEntryValues != null) {
            String value = mEntryValues[mClickedDialogEntryIndex].toString();
            if (callChangeListener(value)) {
                setValue(value);
            }
        }
    }
}

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Android app : show PreferenceFragment as a dialog?


Tag : android , By : negonicrac
Date : March 29 2020, 07:55 AM
hope this fix your issue To do an old fashioned, single page settings screen without PreferenceHeaders, do as follows:
In your activity that invokes the settings screen (example is in onMenuItemSelected):
case R.id.menuSettings:
        Intent settingsIntent = new Intent(this, SettingsActivity.class);
        startActivityForResult(settingsIntent, GC.SETTINGS_ACTIVITY_ID);
        break;
package com.mycompany.project1;

import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;

public class SettingsActivity extends PreferenceActivity {

    private final static String TAG = "SettingsAcitivity";

    public SettingsActivity() {}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyLog.d(TAG, "onCreate");
        getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new LocationFragment()).commit();
    }

    public static class LocationFragment extends PreferenceFragment {

        private String TAG = "LocationFragment";

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            MyLog.d(TAG, "onCreate");
            addPreferencesFromResource(R.xml.settings);
        }
    }
}

How can I display a Dialog from a PreferenceFragment?


Tag : android , By : cmhudson
Date : March 29 2020, 07:55 AM
this will help Building on Steve's answer, I also need to set up the DialogFragment to import from android.app.DialogFragment (instead of android.support.v4.app.DialogFragment). The show() function in that library asks for an android.app.FragmentManager, which I can provide via a call to getFragmentManager() as I did within the code I posted in the question.

File picker dialog in PreferenceFragment


Tag : java , By : user177910
Date : March 29 2020, 07:55 AM
it should still fix some issue I don't think Andorid have a native file chooser, so you'll have to implement one yourself, or find a library.
You could then use the android:onClick property in PreferenceFragment.xml:
<EditTextPreference
    android:key="DB_URI"
    android:title="Database file"
    android:onClick="startFileChooser"
    android:dependency="DB_default"> 
public void startFileChooser(MenuItem i){
     // Start the file chooser here
}

Microsoft Builder SDK 3.8.1.0: Location dialog throws exception Method not found "Void Microsoft.Bot.Builder.Dialog


Tag : chash , By : Roel van Dijk
Date : March 29 2020, 07:55 AM
With these it helps This is known issue the team is going to fix soon. It was reported here and here. To unblock yourself you can just re-compile the library using the latest version of BotBuilder, that will make the trick.
Update

Android changing Dialog content of a PreferenceFragment


Tag : android , By : Robert M
Date : March 29 2020, 07:55 AM
should help you out You should implement Preference.OnPreferenceChangeListener for PreferenceFragment class. Then override onPreferenceChange method and if newValue is correct return true otherwise return false. If you return false then preference value will not change.
public class SettingsFragment extends PreferenceFragmentCompat implements
    Preference.OnPreferenceChangeListener {

       @Override
       public boolean onPreferenceChange(Preference preference, Object newValue) {

             int number = 0;
             try{
                   number = Integer.parseInt(newValue.toString());
              }catch(Exception e){

              }

              if (number > 0 && number < 10){    //It's an example
                     return true;                // Preference will change
              }

              return false;          // newValue rejected and preference not change
       }

}
@Override
public void onCreatePreferences(Bundle bundle, String s) {

    addPreferencesFromResource(R.xml.settings);

    PreferenceScreen prefScreen = getPreferenceScreen();
    Preference editTextPrefernce = prefScreen.findPreference(getString(R.string.pref_key));

    if (editTextPrefernce != null && editTextPrefernce instanceof EditTextPreference) {
        editTextPrefernce.setOnPreferenceChangeListener(this);
    }
}
Related Posts Related QUESTIONS :
  • How do I resolve this diamond problem in Kotlin?
  • How to update the custom value in a view model object
  • How to stop refreshing/re-creating activity when updating Firebase data using setValue?
  • Manifest Permissions vs App Setttings Permissions
  • NumberFormatException in Try-Catch while converting String to Int
  • How to pass observable fields through include tag?
  • Next button on Android keyboard not submitting form
  • Does Firestore support automatic functions for updates?
  • Why app crashing because of a missing dimension (for google maps) only when I apply changes?
  • How to make a grouping, ordering and sorting: Kotlin collection
  • how to use augmented reality to change the color of a qr code once it is detected using with arcore and ml kit firebase
  • how to resolve Firebase projects adding quota problem?
  • Why it Kotlin giving me the wrong Int value when converting from a String
  • How to add bottom navigation listeners to fragments
  • Why is scrollview hiding buttons behind other views?
  • Open Settings using Linking.sendIntent()
  • How do I access an unknown node in firebase while retrieving data?
  • Can't retrieve list of all users from firebase authetication
  • many activities with same content (adview, reward adview, functions...) how to avoid repeating code?
  • Popup doesn't hide when touch outside
  • proper way to handling images in Android? by using Firebase
  • Android: Button is faded gray when it should be transparent
  • Gradle DSL method not found: 'deleteAllActions()'
  • In Clean Architecture, must Repository is Android or Kotlin module?
  • Kotlin parse double from string
  • Inconsistency detected. Invalid view holder adapter positionViewHolder
  • In App Update API always returns 1 (UPDATE_NOT_AVAILABLE)
  • Event of ImageView when click
  • Basic precautions to prevent piracy?
  • What is the difference between 'center' and 'centerCrop' in the "ImageView"?
  • Unable to receive SIP calls when app is killed
  • Android emulator not processing http get request, but iOS simulator works fine
  • Android Problem with sharing temporary file
  • How to add Broadcast in Android?
  • How to hide 'Running in Chrome' toast on TWA application on Android devices?
  • How to Fix Error: Could not find method deleteAllActions() for arguments [] on task
  • Android Deep Linking not working with https
  • WARNING: API 'variant.getMergeResources()' is obsolete and has been replaced with 'variant.getMergeResourcesProvider()'
  • setSupportActionBar (androidx.appcompat.widget.Toolbar) in AppCompatActivity¬†cannot be applied
  • Manifest merger failed after adding Firebase core version 17.0.0
  • Marker from Firestore map data type
  • Cordova signed apk produces a blank screen after splash screen
  • How to clear EditText input from all item views of RecyclerView?
  • Android Studio Mac Fresh install - error after error after error
  • Android Q, programmatically connect to different WiFi AP for internet
  • Application is becoming very slow while retrieving data from server
  • How to get Accelerometer value in Android XAML App(Xamarin.Forms)
  • androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.TextView
  • Synchronized singleton in Kotlin
  • Product details from barcode
  • What is the difference between armeabi-v7a, arm64-v8a, x86?
  • Where did the "Profile or Debug APK..." option disappear to in Android Studio 3.4.1?
  • App crashes when trying to enter onDataChange Firebase Android
  • Combining WorkContinuation doesn't work as expected
  • Could not get unknown property 'config' for type com.google.gms.googleservices.GoogleServicesPlugin$GoogleServicesPlugin
  • Firebase Storage image upload successful through Android App but how to get url?
  • Xamarin cross platform - Couldn't connect debugger
  • How to customize Android Studio's import ordering in Kotlin to ignore whether they are "static" imports?
  • TV Remote Control Spatial Navigation
  • Google Admob always shows test adds on emulator even if the application is download and installed from the play store
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com