0

I have a View looking like this -

<View
            android:layout_width="match_parent"
            android:layout_height="@dimen/line"
            android:background="#000000"></View>

        <Button
            android:id="@+id/button13"
            android:layout_width="match_parent"
            android:layout_height="@dimen/button_height"
            android:background="@drawable/border"
            android:drawableLeft="@mipmap/ic_launcher"
            android:text="Button" />

I want to add it to my activity using JAVA code. i tried doing it manually

LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
    Button btn = new Button(this);
    btn.setText("hello");

    int size = (int)getResources().getDimension(R.dimen.button_height);

    View line = new View(this);
    LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,(int)getResources().getDimension(R.dimen.line));
    line.setLayoutParams(lparams);
    line.setBackgroundColor(Color.BLACK);
    layout.addView(line);


    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, size);
    btn.setLayoutParams( params);
    btn.setBackground(getResources().getDrawable(R.drawable.border));
    layout.addView(btn);

but I can't enter 0.1dp value as the height of the line. how can I add that xml code to the activity over java ?

Meir Tolpin
  • 325
  • 2
  • 13

1 Answers1

0

Please try this.

linearLayout = (LinearLayout) findViewById(R.id.ll_parent);
        Button btn = new Button(this);
        btn.setText("hello");

        View line = new View(this);
        LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,10);
        line.setLayoutParams(lparams);
        line.setBackgroundColor(Color.BLACK);
        linearLayout.addView(line);


        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 120);
        btn.setLayoutParams( params);

        btn.setBackgroundColor(Color.GRAY);
        linearLayout.addView(btn);
iasoftsolutions
  • 251
  • 2
  • 4