0

I want to show two scrollView in one LinearLayout or RelativeLayout. Which property should is set to show first Scrollview on the top of screen and second scrollview just below the first Scrollview ?

I have tried but in linearlayout it shows me only first scrollview and in relativelayout it shows me only second scrollView.

Yes, I want to do all this dynamically without using xml file.

Here is my code

import android.app.Activity;


import android.os.Bundle;
import android.widget.*;
import android.widget.TableLayout.LayoutParams;

public class TableFinal extends Activity {
    LinearLayout linearMain, linearScrollView, linearTextview;
    RelativeLayout relativeMain;
    ScrollView scrollview;
    HorizontalScrollView Hscrollview;
    TableLayout tablelayout;
    TableRow tablerow;
    TextView textview;


    LinearLayout.LayoutParams linearparmas;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        linearparmas=new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);

        relativeMain = new RelativeLayout(this);

        // First Table
        linearScrollView = new LinearLayout(this);
        scrollview = new ScrollView(this);
        Hscrollview = new HorizontalScrollView(this);
        tablelayout = new TableLayout(this);

        // First Table's First Row
        tablerow = new TableRow(this);

        linearTextview = new LinearLayout(this);
        textview = new TextView(this);
        textview.setText("11");
        linearTextview.addView(textview);
        tablerow.addView(linearTextview);

        linearTextview = new LinearLayout(this);
        textview = new TextView(this);
        textview.setText("12");
        linearTextview.addView(textview);
        tablerow.addView(linearTextview);

        tablelayout.addView(tablerow);
        Hscrollview.addView(tablelayout);
        scrollview.addView(Hscrollview);
        linearScrollView.addView(scrollview);

        relativeMain.addView(linearScrollView);
        // first table complete

        // second tabler start
        linearScrollView = new LinearLayout(this);
        scrollview = new ScrollView(this);
        Hscrollview = new HorizontalScrollView(this);
        tablelayout = new TableLayout(this);

        // second Table's First Row
        tablerow = new TableRow(this);

        linearTextview = new LinearLayout(this);
        textview = new TextView(this);
        textview.setText("21");
        linearTextview.addView(textview);
        tablerow.addView(linearTextview);

        linearTextview = new LinearLayout(this);
        textview = new TextView(this);
        textview.setText("22");
        linearTextview.addView(textview);
        tablerow.addView(linearTextview);

        tablelayout.addView(tablerow);
        Hscrollview.addView(tablelayout);
        scrollview.addView(Hscrollview);
        linearScrollView.addView(scrollview);

        relativeMain.addView(linearScrollView);
        // second tabler complete

        setContentView(relativeMain);

    }
}

Thank you.

Nirav
  • 5,700
  • 4
  • 34
  • 44

1 Answers1

2

You can use LinearLayout or RelativeLayout. For all layouts you need to get displayHeight.

    Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int displayHeight = display.getHeight();

Then for all layouts set layoutParams to fill parent.

Now for scrollViews differences begin.

In LinearLayout for both scrollViews set the same layoutParams (LinearLayout.layoutParams)

scrollView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, displayHeight/2)); 

And add both to linearLayout

In RelativeLayout set layoutParams like in my example (RelativeLayout.layoutParams)

            RelativeLayout.LayoutParams lp1 = new LayoutParams(LayoutParams.FILL_PARENT, displayHeight);
            lp1.addRule(ALIGN_PARENT_TOP);
            scrollView1.setLayoutParams(lp1);
            RelativeLayout.LayoutParams lp2 = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
            lp2.addRule(BELOW, scrollView1.getId());
            scrollView2.setLayoutParams(lp2);

And add both to layout.

Should work in all display resolution if I did not messed up.

RomaTTi
  • 327
  • 2
  • 12