Android Open Source - android-gskbyte-utils Picker Dialog Fragment






From Project

Back to project page android-gskbyte-utils.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project android-gskbyte-utils listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.gskbyte.dialog;
/*from   ww w.j a v a  2  s . c  om*/
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.gskbyte.R;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class PickerDialogFragment
extends DialogFragment
implements DialogInterface.OnMultiChoiceClickListener, OnCheckedChangeListener
{

public interface ActionListener
{
    public void onPickerDialogFragmentCompleted(PickerDialogFragment dialog, List<String> options, Set<Integer> selectedIndices);
    public void onPickerDialogFragmentCanceled(PickerDialogFragment dialog);
}

protected static final String KEY_TITLE = "title";
protected static final String KEY_TITLE_RES = "titleRes";
protected static final String KEY_ICON_RES = "iconRes";
protected static final String KEY_OPTIONS = "options";
protected static final String KEY_SELECTED_INDICES = "selected";
protected static final String KEY_ALL_ELEMENTS_TITLE = "allElements";

protected String title, allElements;
protected int iconRes;
protected ArrayList<String> options;
protected Set<Integer> selectedIndices;
protected boolean allOptionsSelected;

protected PickerTitle dialogTitleView;
protected AlertDialog dialog;

private ActionListener actionListener = null;

public static PickerDialogFragment newInstance(String title, int iconRes, List<String> options, Set<Integer> selectedIndices)
{
    PickerDialogFragment f = new PickerDialogFragment();
    
    Bundle args = new Bundle();
    args.putString(KEY_TITLE, title);
    args.putInt(KEY_ICON_RES, iconRes);
    args.putStringArrayList(KEY_OPTIONS, new ArrayList<String>(options));
    args.putIntegerArrayList(KEY_SELECTED_INDICES, new ArrayList<Integer>(selectedIndices));
    f.setArguments(args);
    
    return f;
}

public void setAllElementsTitle(String s)
{ this.allElements = s; }

public boolean areAllOptionsSelected()
{ return allOptionsSelected; }

public void setActionListener(ActionListener listener)
{
    actionListener = listener;
}

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    
    // could this be done on onCreateDialog()?
    Bundle args = getArguments();
    iconRes = args.getInt(KEY_ICON_RES, 0);
    
    int titleRes = args.getInt(KEY_TITLE_RES);
    if(titleRes != 0) {
        title = getString(titleRes);
    } else {
        title = args.getString(KEY_TITLE);
    }
    
    if(allElements == null) {
        allElements = getString(R.string.all_elements);
    }
    
    options = args.getStringArrayList(KEY_OPTIONS);
    selectedIndices = new TreeSet<Integer>( args.getIntegerArrayList(KEY_SELECTED_INDICES) );
}

public Dialog onCreateDialog(Bundle savedInstanceState)
{
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
    
    boolean [] selectedOptions = new boolean[ options.size() ];
    for(int i : selectedIndices) {
        selectedOptions[i] = true;
    }
    alertDialogBuilder.setMultiChoiceItems(options.toArray(new String[options.size()]), selectedOptions, this);
    
    dialogTitleView = new PickerTitle(getActivity(), null);
    dialogTitleView.setIconResource(iconRes);
    dialogTitleView.setTitle( title );
    dialogTitleView.setAllElementsTitle( allElements );
    dialogTitleView.allElements.setOnCheckedChangeListener(this);
    
    alertDialogBuilder.setCustomTitle(dialogTitleView);
    
    alertDialogBuilder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which)
        {
            onOkButtonPressed();
        }
    });
    
    alertDialogBuilder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            onCancelButtonPressed();
        }
    });
    
    dialog = alertDialogBuilder.create();
    updateCheckStatus();
    return dialog;
}

protected void onOkButtonPressed()
{
    if(actionListener != null)
        actionListener.onPickerDialogFragmentCompleted(this, options, selectedIndices);
}

protected void onCancelButtonPressed()
{
    dialog.dismiss();
    if(actionListener != null)
        actionListener.onPickerDialogFragmentCanceled(this);
}

protected void updateCheckStatus()
{    
    this.allOptionsSelected = options.size()==selectedIndices.size();
    
    dialogTitleView.allElements.setOnCheckedChangeListener(null);
    dialogTitleView.setChecked(this.allOptionsSelected);
    ListView listView = dialog.getListView();
    for(int i=0; i<options.size(); ++i) {
        listView.setItemChecked(i, selectedIndices.contains(i));
    }
    dialogTitleView.allElements.setOnCheckedChangeListener(this);
}

public void onClick(DialogInterface dialog, int which, boolean isChecked)
{
    if(isChecked) {
        selectedIndices.add(which);
    } else {
        selectedIndices.remove(which);
    }
    updateCheckStatus();
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
    if(isChecked) {
        for(int i=0; i<options.size(); ++i) {
            selectedIndices.add(i);
        }
    } else {
        selectedIndices.clear();
    }
    
    updateCheckStatus();
}

@SuppressWarnings("unused")
private static class PickerTitle
extends RelativeLayout
{
    ImageView icon;
    TextView title;
    CheckBox allElements;
    
    public PickerTitle(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.view_data_picker_title, this);
        icon = (ImageView) findViewById(R.id.icon);
        title = (TextView) findViewById(R.id.title);
        allElements = (CheckBox) findViewById(R.id.all_elements);
        
        setIconResource(0);
    }
    
    public void setTitle(CharSequence text)
    { title.setText(text); }
    
    public void setAllElementsTitle(CharSequence text)
    { allElements.setText(text); }
    
    public void setChecked(boolean checked)
    { allElements.setChecked(checked); }
    
    public boolean isChecked(boolean checked)
    { return allElements.isChecked(); }
    
    public void setIconResource(int resId)
    {
        if(resId != 0) {
            icon.setVisibility(VISIBLE); icon.setImageResource(resId);
        } else {
            icon.setVisibility(GONE); 
        }
    }
    
}

}




Java Source Code List

com.woozzu.android.widget.IndexScroller.java
com.woozzu.android.widget.IndexableListView.java
org.gskbyte.FragmentWrapperActivity.java
org.gskbyte.animation.ExpandAnimation.java
org.gskbyte.bitmap.AbstractBitmapManager.java
org.gskbyte.bitmap.BitmapColorizer.java
org.gskbyte.bitmap.BitmapManager.java
org.gskbyte.bitmap.CachedBitmapColorizer.java
org.gskbyte.bitmap.IndexedBitmaps.java
org.gskbyte.bitmap.LRUBitmapCache.java
org.gskbyte.bitmap.LRUBitmapManager.java
org.gskbyte.bitmap.PrivateBitmapManager.java
org.gskbyte.bitmap.ReferencedBitmaps.java
org.gskbyte.collection.ArrayHashMap.java
org.gskbyte.collection.DoubleSparseArray.java
org.gskbyte.collection.ListHashMap.java
org.gskbyte.dialog.DownloadDialogFragment.java
org.gskbyte.dialog.LoadDialogFragment.java
org.gskbyte.dialog.OpenLinkDialogBuilder.java
org.gskbyte.dialog.PickerDialogFragment.java
org.gskbyte.download.DiskDownload.java
org.gskbyte.download.DownloadManager.java
org.gskbyte.download.Download.java
org.gskbyte.download.MemoryDownload.java
org.gskbyte.drawable.AutoBackgroundButtonDrawable.java
org.gskbyte.listener.IListenable.java
org.gskbyte.listener.ListenableNG.java
org.gskbyte.listener.Listenable.java
org.gskbyte.preferences.DialogSeekBarPreference.java
org.gskbyte.preferences.InlineSeekBarPreference.java
org.gskbyte.remote.AsyncURLRequest.java
org.gskbyte.remote.URLRequest.java
org.gskbyte.tasks.QueuedTaskExecutor.java
org.gskbyte.tasks.TaskStep.java
org.gskbyte.tasks.Task.java
org.gskbyte.ui.ArrayAdapterWithDefaultValue.java
org.gskbyte.ui.ListAdapter.java
org.gskbyte.ui.ColorDialog.ColorDialog.java
org.gskbyte.ui.ColorDialog.ColorPreference.java
org.gskbyte.ui.iconifiedMainMenuList.EntryView.java
org.gskbyte.ui.iconifiedMainMenuList.MainMenuAdapter.java
org.gskbyte.ui.iconifiedMainMenuList.MenuEntry.java
org.gskbyte.util.FrequentIntents.java
org.gskbyte.util.IOUtils.java
org.gskbyte.util.Logger.java
org.gskbyte.util.OpenFileHandlerFactory.java
org.gskbyte.util.OpenFileHandler.java
org.gskbyte.util.XmlUtils.java
org.gskbyte.view.AsyncImageView.java
org.gskbyte.view.AutoBackgroundButton.java
org.gskbyte.view.AutoBackgroundImageButton.java
org.gskbyte.view.AutoHeightImageView.java
org.gskbyte.view.ExpandedGridView.java
org.gskbyte.view.ExpandedListView.java
org.gskbyte.view.FontUtil.java
org.gskbyte.view.FontableButton.java
org.gskbyte.view.FontableCheckBox.java
org.gskbyte.view.FontableEditText.java
org.gskbyte.view.FontableTextView.java
org.gskbyte.view.FullWidthImageView.java
org.gskbyte.view.ProportionalHeightLayout.java
org.gskbyte.view.PullToRefreshListView.java
org.gskbyte.view.SquaredLayout.java
org.gskbyte.view.StepSeekBar.java
org.gskbyte.view.TextViewUtil.java
org.gskbyte.view.ViewUtils.java