Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create layout through code

Tags:

android

I created layout through code so that i can add multiple views on that layout using code below.

public class AndroidPrepChart extends Activity {

    GraphicalView gView;
    GraphicalView gView2;
    GraphicalView gView3;

    BarChart barChart = new BarChart();
    BarChart2 barChart2 = new BarChart2();
    BarChart3 barChart3 = new BarChart3();

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

        gView = barChart.execute2(this);
        gView2 = barChart2.execute2(this);
        gView3 = barChart3.execute2(this);

        LinearLayout layout = new LinearLayout(this);

        layout.addView(gView, 150, 200);
        layout.addView(gView2, 150, 200);
        layout.addView(gView3, 150, 150);

        setContentView(layout);

     }
}

Here output screen contains three charts but i want to position third chart in the second line. Please help me to solve this problem. I am beginner in Android.

like image 436
shobhit Avatar asked Sep 10 '26 22:09

shobhit


1 Answers

You can achieve this by nesting multiple LinearLayouts and changing the orientation property

in XML this would look something like this (showing only the relevant elements and attributes):

<LinearLayout android:orientation="vertical">
    <LinearLayout android:orientation="horizontal">
        <!-- Your first 2 graphs go in this LinearLayour -->
    </LinearLayout>

    <LinearLayout android:orientation="horizontal">
        <!-- The third graph goes in here -->
    </LinearLayout>
</LinearLayout>

You can programmatically manipulate the orientation of the LinearLayout by using the setOrientation method. E.g:

LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
like image 109
chrisbunney Avatar answered Sep 12 '26 10:09

chrisbunney