Thursday, June 12, 2014

[Java Android] Create Circle and Line using Canvas on Eclipse with ADT (just for self documentation)

here it is,

I just create new Android app and go straigth to without setting up the interface, so res/value/strings.xml  and res/layout/* is left as is

and my activity is mainly in MainActivity :)

package com.nugnux.gambar;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
//import android.os.Build;
import android.widget.Toast;
public class MainActivity extends Activity {
    private String tulisan="";
    private final int interval = 3000; // 1 Second
    private Handler handler = new Handler();
    private Runnable runnable = new Runnable(){
        public void run() {
            Toast.makeText(MainActivity.this, "Tadaa...", Toast.LENGTH_SHORT).show();
    DrawView drawView;
    protected void onCreate(Bundle savedInstanceState) {
        tulisan = Long.toString(System.currentTimeMillis());
        Toast.makeText(MainActivity.this, "Heheh..."+tulisan, Toast.LENGTH_SHORT).show();
        drawView = new DrawView(this);
        //handler.postAtTime(runnable, System.currentTimeMillis()+interval);
        handler.postDelayed(runnable, interval);
        //handler.postDelayed(r, delayMillis)
    class DrawView extends View {
        Paint paint = new Paint();
        public DrawView(Context context) {
        public void onDraw(Canvas canvas) {
                canvas.drawLine(10, 20, 30, 40, paint);
                canvas.drawLine(20, 10, 50, 20, paint);
                canvas.drawCircle(100, 100, 10, paint);
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(, menu);
        return true;
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == {
            return true;
        return super.onOptionsItemSelected(item);
     * A placeholder fragment containing a simple view.
    public static class PlaceholderFragment extends Fragment {
        public PlaceholderFragment() {
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
            return rootView;
...still figuring how to create moveTo lineTo since it'll come handy for drawing curve (sine, cosine, some exotic function) and animating it :)