Skip to content

Commit

Permalink
Merge pull request #10 from Pankaj-Meharchandani/wheel
Browse files Browse the repository at this point in the history
Wheel
  • Loading branch information
Pankaj-Meharchandani authored Mar 27, 2024
2 parents 21b9ea8 + 6f91556 commit 8b572e9
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 100 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies {
implementation("io.github.eltos:simpledialogfragments:3.8.3")
implementation("com.google.android.material:material")
implementation("androidx.palette:palette:1.0.0")
implementation("com.github.yukuku:ambilwarna:2.0.1")
// App Center dependencies
val appCenterSdkVersion = "5.0.4"
implementation("com.microsoft.appcenter:appcenter-analytics:$appCenterSdkVersion")
Expand Down
139 changes: 81 additions & 58 deletions app/src/main/java/com/example/waller/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.example.waller;

import android.Manifest;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.WallpaperManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
Expand All @@ -21,39 +25,40 @@
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.Toast;
import android.app.AlertDialog;
import android.app.WallpaperManager;
import android.content.DialogInterface;
import com.google.android.material.color.DynamicColors;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.app.AlertDialog;
import android.content.DialogInterface;
import java.io.IOException;
import java.util.Random;

import android.app.WallpaperManager;
import android.Manifest;
//appcenter
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.color.DynamicColors;
import com.microsoft.appcenter.AppCenter;
import com.microsoft.appcenter.analytics.Analytics;
import com.microsoft.appcenter.crashes.Crashes;

import java.io.IOException;
import java.util.Random;

import eltos.simpledialogfragment.SimpleDialog.OnDialogResultListener;
import eltos.simpledialogfragment.color.SimpleColorDialog;


public class MainActivity extends AppCompatActivity {
private GridView gridView;
public class MainActivity extends AppCompatActivity implements OnDialogResultListener {
private GridView gridView;
private Bitmap selectedImage;

// Initialize color variables to 0 initially
private int selectedColor1 = 0;
private int selectedColor2 = 0;
private int selectedColor3 = 0;
private int selectedColor4 = 0;
private final String TAG_PRIMARY_COLOR = "tagPrimary";
private final String TAG_SECONDARY_COLOR = "tagSecondary";

private EditText editPrimaryColor;
private EditText editSecondaryColor;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -246,20 +251,35 @@ private void showColorPickerDialog() {
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.color_picker_dialog);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setOnDismissListener(dialog1 -> {
editPrimaryColor = null;
editSecondaryColor = null;
});

final EditText editTextColor2 = dialog.findViewById(R.id.editTextColor2);
final EditText editTextColor3 = dialog.findViewById(R.id.editTextColor3);
editPrimaryColor = dialog.findViewById(R.id.editColor2);
editSecondaryColor = dialog.findViewById(R.id.editColor3);

ImageButton colorPaletteButton = dialog.findViewById(R.id.colorPaletteButton);
colorPaletteButton.setOnClickListener(v -> {
// Call the method to show the color wheel dialog
showColorWheelDialog(TAG_PRIMARY_COLOR);
});

ImageButton colorPaletteButton2 = dialog.findViewById(R.id.colorPaletteButton2);
colorPaletteButton2.setOnClickListener(v -> {
// Call the method to show the color wheel dialog
showColorWheelDialog(TAG_SECONDARY_COLOR);
});

// Update EditText fields with previously selected colors if available
if (selectedColor2 != 0) {
editTextColor2.setText(ColorUtils.colorToHexString(selectedColor2).substring(1)); // Remove '#'
editPrimaryColor.setText(ColorUtils.colorToHexString(selectedColor2).substring(1)); // Remove '#'
}
if (selectedColor3 != 0) {
editTextColor3.setText(ColorUtils.colorToHexString(selectedColor3).substring(1)); // Remove '#'
editSecondaryColor.setText(ColorUtils.colorToHexString(selectedColor3).substring(1)); // Remove '#'
}

// Add a TextWatcher to the first EditText
editTextColor2.addTextChangedListener(new TextWatcher() {
editPrimaryColor.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
Expand All @@ -271,7 +291,7 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
@Override
public void afterTextChanged(Editable s) {
if (s.length() == 6) { // Check if 6 digits have been entered
editTextColor3.requestFocus(); // Move focus to the next EditText
editSecondaryColor.requestFocus(); // Move focus to the next EditText
}
}
});
Expand All @@ -283,9 +303,9 @@ public void afterTextChanged(Editable s) {
@Override
public void onClick(View v) {
// Validate and save selected colors from hex codes
if (validateAndSaveColors(editTextColor2, editTextColor3)) {
selectedColor2 = ColorUtils.hexStringToColor("#" + editTextColor2.getText().toString());
selectedColor3 = ColorUtils.hexStringToColor("#" + editTextColor3.getText().toString());
if (validateAndSaveColors()) {
selectedColor2 = ColorUtils.hexStringToColor("#" + editPrimaryColor.getText().toString());
selectedColor3 = ColorUtils.hexStringToColor("#" + editSecondaryColor.getText().toString());
// Regenerate images with the selected colors
regenerateImages(selectedColor2, selectedColor3);
dialog.dismiss();
Expand Down Expand Up @@ -317,30 +337,26 @@ public void onClick(View v) {

dialog.show();
}
// private void clearTextFields() {
// EditText editTextColor2 = findViewById(R.id.editTextColor2);
// EditText editTextColor3 = findViewById(R.id.editTextColor3);
// editTextColor2.setText("");
// editTextColor3.setText("");
// }

private boolean validateAndSaveColors(EditText editText2, EditText editText3) {
if ( validateAndSaveColor(editText2, 1) &&
validateAndSaveColor(editText3, 2)) {
return true;
}

else {
//Toast.makeText(MainActivity.this, "Invalid color code(s)", Toast.LENGTH_SHORT).show();
return false;
}
private void showColorWheelDialog(final String tag) {
SimpleColorDialog.build()
.title("Pick a Color")
.colorPreset(Color.WHITE)
.allowCustom(true)
.show(this, tag);
}

private boolean validateAndSaveColors() {
//Toast.makeText(MainActivity.this, "Invalid color code(s)", Toast.LENGTH_SHORT).show();
return validateAndSaveColor(editPrimaryColor, 1) &&
validateAndSaveColor(editSecondaryColor, 2);
}

private boolean validateAndSaveColor(EditText editText, int colorNumber) {
String hexCode = editText.getText().toString();
if (!TextUtils.isEmpty(hexCode)) {
try {
int color = ColorUtils.hexStringToColor("#" + hexCode);
int color = Color.parseColor("#" + hexCode);
switch (colorNumber) {
case 2:
selectedColor2 = color;
Expand All @@ -359,24 +375,9 @@ private boolean validateAndSaveColor(EditText editText, int colorNumber) {
// Empty color code
Toast.makeText(MainActivity.this, "Empty color code for Color " + colorNumber, Toast.LENGTH_SHORT).show();
return false;
// try {
// int color = ColorUtils.hexStringToColor("#" + hexCode);
// switch (colorNumber) {
// case 2:
// selectedColor2 = color;
// break;
// case 3:
// selectedColor3 = color;
// break;
// }
// return true;
// } catch (IllegalArgumentException e) {
// // Invalid color code
// Toast.makeText(MainActivity.this, "Invalid color code for Color " + colorNumber+"\n Usng Default color", Toast.LENGTH_SHORT).show();
// editText.setText("BABABA");
// return true;
}
}

private void setWallpaper(Bitmap bitmap, int flags) {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(MainActivity.this);
try {
Expand Down Expand Up @@ -437,6 +438,28 @@ private Bitmap generateRandomImage(int color2, GradientType gradientType) {
return selectedImage;
}

@Override
public boolean onResult(@NonNull String dialogTag, int which, @NonNull Bundle extras) {
if (which == BUTTON_POSITIVE) {
int selectedColor = extras.getInt(SimpleColorDialog.COLOR);

// Get the hex code from the selected color
String hexCode = String.format("%06X", (0xFFFFFF & selectedColor));

if (dialogTag.equals(TAG_PRIMARY_COLOR) && editPrimaryColor != null) {
editPrimaryColor.setText(hexCode);
} else if (dialogTag.equals(TAG_SECONDARY_COLOR) && editSecondaryColor != null) {
editSecondaryColor.setText(hexCode);
} else {
return false;
}

return true;
}
return false;
}


// Enumeration for gradient types
private enum GradientType {
LINEAR, ANGULAR, BILINEAR, DIAGONAL
Expand Down
14 changes: 2 additions & 12 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,13 @@
<!-- GridView -->
<GridView
android:id="@+id/gridView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/topButtonsLayout"
android:horizontalSpacing="7dp"
android:numColumns="2"
android:verticalSpacing="5dp"
android:layout_marginTop="16dp" />

<!-- &lt;!&ndash; Floating Action Button (FAB) &ndash;&gt;-->
<!-- <com.google.android.material.floatingactionbutton.FloatingActionButton-->
<!-- android:id="@+id/fabChangeLayout"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_alignParentStart="true"-->
<!-- android:layout_alignParentBottom="true"-->
<!-- android:layout_marginEnd="16dp"-->
<!-- android:layout_marginBottom="16dp"-->
<!-- android:src="@drawable/ic_palette_color" />-->

</RelativeLayout>
54 changes: 24 additions & 30 deletions app/src/main/res/layout/color_picker_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,9 @@
android:padding="16dp"
android:background="@drawable/background_border2">

<!-- <EditText-->
<!-- android:id="@+id/editTextColor1"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:inputType="textCapCharacters"-->
<!-- android:hint="Color 1 (hex)"/>-->

<EditText
android:id="@+id/editTextColor2"
android:id="@+id/editColor2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapCharacters"
Expand All @@ -24,32 +18,41 @@
android:textColor="@color/white"/>

<EditText
android:id="@+id/editTextColor3"
android:id="@+id/editColor3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapCharacters"
android:layout_below="@id/editTextColor2"
android:layout_below="@id/editColor2"
android:maxLength="6"
android:inputType="textCapCharacters"
android:layout_marginTop="16dp"
android:hint="Secondary Color (hex)"
android:textColorHint="#898989"
android:textColor="@color/white"/>
<ImageButton
android:id="@+id/colorPaletteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/editColor2"
android:layout_alignBottom="@id/editColor2"
android:layout_alignEnd="@id/editColor2"
android:src="@drawable/ic_palette_color"
android:background="?attr/selectableItemBackgroundBorderless" />

<!-- <EditText-->
<!-- android:id="@+id/editTextColor4"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:inputType="textCapCharacters"-->
<!-- android:layout_below="@id/editTextColor3"-->
<!-- android:layout_marginTop="16dp"-->
<!-- android:hint="Color 4 (hex)"/>-->

<ImageButton
android:id="@+id/colorPaletteButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/editColor3"
android:layout_alignBottom="@id/editColor3"
android:layout_alignEnd="@id/editColor3"
android:src="@drawable/ic_palette_color"
android:background="?attr/selectableItemBackgroundBorderless" />

<View
android:id="@+id/colorPreview"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@id/editTextColor3"/>
android:layout_below="@id/editColor3"/>

<Button
android:id="@+id/btnSaveColors"
Expand All @@ -59,16 +62,7 @@
android:layout_below="@id/colorPreview"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:background="@drawable/button_color"/>
<!-- <Button-->
<!-- android:id="@+id/btnClearColors"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_weight="1"-->
<!-- android:text="cls"-->
<!-- android:layout_marginTop="400dp"-->
<!-- android:textColor="@android:color/white"-->
<!-- android:background="@drawable/button_outline"/>-->
android:background="@drawable/button_color" />

<View
android:id="@+id/view2"
Expand Down

0 comments on commit 8b572e9

Please sign in to comment.