Back to project page Joetz-Android-V2.
The source code is released under:
GNU General Public License
If you think the Android project Joetz-Android-V2 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.example.jens.myapplication.camps; //from w ww.j a v a 2s . co m import android.app.AlertDialog; import android.app.Dialog; import android.app.Fragment; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.view.LayoutInflater; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.FrameLayout; import android.widget.TextView; import com.afollestad.materialdialogs.MaterialDialog; import com.example.jens.myapplication.R; import com.example.jens.myapplication.domain.PeriodCategory; import com.example.jens.myapplication.lib.RangeSeekBar; import java.text.NumberFormat; /** * Fragment for setting the filters to filter camps */ public class FilterFragment extends DialogFragment { public static final String RES_PRICE_MIN = "minprice"; public static final String RES_PRICE_MAX = "maxprice"; public static final String RES_AGE_MIN = "minage"; public static final String RES_AGE_MAX = "maxage"; public static final String RES_PERIOD_CATEGORY = "periodcat"; public static final int FRAGMENT_CODE = 111; private static final int priceDivisor = 5; private RangeSeekBar<Integer> mBarPrice; private RangeSeekBar<Integer> mBarAge; // private TextView mTxtMinPrice; // private TextView mTxtMaxPrice; private TextView mTxtMinMaxPrice; private int mMinPrice; private int mMaxPrice; private int mCurMinPrice; private int mCurMaxPrice; // private TextView mTxtMinAge; // private TextView mTxtMaxAge; private TextView mTxtMinMaxAge; private int mMinAge; private int mMaxAge; private int mCurMinAge; private int mCurMaxAge; private Button mBtnPeriod; private PeriodCategory mPeriodCategory; public FilterFragment() { } /** * Create a new instance of the FilterFragment * @param priceMin Minimum price * @param priceMax Maximum price * @param priceCurMin Current minimum price * @param priceCurMax Current maximum price * @param ageMin Minimum age * @param ageMax Maximum age * @param ageCurMin Current minimum age * @param ageCurMax Current maximum age * @param periodCategory Current period category * @return */ public static FilterFragment newInstance(int priceMin, int priceMax, int priceCurMin, int priceCurMax, int ageMin, int ageMax, int ageCurMin, int ageCurMax, PeriodCategory periodCategory) { FilterFragment f = new FilterFragment(); Bundle args = new Bundle(); args.putInt("priceMin", priceMin); args.putInt("priceMax", priceMax); args.putInt("priceCurMin", priceCurMin); args.putInt("priceCurMax", priceCurMax); args.putInt("ageMin", ageMin); args.putInt("ageMax", ageMax); args.putInt("ageCurMin", ageCurMin); args.putInt("ageCurMax", ageCurMax); args.putSerializable("period", periodCategory); f.setArguments(args); return f; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mMinPrice = getArguments().getInt("priceMin"); mMaxPrice = getArguments().getInt("priceMax"); mCurMinPrice = getArguments().getInt("priceCurMin"); mCurMaxPrice = getArguments().getInt("priceCurMax"); mMinAge = getArguments().getInt("ageMin"); mMaxAge = getArguments().getInt("ageMax"); mCurMinAge = getArguments().getInt("ageCurMin"); mCurMaxAge = getArguments().getInt("ageCurMax"); mPeriodCategory = (PeriodCategory) getArguments().getSerializable("period"); } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { LayoutInflater inflater = getActivity().getLayoutInflater(); View v = inflater.inflate(R.layout.fragment_filter, null); mTxtMinMaxPrice = (TextView) v.findViewById(R.id.lblMinMaxPrice); mTxtMinMaxAge = (TextView) v.findViewById(R.id.lblMinMaxAge); int x = mMinPrice / priceDivisor; mBarPrice = new RangeSeekBar<Integer>(mMinPrice / priceDivisor, mMaxPrice / priceDivisor + 1, getActivity()); mBarPrice.setOnRangeSeekBarChangeListener(new RangeSeekBar.OnRangeSeekBarChangeListener<Integer>() { @Override public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, Integer minValue, Integer maxValue) { // handle changed range values setPriceFields(minValue, maxValue); } }); mBarPrice.setSelectedMinValue(mCurMinPrice / priceDivisor); mBarPrice.setSelectedMaxValue(mCurMaxPrice / priceDivisor); // add RangeSeekBar to pre-defined layout FrameLayout.LayoutParams paramsPrice = new FrameLayout.LayoutParams( FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT); FrameLayout framePriceBar = (FrameLayout) v.findViewById(R.id.framePriceBar); framePriceBar.removeAllViews(); framePriceBar.addView(mBarPrice, paramsPrice); setPriceFields(mBarPrice.getSelectedMinValue(), mBarPrice.getSelectedMaxValue()); mBarAge = new RangeSeekBar<Integer>(mMinAge, mMaxAge, getActivity()); mBarAge.setOnRangeSeekBarChangeListener(new RangeSeekBar.OnRangeSeekBarChangeListener<Integer>() { @Override public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, Integer minValue, Integer maxValue) { setAgeFields(minValue, maxValue); } }); mBarAge.setSelectedMinValue(mCurMinAge); mBarAge.setSelectedMaxValue(mCurMaxAge); FrameLayout.LayoutParams paramsAge = new FrameLayout.LayoutParams( FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT); FrameLayout frameAgeBar = (FrameLayout) v.findViewById(R.id.frameAgeBar); frameAgeBar.removeAllViews(); frameAgeBar.addView(mBarAge, paramsAge); mBtnPeriod = (Button) v.findViewById(R.id.btnPeriod); mBtnPeriod.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { choosePeriod(); } }); refreshPeriodText(); setAgeFields(mBarAge.getSelectedMinValue(), mBarAge.getSelectedMaxValue()); MaterialDialog.Builder dialog = new MaterialDialog.Builder(getActivity()) .customView(v) .title(R.string.filter_camps) .positiveText(R.string.to_filter) .positiveColor(getResources().getColor(R.color.color_action_bar)) .callback(new MaterialDialog.SimpleCallback() { @Override public void onPositive(MaterialDialog dialog) { sendResult(FRAGMENT_CODE); } }); // BUILDING DIALOG /* AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder .setView(v) .setTitle("Filter kampen") .setPositiveButton("Filter", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { sendResult(FRAGMENT_CODE); } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { FilterFragment.this.getDialog().cancel(); } });*/ return dialog.build(); } private void setPriceFields(int minValue, int maxValue) { NumberFormat formatter = NumberFormat.getCurrencyInstance(); mTxtMinMaxPrice.setText( formatter.format(minValue * priceDivisor) + " " + getString(R.string.until) + " " + formatter.format(maxValue * priceDivisor)); } private void setAgeFields(int minValue, int maxValue) { mTxtMinMaxAge.setText(String.valueOf(minValue) + " - " + String.valueOf(maxValue) + " " + getString(R.string.year)); } private void refreshPeriodText(){ int text; if(mPeriodCategory == null){ text = R.string.dont_filter; } else{ text = mPeriodCategory.getNameResourceId(); } mBtnPeriod.setText(text); } /** * Popup to choose the period to filter on */ private void choosePeriod(){ ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.select_dialog_item); adapter.add(getString(R.string.dont_filter)); for(PeriodCategory cat : PeriodCategory.values()){ if(cat != PeriodCategory.OTHER){ adapter.add(getString(cat.getNameResourceId())); } } AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.filter_period_on); builder.setNegativeButton(R.string.cancel, null); builder.setAdapter(adapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if(which == 0){ mPeriodCategory = null; }else{ mPeriodCategory = PeriodCategory.values()[which]; } refreshPeriodText(); } }); builder.create().show(); } private void sendResult(int code) { Intent data = new Intent(); data.putExtra(RES_PRICE_MIN, mBarPrice.getSelectedMinValue() * priceDivisor); data.putExtra(RES_PRICE_MAX, mBarPrice.getSelectedMaxValue() * priceDivisor); data.putExtra(RES_AGE_MIN, mBarAge.getSelectedMinValue()); data.putExtra(RES_AGE_MAX, mBarAge.getSelectedMaxValue()); data.putExtra(RES_PERIOD_CATEGORY, mPeriodCategory); getTargetFragment().onActivityResult(getTargetRequestCode(), code, data); } }