Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move ColorChooser to own ViewComponent
- Loading branch information
1 parent
1195cd6
commit 7df89b4
Showing
5 changed files
with
124 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
app/src/main/java/it/niedermann/nextcloud/deck/ui/helper/colorchooser/ColorChooser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package it.niedermann.nextcloud.deck.ui.helper.colorchooser; | ||
|
||
import android.content.Context; | ||
import android.content.res.TypedArray; | ||
import android.util.AttributeSet; | ||
import android.view.LayoutInflater; | ||
import android.widget.ImageView; | ||
import android.widget.LinearLayout; | ||
|
||
import androidx.annotation.Nullable; | ||
|
||
import com.google.android.flexbox.FlexboxLayout; | ||
|
||
import butterknife.BindView; | ||
import butterknife.ButterKnife; | ||
import it.niedermann.nextcloud.deck.R; | ||
import it.niedermann.nextcloud.deck.util.ViewUtil; | ||
|
||
public class ColorChooser extends LinearLayout { | ||
|
||
private Context context; | ||
|
||
@BindView(R.id.colorPicker) | ||
FlexboxLayout colorPicker; | ||
|
||
private String selectedColor; | ||
private String previouslySelectedColor; | ||
private ImageView previouslySelectedImageView; | ||
|
||
|
||
public ColorChooser(Context context, @Nullable AttributeSet attrs) { | ||
super(context, attrs); | ||
this.context = context; | ||
|
||
TypedArray a = context.obtainStyledAttributes(attrs, | ||
R.styleable.ColorChooser, 0, 0); | ||
String[] colors = getResources().getStringArray(a.getResourceId(R.styleable.ColorChooser_colors, 0)); | ||
String defaultColor = a.getString(R.styleable.ColorChooser_defaultColor); | ||
a.recycle(); | ||
|
||
LayoutInflater inflater = (LayoutInflater) context | ||
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | ||
inflater.inflate(R.layout.color_chooser, this, true); | ||
ButterKnife.bind(this); | ||
initDefaultColors(colors, defaultColor); | ||
} | ||
|
||
private void initDefaultColors(String[] colors, String defaultColor) { | ||
for (final String color : colors) { | ||
ImageView image = new ImageView(getContext()); | ||
image.setOnClickListener((imageView) -> { | ||
if (previouslySelectedImageView != null) { // null when first selection | ||
previouslySelectedImageView.setImageDrawable(ViewUtil.getTintedImageView(this.context, R.drawable.circle_grey600_36dp, previouslySelectedColor)); | ||
} | ||
image.setImageDrawable(ViewUtil.getTintedImageView(this.context, R.drawable.circle_alpha_check_36dp, color)); | ||
selectedColor = color; | ||
this.previouslySelectedColor = color; | ||
this.previouslySelectedImageView = image; | ||
}); | ||
if(color.toLowerCase().equals("#" + defaultColor.toLowerCase())) { | ||
this.selectedColor = color; | ||
this.previouslySelectedColor = color; | ||
this.previouslySelectedImageView = image; | ||
image.setImageDrawable(ViewUtil.getTintedImageView(this.context, R.drawable.circle_alpha_check_36dp, color)); | ||
} else { | ||
image.setImageDrawable(ViewUtil.getTintedImageView(this.context, R.drawable.circle_grey600_36dp, color)); | ||
} | ||
colorPicker.addView(image); | ||
} | ||
|
||
// Preselect when create a new board | ||
// if(colorPicker.getChildCount() > 0) { | ||
// colorPicker.getChildAt(0).performClick(); | ||
// } | ||
} | ||
|
||
public void selectColor(int index) { | ||
if(colorPicker.getChildCount() > 0) { | ||
colorPicker.getChildAt(0); | ||
} else { | ||
throw new IndexOutOfBoundsException("index not available"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:orientation="vertical" android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<com.google.android.flexbox.FlexboxLayout | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
android:id="@+id/colorPicker" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_marginTop="@dimen/standard_margin" | ||
app:flexWrap="wrap" | ||
app:alignItems="stretch" | ||
app:justifyContent="space_between" /> | ||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<declare-styleable name="ColorChooser"> | ||
<attr name="defaultColor" format="string" /> | ||
<attr name="colors" format="reference" /> | ||
</declare-styleable> | ||
</resources> |