Skip to content

Commit

Permalink
improved ui, added toggle button, dropdown to select wallpapers from(…
Browse files Browse the repository at this point in the history
…app/device) in p4
  • Loading branch information
Raghuvorkady committed May 29, 2021
1 parent 40cc075 commit a9b9829
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 110 deletions.
126 changes: 80 additions & 46 deletions p4-WallpaperApp/app/src/main/java/csmp/part_a/p4/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package csmp.part_a.p4;

import android.annotation.SuppressLint;
import android.app.WallpaperManager;
import android.content.Intent;
import android.graphics.Bitmap;
Expand All @@ -9,11 +8,15 @@
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import androidx.appcompat.app.AppCompatActivity;

Expand All @@ -24,14 +27,17 @@
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public class MainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemSelectedListener, CompoundButton.OnCheckedChangeListener {

static final int REQUEST_IMAGE_GET = 1;
private ImageView imageView;
private TextView selectedImagesTextBox;
private static final int REQUEST_IMAGE_GET = 1;
private final String[] dropDownOptions = {"App", "Device"};
private final int[] appWallpapers = {R.drawable.wallpaper1, R.drawable.wallpaper2, R.drawable.wallpaper3, R.drawable.wallpaper4};

private TextView selectedImagesTextBox, selectedWallpaperText;
private Button addImageButton;
private EditText timeIntervalText;

private List<Uri> images; // to store selected images
private List<Uri> addedImages; // to store selected images
private Handler handler;
private Runnable runnable;

Expand All @@ -40,46 +46,28 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

imageView = findViewById(R.id.imageView);
selectedImagesTextBox = findViewById(R.id.imagesListTextView);
Button changeWallpaperButton = findViewById(R.id.changeButton);
Button addImageButton = findViewById(R.id.addImage);
ToggleButton changeWallpaperButton = findViewById(R.id.changeButton);
addImageButton = findViewById(R.id.addImage);
timeIntervalText = findViewById(R.id.editTextTime);
Button stopButton = findViewById(R.id.stopButton);

addImageButton.setOnClickListener(this);
changeWallpaperButton.setOnClickListener(this);
stopButton.setOnClickListener(this);
selectedWallpaperText = findViewById(R.id.selectedWallpaperText);

images = new ArrayList<>();
addedImages = new ArrayList<>();
handler = new Handler();

Spinner spinner = findViewById(R.id.planets_spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, dropDownOptions);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);

addImageButton.setOnClickListener(this);
changeWallpaperButton.setOnCheckedChangeListener(this);
}

@SuppressLint("NonConstantResourceId")
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.addImage:
selectImage();
break;
case R.id.changeButton:
if (images.size() != 0) {
String time = timeIntervalText.getText().toString();
int timeDelayInSeconds;

if (time.isEmpty())
makeToast("Please enter the time interval...");
else {
timeDelayInSeconds = Integer.parseInt(time);
scheduleWallpaperChange(timeDelayInSeconds);
}
} else makeToast("Select at least one image for wallpaper");
break;
case R.id.stopButton:
makeToast("Stopped");
handler.removeCallbacks(runnable);
break;
}
selectImage();
}

public void selectImage() {
Expand All @@ -93,7 +81,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_GET && resultCode == RESULT_OK) {
Uri imageUri = data.getData();
images.add(imageUri);
addedImages.add(imageUri);

File file = new File(imageUri.getPath());
String text = file.getName() + "\n";
Expand All @@ -107,25 +95,28 @@ public void scheduleWallpaperChange(final int timeDelay) {
@Override
public void run() {
Random random = new Random();
int bound = images.size();
int bound = addImageButton.isEnabled() ? addedImages.size() : appWallpapers.length;
int randomInt = random.nextInt(bound);
makeToast("Selected wallpaper no: " + randomInt);
changeWallpaperFun(randomInt);
handler.postDelayed(this, 1000 * timeDelay);
}
};
handler.post(runnable);
//handler.removeCallbacks(runnable);
}

private void changeWallpaperFun(int imageIndex) {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);

try {
Uri uri = images.get(imageIndex);
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
imageView.setImageBitmap(bitmap);
Bitmap bitmap;
if (addImageButton.isEnabled()) // addImageButton is used to indicate type of option selected from the drop down
{
Uri uri = addedImages.get(imageIndex);
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
} else bitmap = BitmapFactory.decodeResource(getResources(), appWallpapers[imageIndex]);
wallpaperManager.setBitmap(bitmap);
selectedWallpaperText.setVisibility(View.VISIBLE);
selectedWallpaperText.setText("Randomly selected wallpaper no : " + (imageIndex + 1));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
Expand All @@ -136,4 +127,47 @@ private void changeWallpaperFun(int imageIndex) {
private void makeToast(String toastMessage) {
Toast.makeText(getApplicationContext(), toastMessage, Toast.LENGTH_SHORT).show();
}

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
selectedImagesTextBox.setText("");
if (i == 0) { // if App is selected
addImageButton.setEnabled(false);
for (int id : appWallpapers)
selectedImagesTextBox.append(getString(id).split("/")[2] + "\n");
} else {
addImageButton.setEnabled(true);
selectedImagesTextBox.setText("");
addedImages.clear();
}
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}

@Override
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
if (isChecked) {
if (addImageButton.isEnabled()) {
if (addedImages.size() == 0) {
makeToast("Select at least one image for wallpaper");
button.setChecked(false); // to avoid change of button state
return;
}
}
String time = timeIntervalText.getText().toString();

if (time.isEmpty()) {
makeToast("Please enter the time interval...");
button.setChecked(false); // to avoid change of button state
return;
}

scheduleWallpaperChange(Integer.parseInt(time));
} else {
makeToast("Stopped");
handler.removeCallbacks(runnable);
}
}
}
124 changes: 61 additions & 63 deletions p4-WallpaperApp/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,74 +16,49 @@
android:padding="10dp"
android:text="@string/changing_wallpaper_application"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />


<LinearLayout
android:id="@+id/spinner_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="72dp"
android:layout_marginTop="30dp"
android:gravity="center_horizontal"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">

<Button
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/textView">

android:id="@+id/stopButton"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:text="@string/stop"
android:textAllCaps="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="ButtonStyle" />
android:text="@string/select_wallpapers_from"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
android:textSize="16sp" />

<Button
android:id="@+id/changeButton"
<Spinner
android:id="@+id/planets_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:text="@string/changeBtnText"
android:textAllCaps="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="ButtonStyle" />

android:layout_height="wrap_content" />

</LinearLayout>

<Button
android:id="@+id/addImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="148dp"
android:text="@string/add_images"
android:textAllCaps="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.95"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="250dp"
android:layout_height="257dp"
android:layout_marginTop="30dp"
android:layout_width="220dp"
android:layout_height="200dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="50dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.15"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.235">
app:layout_constraintTop_toBottomOf="@+id/spinner_layout">

<TextView
android:id="@+id/textView2"
Expand All @@ -93,48 +68,71 @@
android:gravity="center"
android:padding="5dp"
android:text="@string/selected_images"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textColor="@android:color/black"
tools:layout_editor_absoluteX="152dp"
tools:layout_editor_absoluteY="221dp" />

<TextView
android:id="@+id/imagesListTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="5dp"
android:background="@color/lightBrown"
android:gravity="center_horizontal"
android:hint="@string/no_image_selected"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
</LinearLayout>

<ImageView
android:id="@+id/imageView"
android:layout_width="88dp"
android:layout_height="97dp"
android:layout_marginTop="36dp"
android:contentDescription="@string/selected_images"
<Button
android:id="@+id/addImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginRight="24dp"
android:text="@string/add_images"
android:textAllCaps="false"
app:layout_constraintBottom_toBottomOf="@+id/linearLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/linearLayout"
app:layout_constraintTop_toBottomOf="@+id/addImage"
tools:srcCompat="@tools:sample/avatars" />
app:layout_constraintTop_toTopOf="@+id/linearLayout" />

<EditText
android:id="@+id/editTextTime"
android:layout_width="110dp"
android:layout_height="65dp"
android:layout_marginTop="39dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="center"
android:hint="@string/enter_time_interval_nfor_changing_nwallpaper_in_seconds"
android:importantForAutofill="no"
android:inputType="number"
android:padding="4dp"
android:layout_marginBottom="28dp"
android:textSize="12sp"
app:layout_constraintBottom_toTopOf="@+id/changeButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<ToggleButton
android:id="@+id/changeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:textAllCaps="false"
android:textOff="@string/changeBtnText"
android:textOn="@string/stop"
app:layout_constraintBottom_toTopOf="@id/selectedWallpaperText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/linearLayout"
app:layout_constraintTop_toBottomOf="@+id/textView" />
app:layout_constraintStart_toStartOf="parent"
tools:ignore="ButtonStyle" />

<TextView
android:id="@+id/selectedWallpaperText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
3 changes: 2 additions & 1 deletion p4-WallpaperApp/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<string name="add_images">Add images</string>
<string name="selected_images">Selected images:</string>
<string name="no_image_selected">No image selected</string>
<string name="enter_time_interval_nfor_changing_nwallpaper_in_seconds">enter time interval\nfor changing\nwallpaper in seconds:</string>
<string name="enter_time_interval_nfor_changing_nwallpaper_in_seconds">enter Time Interval\nfor changing Wallpaper:</string>
<string name="stop">Stop</string>
<string name="select_wallpapers_from">Select wallpapers from</string>
</resources>

0 comments on commit a9b9829

Please sign in to comment.