tags:

views:

59

answers:

2

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.

A: 

Are you completely sure that you can't do it with an XML? Coding gui from the code is quite more difficult.

In relation to your question: I guess you want the third GraphicalView to be to the right of the second one.

Two ways to do it: Using a RelativeLayout or using a second LinearLayout.

Example:

LinearLayout layout = new LinearLayout(this);
layout.addView(gView, 150, 200);

LinearLayout two = new LinearLayout(this);
two.setOrientation(LinearLayout.VERTICAL);
two.addView(gView2, 150, 200);
two.addView(gView3, 150, 150);

layout.addView(two);

Try not using that way of giving size to a View, you should use setLayoutParams.

For instance your layout should have something like:

layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
Macarse
Hi chrisbunney,Actually its good that it will all handled with layout file but I didn't get success. Can you plaese tell how I get achived via layout file. If this work is done by using layoutfile then it means problem is resolved
shobhit
I am getting GraphicalView through code and now I want to reference it to layout file. So that I can control others layout parameter which is difficult to handle with code. For example if I want to set layout parameters layout_X and layout_Y then its good that I use layout xml file.Please suggest! any suggestions will be highly appreciated and if want more clarification then please tell.
shobhit
@shobhit: to use the layout file you need to call `setContentView()` from your `Activity's onCreate()` method. I don't know what you are missing.
Macarse
Thanks for your kind reply!I want to set the layout parameters like layout_X and layout_Y for GraphicalView. Can you please tell how can I do?
shobhit
@shobhit: From the code? using `.setLayoutParams(new LayoutParams(x, y));`
Macarse
in this way i can control width and height of the view but i want to control x and y coordinates of the view
shobhit
+1  A: 

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);
chrisbunney