I am new to android. Sorry if this question is very silly...
I am trying to create a simple app.
The rule is .. whenever i touch or click a button which is located at the center, the radius of circle should get increased. And whenever i stopped clicking , then the radius of the circle should be reduced slowly. Here i created a circle, but i can't update it. I used button click listner and onTouchListner but nothing works... Please help me to do this app... Thank you so much for your help..
public class TestcircleActivity extends Activity {
/** Called when the activity is first created. */
Button b;
int clicks = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
public class MyView extends View implements OnTouchListener {
public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
int x = getWidth();
int y = getHeight();
int radius;
radius = 100;
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.WHITE);
canvas.drawPaint(paint);
// Use Color.parseColor to define HTML colors
paint.setColor(Color.parseColor("#CD5C5C"));
canvas.drawCircle(x / 2, y / 2, radius, paint);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
}
}
Related
This code shows nothing on my screen when run. I need to draw a simple circle wherever you touch.
public class Touchevent extends AppCompatActivity {
float mLastTouchX = 0;
float mLastTouchY = 0;
Canvas zex=new Canvas();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_touchevent);
RelativeLayout mylayout = (RelativeLayout)findViewById(R.id.test);
mylayout.setOnTouchListener(
new RelativeLayout.OnTouchListener() {
public boolean onTouch(View v, MotionEvent m) {
onTouchEvent(m);
onDraw(zex);
return true;
}
}
);
}
public void onDraw(Canvas canvasx) {
canvasx.drawColor(Color.WHITE);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
canvasx.drawCircle(mLastTouchX,mLastTouchY,100,paint);
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
mLastTouchX = ev.getX();
mLastTouchY = ev.getY();
return true;
}
}
The code shows no error. Do I need to add a custom view? Then how to implement a click listener?
been working on a prject for a while, now I want it so that when the user touches the screen it changes the color of the textView(DigitalClock). I'm a bit of a noob when it comes to java so I really need a fully working example if possible? Here's my code so far:
public class MainActivity extends Activity {
private static final Random RANDOM = new Random();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
Handler handler = new RandomMoveHandler((TextView) findViewById(R.id.digitalClock1));
handler.sendEmptyMessage(0);
}
private static class RandomMoveHandler extends Handler {
private final WeakReference<TextView> textViewWeakReference;
private RandomMoveHandler(TextView textView) {
this.textViewWeakReference = new WeakReference<TextView>(textView);
}
#Override
public void handleMessage(Message msg) {
TextView textView = textViewWeakReference.get();
if (textView == null) {
Log.i(TAG, "WeakReference is gone so giving up.");
return;
}
int x = RANDOM.nextInt(670 - 100);
int y = RANDOM.nextInt(1230 - 100);
Log.i(TAG, String.format("Moving text view to (%d, %d)", x, y));
textView.setX(x);
textView.setY(y);
//change the text position here
this.sendEmptyMessageDelayed(0, 30000);
}
}
textView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// use any of your own colors here...
textView.setTextColor(android.R.color.holo_purple);
return true;
}
});
See also https://developer.android.com/reference/android/widget/TextView.html#setTextColor(int)
I'm trying to detect the y-axis rotation angle (forward-backward tilt when holding in landscape) but I'm obviously doing thins wrong as the onSensorChanged event never gets triggered
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
...
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
mSensorManager.registerListener(this.mSensorEventListener, mSensor, SensorManager.SENSOR_DELAY_GAME);
}
...
private SensorEventListener mSensorEventListener = new SensorEventListener() {
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
#Override
public void onSensorChanged(SensorEvent event) {
float aX = event.values[1];
float aY = event.values[2];
double angle = aY * (Math.PI / 180);
btnStop.setText(Double.toString(angle));
};
Your device probably does not have Sensor.TYPE_ROTATION_VECTOR
I want to call MyView.circle() method when I click on menu Circle but when I'm clicking on menu it is throwing a NullPointerException, which means that the object is not initialized as I expected.
Here is my code. Where am I going wrong?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0,1,1,"Circle");
menu.add(0,1,2,"Rect");
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId())
{
case 1:
MyView.circle();
break;
}
return super.onOptionsItemSelected(item);
}
public static class MyView extends View {
private Context context;
static Canvas can=null;
public MyView(Context c) {
super(c);
context=c;
}
#Override
protected void onDraw(Canvas canvas) {
mPaint=new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.BLUE);
mPaint.setAntiAlias(true);
//circle(canvas);
}
public static void circle() {
// TODO Auto-generated method stub
can.drawCircle(50, 50, 50, mPaint);
}
}
}
public void static circle(c) {
needs to be changed to
public void static circle(Canvas c) {
Might I recommend spending some time learning Java before writing Android apps?
The question of Android SurfaceView.
I want to draw a Circle in SurfaceView ,but canvas is null in SimpleDraw method.Why?
No 1 ,canvas is null.
No 2,SimpleDraw method run in ClickListener,is right , canvas is not null,why?
1.public class SurfaceTestActivity extends Activity {
SurfaceView sfv;
SurfaceHolder sfh;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sfv=(SurfaceView)findViewById(R.id.surface);
sfh=sfv.getHolder();
SimpleDraw();
}
void SimpleDraw(){
Canvas canvas = sfh.lockCanvas(new Rect(0, 0, 300,
getWindowManager().getDefaultDisplay().getHeight()));
Paint mPaint = new Paint();
mPaint.setColor(Color.GREEN);
mPaint.setStrokeWidth(2);
canvas.drawCircle(150, 150, 80, mPaint);
sfh.unlockCanvasAndPost(canvas);
}
}
2.public class SurfaceTestActivity extends Activity {
Button simpleButton;
SurfaceView sfv;
SurfaceHolder sfh;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
simpleButton=(Button)findViewById(R.id.simple);
sfv=(SurfaceView)findViewById(R.id.surface);
sfh=sfv.getHolder();
simpleButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//SimpleDraw();
}
});
}
void SimpleDraw(){
Canvas canvas = sfh.lockCanvas(new Rect(0, 0, 300,
getWindowManager().getDefaultDisplay().getHeight()));
Paint mPaint = new Paint();
mPaint.setColor(Color.GREEN);
mPaint.setStrokeWidth(2);
canvas.drawCircle(150, 150, 80, mPaint);
sfh.unlockCanvasAndPost(canvas);
}
}
The difference is timing.
No. 1 Tries to draw to the canvas before it is created and fails. The call to sfh.lockCanvas(...) fails and returns null, not the Canvas.
No. 2 Tries to draw after the canvas has been created, so it works.
Do this:
public class SurfaceTestActivity extends Activity implements SurfaceHolder.Callback {
SurfaceView sfv;
SurfaceHolder sfh;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sfv=(SurfaceView)findViewById(R.id.surface);
sfh=sfv.getHolder();
sfh.addCallback(this);
}
void SimpleDraw(){
Canvas canvas = sfh.lockCanvas(new Rect(0, 0, 300,
getWindowManager().getDefaultDisplay().getHeight()));
Paint mPaint = new Paint();
mPaint.setColor(Color.GREEN);
mPaint.setStrokeWidth(2);
canvas.drawCircle(150, 150, 80, mPaint);
sfh.unlockCanvasAndPost(canvas);
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
// draw AFTER surface created!
simpleDraw();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
CHANGES:
adding SurfaceHolder.Callback
adding sfh.addCallback(this);
adding 3 #Override methods with new SimpleDraw -- after the canvas is created.