Thursday, September 13, 2012

touch draw on Bitmap (android)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public class MainActivity extends Activity implements OnTouchListener {

 ImageView imageView;
 Bitmap bitmap;
 Canvas canvas;
 Paint paint;

 float bx = 0;
 float by = 0;
 float dx = 0;
 float dy = 0;
 float ex = 0;
 float ey = 0;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

  Display currentDisplay = getWindowManager().getDefaultDisplay();
  float dw = currentDisplay.getWidth();
  float dh = currentDisplay.getHeight();

  bitmap = Bitmap.createBitmap((int) dw, (int) dh,Bitmap.Config.ARGB_8888);
  canvas = new Canvas(bitmap);
  paint = new Paint();
  paint.setColor(Color.BLACK);
        
  imageView = new ImageView(this);
  imageView.setImageBitmap(bitmap);
  imageView.setOnTouchListener(this);
  setContentView(imageView);
 }
    
    public boolean onTouch(View v, MotionEvent event) {
     int action = event.getAction();
     switch (action) {
      case MotionEvent.ACTION_DOWN:
       bx = event.getX();
       by = event.getY();
       break;
      case MotionEvent.ACTION_MOVE:
       canvas.drawLine(bx, by, event.getX(), event.getY(), paint);
       imageView.invalidate();
       bx = event.getX();
       by = event.getY();
       break;
      case MotionEvent.ACTION_UP:
       break;
      case MotionEvent.ACTION_CANCEL:
       break;
      default:
       break;
     }
     return true;
    }  
}



Tuesday, September 11, 2012

touch event in custom view (android)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        CustomView cv = new CustomView(this);
        cv.setBackgroundColor(Color.GRAY);
        
        setContentView(cv);
    }
}

class CustomView extends View
{  
 private Paint paint;
 private int positionX;
 private int positionY;
 private String l = "";
 
 public CustomView(Context context) {  
  super(context); 
  paint = new Paint();
  paint.setColor(Color.WHITE);
  paint.setTextSize(18);
  paint.setAntiAlias(true);
 }  
 protected void onDraw(Canvas canvas) {  
  // TODO Auto-generated method stub  
  super.onDraw(canvas);  
 
  canvas.drawText(l, 10, 100, paint);
  invalidate();
 }
 public boolean onTouchEvent(MotionEvent event)  
 {  
  positionX = (int)event.getRawX();  
  positionY = (int)event.getRawY();  
    
  switch(event.getAction())  
  {  
   case MotionEvent.ACTION_DOWN:  
    l = "DOWN - " + String.valueOf(positionX) + " " + String.valueOf(positionY); 
    break;  
      
   case MotionEvent.ACTION_MOVE:  
    l = "MOVE - " + String.valueOf(positionX) + " " + String.valueOf(positionY); 
    break;  

   case MotionEvent.ACTION_UP: 
    l = "UP - " + String.valueOf(positionX) + " " + String.valueOf(positionY); 
    break;   
  }
  return true;  
 }  
}  

Monday, September 10, 2012

Relative Layout + custom view (android)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        CustomView v = new CustomView(this);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(100,100);
        lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        //v.setLayoutParams(lp2);
        v.setBackgroundColor(Color.parseColor("#333333"));
        
        CustomView v2 = new CustomView(this);
        RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(100,100);
        lp2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        //v.setLayoutParams(lp2);
        v2.setBackgroundColor(Color.parseColor("#333333"));

        RelativeLayout relativeLayout = new RelativeLayout(this);
        RelativeLayout.LayoutParams _lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.FILL_PARENT);
        //relativeLayout.setLayoutParams(lp);
        relativeLayout.addView(v,lp);
        relativeLayout.addView(v2,lp2);
        
        setContentView(relativeLayout,_lp);
    }
}

class CustomView extends View
{  
 private Paint paint;
 
 public CustomView(Context context) {  
  super(context); 
  paint = new Paint();
  paint.setColor(Color.WHITE);
  paint.setTextSize(18);
  paint.setAntiAlias(true);
 }  
 protected void onDraw(Canvas canvas) {  
 // TODO Auto-generated method stub  
  super.onDraw(canvas);  
 
  canvas.drawText("Hello World", 5, 30, paint);
  invalidate();
 }
}  



Sunday, September 9, 2012

Monday, September 3, 2012

Simple Image Processing - Grayscale (C#)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
 * Created by SharpDevelop.
 * User: wai
 * Date: 2/9/2012
 * Time: 21:09
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.                                                       /
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;

namespace PictureBox_Test
{
 /// <summary>
 /// Description of MainForm.
 /// </summary>
 public partial class MainForm : Form
 {
  PictureBox imageControl;
  
  public MainForm()
  {
   //
   // The InitializeComponent() call is required for Windows Forms designer support.
   //
   InitializeComponent();
   
   this.Size = new Size(400,300);
   
   imageControl = new PictureBox();
   imageControl.ImageLocation = "http://blog-imgs-31-origin.fc2.com/h/a/n/hanmans/pachinkotokyo18.jpg";
   imageControl.InitialImage = null;
   imageControl.Width = 400;
   imageControl.Height = 300;
   this.Controls.Add(imageControl);
   
   imageControl.LoadCompleted += PictureBox1_LoadCompleted;
  }
  private void PictureBox1_LoadCompleted(Object sender, AsyncCompletedEventArgs e) 
  {
   Bitmap image = new Bitmap(imageControl.Image);
   imageControl.Image = SetGrayscale(image);
  } 
  public Bitmap SetGrayscale(Bitmap image)
  {
   Bitmap bmap = image;
   Color c;
   for (int i = 0; i < bmap.Width; i++)
   {
    for (int j = 0; j < bmap.Height; j++)
    {
     c = bmap.GetPixel(i, j);
     byte gray = (byte)(.299 * c.R + .587 * c.G + .114 * c.B);

     bmap.SetPixel(i, j, Color.FromArgb(gray, gray, gray));
    }
   }
   return bmap;
  }  
 }
}



PictureBox load image from url (C#)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
 * Created by SharpDevelop.
 * User: wai
 * Date: 2/9/2012
 * Time: 21:09
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.                                                       /
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace PictureBox_Test
{
 /// <summary>
 /// Description of MainForm.
 /// </summary>
 public partial class MainForm : Form
 {
  public MainForm()
  {
   //
   // The InitializeComponent() call is required for Windows Forms designer support.
   //
   InitializeComponent();
   
   this.Size = new Size(400,300);
   
   PictureBox imageControl = new PictureBox();
   imageControl.ImageLocation = "http://blog-imgs-31-origin.fc2.com/h/a/n/hanmans/pachinkotokyo18.jpg";
   imageControl.Width = 400;
   imageControl.Height = 300;
   this.Controls.Add(imageControl);
  }
 }
}



Sunday, September 2, 2012

Hello World 2 (C#)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
 * Created by SharpDevelop.
 * User: wai
 * Date: 2/9/2012
 * Time: 15:34
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace HelloWorld
{
 public partial class MainForm : Form
 {
  public MainForm()
  {
   InitializeComponent();
   
   this.Size = new Size(220, 150);
   
   Button button = new Button();
   button.Name = "Button01";
   button.Text = "Hi";
   button.Location = new Point(0,0);
   button.Size = new Size(100, 40);
   button.Click += new EventHandler(Click);
   this.Controls.Add(button);
  }
  
  private void Click(object sender, EventArgs e)
  {
     MessageBox.Show("Wow", "Message");
  }
 }
}



Hello World (C#)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
using System;

namespace HelloWorld
{
 class Program
 {
  public static void Main(string[] args)
  {
   Console.Write("Hello World !");
   Console.ReadKey(true);
  }
 }
}


Simple Vertical ScrollView (android)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;

public class VerticalScrollViewActivity extends Activity {
 
    ImageView i;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        LinearLayout l = new LinearLayout(this);
        l.setOrientation(LinearLayout.VERTICAL);
        
        i = new ImageView(this);
        i.setImageResource(R.drawable.android);
        i.setPadding(5, 5, 0, 0);
        l.addView(i,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        
        i = new ImageView(this);
        i.setImageResource(R.drawable.android);
        i.setPadding(5, 5, 0, 0);
        l.addView(i,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        
        i = new ImageView(this);
        i.setImageResource(R.drawable.android);
        i.setPadding(5, 5, 0, 0);
        l.addView(i,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        
        i = new ImageView(this);
        i.setImageResource(R.drawable.android);
        i.setPadding(5, 5, 0, 0);
        l.addView(i,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        
        i = new ImageView(this);
        i.setImageResource(R.drawable.android);
        i.setPadding(5, 5, 0, 0);
        l.addView(i,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        
        i = new ImageView(this);
        i.setImageResource(R.drawable.android);
        i.setPadding(5, 5, 0, 0);
        l.addView(i,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        
        ScrollView m_Scroll = new ScrollView(this);
        m_Scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        m_Scroll.addView(l, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT) );
        m_Scroll.setOverScrollMode(1);
        m_Scroll.setSmoothScrollingEnabled(true);
        
        //addContentView(m_Scroll, new LayoutParams(LayoutParams.FILL_PARENT,
        //LayoutParams.WRAP_CONTENT));

        setContentView(m_Scroll);
    }
}



Google Map Test (android)

Friday, August 31, 2012

Location service (android)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
public class LocationTestActivity extends Activity implements LocationListener {

    private LocationManager service;
    private TextView tv;
    private String provider;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      
        tv = (TextView) findViewById(R.id.display);
      
        // Get the location manager
        service = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        provider = service.getBestProvider(criteria, false);
        Location location = service.getLastKnownLocation(provider);
      
        // Initialize the location fields
        if (location != null) {
          onLocationChanged(location);
        } else {
          tv.setText("Location not available");
        }
        boolean enabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER);
        if (!enabled) {
          Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
          startActivity(intent);
        }
    }
    public void onLocationChanged(Location arg0) {
     
        //int lat = (int) (location.getLatitude());
        //int lng = (int) (location.getLongitude());
        //latituteField.setText(String.valueOf(lat));
        //longitudeField.setText(String.valueOf(lng));
     
        String lat = String.valueOf(arg0.getLatitude());
        String lon = String.valueOf(arg0.getLongitude());
        Log.e("GPS", "location changed: lat="+lat+", lon="+lon);
        tv.setText("lat="+lat+", lon="+lon);
    }
    public void onProviderDisabled(String arg0) {
        Toast.makeText(this, "Disabled provider " + provider,Toast.LENGTH_SHORT).show();
        Log.e("GPS", "provider disabled " + arg0);
    }
    public void onProviderEnabled(String arg0) {
        Toast.makeText(this, "Enabled new provider " + provider, Toast.LENGTH_SHORT).show();
        Log.e("GPS", "provider enabled " + arg0);
    }
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        Log.e("GPS", "status changed to " + arg0 + " [" + arg1 + "]");
    }
    /* Request updates at startup */
    @Override
    protected void onResume() {
        super.onResume();
        service.requestLocationUpdates(provider, 400, 1, this);
    }
    /* Remove the locationlistener updates when Activity is paused */
    @Override
    protected void onPause() {
        super.onPause();
        service.removeUpdates(this);
    } 
}




Simple ListView 2 - with Dialog (android)


public class HelloListViewActivity extends ListActivity {

    String[] data;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // storing string resources into Array
        data = getResources().getStringArray(R.array.data);
        // Binding resources Array to ListAdapter
        this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_cell, R.id.label, data));

        ListView lv = getListView();
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                createDialog(data[position]);
            }
        });
    }

    private void createDialog(String s)
    {
        new AlertDialog.Builder(this)
        .setTitle("Message")
        .setMessage("Go to " + s + " page ?")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

            }
        })
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

            }
        })
        .show();    
    }
}


Thursday, August 30, 2012

Simple ListView (android)


res/layout/list_cell.xml

<?xml version="1.0" encoding="utf-8"?>
<!--  Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="10dip"
        android:textSize="16dip"
        android:textStyle="bold" >
</TextView>

res/values/list.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="data">
        <item>Apple</item>
        <item>Orange</item>
        <item>Banana</item>
        <item>Milk</item>
    </string-array>
</resources>


import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class HelloListViewActivity extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // storing string resources into Array
        String[] data = getResources().getStringArray(R.array.data);
        // Binding resources Array to ListAdapter
        this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_cell, R.id.label, data));
    }
}




Wednesday, August 29, 2012

ImageView from URL


public class ImageLoadActivity extends Activity{

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     
        LinearLayout m_linear = new LinearLayout(this);
        m_linear.setOrientation(LinearLayout.VERTICAL);
     
        ImageView i = new ImageView(this);
        m_linear.addView(i);
     
        new DownloadImage(i).execute("http://www.mobisights.com/wp-content/uploads/2012/08/001028312.jpg");

        setContentView(m_linear);
    }
 
    private class DownloadImage extends AsyncTask<String, Void, Bitmap> {
        ImageView image;

        public DownloadImage(ImageView image) {
            this.image = image;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap bitmap = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                bitmap = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return bitmap;
        }

        protected void onPostExecute(Bitmap result) {
            image.setImageBitmap(result);
        }
    }
}


Find UIViewController from UIView (iphone)


- (UIViewController*)viewController
{
    for (UIView* next = [self superview]; next; next = next.superview)
    {
        UIResponder* nextResponder = [next nextResponder];

        if ([nextResponder isKindOfClass:[UIViewController class]])
        {
            return (UIViewController*)nextResponder;
        }
    }

    return nil;
}

Tuesday, August 28, 2012

UIButton - Programmetically (iphone)


#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   
    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    myButton.frame = CGRectMake((320-100)/2, (460-44)/2, 100, 44);
    [myButton setTitle:@"Click Me" forState:UIControlStateNormal];
    [myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:myButton];
    myButton.tag = 100;
   
}

-(void)buttonClicked:(id)sender
{
    NSArray *subviews = [self.view subviews];
    if ([subviews count] == 0) return;
    for (UIView *subview in subviews) {
       
        NSLog(@"%@", subview);

    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end

Monday, August 27, 2012

Rotate Image with SeekBar - Programmetically


public class ImageLoadActivity extends Activity implements OnSeekBarChangeListener{

    private ImageView i;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout m_linear = new LinearLayout(this);
        m_linear.setOrientation(LinearLayout.VERTICAL);
       
        Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.akb48);
        Matrix matrix = new Matrix();
        matrix.postRotate(0);

        SeekBar bar = new SeekBar(this);
        bar.setOnSeekBarChangeListener(this);
        m_linear.addView(bar);
       
        i = new ImageView(this);
        rotateImage(0);
        m_linear.addView(i);

        setContentView(m_linear);
    }
   
    private void rotateImage(int degree)
    {
        Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.akb48);
        Matrix matrix = new Matrix();
        matrix.postRotate(degree);
        Bitmap rotated = Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight(), matrix, true);
        i.setImageBitmap(rotated);
    }
   
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
    {
        rotateImage((int)Math.round(progress*3.6));
    }

    public void onStartTrackingTouch(SeekBar arg0) {
    }
    public void onStopTrackingTouch(SeekBar arg0) {
    }
}

Sunday, August 26, 2012

ImageView - rotate image


public class ImageLoadActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout m_linear = new LinearLayout(this);
        m_linear.setOrientation(LinearLayout.VERTICAL);
     
        Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.akb48);
        Matrix matrix = new Matrix();
        matrix.postRotate(30);

        Bitmap rotated = Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight(), matrix, true);

        ImageView i = new ImageView(this);
        i.setImageBitmap(rotated);
     
        m_linear.addView(i);
        setContentView(m_linear);
    }
}


ImageView - Programmetically


public class ImageLoadActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout m_linear = new LinearLayout(this);
        m_linear.setOrientation(LinearLayout.VERTICAL);
       
        ImageView i = new ImageView(this);
        i.setImageResource(R.drawable.akb48);
        i.setPadding(50, 50, 50, 50);
        m_linear.addView(i,newLayoutParams(
                LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

        //m_linear.addView(i);
        setContentView(m_linear);
}
}

Add Event to Button - onClickListener

public class ButtonClickActivity extends Activity {

  private Button m_button;
    /** Called when the activity is first created. */
    @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
     
  LinearLayout m_linear = new LinearLayout(this);
  m_linear.setOrientation(LinearLayout.VERTICAL);
     
  m_button = new Button(this);
  m_button.setText("Hello World");
     
  m_button.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View arg0) {

  m_button.setText("click");

            }
  });
     
  m_linear.addView(m_button);
  setContentView(m_linear);
    }
}





Add a Button - Programmetically

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;

public class HelloActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout m_linear = new LinearLayout(this);
        m_linear.setOrientation(LinearLayout.VERTICAL);
     
        Button m_button = new Button(this);
        m_button.setText("Hello World");
     
        m_linear.addView(m_button);
        setContentView(m_linear);
    }
}