• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to create button Programatically?

thangaraj

Lurker
public class SamActivity extends Activity
{
private Paint mPaint;
private MaskFilter mEmboss;
private MaskFilter mBlur;

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



fg();


}




public class MyView extends ImageView {



private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;

public MyView(Context c) {
super(c);

mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);

}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}

@Override
protected void onDraw(Canvas canvas)
{
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);

mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(12);
mPaint.setColor(Color.RED);
mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },
0.4f, 6, 3.5f);

mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);

Bitmap mBitmap1= BitmapFactory.decodeResource(getResources(), R.drawable.base);

Bitmap bMapScaled = Bitmap.createScaledBitmap(mBitmap1, 10, 10, true);
canvas.drawBitmap(bMapScaled, 0,0, mPaint);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
super.onDraw(canvas);
invalidate();
}

private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;

private void touch_start(float x, float y)
{

mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath.reset();
}

@Override
public boolean onTouchEvent(MotionEvent event)
{


float x = event.getX();
float y = event.getY();

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}


}
public void fg()
{

MyView mp = new MyView(this);
setContentView(mp);

}

}
This is my code.In this i draw a line over the image.Now i want to know how to create button programatically?please help me.
 
You have to add it to your layout and then declare it and program it in this code. In the layout file just add:
Code:
<Button  android:layout_height="wrap_content" 
         android:id="@+id/boton" android:text="fin" 
         android:layout_width="match_parent" ></Button>

and in your program you have to modify:

Code:
public class SamActivity extends Activity implements  OnClickListener{
 
 //you have to add:
 private Button boton;
 
 //and in the OnCreate method:
 boton = (Button) findViewById(R.id.boton);
 
 //then you have to add the onclick method:
 
 @Override
                 public void onClick(View v) {
                     // TODO Auto-generated method stub.
                      if(v==boton) {
   // Do what ever you want here
                         
                      }
                      
                      
                      
                 }

Hope I could help =)
 
Back
Top Bottom