Toggle Button inside ListView in android

The XML File:

<RelativeLayout xmlns:android=""
 android:layout_height="match_parent" >
 android:text="@string/On_Off" />
 android:layout_marginLeft="20dp" />

Now The Code:

package com.deepthi.mylistviewwithtogglebutton;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MyListViewWithToggle extends ListActivity {
 public void onCreate(Bundle savedInstanceState) {
 ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(),R.layout.activity_my_list_view_with_toggle,,getResources().getStringArray(R.array.Strings));
 protected void onListItemClick(ListView l, View v, int position, long id) {
 String item = (String) getListAdapter().getItem(position);
 Toast.makeText(this, item+" selected", Toast.LENGTH_LONG).show();
 public boolean onCreateOptionsMenu(Menu menu) {
 getMenuInflater().inflate(, menu);
 return true;


The String.xml is given below:

<string name="app_name">MyListViewWithToggleButton</string>
 <string name="hello_world">Hello world!</string>
 <string name="menu_settings">Settings</string>
 <string name="title_activity_my_list_view_with_toggle">MyListViewWithToggle</string>
 <string name="On_Off">ON/OFF</string>
 <string-array name="Strings">
 <item >Android Desk</item>
 <item >Android Desk by Deepthi.G</item>
 <item >ListView in android</item>
 <item >Android Tutorials</item>

, , , , , , ,

  1. #1 by Haris on September 1, 2012 - 5:43 pm

    Hay I tried your code And it is working………
    But when I click the item I am not getting any response from onclick item listener…….?

  2. #2 by karan balkar (@karanbalkar) on October 16, 2012 - 12:08 pm

    @Haris : you will only get a Toast message indicating the item selected. Apart from that there is nothing happening when you click the item.

  3. #3 by venky on November 7, 2012 - 12:57 pm

    can u tell me , how to check the memory leaks in eclipse

  4. #4 by sushantpatekar on January 8, 2013 - 4:59 pm

    Good work

  5. #5 by Joshua on February 10, 2013 - 5:12 am

    Thank you so much, this was incredibly helpful!

  6. #6 by Israel on April 19, 2013 - 3:10 pm

    Can we use Two different shared preferences in a single class?

  7. #7 by Andrés Medina on May 17, 2013 - 11:04 pm

    hi, how do you for send a file “txt” from device mobile to computer via bluetooth.. I’m search on method Data transfer :)..

    Please, I need help.. I’m try send data between two device 😉

  8. #8 by Mohamed Ibrahim S on August 3, 2013 - 11:55 am

    Really worth full for beginners.

  9. #9 by Darshan Kalsariya on March 12, 2014 - 3:10 pm

    Hello Mem,
    I want help. I want to set notification in my java file for particular date which is already settled in prog and user just get notification on settled date. I hope i will get reply as soon as possible on

  10. #10 by on September 15, 2014 - 2:31 pm

    There is any ways to use multiple shared preferences in single app.please can you explain it

  11. #11 by Your Hacks Forum on April 23, 2015 - 1:28 am

    Very quickly this website will be famous among all blog viewers, due to it’s pleasant content

  12. #12 by Venkatesh on July 12, 2016 - 12:33 pm

    can you please tell how to notification like gmail or native messaging app in one notification for multiple messages.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: