How to draw on canvas using database data in android - android

I'm new to Android and I have been struggling to draw on canvas a rectangle and text where the size of the rectangle, the text and the color depend on values selected from a MySql database.
I managed to get data selected inside my Activity, but I just can't figure it out how to pass the MySql data to the ondraw() method so I can draw the rectangle and text using the data.
Any help will be greatly appreciated.
public class MyTankActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
int useridInt = intent.getIntExtra("userid", -1);
String userid = useridInt+"";
Response.Listener<String> responseListener = new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
int success = jsonResponse.getInt("success");
JSONArray tank_data = jsonResponse.getJSONArray("tank_data");
if (success == 1) {
int i;
for(i=0;i<tank_data.length();i++){
// Log.v("Result--", "" + tank_data.getString(i));
JSONObject tankObj = tank_data.getJSONObject(0);
String location = (String) tankObj.getString("Location");
String color = (String) tankObj.getString("Color");
String Level = (String) tankObj.getString("Level");
}
} else {
// No records found in database
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
MyTankRequest myTankRequest = new MyTankRequest(userid, responseListener);
RequestQueue queue = Volley.newRequestQueue(MyTankActivity.this);
queue.add(myTankRequest);
setContentView(new TankView(this));
}
}
XML layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.nivelsonic.nivelsonic.MyTankActivity"
android:background="#AEECFF">
<com.nivelsonic.nivelsonic.TankView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
TankView class:
package com.nivelsonic.nivelsonic;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class TankView extends View {
private Paint _paintTank = new Paint();
private Path _path = new Path();
public TankView(Context context) {
super(context);
// TODO Auto-generated constructor stub
init(null, 0);
}
public TankView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
init(attrs, 0);
}
public TankView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init(attrs, 0);
}
private void init(AttributeSet attrs, int defStyle) {
_paintTank.setColor(Color.RED);
_paintTank.setAntiAlias(true);
_paintTank.setStyle(Paint.Style.STROKE);
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
// canvas.drawText();
// canvas.rect();
}
}

One suggestion:
Move JSON data to global variables
public class MyTankActivity extends AppCompatActivity {
String location;
String color;
String Level;
//....
And change setContentView with different constructor of TankView that allows to pass the obtained data:
setContentView(new TankView(this, location, color, Level));
Modify the constructor of TankView accordingly, also add some variables to TankView to store this values:
public TankView(Context context, String loc, String color, String level) {
super(context);
init(null, 0);
this.location = loc;// ... do the same with color/level
...
}
Modify the onDraw after you get the data
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
//using this.Location, color, text
// canvas.drawText();.
// canvas.rect();
}

Related

I would like to know how to draw using canvas over my XML layout

I've created a custom XML layout and I’m trying to draw the map background and the pawn player( the bitmap) over it.
Instead, its painting the pawn player over a white background without the map background that I put as the background on the XML file.
MyViev Class:
package com.example.alpha;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
public class MyView extends View {
Bitmap playerW;
float changingY;
float changingX;
public MyView(Context context) {
super(context);
playerW = BitmapFactory
.decodeResource(getResources(), R.drawable.black);
changingY=0;
// TODO Auto-generated constructor stub
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawBitmap(playerW,4+changingX, (canvas.getHeight())-288-changingY, null);
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
}
my MainActivity Class:
package com.example.alpha;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
public class MainActivity extends ActionBarActivity {
MyView ourView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ourView = new MyView(this);
setContentView(ourView);
}
}
My XML file:
<?xml version="1.0" encoding="utf-8"?>
<view class="com.example.alpha.MyView"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/mapeasy"
/>
It's because you're not actually setting it to use your XML layout. Instead, you've set the content View to be a new instance of MyView, which doesn't have the XML background property set.
Therefor you have 2 options:
Option 1: Call ourView.setBackgroundDrawable(R.drawable.mapeasy); after you created a new instance of MyView.
or
Option 2: Set the content View to be your actual layout file and then find your MyView by using findViewById(int).
Eg.
public class MainActivity extends ActionBarActivity {
MyView ourView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout_file);
ourView = (MyView)findViewById(R.id.myView);
}
}

Set an image as background android, using canvas

My app consist on moving a picture when the user speaks. I have done this, but what I want to do now is that I want to set a image as background for my app. I am working with canvas, as you can see in the class I have included below. So how can I set a background using canvas and not influencing the movement of my picture. Or is there any possibility to connect this class with a xml file where I can define the background?
Thanks in advance
Here is the class:
package com.example.prova1;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MoveBalloon extends Activity {
Bitmap balloon;
DrawBalloon myView;
float x,y,sensorX, sensorY;
SensorManager sm;
Microphone mic;
public class DrawBalloon extends SurfaceView implements Runnable {
SurfaceHolder ourHolder ;
Thread ourThread = null;
boolean isRunning=true;
public DrawBalloon(Context context) {
super(context);
ourHolder= getHolder();
}
public void pause() {
isRunning=false;
while(true){
try{
ourThread.join();
} catch (InterruptedException e){
e.printStackTrace();
}
break;
}
ourThread=null;
}
public void resume(){
isRunning=true;
ourThread = new Thread(this);
ourThread.start();
}
#Override
public void run (){
while(isRunning){
if(!ourHolder.getSurface().isValid())
continue;
Canvas canvas = ourHolder.lockCanvas();
updateMic();
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(balloon, sensorX, sensorY,null);
ourHolder.unlockCanvasAndPost(canvas);
}
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
/*sm= (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if(sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size()!=0){
Sensor s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(o);
sm.registerListener(this,s ,SensorManager.SENSOR_DELAY_NORMAL);
}*/
mic = new Microphone();
balloon = BitmapFactory.decodeResource(getResources(), R.drawable.images);
sensorX=150;
sensorY=350;
//x=y=sensorX=sensorY=0;
myView= new DrawBalloon (this);
myView.resume();
setContentView(myView);
}
/*#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
#Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sensorX=event.values[0];
sensorY=event.values[1];
}
*/
public void updateMic(){
int level = mic.getLevel();
sensorY-=level;
}
#Override
public void onBackPressed() {
finish();
}
}
You can use an xml layout instead of setting content view with a java object :
setContentView(R.layout.move_balloon);
and add your custom view (DrawBalloon) in the xml layout by specifying it's package location, and change the background by setting an image to the background of the root element :
<RelativeLayout 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"
tools:context=".MoveBalloon"
android:background="#drawable/ic_launcher"
>
<com.example.prova1.MoveBalloon.DrawBalloon
android:id="#+id/drawBalloon1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="182dp" />
</RelativeLayout>
but for this to work , your custom view must have a special constructor in able to be inflated from xml layout file :
public DrawBalloon(Context context, AttributeSet attrs) {
super(context, attrs);
ourHolder= getHolder();
}

Passing Parameters with Button Costructor

this here is driving me nuts...
I want to pass some Parameters with a Button Constructor
I did create myButton extending Button:
package com.canbluetoothinterface.utilities;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class myActivityStartButton extends Button implements OnClickListener {
protected EditText[] Array;
private String Name;
private String BufferName;
private Activity activityinstance;
public String[] sValues;
Class<?> cls;
private Method m;
public myActivityStartButton(String Name, Class<?>clsin, Context context) {
super(context);
cls = clsin;
activityinstance = (Activity) context;
setId(mygetId());
this.Name = Name;
init();
}
private void init(){
setOnClickListener(this);
}
public myActivityStartButton(String Name, Class<?>clsin, Context context, Method min) {
super(context);
this.cls = clsin;
this.Name = Name;
this.m = min;
activityinstance = (Activity) context;
setId(mygetId());
setTag(findViewById(mygetId()));
init();
}
#Override
public void setId(int id) {
super.setId(id);
}
private int mygetId() {
int id = 0;
BufferName = Name;
id = activityinstance.getResources().getIdentifier(BufferName, "id", activityinstance.getPackageName());
return id;
}
#Override
public void onClick(View v) {
try {
m.invoke(null, (Object)null);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent intent = new Intent(activityinstance, cls );
activityinstance.startActivity(intent);
}
}
In my Activity I call:
Start = new myActivityStartButton("act_testdriveconfiguration_btn_start", DeviceListActivity.class, this, mstartbutton);
But my OnClick is never called...
What am I doing wrong?
Thank you
1.double check your init() is called.
The following code works for me.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.main_container);
SomeButton btn = new SomeButton(this);
layout.addView(btn);
}
private class SomeButton extends Button implements OnClickListener {
public SomeButton(Context context) {
super(context);
init();
}
private void init() {
setOnClickListener(this);
}
#Override
public void onClick(View v) {
Log.w("log", "click");
}
}
NOTE:
if your button is created in xml, you need these constructors.
public setParams(String Name, Class<?>clsin,etc params)
{
//saving params
}
public SomeButton(Context context) {
super(context);
init();
}
public SomeButton(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public SomeButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}

android XML Error inflating class

I am trying to overlay a drawing on top of a camera preview. I made two custom class extended from SurfaceView: one for the overlay, and one for the cam.
Here is my logcat (only the "Caused by" statements) :
04-11 19:58:06.549: W/dalvikvm(867): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-11 19:58:06.609: E/AndroidRuntime(867): Caused by: android.view.InflateException: Binary XML file line #5: Error inflating class com.example.gui_v9.Activity1.CamView
04-11 19:58:06.609: E/AndroidRuntime(867): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.gui_v9.Activity1.CamView" on path: /data/app/com.example.gui_v9-2.apk
The second error where it cannot find the class, is because it did not inflate.
Eclipse Graphical Layout successfully compiles the xml code and gives a decent preview. Here is my XML code:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.example.gui_v9.Activity1.CamView
android:id="#+id/camview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<com.example.gui_v9.Activity1.OverlayView
android:id="#+id/overlay"
android:layout_width="200dp"
android:layout_height="200dp"/>
</FrameLayout>
I tried to change the flags from
<com.example.gui_v9.Activity1.OverlayView .../>
to
<com.example.gui_v9.Activity1.OverlayView ...></com.example.gui_v9.Activity1.OverlayView>
or to
<SurfaceView class="com.example.gui_v9.Activity1$OverlayView" ... />
or even
<SurfaceView class="com.example.gui_v9.Activity1$OverlayView" ... ></SurfaceView>
without luck. In the latter case, I have a casting problem (CamView cannot be casted in SurfaceView) and Eclipse's XML Graphical Layout fails to render, so I m guessing that the first one takes me further in the compiling process.
You may also take a look at my main activity:
package com.example.gui_v9;
import java.io.IOException;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.hardware.Camera;
public class Activity1 extends Activity {
CamView camview = null;
OverlayView overlay = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
camview = (CamView) findViewById(R.id.camview);
overlay = (OverlayView) findViewById(R.id.overlay);
overlay.getHolder().setFormat(PixelFormat.TRANSLUCENT);
}
public static class CamView extends SurfaceView implements SurfaceHolder.Callback {
Camera cam = null;
SurfaceHolder camviewholder = null;
public CamView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public CamView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public CamView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
#Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
if (cam!=null) {
try {
cam.setPreviewDisplay(camviewholder);
}
catch (IOException e) {
}
cam.startPreview();
}
}
#Override
public void surfaceCreated(SurfaceHolder arg0) {
cam = Camera.open();
}
#Override
public void surfaceDestroyed(SurfaceHolder arg0) {
cam.stopPreview();
cam.release();
}
}
public static class OverlayView extends SurfaceView implements SurfaceHolder.Callback {
private Paint paint = new Paint();
OverlayThread overlaythread = null;
public OverlayView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public OverlayView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public OverlayView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
#Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.BLUE);
canvas.drawCircle(100, 100, 100, paint);
}
#Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
}
#Override
public void surfaceCreated(SurfaceHolder arg0) {
setWillNotDraw(false); //Allows us to use invalidate() to call onDraw()
overlaythread = new OverlayThread(getHolder(), this);
overlaythread.setRunning(true);
overlaythread.start();
}
#Override
public void surfaceDestroyed(SurfaceHolder arg0) {
try {
overlaythread.setRunning(false);
overlaythread.join();
}
catch (InterruptedException e) {
}
}
}
public static class OverlayThread extends Thread {
private OverlayView overlay;
private SurfaceHolder overlayholder;
private boolean _run = false;
public OverlayThread(SurfaceHolder _overlayholder, OverlayView _overlay) {
overlayholder = _overlayholder;
overlay = _overlay;
}
public void setRunning(boolean run) {
_run = run;
}
#Override
public void run() {
Canvas c;
while (_run) {
c = null;
try {
c = overlayholder.lockCanvas(null);
synchronized (overlayholder) {
overlay.invalidate();
}
}
finally {
if (c != null) {
overlayholder.unlockCanvasAndPost(c);
}
}
}
}
}
}
As you can see, I have the three constructors for each classes (with 1,2 and 3 arguments) which is a common error in inflating class failure.
I got this to work when I implemented CamView and OverlayView in two separate classes (CamView.java and OverlayView.java), respectively. So I don't understand why importing classes makes it work. Maybe something with the static workspace?
Thank yall for your help!

AdView requests new add every time SeekBar is touched

I am creating a live wallpaper settings activity. It has a few seek bars and an Ad Mob ad view. Every time I touch one of the seek bars the ad temporarily disappears and then reloads a new add. However, the ad does not change when any of the other preferences are touched. How do I stop the ad from reloading when a seek bar preference is touched? I am pretty stumped on this one :-). Code follows:
The ad preference class:
import android.app.Activity;
import android.content.Context;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
public class AdPreference extends Preference {
public AdPreference(Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle);}
public AdPreference(Context context, AttributeSet attrs) {super(context, attrs);}
public AdPreference(Context context) {super(context);}
#Override
protected View onCreateView(ViewGroup parent) {
// this will create the linear layout defined in ads_layout.xml
View view = super.onCreateView(parent);
// the context is a PreferenceActivity
Activity activity = (Activity)getContext();
// Create the adView
AdView adView = new AdView(activity, AdSize.BANNER, "a151390e12917b5");
((LinearLayout)view).addView(adView);
// Initiate a generic request to load it with an ad
AdRequest request = new AdRequest();
request.addTestDevice("23392C83B8B55DE893A18286CB92DDA2");
request.addTestDevice("E1BAA0317138AEE05268B2E4F76B2D3F");
adView.loadAd(request);
return view;
}
}
The seek bar class:
import android.content.Context;
import android.content.res.TypedArray;
import android.preference.Preference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class SeekBarPreference extends Preference implements OnSeekBarChangeListener {
private final String TAG = getClass().getName();
private static final String ANDROIDNS="http://schemas.android.com/apk/res/android";
private static final String ROBOBUNNYNS="http://robobunny.com";
private static final int DEFAULT_VALUE = 50;
private int mMaxValue = 100;
private int mMinValue = 0;
private int mInterval = 1;
private int mCurrentValue;
private String mUnitsLeft = "";
private String mUnitsRight = "";
private SeekBar mSeekBar;
private TextView mStatusText;
public SeekBarPreference(Context context, AttributeSet attrs) {
super(context, attrs);
initPreference(context, attrs);
}
public SeekBarPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initPreference(context, attrs);
}
private void initPreference(Context context, AttributeSet attrs) {
setValuesFromXml(attrs);
mSeekBar = new SeekBar(context, attrs);
mSeekBar.setMax(mMaxValue - mMinValue);
mSeekBar.setOnSeekBarChangeListener(this);
}
private void setValuesFromXml(AttributeSet attrs) {
mMaxValue = attrs.getAttributeIntValue(ANDROIDNS, "max", 100);
mMinValue = attrs.getAttributeIntValue(ROBOBUNNYNS, "min", 0);
mUnitsLeft = getAttributeStringValue(attrs, ROBOBUNNYNS, "unitsLeft", "");
String units = getAttributeStringValue(attrs, ROBOBUNNYNS, "units", "");
mUnitsRight = getAttributeStringValue(attrs, ROBOBUNNYNS, "unitsRight", units);
try {
String newInterval = attrs.getAttributeValue(ROBOBUNNYNS, "interval");
if(newInterval != null)
mInterval = Integer.parseInt(newInterval);
}
catch(Exception e) {
Log.e(TAG, "Invalid interval value", e);
}
}
private String getAttributeStringValue(AttributeSet attrs, String namespace, String name, String defaultValue) {
String value = attrs.getAttributeValue(namespace, name);
if(value == null)
value = defaultValue;
return value;
}
#Override
protected View onCreateView(ViewGroup parent){
RelativeLayout layout = null;
try {
LayoutInflater mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = (RelativeLayout)mInflater.inflate(R.layout.seek_bar_preference, parent, false);
}
catch(Exception e)
{
Log.e(TAG, "Error creating seek bar preference", e);
}
return layout;
}
#Override
public void onBindView(View view) {
super.onBindView(view);
try
{
// move our seekbar to the new view we've been given
ViewParent oldContainer = mSeekBar.getParent();
ViewGroup newContainer = (ViewGroup) view.findViewById(R.id.seekBarPrefBarContainer);
if (oldContainer != newContainer) {
// remove the seekbar from the old view
if (oldContainer != null) {
((ViewGroup) oldContainer).removeView(mSeekBar);
}
// remove the existing seekbar (there may not be one) and add ours
newContainer.removeAllViews();
newContainer.addView(mSeekBar, ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
}
catch(Exception ex) {
Log.e(TAG, "Error binding view: " + ex.toString());
}
updateView(view);
}
/**
* Update a SeekBarPreference view with our current state
* #param view
*/
protected void updateView(View view) {
try {
RelativeLayout layout = (RelativeLayout)view;
mStatusText = (TextView)layout.findViewById(R.id.seekBarPrefValue);
mStatusText.setText(String.valueOf(mCurrentValue));
mStatusText.setMinimumWidth(30);
mSeekBar.setProgress(mCurrentValue - mMinValue);
TextView unitsRight = (TextView)layout.findViewById(R.id.seekBarPrefUnitsRight);
unitsRight.setText(mUnitsRight);
TextView unitsLeft = (TextView)layout.findViewById(R.id.seekBarPrefUnitsLeft);
unitsLeft.setText(mUnitsLeft);
}
catch(Exception e) {
Log.e(TAG, "Error updating seek bar preference", e);
}
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int newValue = progress + mMinValue;
if(newValue > mMaxValue)
newValue = mMaxValue;
else if(newValue < mMinValue)
newValue = mMinValue;
else if(mInterval != 1 && newValue % mInterval != 0)
newValue = Math.round(((float)newValue)/mInterval)*mInterval;
// change rejected, revert to the previous value
if(!callChangeListener(newValue)){
seekBar.setProgress(mCurrentValue - mMinValue);
return;
}
// change accepted, store it
mCurrentValue = newValue;
mStatusText.setText(String.valueOf(newValue));
persistInt(newValue);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
notifyChanged();
}
#Override
protected Object onGetDefaultValue(TypedArray ta, int index){
int defaultValue = ta.getInt(index, DEFAULT_VALUE);
return defaultValue;
}
#Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
if(restoreValue) {
mCurrentValue = getPersistedInt(mCurrentValue);
}
else {
int temp = 0;
try {
temp = (Integer)defaultValue;
}
catch(Exception ex) {
Log.e(TAG, "Invalid default value: " + defaultValue.toString());
}
persistInt(temp);
mCurrentValue = temp;
}
}
}
The xml layout for the settings activity:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:robobunny="http://robobunny.com"
android:key="disco_wormhole_settings" >
<com.package.name.and.AdPreference
android:layout_width="fill_parent"
androidLlayout_height="wrap_content" />
<Preference
android:key="title"
android:summary="#string/settings_summary"
android:title="#string/settings_title" />
<com.package.name.and.SeekBarPreference
android:defaultValue="50"
android:key="flight_speed"
android:max="100"
android:progressDrawable="#drawable/seek_bar_progress"
android:summary="#string/flight_speed_summary"
android:title="#string/flight_speed_title"
robobunny:min="1"
robobunny:unitsLeft=""
robobunny:unitsRight="%" />
<com.package.name.and.SeekBarPreference
android:defaultValue="30"
android:key="num_rings"
android:max="40"
android:progressDrawable="#drawable/seek_bar_progress"
android:summary="#string/num_rings_summary"
android:title="#string/num_rings_title"
robobunny:min="1"
robobunny:unitsLeft=""
robobunny:unitsRight="" />
<com.package.name.and.SeekBarPreference
android:defaultValue="50"
android:key="particle_speed"
android:max="100"
android:progressDrawable="#drawable/seek_bar_progress"
android:summary="#string/particle_speed_summary"
android:title="#string/particle_speed_title"
robobunny:min="1"
robobunny:unitsLeft=""
robobunny:unitsRight="%" />
<Preference
android:summary="#string/colors_summary"
android:title="#string/colors_title" >
</Preference>
<Preference
android:defaultValue="0xff7d9fff"
android:key="color_one"
android:title="#string/color_one" >
</Preference>
<Preference
android:defaultValue="0xffff4b31"
android:key="color_two"
android:title="#string/color_two" >
</Preference>
<Preference
android:defaultValue="0xff64ff46"
android:key="color_three"
android:title="#string/color_three" >
</Preference>
<CheckBoxPreference
android:defaultValue="true"
android:key="use_space_dust"
android:title="#string/use_space_dust_title" />
<Preference
android:key="spacer"
android:title="#string/single_space" />
<Preference>
</Preference>
</PreferenceScreen>
Thanks in advance for your help,
Chris
I think onCreateView will be called multiple times so an ad request will be created every time. Try moving your admob code to something like onBind or onAttached.

Categories

Resources