Button OnclickEvent in Custom Row View ListView in android
<p>I am new to android. i was created custome_row_view.xml file for row view.</p>

<p>using list i display two row. now i added one button in custome_row_view.</p>

<p>how to write code for that button onclick</p>

<p>Main. XML
    
    </p>

<pre><code>    &lt;ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" &gt;
    &lt;/ListView&gt;

&lt;/LinearLayout&gt;
</code></pre>

<p>custom_row_view.xml</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" &gt;

&lt;TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" /&gt;

&lt;Button
    android:id="@+id/buton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" /&gt;

&lt;TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" /&gt;

&lt;/LinearLayout&gt;
</code></pre>

<p>CustomeViewActivity.java</p>

<pre><code>package naresh.custom.view;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;

public class CustomeViewActivity extends ListActivity implements OnItemClickListener{
    static final ArrayList&lt;HashMap&lt;String,String&gt;&gt; list = new ArrayList&lt;HashMap&lt;String,String&gt;&gt;();

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

        ListView lista=(ListView)findViewById(android.R.id.list);
        int count = lista.getChildCount();

        SimpleAdapter adapter = new SimpleAdapter(
            this,
            list,
            R.layout.custom_row_view,
            new String[] {"title","description"},
            new int[] {R.id.textView1,R.id.textView2}
        );

        if(count&lt;5){
            HashMap&lt;String,String&gt; temp = new HashMap&lt;String,String&gt;();
            temp.put("title","On/Off");
            temp.put("description", "Alert to be On or Off");
            list.add(temp);
            HashMap&lt;String,String&gt; temp1 = new HashMap&lt;String,String&gt;();
            temp1.put("title","Select tone");
            temp1.put("description", "Select tone for alerting");

            list.add(temp1);
            HashMap&lt;String,String&gt; temp2 = new HashMap&lt;String,String&gt;();
            temp2.put("title","Alert Time");
            temp2.put("description", "Select Time for alerting Before/After");
            list.add(temp2);
            HashMap&lt;String,String&gt; temp3 = new HashMap&lt;String,String&gt;();
            temp3.put("title","Change Password");
            temp3.put("description", "Can cahnge password any time");
            list.add(temp3);

            setListAdapter(null);
            setListAdapter(adapter);
        }
        getListView().setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        Toast.makeText(this,"ITem clicked", Toast.LENGTH_LONG).show();
    }
}
</code></pre>
