Skip to content

Commit 5299fc1

Browse files
author
Paul Ruiz
committed
added demo for new drawable states in a custom view
1 parent 528d5de commit 5299fc1

File tree

29 files changed

+592
-0
lines changed

29 files changed

+592
-0
lines changed

CustomDrawableStates/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.gradle
2+
/local.properties
3+
/.idea/workspace.xml
4+
/.idea/libraries
5+
.DS_Store
6+
/build

CustomDrawableStates/app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

CustomDrawableStates/app/build.gradle

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 21
5+
buildToolsVersion "21.1.2"
6+
7+
defaultConfig {
8+
applicationId "com.ptrprograms.customdrawablestates"
9+
minSdkVersion 14
10+
targetSdkVersion 21
11+
versionCode 1
12+
versionName "1.0"
13+
}
14+
buildTypes {
15+
release {
16+
minifyEnabled false
17+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18+
}
19+
}
20+
}
21+
22+
dependencies {
23+
compile fileTree(dir: 'libs', include: ['*.jar'])
24+
compile 'com.android.support:appcompat-v7:21.0.3'
25+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /Users/paulruiz/Library/Android/sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.ptrprograms.customdrawablestates;
2+
3+
import android.app.Application;
4+
import android.test.ApplicationTestCase;
5+
6+
/**
7+
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
8+
*/
9+
public class ApplicationTest extends ApplicationTestCase<Application> {
10+
public ApplicationTest() {
11+
super(Application.class);
12+
}
13+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.ptrprograms.customdrawablestates" >
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@drawable/ic_launcher"
8+
android:label="@string/app_name"
9+
android:theme="@style/AppTheme" >
10+
<activity
11+
android:name=".MainActivity"
12+
android:label="@string/app_name" >
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
</application>
20+
21+
</manifest>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.ptrprograms.customdrawablestates;
2+
3+
import android.content.Context;
4+
import android.util.AttributeSet;
5+
import android.widget.TextView;
6+
7+
/**
8+
* Created by paulruiz on 1/11/15.
9+
*/
10+
public class CustomDrawableTextView extends TextView {
11+
12+
protected static int[] STATE_GO = { R.attr.state_go };
13+
protected static int[] STATE_SLOW_DOWN = { R.attr.state_slow_down };
14+
protected static int[] STATE_STOP = { R.attr.state_stop };
15+
16+
private CustomState mState;
17+
18+
public CustomDrawableTextView(Context context) {
19+
super(context);
20+
}
21+
22+
public CustomDrawableTextView(Context context, AttributeSet attrs) {
23+
super(context, attrs);
24+
}
25+
26+
public CustomDrawableTextView(Context context, AttributeSet attrs, int defStyleAttr) {
27+
super(context, attrs, defStyleAttr);
28+
}
29+
30+
public void update( CustomState state ) {
31+
32+
if( state != null )
33+
mState = state;
34+
35+
if( CustomState.GO.equals( mState ) ) {
36+
setText( "GO" );
37+
} else if( CustomState.SLOW_DOWN.equals( mState ) ) {
38+
setText( "SLOW DOWN" );
39+
} else if( CustomState.STOP.equals( mState) ) {
40+
setText( "STOP" );
41+
}
42+
43+
refreshDrawableState();
44+
}
45+
46+
@Override
47+
protected int[] onCreateDrawableState(int extraSpace) {
48+
if( mState == null )
49+
return super.onCreateDrawableState(extraSpace);
50+
51+
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
52+
53+
if( CustomState.GO.equals( mState ) ) {
54+
mergeDrawableStates( drawableState, STATE_GO );
55+
return drawableState;
56+
} else if( CustomState.SLOW_DOWN.equals( mState ) ) {
57+
mergeDrawableStates( drawableState, STATE_SLOW_DOWN );
58+
return drawableState;
59+
} else if( CustomState.STOP.equals( mState) ) {
60+
mergeDrawableStates( drawableState, STATE_STOP );
61+
return drawableState;
62+
} else {
63+
return super.onCreateDrawableState(extraSpace);
64+
}
65+
}
66+
67+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.ptrprograms.customdrawablestates;
2+
3+
/**
4+
* Created by paulruiz on 1/11/15.
5+
*/
6+
public enum CustomState {
7+
GO,
8+
SLOW_DOWN,
9+
STOP
10+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.ptrprograms.customdrawablestates;
2+
3+
import android.os.Handler;
4+
import android.support.v7.app.ActionBarActivity;
5+
import android.os.Bundle;
6+
import android.util.Log;
7+
import android.view.Menu;
8+
import android.view.MenuItem;
9+
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
14+
public class MainActivity extends ActionBarActivity {
15+
16+
private List<CustomState> mCustomStates;
17+
private Handler mHandler;
18+
private CustomDrawableTextView mCustomTextView;
19+
private int mCurrentItem = 0;
20+
21+
@Override
22+
protected void onCreate(Bundle savedInstanceState) {
23+
super.onCreate(savedInstanceState);
24+
setContentView(R.layout.activity_main);
25+
26+
initData();
27+
28+
mCustomTextView = (CustomDrawableTextView) findViewById( R.id.custom_view );
29+
}
30+
31+
private void initData() {
32+
mCustomStates = new ArrayList<>();
33+
mCustomStates.add( CustomState.GO );
34+
mCustomStates.add( CustomState.SLOW_DOWN );
35+
mCustomStates.add( CustomState.STOP );
36+
}
37+
38+
private void startSimulation() {
39+
mHandler = new Handler();
40+
Runnable runnable = new Runnable() {
41+
@Override
42+
public void run() {
43+
if( mCurrentItem >= mCustomStates.size() )
44+
mCurrentItem = 0;
45+
46+
mCustomTextView.update( mCustomStates.get( mCurrentItem++ ) );
47+
48+
if( mHandler != null )
49+
mHandler.postDelayed( this, 3000 );
50+
}
51+
};
52+
53+
runnable.run();
54+
}
55+
56+
private void stopSimulation() {
57+
mHandler = null;
58+
}
59+
60+
@Override
61+
protected void onResume() {
62+
super.onResume();
63+
startSimulation();
64+
}
65+
66+
@Override
67+
protected void onPause() {
68+
stopSimulation();
69+
super.onPause();
70+
}
71+
}
Loading
Loading
Loading
Loading
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:customapp="http://schemas.android.com/apk/res-auto">
2+
<item customapp:state_go="true" android:drawable="@android:color/holo_green_light" />
3+
<item customapp:state_slow_down="true" android:drawable="@color/yellow" />
4+
<item customapp:state_stop="true" android:drawable="@android:color/holo_red_light" />
5+
<item android:drawable="@android:color/white" />
6+
</selector>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
3+
android:layout_height="match_parent">
4+
5+
<com.ptrprograms.customdrawablestates.CustomDrawableTextView
6+
android:id="@+id/custom_view"
7+
android:layout_width="match_parent"
8+
android:layout_height="match_parent"
9+
android:textSize="40sp"
10+
android:gravity="center"
11+
android:text="@string/hello_world"
12+
android:background="@drawable/custom_state_background"/>
13+
14+
</RelativeLayout>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<menu xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:app="http://schemas.android.com/apk/res-auto"
3+
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
4+
<item android:id="@+id/action_settings" android:title="@string/action_settings"
5+
android:orderInCategory="100" app:showAsAction="never" />
6+
</menu>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<resources>
2+
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
3+
(such as screen margins) for screens with more than 820dp of available width. This
4+
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
5+
<dimen name="activity_horizontal_margin">64dp</dimen>
6+
</resources>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<attr name="state_go" format="boolean" />
4+
<attr name="state_slow_down" format="boolean" />
5+
<attr name="state_stop" format="boolean" />
6+
</resources>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<color name="yellow">#FFFF00</color>
4+
</resources>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<resources>
2+
<!-- Default screen margins, per the Android Design guidelines. -->
3+
<dimen name="activity_horizontal_margin">16dp</dimen>
4+
<dimen name="activity_vertical_margin">16dp</dimen>
5+
</resources>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
4+
<string name="app_name">CustomDrawableStates</string>
5+
<string name="hello_world">Hello world!</string>
6+
<string name="action_settings">Settings</string>
7+
8+
</resources>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<resources>
2+
3+
<!-- Base application theme. -->
4+
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
5+
<!-- Customize your theme here. -->
6+
</style>
7+
8+
</resources>

CustomDrawableStates/build.gradle

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Top-level build file where you can add configuration options common to all sub-projects/modules.
2+
3+
buildscript {
4+
repositories {
5+
jcenter()
6+
}
7+
dependencies {
8+
classpath 'com.android.tools.build:gradle:1.0.0'
9+
10+
// NOTE: Do not place your application dependencies here; they belong
11+
// in the individual module build.gradle files
12+
}
13+
}
14+
15+
allprojects {
16+
repositories {
17+
jcenter()
18+
}
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Project-wide Gradle settings.
2+
3+
# IDE (e.g. Android Studio) users:
4+
# Gradle settings configured through the IDE *will override*
5+
# any settings specified in this file.
6+
7+
# For more details on how to configure your build environment visit
8+
# http://www.gradle.org/docs/current/userguide/build_environment.html
9+
10+
# Specifies the JVM arguments used for the daemon process.
11+
# The setting is particularly useful for tweaking memory settings.
12+
# Default value: -Xmx10248m -XX:MaxPermSize=256m
13+
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14+
15+
# When configured, Gradle will run in incubating parallel mode.
16+
# This option should only be used with decoupled projects. More details, visit
17+
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18+
# org.gradle.parallel=true
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Wed Apr 10 15:27:10 PDT 2013
2+
distributionBase=GRADLE_USER_HOME
3+
distributionPath=wrapper/dists
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip

0 commit comments

Comments
 (0)