Create button through Java code without using XML

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

public class SampleprogActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout l=new LinearLayout(this);
        l.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        setContentView(l);
        l.setOrientation(LinearLayout.VERTICAL);
        Button b=new Button(this);
      //  b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        b.setText("Click Me");
      
        Button b1=new Button(this);
      //  b1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        b1.setText("Choose Me");
        l.addView(b,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        l.addView(b1,new LayoutParams(100,200));
    }
}

and register your activity in AndroidManifest.xml file.

Comments

  1. How we apply scrollview in and adding margin in android widget without using xml ??

    ReplyDelete

Post a Comment

Please post comments here:-)

Popular posts from this blog

Android Objective type Question and Answers

Android Questions and Answers for written exams