Question

I am trying to programatically add a whole bunch of textViews of a certain width and at a certain location onto a tab. Now their setX() might be placed beyond the resolution of the screen. For instance, my tab is 1240 pixels in width, and I want to place a TextView at 2000 pixels and of course have a horizontal scroll feature available. I'm essentially creating a timeline on the fly depending on the data pulled.

I'm just trying to (at the moment) get multiple TextViews thrown on to the screen, and to have the horizontal scroll view for them. I am not sure if even doing a setX(2000); will populate a TextView beyond the screen. How can I get the HorizontalScrollView to work so that I may slide my main layout to the right to see the remaining two TextViews that were created?

Some basic code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative_layout"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" 
android:orientation="horizontal">

<HorizontalScrollView
    android:id="@+id/horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
</HorizontalScrollView>

</RelativeLayout>

The MainActivity:

public class MainActivity extends Activity {

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

    RelativeLayout layout = (RelativeLayout) findViewById(R.id.relative_layout);
    for(int i = 50; i < 550; i+=100){
        TextView myText = new TextView(this);
        myText.setX(i * 3);
        myText.setText("HELLLLLOOOO");
        layout.addView(myText);
    }
}

@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;
}
}
Was it helpful?

Solution

MainActivity

public class MainActivity extends Activity {

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

         LinearLayout sv = (LinearLayout)findViewById(R.id.ll);
            for(int i = 50; i < 550; i+=50){
                TextView myText = new TextView(this);
                myText.setX(i * 3);
                myText.setText("HELLLLLOOOO");
                sv.addView(myText);
            }
            }

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

}

xml

<HorizontalScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >


    <LinearLayout
        android:id="@+id/ll"        
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    </LinearLayout>
</HorizontalScrollView>

OTHER TIPS

Create one custom view for example

public class Example extends HorizontalScrollView {

}

Execute all unimplemented methods, then write this:

LinearLayout l = new LinearLayout(context);

TextView tv = new TextView(context);

tv.setText("Example");

tv.setTextColor(Color.RED);

l.addView(tv);

addView(l);

Now in your xml, put the packagename and class name like this:

<com.argha.Example android:height and all /> 

Done, now you have HorizontalScroll view with a TextView... more text you want just do same as above.

Sorry if you found any code error, because I typed with my phone.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top