Skip to content

Commit

Permalink
Added Java Examples
Browse files Browse the repository at this point in the history
Added examples for Java

#8
  • Loading branch information
Dhaval2404 committed Nov 20, 2020
1 parent 3b92c13 commit cbf1c37
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.github.dhaval2404.colorpicker.sample;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatButton;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.github.dhaval2404.colorpicker.ColorPickerDialog;
import com.github.dhaval2404.colorpicker.ColorPickerView;
import com.github.dhaval2404.colorpicker.listener.ColorListener;
import com.github.dhaval2404.colorpicker.model.ColorShape;
import com.github.dhaval2404.colorpicker.util.ColorUtil;
import com.github.dhaval2404.colorpicker.util.SharedPref;

import org.jetbrains.annotations.NotNull;

public class ColorPickerFragmentJava extends Fragment {

private int mColor = 0;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_color_picker, container, false);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

View rootView = getView();
final AppCompatButton colorPickerBtn = rootView.findViewById(R.id.colorPickerBtn);
final ColorPickerView colorPickerView = rootView.findViewById(R.id.colorPickerView);

int primaryColor = ContextCompat.getColor(requireContext(), R.color.colorPrimary);
mColor = new SharedPref(requireContext()).getRecentColor(primaryColor);
getView().findViewById(R.id.colorPickerBtn).setOnClickListener(view -> {
new ColorPickerDialog.Builder(requireActivity())
.setColorShape(ColorShape.SQAURE)
.setDefaultColor(mColor)
.setColorListener(new ColorListener() {
@Override
public void onColorSelected(int color, @NotNull String colorHex) {
mColor = color;
colorPickerView.setColor(color);
setButtonBackground(colorPickerBtn, color);
}
})
.show();
});
}

private void setButtonBackground(AppCompatButton btn, int color) {
if (ColorUtil.isDarkColor(color)) {
btn.setTextColor(Color.WHITE);
} else {
btn.setTextColor(Color.BLACK);
}
btn.setBackgroundTintList(ColorStateList.valueOf(color));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package com.github.dhaval2404.colorpicker.sample;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatButton;
import androidx.fragment.app.Fragment;

import com.github.dhaval2404.colorpicker.MaterialColorPickerDialog;
import com.github.dhaval2404.colorpicker.listener.ColorListener;
import com.github.dhaval2404.colorpicker.model.ColorShape;
import com.github.dhaval2404.colorpicker.model.ColorSwatch;
import com.github.dhaval2404.colorpicker.util.ColorUtil;

import org.jetbrains.annotations.NotNull;

public class MaterialColorPickerFragmentJava extends Fragment {

private String mMaterialColorSquare = "";
private String mMaterialColorCircle = "";
private String mMaterialColorBottomSheet = "";
private String mMaterialPreDefinedColor = "";

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_material_color_picker, container, false);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

View rootView = getView();
final AppCompatButton materialPreDefinedColorPickerBtn = rootView.findViewById(R.id.materialPreDefinedColorPickerBtn);
final AppCompatButton materialDialogPickerCircleBtn = rootView.findViewById(R.id.materialDialogPickerCircleBtn);
final AppCompatButton materialBottomSheetDialogBtn = rootView.findViewById(R.id.materialBottomSheetDialogBtn);
final AppCompatButton materialDialogPickerSquareBtn = rootView.findViewById(R.id.materialDialogPickerSquareBtn);

materialDialogPickerSquareBtn.setOnClickListener(view -> {
new MaterialColorPickerDialog
.Builder(requireActivity())
.setColorShape(ColorShape.SQAURE) // Or ColorShape.CIRCLE
.setColorSwatch(ColorSwatch._300) // Default ColorSwatch._500
.setDefaultColor(mMaterialColorSquare) // Pass Default Color
.setColorListener(new ColorListener() {
@Override
public void onColorSelected(int color, @NotNull String colorHex) {
mMaterialColorSquare = colorHex;
setButtonBackground(materialDialogPickerSquareBtn, color);
}
})
.show();
});

materialDialogPickerCircleBtn.setOnClickListener(view -> {
new MaterialColorPickerDialog
.Builder(requireActivity())
.setColorSwatch(ColorSwatch._500)
.setDefaultColor(mMaterialColorCircle)
.setColorListener(new ColorListener() {
@Override
public void onColorSelected(int color, @NotNull String colorHex) {
mMaterialColorCircle = colorHex;
setButtonBackground(materialDialogPickerCircleBtn, color);
}
})
.show();
});

materialBottomSheetDialogBtn.setOnClickListener(view -> {
new MaterialColorPickerDialog
.Builder(requireActivity())
.setColorSwatch(ColorSwatch._300)
.setDefaultColor(mMaterialColorBottomSheet)
.setColorListener(new ColorListener() {
@Override
public void onColorSelected(int color, @NotNull String colorHex) {
mMaterialColorBottomSheet = colorHex;
setButtonBackground(materialBottomSheetDialogBtn, color);
}
})
.showBottomSheet(getFragmentManager());
});

materialPreDefinedColorPickerBtn.setOnClickListener(view -> {
String[] colorArray = new String[]{"#f6e58d", "#ffbe76", "#ff7979", "#badc58", "#dff9fb",
"#7ed6df", "#e056fd", "#686de0", "#30336b", "#95afc0"};
int[] colorResArray = getResources().getIntArray(R.array.themeColors);
String[] colorHexArray = getResources().getStringArray(R.array.themeColorHex);

new MaterialColorPickerDialog
.Builder(requireActivity())
.setColorRes(colorResArray)
.setColors(colorHexArray)
.setColors(colorArray)
.setDefaultColor(mMaterialPreDefinedColor)
.setColorListener(new ColorListener() {
@Override
public void onColorSelected(int color, @NotNull String colorHex) {
mMaterialPreDefinedColor = colorHex;
setButtonBackground(materialPreDefinedColorPickerBtn, color);
}
})
.showBottomSheet(getChildFragmentManager());
});

}

private void setButtonBackground(AppCompatButton btn, int color) {
if (ColorUtil.isDarkColor(color)) {
btn.setTextColor(Color.WHITE);
} else {
btn.setTextColor(Color.BLACK);
}
btn.setBackgroundTintList(ColorStateList.valueOf(color));
}

}

0 comments on commit cbf1c37

Please sign in to comment.