Sunday, June 22, 2014

Implement BroadcastReceiver to monitor Bluetooth state changed

Last example show how to Enable Bluetooth using Intent of BluetoothAdapter.ACTION_REQUEST_ENABLE. In this example, we implement a new class BTStateChangedBroadcastReceiver, extends BroadcastReceiver, to monitor Bluetooth state changed.


BTStateChangedBroadcastReceiver.java
package com.example.androidbluetooth;

import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class BTStateChangedBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
-1);

switch(state){
case BluetoothAdapter.STATE_CONNECTED:
Toast.makeText(context,
"BTStateChangedBroadcastReceiver: STATE_CONNECTED",
Toast.LENGTH_SHORT).show();
break;
case BluetoothAdapter.STATE_CONNECTING:
Toast.makeText(context,
"BTStateChangedBroadcastReceiver: STATE_CONNECTING",
Toast.LENGTH_SHORT).show();
break;
case BluetoothAdapter.STATE_DISCONNECTED:
Toast.makeText(context,
"BTStateChangedBroadcastReceiver: STATE_DISCONNECTED",
Toast.LENGTH_SHORT).show();
break;
case BluetoothAdapter.STATE_DISCONNECTING:
Toast.makeText(context,
"BTStateChangedBroadcastReceiver: STATE_DISCONNECTING",
Toast.LENGTH_SHORT).show();
break;
case BluetoothAdapter.STATE_OFF:
Toast.makeText(context,
"BTStateChangedBroadcastReceiver: STATE_OFF",
Toast.LENGTH_SHORT).show();
break;
case BluetoothAdapter.STATE_ON:
Toast.makeText(context,
"BTStateChangedBroadcastReceiver: STATE_ON",
Toast.LENGTH_SHORT).show();
break;
case BluetoothAdapter.STATE_TURNING_OFF:
Toast.makeText(context,
"BTStateChangedBroadcastReceiver: STATE_TURNING_OFF",
Toast.LENGTH_SHORT).show();
break;
case BluetoothAdapter.STATE_TURNING_ON:
Toast.makeText(context,
"BTStateChangedBroadcastReceiver: STATE_TURNING_ON",
Toast.LENGTH_SHORT).show();
break;
}
}

}

Call registerReceiver(...) to register new BTStateChangedBroadcastReceiver(), to the intent of BluetoothAdapter.ACTION_STATE_CHANGED.
package com.example.androidbluetooth;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

TextView textInfo;
Button buttonEnableBT;
BluetoothAdapter bluetoothAdapter;

final static int REQUEST_ENABLE_BT = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textInfo = (TextView)findViewById(R.id.info);
buttonEnableBT = (Button)findViewById(R.id.enablebt);

bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
textInfo.setText("BlueTooth not supported in this device");
buttonEnableBT.setEnabled(false);
}else{
if (bluetoothAdapter.isEnabled()) {
buttonEnableBT.setEnabled(false);
textInfo.setText("BlueTooth enabled");
}else{
buttonEnableBT.setEnabled(true);
textInfo.setText("BlueTooth disabled, click button to turn on BlueTooth.");
}

//register BroadcastReceiver
registerReceiver(new BTStateChangedBroadcastReceiver(),
new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
}

buttonEnableBT.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}});

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if(requestCode == REQUEST_ENABLE_BT){
if(resultCode==RESULT_OK){
Toast.makeText(MainActivity.this, "BlueTooth Turned On", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this, "Cancelled", Toast.LENGTH_LONG).show();
}
}

if (bluetoothAdapter.isEnabled()) {
buttonEnableBT.setEnabled(false);
textInfo.setText("BlueTooth enabled");
}else{
buttonEnableBT.setEnabled(true);
textInfo.setText("BlueTooth disabled, click button to turn on BlueTooth.");
}

}

}

activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.androidbluetooth.MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android-coding.blogspot.com" />
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/enablebt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enable BlueTooth" />

</LinearLayout>

Permission of "android.permission.BLUETOOTH" is needed in AndroidManifest.xml.
<uses-permission android:name="android.permission.BLUETOOTH"/>


Wednesday, June 18, 2014

Check BlueTooth status, and enable it using Intent of BluetoothAdapter.ACTION_REQUEST_ENABLE

This example check if Bluetooth supported in running device and its ON/OFF status. Then call startActivityForResult() with Intent of BluetoothAdapter.ACTION_REQUEST_ENABLE, when user click button to enable it. Then re-check the status again in onActivityResult() when result returned.


MainActivity.java
package com.example.androidbluetooth;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

TextView textInfo;
Button buttonEnableBT;
BluetoothAdapter bluetoothAdapter;

final static int REQUEST_ENABLE_BT = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textInfo = (TextView)findViewById(R.id.info);
buttonEnableBT = (Button)findViewById(R.id.enablebt);

bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
textInfo.setText("BlueTooth not supported in this device");
buttonEnableBT.setEnabled(false);
}else{
if (bluetoothAdapter.isEnabled()) {
buttonEnableBT.setEnabled(false);
textInfo.setText("BlueTooth enabled");
}else{
buttonEnableBT.setEnabled(true);
textInfo.setText("BlueTooth disabled, click button to turn on BlueTooth.");
}

}

buttonEnableBT.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}});

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if(requestCode == REQUEST_ENABLE_BT){
if(resultCode==RESULT_OK){
Toast.makeText(MainActivity.this, "BlueTooth Turned On", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this, "Cancelled", Toast.LENGTH_LONG).show();
}
}

if (bluetoothAdapter.isEnabled()) {
buttonEnableBT.setEnabled(false);
textInfo.setText("BlueTooth enabled");
}else{
buttonEnableBT.setEnabled(true);
textInfo.setText("BlueTooth disabled, click button to turn on BlueTooth.");
}

}

}

activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.androidbluetooth.MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android-coding.blogspot.com" />
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/enablebt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enable BlueTooth" />

</LinearLayout>

Add <uses-permission android:name="android.permission.BLUETOOTH"/> in AndroidManifest.xml.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidbluetooth"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.BLUETOOTH"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.androidbluetooth.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

Monday, May 19, 2014

Save something in SharedPreferences using SharedPreferences.Editor

If you want to save something (String, setting, value...) in SharedPreferences, such that it can be retrieved later after Activity exited, we can use SharedPreferences.Editor.

Example:

package com.example.androidsharedpreferences;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class MainActivity extends Activity {

String[] dayOfWeek = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"};

EditText editText;
Button buttonSave;
Spinner spinner;

SharedPreferences prefs;
final String KEY_SavedText = "Saved Text";
final String KEY_SavedSel = "Saved Selection";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

editText = (EditText)findViewById(R.id.edittext);
buttonSave = (Button)findViewById(R.id.save);
spinner = (Spinner)findViewById(R.id.spinner);

ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, dayOfWeek);
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

//retrieve saved preferences
prefs = getPreferences(MODE_PRIVATE);
String prefsString = prefs.getString(KEY_SavedText, null);
if(prefsString != null){
editText.setText(prefsString);
}
int prefsInt = prefs.getInt(KEY_SavedSel, -1);
if(prefsInt != -1){
spinner.setSelection(prefsInt);
}

//Save EditText if buttonSave clicked
buttonSave.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putString(KEY_SavedText, editText.getText().toString());
editor.commit();
}
});

//Save Spinner selection
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putInt(KEY_SavedSel, position);
editor.commit();
}

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

}

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.androidsharedpreferences.MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android-coding.blogspot.com" />

<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save Text" />
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

Wednesday, May 14, 2014

Display TextView with multi-color

This example show how to display TextView with multi-color, using Html.fromHtml() and SpannableString.

TextView with multi-color

package com.example.androidcolortext;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.text.SpannableString;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;
import android.widget.TextView.BufferType;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView colorText1 = (TextView)findViewById(R.id.colortext1);
String text1 = "<font COLOR=\'RED\'><b>" + "android-coding" + "</b></font>"
+ "<font COLOR=\'#00FF00\'><i>" + ".blogspot" + "</i></font>"
+ ".com";
colorText1.setText(Html.fromHtml(text1));

TextView colorText2 = (TextView)findViewById(R.id.colortext2);
SpannableString text2 = new SpannableString("android-coding.blogspot.com");
text2.setSpan(new ForegroundColorSpan(Color.RED), 0, 14, 0);
text2.setSpan(new ForegroundColorSpan(Color.GREEN), 6, 11, 0);
text2.setSpan(new ForegroundColorSpan(Color.BLUE), 15, text2.length(), 0);
colorText2.setText(text2, BufferType.SPANNABLE);

TextView colorText3 = (TextView)findViewById(R.id.colortext3);
SpannableString text3 = new SpannableString("android-coding.blogspot.com");
text3.setSpan(new BackgroundColorSpan(Color.LTGRAY), 0, text3.length(), 0);
text3.setSpan(new ForegroundColorSpan(Color.RED), 0, 14, 0);
text3.setSpan(new ForegroundColorSpan(Color.GREEN), 6, 11, 0);
text3.setSpan(new ForegroundColorSpan(0xFF0000FF), 14, 23, 0);
text3.setSpan(new ForegroundColorSpan(0x500000FF), 23, text3.length(), 0);
colorText3.setText(text3, BufferType.SPANNABLE);

}

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.androidcolortext.MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android-coding.blogspot.com" />

<TextView
android:id="@+id/colortext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp" />
<TextView
android:id="@+id/colortext2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp" />
<TextView
android:id="@+id/colortext3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp" />

</LinearLayout>

Tuesday, May 13, 2014

ComposePathEffect vs SumPathEffect

This example show different effect of ComposePathEffect vs SumPathEffect, on CornerPathEffect and DashPathEffect.


/res/layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.androidview.MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android-coding.blogspot.com" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CornerPathEffect radius" />

<SeekBar
android:id="@+id/radius"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="500"
android:progress="30" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="DashPathEffect phase" />

<SeekBar
android:id="@+id/phase"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="30" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<ToggleButton
android:id="@+id/combineffect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="SumPathEffect"
android:textOn="ComposePathEffect" />

<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="@+id/styleFill"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FILL"
android:checked="true" />
<RadioButton
android:id="@+id/styleStroke"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="STROKE" />
<RadioButton
android:id="@+id/styleFillAndStroke"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FILL_AND_STROKE" />
</RadioGroup>
</LinearLayout>

<com.example.androidview.MyView
android:id="@+id/myview"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

MyView.java
package com.example.androidview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposePathEffect;
import android.graphics.CornerPathEffect;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.SumPathEffect;
import android.graphics.Path.Direction;
import android.graphics.PathEffect;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View {

Paint paintBorder, paintCircle;
Path pathBorder, pathCircle;

Path pathShape;
float cornerRadius;
float dashPhase;
float[] intervals = {50.0f, 50.0f};

//true: ComposePathEffect
//false: SumPathEffect
boolean combinPathEffect;

Paint.Style style = Paint.Style.FILL;

public MyView(Context context) {
super(context);
init();
}

public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}

private void init() {
paintBorder = new Paint();
paintBorder.setColor(Color.BLUE);
paintBorder.setStrokeWidth(5);

paintCircle = new Paint();
paintCircle.setColor(Color.RED);
paintCircle.setStrokeWidth(5);

pathBorder = new Path();
pathCircle = new Path();

pathShape = new Path();
pathShape.moveTo(0, 0);
pathShape.lineTo(10, 20);
pathShape.lineTo(20, 0);
pathShape.close();

cornerRadius = 30.0f;
dashPhase = 30.0f;
}

@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.GRAY);

pathBorder.reset();
pathBorder.moveTo(50, 50);
pathBorder.lineTo(50, getHeight()-50);
pathBorder.lineTo(getWidth()-50, getHeight()-50);
pathBorder.lineTo(getWidth()-50, 50);
pathBorder.close();

float radius;
pathCircle.reset();
if(getWidth()>getHeight()){
radius = getHeight()/4;
}else{
radius = getWidth()/4;
}
pathCircle.addCircle(getWidth()/2, getHeight()/2, radius, Direction.CCW);

CornerPathEffect cornerPathEffect = new CornerPathEffect(cornerRadius);
DashPathEffect dashPathEffect = new DashPathEffect(intervals, dashPhase);

PathEffect pathEffect;

if(combinPathEffect){
pathEffect = new ComposePathEffect(dashPathEffect, cornerPathEffect);
}else{
pathEffect = new SumPathEffect(dashPathEffect, cornerPathEffect);
}

paintBorder.setStyle(style);
paintCircle.setStyle(style);

paintBorder.setPathEffect(pathEffect);
paintCircle.setPathEffect(pathEffect);

canvas.drawPath(pathBorder, paintBorder);
canvas.drawPath(pathCircle, paintCircle);
}

public void setCornerRadius(float r){
cornerRadius = r;
invalidate();
}
public void setDashPhase(float p){
dashPhase = p;
invalidate();
}

public void setCombinPathEffect(boolean e){
combinPathEffect = e;
invalidate();
}

public void setStyle(Paint.Style s){
style = s;
invalidate();
}

}

MainActivity.java
package com.example.androidview;

import android.app.Activity;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.ToggleButton;

public class MainActivity extends Activity {

private MyView myView;
private SeekBar seekBarRadius, seekBarPhase;
ToggleButton buttonEffect;
RadioButton radioFill, radioStroke, radioFillAndStroke;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

myView = (MyView)findViewById(R.id.myview);

seekBarRadius = (SeekBar)findViewById(R.id.radius);
seekBarRadius.setOnSeekBarChangeListener(seekBarRadiusChangeListener);
seekBarPhase = (SeekBar)findViewById(R.id.phase);
seekBarPhase.setOnSeekBarChangeListener(seekBarPhaseChangeListener);

buttonEffect = (ToggleButton)findViewById(R.id.combineffect);
buttonEffect.setOnCheckedChangeListener(buttonEffectCheckedChangeListener);

radioFill = (RadioButton)findViewById(R.id.styleFill);
radioStroke = (RadioButton)findViewById(R.id.styleStroke);
radioFillAndStroke = (RadioButton)findViewById(R.id.styleFillAndStroke);
radioFill.setOnCheckedChangeListener(radioOnCheckedChangeListener);
radioStroke.setOnCheckedChangeListener(radioOnCheckedChangeListener);
radioFillAndStroke.setOnCheckedChangeListener(radioOnCheckedChangeListener);
}

OnSeekBarChangeListener seekBarRadiusChangeListener =
new OnSeekBarChangeListener(){

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
myView.setCornerRadius(progress);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {}

};

OnSeekBarChangeListener seekBarPhaseChangeListener =
new OnSeekBarChangeListener(){

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
myView.setDashPhase(progress);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}

};

OnCheckedChangeListener buttonEffectCheckedChangeListener =
new OnCheckedChangeListener(){

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
myView.setCombinPathEffect(isChecked);
}};

OnCheckedChangeListener radioOnCheckedChangeListener =
new OnCheckedChangeListener(){

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(radioFill.isChecked()){
myView.setStyle(Paint.Style.FILL);
}else if(radioStroke.isChecked()){
myView.setStyle(Paint.Style.STROKE);
}else{
myView.setStyle(Paint.Style.FILL_AND_STROKE);
}
}

};

}


- More examples of drawing path on custom View.