Answered step by step
Verified Expert Solution
Question
1 Approved Answer
What's wrong with the codes? The colors are not correct when displayed??? public class ColorPickerDialogFragment extends DialogFragment { SeekBar red, green, blue; final int[] COLOR_VALUES
What's wrong with the codes? The colors are not correct when displayed??? public class ColorPickerDialogFragment extends DialogFragment { SeekBar red, green, blue; final int[] COLOR_VALUES = {Color.RED, Color.GREEN, Color.BLUE}; interface ColorDialogSelectionListener { void colorSelected(int red, int green, int blue); } private ColorDialogSelectionListener mListener; public static ColorPickerDialogFragment newInstance() { ColorPickerDialogFragment fragment = new ColorPickerDialogFragment(); return fragment; } @Override // Override onAttach to configure listener public void onAttach(Activity activity) { super.onAttach(activity); if (activity instanceof ColorDialogSelectionListener) { mListener = (ColorDialogSelectionListener) activity; } else { throw new RuntimeException(activity.toString() + " must implement ColorDialogSelectionListener"); } } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); LayoutInflater inflater = getActivity().getLayoutInflater(); View view = inflater.inflate(R.layout.color_picker_seekbar, null); red = view.findViewById(R.id.seekbar_red); green = view.findViewById(R.id.seekbar_green); blue = view.findViewById(R.id.seekbar_blue); red.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean b) { COLOR_VALUES [0] = progress; } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); green.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean b) { COLOR_VALUES [1] = progress; } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); blue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean b) { COLOR_VALUES [2] = progress; } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); builder.setTitle("Choose a background color") .setView(view) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { mListener.colorSelected(COLOR_VALUES[0], COLOR_VALUES[1], COLOR_VALUES[2]); } }); return builder.create(); } }
________________________________________________________________________
MAIN
public class ColorDialogSeekbar extends AppCompatActivity implements ColorPickerDialogFragment.ColorDialogSelectionListener { Button mChooseBackgroundColor; RelativeLayout mBackground; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_color_dialog_seekbar); mBackground = findViewById(R.id.background); mChooseBackgroundColor = findViewById(R.id.show_color_dialog_button); mChooseBackgroundColor.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Show dialog ColorPickerDialogFragment dialog = ColorPickerDialogFragment.newInstance(); dialog.show(getSupportFragmentManager(), "Color Dialog"); } }); } @Override public void colorSelected(int red, int green, int blue) { mBackground.setBackgroundColor(Color.rgb(red, green, blue)); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started