Skip to content

Commit

Permalink
using stateListDrawable for the bit on/off drawable. Let us change th…
Browse files Browse the repository at this point in the history
…e bit with the view state
  • Loading branch information
neuracr committed Feb 21, 2019
1 parent 682d603 commit 9ba8f62
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 64 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 15 additions & 11 deletions app/src/main/java/com/example/binaryclock/SettingsActivity.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
package com.example.binaryclock;

import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.StateListDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.GridLayoutAnimationController;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class SettingsActivity extends AppCompatActivity {
private boolean state = true;
private int num = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);

Button sw = findViewById(R.id.button);
sw.setOnClickListener(new View.OnClickListener() {
TextView tv_number = findViewById(R.id.num);
tv_number.setText(""+num);

Button change = findViewById(R.id.change);
change.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageView bit = findViewById(R.id.d12);
if (state){
bit.setBackgroundDrawable(getDrawable(R.drawable.bit_off));
}
else{
bit.setBackgroundDrawable(getDrawable(R.drawable.bit_on));
}
state = !state;
View bit = findViewById(R.id.d00);
bit.setActivated(!bit.isActivated());
}
});


}
}
8 changes: 8 additions & 0 deletions app/src/main/res/drawable/bit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="true"
android:variablePadding="false">
<item android:state_activated="true"
android:drawable="@drawable/bit_on" /> <!-- bit on -->
<item android:drawable="@drawable/bit_off" /> <!-- default - bit off -->
</selector>
129 changes: 76 additions & 53 deletions app/src/main/res/layout/activity_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_editor_absoluteX="57dp">

<TableRow
android:id="@+id/r0"
Expand All @@ -21,8 +24,8 @@
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/dot"
android:background="@drawable/bit_on" />
android:background="@drawable/bit"
android:contentDescription="@string/dot" />


<ImageView
Expand All @@ -31,44 +34,44 @@
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/dot"
android:background="@drawable/bit_on" />
android:background="@drawable/bit"
android:contentDescription="@string/dot" />

<ImageView
android:id="@+id/d02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/dot"
android:background="@drawable/bit_on" />
android:background="@drawable/bit"
android:contentDescription="@string/dot" />

<ImageView
android:id="@+id/d03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/dot"
android:background="@drawable/bit_on" />
android:background="@drawable/bit"
android:contentDescription="@string/dot" />

<ImageView
android:id="@+id/d04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/dot"
android:background="@drawable/bit_on" />
android:background="@drawable/bit"
android:contentDescription="@string/dot" />

<ImageView
android:id="@+id/d05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/dot"
android:background="@drawable/bit_on" />
android:background="@drawable/bit"
android:contentDescription="@string/dot" />

</TableRow>

Expand All @@ -83,53 +86,53 @@
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/dot"
android:background="@drawable/bit_on" />
android:background="@drawable/bit"
android:contentDescription="@string/dot" />

<ImageView
android:id="@+id/d11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/dot"
android:background="@drawable/bit_on" />
android:background="@drawable/bit"
android:contentDescription="@string/dot" />

<ImageView
android:id="@+id/d12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/dot"
android:background="@drawable/bit_on" />
android:background="@drawable/bit"
android:contentDescription="@string/dot" />

<ImageView
android:id="@+id/d13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/dot"
android:background="@drawable/bit_on" />
android:background="@drawable/bit"
android:contentDescription="@string/dot" />

<ImageView
android:id="@+id/d14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/dot"
android:background="@drawable/bit_on" />
android:background="@drawable/bit"
android:contentDescription="@string/dot" />

<ImageView
android:id="@+id/d15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/dot"
android:background="@drawable/bit_on" />
android:background="@drawable/bit"
android:contentDescription="@string/dot" />
</TableRow>

<TableRow
Expand All @@ -143,53 +146,53 @@
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/dot"
android:background="@drawable/bit_on" />
android:background="@drawable/bit"
android:contentDescription="@string/dot" />

<ImageView
android:id="@+id/d21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/dot"
android:background="@drawable/bit_on" />
android:background="@drawable/bit"
android:contentDescription="@string/dot" />

<ImageView
android:id="@+id/d22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/dot"
android:background="@drawable/bit_on" />
android:background="@drawable/bit"
android:contentDescription="@string/dot" />

<ImageView
android:id="@+id/d23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/dot"
android:background="@drawable/bit_on" />
android:background="@drawable/bit"
android:contentDescription="@string/dot" />

<ImageView
android:id="@+id/d24"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/dot"
android:background="@drawable/bit_on" />
android:background="@drawable/bit"
android:contentDescription="@string/dot" />

<ImageView
android:id="@+id/d25"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/dot"
android:background="@drawable/bit_on" />
android:background="@drawable/bit"
android:contentDescription="@string/dot" />
</TableRow>

<TableRow
Expand All @@ -203,63 +206,83 @@
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/dot"
android:background="@drawable/bit_on" />
android:background="@drawable/bit"
android:contentDescription="@string/dot" />

<ImageView
android:id="@+id/d31"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/dot"
android:background="@drawable/bit_on" />
android:background="@drawable/bit"
android:contentDescription="@string/dot" />

<ImageView
android:id="@+id/d32"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/dot"
android:background="@drawable/bit_on" />
android:background="@drawable/bit"
android:contentDescription="@string/dot" />

<ImageView
android:id="@+id/d33"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/dot"
android:background="@drawable/bit_on" />
android:background="@drawable/bit"
android:contentDescription="@string/dot" />

<ImageView
android:id="@+id/d34"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/dot"
android:background="@drawable/bit_on" />
android:background="@drawable/bit"
android:contentDescription="@string/dot" />

<ImageView
android:id="@+id/d35"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/dot"
android:background="@drawable/bit_on" />
android:background="@drawable/bit"
android:contentDescription="@string/dot" />
</TableRow>
</TableLayout>

<Button
android:id="@+id/button"
android:id="@+id/change"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="change"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="Button"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="201dp" />
android:layout_marginBottom="8dp"
android:text="next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/change" />

<TextView
android:id="@+id/num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="36dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="@+id/next"
app:layout_constraintStart_toEndOf="@+id/next"
app:layout_constraintTop_toTopOf="@+id/next" />

</android.support.constraint.ConstraintLayout>

0 comments on commit 9ba8f62

Please sign in to comment.