Pages

Thursday, June 30, 2016

Android ListView With Social Networking Icon Example

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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">


    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true" />
</RelativeLayout>
MainActivity.java
package in.kantapp.androidlistviewwithsocialnetworkingiconexample;

import android.content.Context;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    ListView list;
    String[] title;
    String[] dis;

    int[] img = {R.drawable.bing, R.drawable.blog, R.drawable.drive,
            R.drawable.email, R.drawable.flikr, R.drawable.
            flipboard, R.drawable.goodread, R.drawable.google,};

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


        Resources res = getResources();

        title = res.getStringArray(R.array.Title);
        dis = res.getStringArray(R.array.Dis);
        list = (ListView) findViewById(R.id.listView);

        Adapter adapter = new adopter(this, title, dis, img);
        list.setAdapter((ListAdapter) adapter);
    }


}

class adopter extends ArrayAdapter<String> {


    private final Context c;
    private final String[] Title;
    private final String[] dis;
    private final int[] img;

    public adopter(Context c, String[] Title, String[] dis, int[] img) {

        super(c, R.layout.image, R.id.title, Title);
        this.c = c;
        this.Title = Title;
        this.dis = dis;
        this.img = img;


    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) c.getSystemService(c.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.image, parent, false);
        ImageView imgs = (ImageView) row.findViewById(R.id.imageView);
        TextView text = (TextView) row.findViewById(R.id.title);
        TextView diss = (TextView) row.findViewById(R.id.dis);

        imgs.setImageResource(img[position]);
        text.setText(Title[position]);
        diss.setText(dis[position]);
        return row;

    }
}
string.xml
<resources>
    <string name="app_name">Android ListView With Social Networking Icon Example</string>
    <string-array name="Title">
        <item>Bing</item>
        <item>Blog</item>
        <item>Drive</item>
        <item>Mail</item>
        <item>Myspace</item>
        <item>flipboard</item>
        <item>goodread</item>
    </string-array>
    <string-array name="Dis">
        <item>bing.com</item>
        <item>blogger.com</item>
        <item>drive.google.com</item>
        <item>mail.google.com</item>
        <item>myspace.com</item>
        <item>flipboard.com</item>
        <item>goodread.com</item>
    </string-array>
</resources>

No comments:

Post a Comment