Pages

Thursday, June 30, 2016

Android Custom ListView With Image and Image Click 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"

    tools:context="MainActivity">

    <ImageButton
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/beach"
        android:layout_centerHorizontal="true"
        android:src="@drawable/simle"
        android:layout_alignParentTop="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Click Anyware"
        android:id="@+id/textView2"
        android:textColor="#ffffff"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="74dp"
        android:layout_marginEnd="74dp"
        android:layout_marginTop="68dp" />
</RelativeLayout>
image_text.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="62dp"
        android:layout_height="62dp"
        android:id="@+id/imageView"
        android:src="@drawable/superman" />
<LinearLayout
    android:layout_width="match_parent"
    android:padding="10dp"
    android:orientation="vertical"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Title"
        android:id="@+id/title" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Dis"
        android:id="@+id/textView" />
</LinearLayout>

</LinearLayout>
listview.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">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/weblist" />
</LinearLayout>
MainActivity.java
package in.kantapp.androidlistviewwithimageclick;


import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageButton img = (ImageButton) findViewById(R.id.beach);
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(), Listviews.class));
            }
        });
    }
}
ListView.java
package in.kantapp.androidlistviewwithimageclick;


        import android.content.Context;
        import android.content.res.Resources;
        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;


/**
 * Created by Kant on 3/2/2016.
 */
public class Listviews extends MainActivity
{
    ListView list;
    String[] title;
    String[] dis;

    int[] img={R.drawable.reading,R.drawable.redlite,R.drawable.shy,R.drawable.superman};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);

        Resources res = getResources();

        title=res.getStringArray(R.array.Title);
        dis=res.getStringArray(R.array.dis);

        list=(ListView)findViewById(R.id.weblist);

        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_text,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_text,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.textView);

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

    }
}

string.xml
<resources>
    <string name="app_name">Android ListView with ImageClick</string>

    <string-array name="Title">

        <item>minion 1</item>
        <item>minion 2</item>
        <item>minion 3</item>
        <item>minion 4</item>
    </string-array>
    <string-array name="dis">

        <item>Writing</item>
        <item>Hello Hello</item>
        <item>How Are you</item>
        <item>Give Me That</item>
    </string-array>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.kantapp.androidlistviewwithimageclick">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Listviews"/>
    </application>

</manifest>
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3d65aa</color>
    <color name="colorPrimaryDark">#3d65aa</color>
    <color name="colorAccent">#FF4081</color>
</resources>

No comments:

Post a Comment