12

I have one table "TABLE_SUBJECT" which contain a number of subjects. I need to create
one horizontal scroll view with Subject.

How do I create a ScrollView with database items programmatically? If I enter 1o subject then it will be appear in scroll view as a button. Is it possible?

5 Answers 5

25

you may create it as below:

ScrollView scroll = new ScrollView(context);
scroll.setBackgroundColor(android.R.color.transparent);
scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                                             LayoutParams.FILL_PARENT));
scroll.addView(yourTableView);
Sign up to request clarification or add additional context in comments.

4 Comments

Hi Waqas,Normal Activity its Working Good.But, is it possible to add this scroll view to GLSurfaceview in android OpenGL ES2.0 ? my sample code here..mediafire.com/download/09f9q51xnqhq2l7/LineDraw_samplecode.zip
how to make scrollbar visible and also adjust it's width?
@waqaslam Which LayoutParams are used?
@JohnnyFive I guess ViewGroup should do fine here
5

if you have many elements first you need to wrap-up and add in the Scroll view; for example i need a many text view inside of scrollview, so you need to create ScrollView->LinearLayout->Many textview

                ScrollView scrollView = new ScrollView(context);
                scrollView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

                TextView textView = new TextView(context);
                textView.setText("my text");

                LinearLayout linearLayout = new LinearLayout(context);
                linearLayout.setOrientation(LinearLayout.VERTICAL);
                linearLayout.setGravity(Gravity.RIGHT);
                linearLayout.addView(textView);
                scrollView.addView(linearLayout);

Comments

3

this may help you.

    HorizontalScrollView hsrll = (HorizontalScrollView)findViewById(R.id.hrsll);

    b = new Button(this);

    for (int i = 0; i < 5; i++) {

        b.setWidth(LayoutParams.WRAP_CONTENT);
        b.setHeight(LayoutParams.WRAP_CONTENT);

        b.setText("b"+i);
        b.setId(100+i);

        hsrll.addView(b);
    }

instead of for loop just modify the code as your need(no of records in db). but this the code for creating buttons in dynamically.

2 Comments

ScrollViews can only have one child widget. If you want more children, wrap them in a container layout
yep,because of this you should create at least one Layout(exp:Linearlayout) in the ScrollView and then add Button(etc.) to the Layout.
2

I was doing it like this:

  • Create xml with LinearLayout inside the ScrollView
  • Create xml as item in ScrollView
  • In activity set main content as xml with ScrollView
  • Loop through all table elements with adding new View to LinearLayout form main view

For me works fine.

Comments

0

In Kotlin you can use the below code

    val scroll = ScrollView(context)
    scroll.setBackgroundColor(R.color.transparent)
    scroll.layoutParams = LayoutParams(
        LayoutParams.FILL_PARENT,
        LayoutParams.FILL_PARENT
    )
    scroll.addView(yourTableView)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.