Question

I am trying to create a horizontalscrollview in the onCreate() method of my first activity, since I want to make a large number of textviews to scroll through. Here is what I have so far:

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.ViewGroup.LayoutParams;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

    LinearLayout linscrollview;
    HorizontalScrollView scrollview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        scrollview = (HorizontalScrollView) findViewById(R.id.scrollview_layout);
        linscrollview = new LinearLayout(this);


        for(int i=0; i<5; i++) {
            TextView tv = new TextView(this);
            tv.setWidth(LayoutParams.WRAP_CONTENT);
            tv.setHeight(LayoutParams.WRAP_CONTENT);
            tv.setText("" + i);
            tv.setTextSize(20);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            params.setMargins(10, 0, 10, 0);
            tv.setLayoutParams(params);
            tv.setId(i);
            linscrollview.addView(tv);
        }


        scrollview.addView(linscrollview);
    }

    @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;
    }

}

I am not getting any errors, however no textviews are showing up.

Was it helpful?

Solution

Your problem is likely to do with the setWidth and setHeight methods. They set the exact value of the TextView width and height in pixels as described in the documentation:

Makes the TextView exactly this many pixels wide. You could do the same thing by specifying this number in the LayoutParams.

http://developer.android.com/reference/android/widget/TextView.html#setWidth(int)

What you want to do is set the LayoutParams for the TextView as you are already going slightly further down your code. So just get rid of those two method calls and it should work.

OTHER TIPS

this s a chunk of code to implement horizontal scrolling for textview, modify the same according to requirements.

                    textView.setHorizontallyScrolling(true);
                    textView.setSingleLine(true);
                    textView.setMovementMethod(new ScrollingMovementMethod());
                    textView.setHorizontalScrollBarEnabled(true);
                    textView.setSelected(true);

Please do like this

linscrollview .setOrientation(LinearLayout.HORIZONTAL); 

and ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(FILL_PARENT, WRAP_CONTENT);

My coding here. it will gives you list like horizonatal listview

String[] name={"PRASHANT","PRASHANT","PRASHANT","PRASHANT","PRASHANT","PRASHANT","PRASHANT"} ;

myLInearLayoutmain =(LinearLayout) findViewById(R.id.linearLayoutmain);



for(int i =0;i<6;i++)
{
    LinearLayout li=new LinearLayout(getApplicationContext());
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    li.setOrientation(LinearLayout.VERTICAL);
    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    LinearLayout.LayoutParams paramsnew = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);


    params1.setMargins(30, 20, 30, 0);
    //add textView
    valueTV = new TextView(this);
    valueTV.setText(""+name[i]);
    valueTV.setId(5);
    valueTV.setLayoutParams(paramsnew);
    valueTV.setGravity(Gravity.CENTER);

    // adding Button to linear
    valueB = new Button(this);
    valueB.setText(""+name[i]);
    valueB.setId(i);
    valueB.setLayoutParams(params);
    valueB.setOnClickListener(this);
    valueB.setGravity(Gravity.CENTER);


    //add the textView and the Button to LinearLayout
    li.addView(valueTV);
    li.addView(valueB);
    li.addView(img);

    li.setLayoutParams(params1);
    myLInearLayoutmain.addView(li);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top