Search code examples
androidandroid-layoutandroid-scrollview

Android Scroll View not working programmatically?


I tried the following code hoping to scroll horizontally. A total of four buttons are created, Two should be on screen and the other two should come once scrolled. Additionally, i want circular wrapping in both side.

public class MainActivity extends Activity
{

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

        LinearLayout rootlayout = new LinearLayout(this);
        rootlayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        rootlayout.setBackgroundColor(Color.parseColor("#5b331d"));
        rootlayout.setOrientation(LinearLayout.VERTICAL);
        rootlayout.setPadding(pxToDp(50), 0, pxToDp(50), 0);
        setContentView(rootlayout);

        LinearLayout historyLayout = new LinearLayout(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, pxToDp(100));
//      params.weight=0.2f;
        historyLayout.setLayoutParams(params);
        historyLayout.setOrientation(LinearLayout.HORIZONTAL);
        historyLayout.setBackgroundColor(Color.parseColor(("#fbe0d9")));
        rootlayout.addView(historyLayout);

        ScrollView scrollView = new ScrollView(this);
        LinearLayout.LayoutParams scrollParams = new LinearLayout.LayoutParams((getWidthDp()-pxToDp(100))*2,LayoutParams.MATCH_PARENT);
//      scrollParams.weight = 0.5f;
        scrollView.setLayoutParams(scrollParams);
        scrollView.setBackgroundColor(Color.GREEN);
        scrollView.setFillViewport(true);
//      scrollView.setSmoothScrollingEnabled(true);
//      scrollView.canScrollHorizontally(1);
        rootlayout.addView(scrollView);


        LinearLayout historyLayout1 = new LinearLayout(this);
        LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
//      params1.weight=0.5f;
        historyLayout1.setLayoutParams(params1);
        historyLayout1.setOrientation(LinearLayout.HORIZONTAL);
        historyLayout1.setBackgroundColor(Color.parseColor(("#bfcada")));
        scrollView.addView(historyLayout1);
//      rootlayout.addView(historyLayout1);


        Button button1 = new Button(this);
        LinearLayout.LayoutParams buttomParam1 = new LinearLayout.LayoutParams((getWidthDp()-pxToDp(100))/2, LayoutParams.MATCH_PARENT);
        button1.setLayoutParams(buttomParam1);
        button1.setText("OK");
        button1.setBackgroundColor(Color.GRAY);
        historyLayout1.addView(button1);

        Button button2 = new Button(this);
        //LinearLayout.LayoutParams buttomParam2 = new LinearLayout.LayoutParams(pxToDp(250), LayoutParams.MATCH_PARENT);
        LinearLayout.LayoutParams buttomParam2 = new LinearLayout.LayoutParams((getWidthDp()-pxToDp(100))/2, LayoutParams.MATCH_PARENT);
        button2.setLayoutParams(buttomParam2);
        button2.setText("OK");
        button2.setBackgroundColor(Color.RED);
        historyLayout1.addView(button2);

        Button button3 = new Button(this);
        LinearLayout.LayoutParams buttomParam3 = new LinearLayout.LayoutParams((getWidthDp()-pxToDp(100))/2, LayoutParams.MATCH_PARENT);
        button3.setLayoutParams(buttomParam3);
        button3.setText("OK");
        button3.setBackgroundColor(Color.GRAY);
        historyLayout1.addView(button3);

        Button button4 = new Button(this);
        LinearLayout.LayoutParams buttomParam4 = new LinearLayout.LayoutParams((getWidthDp()-pxToDp(100))/2, LayoutParams.MATCH_PARENT);
        button4.setLayoutParams(buttomParam4);
        button4.setText("OK");
        button4.setBackgroundColor(Color.RED);
        historyLayout1.addView(button4);


        //setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to
        // the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private static int dpToPx(int dp)
    {
        return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
    }

    private static int pxToDp(int px)
    {
        return (int) (px / Resources.getSystem().getDisplayMetrics().density);
    }

    private int getWidthDp()
    {
        Point size = new Point();
        getWindowManager().getDefaultDisplay().getSize(size);
        return pxToDp(size.x);
    }
    private int getHeightDp()
    {
        Point size = new Point();
        getWindowManager().getDefaultDisplay().getSize(size);
        return pxToDp(size.y);
    }
}

Solution

  • 3 things to modify to make it work, (at least for me it is)

    1. Change ScollView to HorizontalScrollView :

    HorizontalScrollView scrollView = new HorizontalScrollView(this);
    

    2. Change the LayoutParams of your HorizontalScrollView :

    LinearLayout.LayoutParams scrollParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
    

    (Change the width param to WRAP_CONTENT so it would stretch when necessary according to the dimension of it's child.)

    3. Change the size of your button :

    I don't know your screen resolution, at least the button won't be wide enough for scrollView to work on my emulator. (720*1280)

    Hope it helps.