info@thistimebd.com

Thursday 21st of November 10:03:34pm

Custom Adapter Android App Source Code

MainActivity.java

-------------------


package com.sam.web.customadapter;


import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ListView;

import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    private ListView listView;

    private String[] countryNames;

    private int[]flags={R.drawable.australia,R.drawable.bangladesh,R.drawable.brazil,R.drawable.hong_kong,

            R.drawable.india,R.drawable.italy,R.drawable.laos,R.drawable.new_zealand,

            R.drawable.pakistan,R.drawable.russia,R.drawable.uk};


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        countryNames=getResources().getStringArray(R.array.country_names);


        listView=(ListView) findViewById(R.id.listViewId);

        CustomAdapter adapter=new CustomAdapter(this,countryNames,flags);

        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String value=countryNames[position];

                Toast.makeText(MainActivity.this,value,Toast.LENGTH_SHORT).show();

            }

        });

    }

}


========================================================================================================

activity_main.xml

------------------



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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="#22b2ff"

    tools:context="com.sam.web.customadapter.MainActivity">


    <ListView

        android:listSelector="#22ffc4"

        android:dividerHeight="2dp"

        android:divider="#9253b7"

        android:id="@+id/listViewId"

        android:layout_width="match_parent"

        android:layout_height="match_parent"></ListView>


</LinearLayout>


==========================================================================================================

string.xml

-----------


<resources>

    <string name="app_name">CustomAdapter</string>

    <string-array name="country_names">

        <item>Australia</item>

        <item>Bangladesh</item>

        <item>Brazil</item>

        <item>HongKong</item>

        <item>India</item>

        <item>Italy</item>

        <item>Laos</item>

        <item>NewZialand</item>

        <item>Pakistan</item>

        <item>Russia</item>

        <item>UK</item>

    </string-array>

</resources>

=====================================================================================================

CustomAdapter.java

-------------------


package com.sam.web.customadapter;


import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.TextView;


/**

 * Created by maudud on 1/15/18.

 */


public class CustomAdapter extends BaseAdapter {


    int[] flags;

    String[] countryNames;

    Context context;

    private LayoutInflater inflater;

    CustomAdapter(Context context, String[]countryNames, int[]flags){

        this.context=context;

        this.countryNames=countryNames;

        this.flags=flags;



    }



    @Override

    public int getCount() {

        return countryNames.length;

    }


    @Override

    public Object getItem(int position) {

        return null;

    }


    @Override

    public long getItemId(int position) {

        return 0;

    }


    @Override

    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView==null){


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

            convertView=inflater.inflate(R.layout.sample_view,parent,false);

        }

        ImageView imageView=(ImageView) convertView.findViewById(R.id.imageViewId);

        TextView textView=convertView.findViewById(R.id.countryNameId);

        imageView.setImageResource(flags[position]);

        textView.setText(countryNames[position]);

        return convertView;

    }

}

==========================================================================================

sample_view.xml

---------------



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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="match_parent"

    android:layout_height="match_parent">


    <LinearLayout

        android:weightSum="4"

        android:layout_marginTop="10dp"

        android:layout_marginBottom="10dp"

        android:layout_width="match_parent"

        android:layout_height="65dp">

    

    <ImageView

        android:src="@drawable/bangladesh"

        android:layout_weight="1"

        android:id="@+id/imageViewId"

        android:layout_width="0dp"

        android:layout_height="wrap_content" />

    

    <LinearLayout

        android:layout_weight="3"

        android:layout_width="0dp"

        android:layout_height="match_parent"

        android:orientation="vertical">


        <TextView

            android:id="@+id/countryNameId"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_marginTop="10dp"

            android:gravity="center_horizontal"

            android:text="uk"

            android:textAppearance="?android:attr/textAppearanceLarge"

            android:textStyle="bold"

            />


        <TextView

            android:id="@+id/countryDescriptionId"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_marginLeft="20dp"

            android:layout_marginTop="5dp"

            android:gravity="center_horizontal"

            android:text="Clik here to see description"

            android:textAppearance="?android:attr/textAppearanceSmall"

            android:textStyle="bold"

            />

    </LinearLayout>

    </LinearLayout>

    



</LinearLayout>