How to AddViews In Scroll View


MainActivity.java
package android.com.listusingscrollbar;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

        LinearLayout linearLayout= (LinearLayout) findViewById(R.id.linearLayout);

        LayoutInflater inflater= (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);




        String[] data={"Android","Java","Php","Dot net","Ios","Lenovo","Logitech","Android","Java","Php","Dot net","Ios","Lenovo","Logitech"};




        for(int i=0; i<data.length;i++) {


            View v=inflater.inflate(R.layout.item,null);
            TextView tv = (TextView) v.findViewById(R.id.list_item);
            tv.setText(data[i]);
            linearLayout.addView(tv);

        }

    }
}


LAYOUTS
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>

<ScrollView
    android:id="@+id/scrollView"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


    </LinearLayout>

</ScrollView>


item.xml
<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/list_item"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_margin="16sp"
    android:textColor="@color/listColor"
    android:gravity="center"
    android:textSize="30sp"
    android:text="hello"
    xmlns:android="http://schemas.android.com/apk/res/android" />
Share on Google Plus

About Mayank Sharma

Mayadi is a knowlage source. You can learn, explore and play many things in a proper way. Mayadi Provides Best, Perfect and Quality Information. subscribe our youtube channel to be updated and and don't forget to take a look on our blog within every week, because we are here to upgrade your knowladge. We love to see you again and again and again...
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment