Ben Fossen
Ben Fossen

Reputation: 1007

Using android-color-picker in a fragment?

I am fairly new to android development and want to use the Android-Color_picker "AmbilWarna" inside a fragment. I am getting the error:

The constructor AmbilWarnaDialog(HomeFragment, int, new OnAmbilWarnaListener(){}) is undefined.

Is this because I am using a Fragment instead of a Fragment activity The tutorial I was using uses an Activity.

I am using the following tutorial: http://wptrafficanalyzer.in/blog/android-color-picker-application-using-ambilwarna-color-picker-library/

public class HomeFragment extends SherlockFragment implements TabListener {


private View homeView;

@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
        Bundle savedInstanceState) {

    homeView = inflater.inflate(R.layout.homefragment, container, false);

    Button sColorBtn = (Button) homeView.findViewById(R.id.button2);
    OnClickListener clickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            colorpicker();
        }
    };

    // Setting click event listener for the button
    sColorBtn.setOnClickListener(clickListener);
    return sColorBtn;
}

public void colorpicker() {
    //     initialColor is the initially-selected color to be shown in the rectangle on the left of the arrow.
    //     for example, 0xff000000 is black, 0xff0000ff is blue. Please be aware of the initial 0xff which is the alpha.

    AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, 0xff0000ff, new OnAmbilWarnaListener() {

        // Executes, when user click Cancel button
        @Override
        public void onCancel(AmbilWarnaDialog dialog){
        }

        // Executes, when user click OK button
        @Override
        public void onOk(AmbilWarnaDialog dialog, int color) {
            Toast.makeText(getBaseContext(), "Selected Color : " + color, Toast.LENGTH_LONG).show();
        }
    });
    dialog.show();
}

Upvotes: 1

Views: 2956

Answers (2)

lomza
lomza

Reputation: 9716

If you want a fragment solution for Color Picker, I have made a fork of android-color-picker where DialogFragment is used and is re-created on configuration change. Here's the link: https://github.com/lomza/android-color-picker

Upvotes: 0

Tamás Cseh
Tamás Cseh

Reputation: 3090

Use this:

AmbilWarnaDialog dialog = new AmbilWarnaDialog(getActivity().getApplicationContext(), 0xff0000ff, new OnAmbilWarnaListener() {

    // Executes, when user click Cancel button
    @Override
    public void onCancel(AmbilWarnaDialog dialog){
    }

    // Executes, when user click OK button
    @Override
    public void onOk(AmbilWarnaDialog dialog, int color) {
        Toast.makeText(getBaseContext(), "Selected Color : " + color, Toast.LENGTH_LONG).show();
    }
});

So you have to use getActivity().getApplicationContext() instead of this. It will return with the Context.

Upvotes: 2

Related Questions