Skip to main content

Android Listview With Clickable different view






in Main Activity Class :

package com.example.test;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {

    String iMages[] = {
            "http://www.thebiblescholar.com/android_awesome.jpg",
            "http://blogs-images.forbes.com/rogerkay/files/2011/07/Android1.jpg",
            "http://cdn.slashgear.com/wp-content/uploads/2012/10/android-market-leader-smartphone.jpg",
            "http://www.planmyworkshop.com/images/android.jpeg",
            "http://www.androidguys.com/wp-content/uploads/2012/07/01-android2.jpg" };
    String text[] = { "one", "two", "theree", "four", "five" };

    ArrayList<Bitmap> bitmap_array = new ArrayList<Bitmap>();
    private ListView listv;

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

        listv = (ListView) findViewById(R.id.listView1);

        final ProgressDialog progDailog = new ProgressDialog(MainActivity.this);
        final Handler handler = new Handler() {

            public void handleMessage(Message msg) {

                listv.setAdapter(new ListViewAdapter_test(MainActivity.this));
            }
        };
        new Thread() {

            public void run() {

                for (int i = 0; i < iMages.length; i++) {
                    Log.d("i-->" + i, "Url-->" + iMages[i]);
                    Bitmap bit = getBitmapFromURL(iMages[i]);
                    bitmap_array.add(bit);
                }

                progDailog.dismiss();
                handler.sendEmptyMessage(0);

            }
        }.start();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public class ListViewAdapter_test extends BaseAdapter {

        private LayoutInflater mInflater;

        public ListViewAdapter_test(Context con) {
            // TODO Auto-generated constructor stub
            mInflater = LayoutInflater.from(con);
        }

        public int getCount() {
            // TODO Auto-generated method stub
            return iMages.length;
        }

        public Object getItem(int position) {
            // TODO Auto-generated method stub

            return position;
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub

            return position;
        }

        public View getView(final int position, View convertView,
                ViewGroup parent) {
            // TODO Auto-generated method stub
            final ListContent holder;
            View v = convertView;
            if (v == null) {
                v = mInflater.inflate(R.layout.row, null);
                holder = new ListContent();

                holder.name = (TextView) v.findViewById(R.id.textView1);

                holder.button = (Button) v.findViewById(R.id.button1);

                holder.img_p = (ImageView) v.findViewById(R.id.imageView1);

                v.setTag(holder);
            } else {

                holder = (ListContent) v.getTag();
            }

            holder.name.setText(text[position]);
            holder.button.setText(text[position]);

            if (bitmap_array.get(position) != null) {
                holder.img_p.setImageBitmap(bitmap_array.get(position));
            } else {
                holder.img_p.setImageDrawable(getResources().getDrawable(
                        R.drawable.ic_launcher));
            }

            holder.button.setOnClickListener(mOnTitleClickListener_button);

            holder.img_p.setOnClickListener(mOnTitleClickListener_image);

            return v;
        }
    }

    static class ListContent {

        ImageView img_p;

        TextView name;

        Button button;

    }

    public OnClickListener mOnTitleClickListener_image = new OnClickListener() {
        public void onClick(View v) {
            final int position = listv.getPositionForView((View) v.getParent());

            ImageView i = new ImageView(MainActivity.this);
            i.setImageBitmap(bitmap_array.get(position));

            Toast toastView = new Toast(MainActivity.this);
            toastView.setView(i);
            toastView.setDuration(Toast.LENGTH_LONG);
            toastView.setGravity(Gravity.CENTER, 0, 0);
            toastView.show();

            Log.d("you are click on image view", "you are click on image view");

        }
    };

    public OnClickListener mOnTitleClickListener_button = new OnClickListener() {
        public void onClick(View v) {
            final int position = listv.getPositionForView((View) v.getParent());

            Log.d("you are click on image view", "you are click on image view");

            Toast.makeText(MainActivity.this,
                    "click on button " + text[position], Toast.LENGTH_LONG)
                    .show();

        }
    };

    public static Bitmap getBitmapFromURL(String src) {
        try {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}


activity_main.xml



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>

    </LinearLayout>

row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="click me" />
</LinearLayout>
android manifest file :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

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

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

Comments

Popular posts from this blog

Android - Google MAP V2 PART 3 (add Polyline)

Download Full Code  of  PART-1 , 2 , 3 , 4 Before go ahead Please look in to PART-1 & PART-2 I have just modify Main Activity class: package com.djandroid.mapsv2; import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.GoogleMap.OnMapClickListener; import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions; import com.google.android.gms.maps.model.Polyline; import com.google.android.gms.maps.model.PolylineOptions; public class MainActivity extends FragmentActivity { private GoogleMap MAP; private boolean markClic...

Android show Data from Sqlite DB into Grid View

Shaadi.com Matrimonials Shaadi.com Indian Matrimonials Your Main Activity class package com . Sqlite_grid_view ; import java . util . ArrayList ; import java . util . List ; import android . app . Activity ; import android . os . Bundle ; import android . util . Log ; import android . view . View ; import android . widget . AdapterView ; import android . widget . AdapterView . OnItemClickListener ; import android . widget . ArrayAdapter ; import android . widget . GridView ; import android . widget . TextView ; import android . widget . Toast ; public class AndroidSQLiteTutorialActivity extends Activity { private GridView gridView ; public static ArrayList < String > ArrayofName = new ArrayList < String >(); /** Called when the activity is first created. */ @ Override public void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ); setContentView ( R . l...

Connecting mysql Database in ANDROID using PHP & JSON

 To implement this tutorial you should have basic knowledge of how to run PHP script and start server.  If we talk about client-server architecture, client is Android device and in server side there is a combination of PHP Script and MySQL. In short, PHP Script sits in middle as shown in image. Lets suppose that we have a MySQL database named Employee, and a table int created, with the following SQL: CREATE TABLE `employee` (   `emp_id` int(11) NOT NULL auto_increment,   `emp_name` varchar(100) NOT NULL,   PRIMARY KEY  (`emp_id`) ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ; The PHP code will be very simple: Now Create Android Project :  The Android part is only a bit more complicated: -use a HttpPost to get the data -convert response to string -parse JSON data in to List In Your First Activity : O/P :