Below is the code. Here's the question: How do I add a custom view
class to an XML file that already has button in it? My XML-fu is
pretty terrible, which I am beginning to think is my drawback at the
moment. To rephrase, what I want to do is have my custom view class as
well as a simple button (in the XML, instantiated in the onDraw
method) on the same screen.
Thanks in advance,
Jean de Klerk
NCSU Comp. Sci.
Here's what I have:
package game.balance;
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.view.View;
import android.view.ViewGroup;
// This is the 'main'
public class BalanceGame extends Activity
{
private Ball ball;
private Button buttonSS;
private Boolean buttonSwitch = false;
private ViewGroup vg;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ball = new Ball(this);
buttonSS = (Button)findViewById(R.id.BSS);
buttonSS.setOnClickListener(new clicker());
}
class clicker implements Button.OnClickListener
{
public void onClick(View v)
{
if (v == buttonSS)
{
if(buttonSwitch)
{
buttonSS.setText("Start");
buttonSwitch = false;
}
else
{
buttonSS.setText("Stop");
ball.upColor(500);
ball.invalidate();
buttonSwitch = true;
}
}
}
}
}
//This is the class that I want added to my content view
package game.balance;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;
public class Ball extends View
{
private final float x = 5;
private final float y = 5;
private final int r = 5;
private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private final int originalColor = 0xFFFF0000;
int currentColor = originalColor;
public Ball(Context context)
{
super(context);
mPaint.setColor(currentColor);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawCircle(x, y, r, mPaint);
}
protected void upColor(int x)
{
currentColor += x;
mPaint.setColor(currentColor);
}
}
This is my XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:text="Start"
android:id="@+id/BSS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="20px">
</Button>
</LinearLayout>
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en