Friday 26 September 2014

Customized ListView Example

Configuration Of Activities:

You can simply copy from outside image and add into under res/drawable-hdpi/



Your  MainActivity.java like this;

package com.example.customizedlistview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MainActivity extends Activity {

ListView myList;
String[] images;
String[] mobiles;
String[] names;
String[] tags;
String strImage;
ArrayList<HashMap<String, String>> tempList = new ArrayList<HashMap<String, String>>();
ArrayList<HashMap<String, String>> sendList = new ArrayList<HashMap<String, String>>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myList = (ListView) findViewById(R.id.myList);

names = new String[] { "Apple", "HTC", "LG", "Micromax", "Samsung",
"Sony", "Windows" };
images = new String[] { "apple", "htc", "lg", "micromax", "samsung",
"sony", "windows" };
tags = new String[] { "Think Different!", "Quietly Brilliant",
"Life's Good", "Nothing like anything", "Next is What?",
"make belive", " Do more with less" };
mobiles = new String[] { "iphonem", "htcm", "lgm", "micromaxm",
"samsungm", "sonym", "windowsm" };

for (int i = 0; i < tags.length; i++) {
HashMap<String, String> tempName = new HashMap<String, String>();
tempName.put("title", tags[i]);

tempList.add(tempName);
}

String[] from = { "title" };

int[] to = { R.id.tag };

SpecialAdapter adapter = new SpecialAdapter(this, tempList,
R.layout.row_cell, from, to);
myList.setAdapter(adapter);
myList.setOnItemLongClickListener(new OnItemLongClickListener() {

@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long arg3) {
// TODO Auto-generated method stub
HashMap<String, String> sendmobile = new HashMap<String, String>();
sendmobile.put("mobile", mobiles[pos]);
Intent pass = new Intent(MainActivity.this, Companyname.class);

pass.putExtra("hello", sendmobile);

startActivity(pass);

return false;
}

});
myList.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
// TODO Auto-generated method stub

HashMap<String, String> send = new HashMap<String, String>();
send.put("name", names[pos]);
send.put("image", images[pos]);
send.put("tag_line", tags[pos]);
Intent pass = new Intent(MainActivity.this, Description.class);

pass.putExtra("hello", send);

startActivity(pass);

}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

public class SpecialAdapter extends SimpleAdapter {
public SpecialAdapter(Context context,
List<HashMap<String, String>> items, int resource,
String[] from, int[] to) {
super(context, items, resource, from, to);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);

TextView txt_companyName = (TextView) view
.findViewById(R.id.companyName);
txt_companyName.setText(names[position]);

ImageView thumbImage = (ImageView) view
.findViewById(R.id.thumbImage);
ImageView arraowimg = (ImageView) view.findViewById(R.id.arrowimg);
arraowimg.setImageResource(R.drawable.arrow);

strImage = images[position];

Resources res = getResources();
String mDrawableName = strImage;
int resID = res.getIdentifier(mDrawableName, "drawable",
getPackageName());
Drawable drawable = res.getDrawable(resID);
thumbImage.setImageDrawable(drawable);

return view;

}
}

}

2) Companyname.java activity:

package com.example.customizedlistview;

import java.util.HashMap;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

public class Companyname extends Activity {
ImageView imgmobile;
String strImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_companyname);
Intent getval = getIntent();
HashMap<String, String> hashMap = (HashMap<String, String>) getval.getSerializableExtra("hello");
imgmobile=(ImageView)findViewById(R.id.longimage);
strImage=hashMap.get("mobile").toString();
Resources res = getResources();
String mDrawableName = strImage;
int resID = res.getIdentifier(mDrawableName, "drawable",
getPackageName());
Drawable drawable = res.getDrawable(resID);
imgmobile.setImageDrawable(drawable);
imgmobile.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"Mobiles", Toast.LENGTH_LONG).show();

}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_companyname, menu);
return true;
}


}

3) Description.java activity:

package com.example.customizedlistview;

import java.util.HashMap;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.TextView;

public class Description extends Activity {

ImageView imageView;
String name, tagline;
TextView txt_name;
TextView tag;
String strImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_description);
Intent getval = getIntent();
HashMap<String, String> hashMap = (HashMap<String, String>) getval
.getSerializableExtra("hello");
// HashMap<String, String> map = new HashMap<String, String>();

txt_name = (TextView) findViewById(R.id.name);
tag = (TextView) findViewById(R.id.tagline);

imageView = (ImageView) findViewById(R.id.image);
name = hashMap.get("name").toString();
tagline = hashMap.get("tag_line").toString();
strImage = hashMap.get("image").toString();
Resources res = getResources();
String mDrawableName = strImage;
int resID = res.getIdentifier(mDrawableName, "drawable",
getPackageName());
Drawable drawable = res.getDrawable(resID);

imageView.setImageDrawable(drawable);
txt_name.setText(name);
tag.setText(tagline);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_description, menu);
return true;
}


}

Your Layout XML files are:




Your activity_main like this;

<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" >

    <ListView
        android:id="@+id/myList"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>


2) activity_companyname.xml;

<LinearLayout 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:gravity="center"
    tools:context=".Companyname" >

<ImageView 
    android:id="@+id/longimage"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    
    android:layout_gravity="center_horizontal"/>
   

</LinearLayout>

3) activity_description.xml;

<LinearLayout 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:gravity="center_horizontal"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_marginTop="20dp"
        android:clickable="true" />

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_marginTop="30dp"
        android:text="CompanyName"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tagline"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:text="tagline" />

</LinearLayout>

4) row_cell.xml;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/thumbImage"
        android:clickable="true"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_marginLeft="10dp" />

    <LinearLayout
        android:layout_width="160dp"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/companyName"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_marginLeft="10dp"
            android:text="CompanyName"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tag"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:layout_marginLeft="10dp"
            android:text="tagline"/>
    </LinearLayout>
     <ImageView
        android:id="@+id/arrowimg"
        android:clickable="true"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="10dp" />

</LinearLayout>

App like this;





On Simple Click:


On Long Click:











No comments:

Post a Comment