Search code examples
androidlayoutalignment

Programmatically Align Android Elements


I was programatically creating Android view by using database values. but I don't know how to align the elements. I was creating LinearLayout but it is not Aligning properly. My actual output is

enter image description here

Expected Output

enter image description here

My code

lView = new LinearLayout(Main2Activity.this);
// lView.setPadding(0,150,0,0);
lView.setBackgroundColor(Color.parseColor("#FFFFFF"));
lView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams l2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
l2.gravity=Gravity.CENTER;

GradientDrawable gd3 = new GradientDrawable();
gd3.setCornerRadius(30);
gd3.setColor(Color.parseColor("#003366"));
gd3.setStroke(0, 0xFF000000);
et1.setGravity(Gravity.CENTER);
et1.setHint("Select Date");
et1.setBackgroundDrawable(gd3);
lView.setOrientation(LinearLayout.VERTICAL);
lView.addView(et1);

GradientDrawable gd4 = new GradientDrawable();
gd4.setCornerRadius(30);
gd4.setColor(Color.parseColor("#5CB85C"));
gd4.setStroke(3, 0xFFFFFFFF);
 Intime = new TextView(Main2Activity.this);
Intime.setHint("Select In Time");
Intime.setTextColor(Color.WHITE);
Intime.setHintTextColor(Color.WHITE);
Intime.setTextSize(20);
Intime.setHeight(150);
Intime.setWidth(600);
Intime.setGravity(Gravity.CENTER);
Intime.setLayoutParams(l2);
Intime.setBackgroundDrawable(gd4);
lView.setOrientation(LinearLayout.HORIZONTAL);
lView.addView(Intime);

Outtime = new TextView(Main2Activity.this);
Outtime.setHint("Select Out Time");
Outtime.setTextSize(20);
Outtime.setHeight(150);
Outtime.setWidth(600);
Outtime.setGravity(Gravity.CENTER);
Outtime.setTextColor(Color.WHITE);
Outtime.setHintTextColor(Color.WHITE);
Outtime.setLayoutParams(l2);
Outtime.setBackgroundDrawable(gd4);
lView.setOrientation(LinearLayout.HORIZONTAL);
lView.addView(Outtime);

Solution

  • lView = new LinearLayout(Main2Activity.this);
    // lView.setPadding(0,150,0,0);
    lView.setBackgroundColor(Color.parseColor("#FFFFFF"));
    lView.setLayoutParams(new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT)
        );
    LinearLayout.LayoutParams l2 = new LinearLayout.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT
        );
    l2.gravity=Gravity.CENTER;
    
    GradientDrawable gd3 = new GradientDrawable();
    gd3.setCornerRadius(30);
    gd3.setColor(Color.parseColor("#003366"));
    gd3.setStroke(0, 0xFF000000);
    et1.setGravity(Gravity.CENTER);
    et1.setHint("Select Date");
    et1.setBackgroundDrawable(gd3);
    lView.setOrientation(LinearLayout.VERTICAL);
    lView.addView(et1);
    
    GradientDrawable gd4 = new GradientDrawable();
    gd4.setCornerRadius(30);
    gd4.setColor(Color.parseColor("#5CB85C"));
    gd4.setStroke(3, 0xFFFFFFFF);
    Intime = new TextView(Main2Activity.this);
    Intime.setHint("Select In Time");
    Intime.setTextColor(Color.WHITE);
    Intime.setHintTextColor(Color.WHITE);
    Intime.setTextSize(20);
    Intime.setHeight(150);
    Intime.setWidth(600);
    Intime.setGravity(Gravity.CENTER);
    Intime.setLayoutParams(l2);
    Intime.setBackgroundDrawable(gd4);
    
    // lView.addView(Intime);
    
    Outtime = new TextView(Main2Activity.this);
    Outtime.setHint("Select Out Time");
    Outtime.setTextSize(20);
    Outtime.setHeight(150);
    Outtime.setWidth(600);
    Outtime.setGravity(Gravity.CENTER);
    Outtime.setTextColor(Color.WHITE);
    Outtime.setHintTextColor(Color.WHITE);
    Outtime.setLayoutParams(l2);
    Outtime.setBackgroundDrawable(gd4);
    lView.setOrientation(LinearLayout.HORIZONTAL);
    // lView.addView(Outtime);
        
    LinearLayout lHorizontalView=new LinearLayout(Main2Activity.this);
    
    LinearLayout.LayoutParams l3 = new LinearLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT, 
        ViewGroup.LayoutParams.WRAP_CONTENT
        );
    l3.gravity=Gravity.CENTER; 
    
    lHorizontalView.setOrientation(LinearLayout.HORIZONTAL);
    lHorizontalView.setLayoutParams(l3);
    lHorizontalView.addView(Intime);
    lHorizontalView.addView(Outtime);
    
    lView.addView(lHorizontalView);