For this we just need a class and we will programmatically create the Layouts. And after this we will add Buttons in each layout.
package com.AndroidDevelopmentGuru.progmatic;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
public class progmmatic extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//creating a Linear Layout
LinearLayout parent_layout = new LinearLayout(this);
//Specifying Orientation
parent_layout.setOrientation(LinearLayout.VERTICAL);
setContentView(parent_layout);
//Creating first child
LinearLayout child_one = new LinearLayout(this);
LinearLayout.LayoutParams one_params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1f);
child_one.setBackgroundColor(Color.parseColor("#EEFFAA"));
child_one.setLayoutParams(one_params);
// creating second Child
LinearLayout child_Second = new LinearLayout(this);
LinearLayout.LayoutParams second_params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT,1f);
child_one.setBackgroundColor(Color.parseColor("#FF9999"));
child_Second.setLayoutParams(second_params);
second_params.gravity= Gravity.CENTER;
child_Second.setOrientation(LinearLayout.VERTICAL);
//Adding both child layout into parent layout
parent_layout.addView(child_one);
parent_layout.addView(child_Second);
Button b1 = new Button(this)
b1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
b1.setText("First Progmmatic Button");
child_one.addView(b1);
Button b2 = new Button(this);
b2.setText("Second Progmmatic Button");
LayoutParams btn_parms = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
btn_parms.gravity=Gravity.CENTER_HORIZONTAL;
child_Second.addView(b2, btn_parms);
}
}
Download this Code
Output:
package com.AndroidDevelopmentGuru.progmatic;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
public class progmmatic extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//creating a Linear Layout
LinearLayout parent_layout = new LinearLayout(this);
//Specifying Orientation
parent_layout.setOrientation(LinearLayout.VERTICAL);
setContentView(parent_layout);
//Creating first child
LinearLayout child_one = new LinearLayout(this);
LinearLayout.LayoutParams one_params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1f);
child_one.setBackgroundColor(Color.parseColor("#EEFFAA"));
child_one.setLayoutParams(one_params);
// creating second Child
LinearLayout child_Second = new LinearLayout(this);
LinearLayout.LayoutParams second_params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT,1f);
child_one.setBackgroundColor(Color.parseColor("#FF9999"));
child_Second.setLayoutParams(second_params);
second_params.gravity= Gravity.CENTER;
child_Second.setOrientation(LinearLayout.VERTICAL);
//Adding both child layout into parent layout
parent_layout.addView(child_one);
parent_layout.addView(child_Second);
Button b1 = new Button(this)
b1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
b1.setText("First Progmmatic Button");
child_one.addView(b1);
Button b2 = new Button(this);
b2.setText("Second Progmmatic Button");
LayoutParams btn_parms = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
btn_parms.gravity=Gravity.CENTER_HORIZONTAL;
child_Second.addView(b2, btn_parms);
}
}
Download this Code
Output:
Comments
Post a Comment