Toggle Button inside ListView in android

The XML File:

<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" >
<ToggleButton
 android:id="@+id/toggleButton1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:layout_alignParentTop="true"
 android:text="@string/On_Off" />
<TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:layout_below="@+id/toggleButton1"
 android:layout_marginLeft="20dp" />

</RelativeLayout>
Now The Code:

package com.deepthi.mylistviewwithtogglebutton;
import android.os.Bundle;
import android.app.ListActivity;
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 {
@Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(),R.layout.activity_my_list_view_with_toggle,R.id.textView1,getResources().getStringArray(R.array.Strings));
 setListAdapter(adapter);
 
 }
 @Override
 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();
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
 getMenuInflater().inflate(R.menu.activity_my_list_view_with_toggle, menu);
 return true;
 }
}

 

The String.xml is given below:

<resources>
<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>
 </string-array>
</resources>
Advertisements

, , , , , , ,

  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 Mohamed Ibrahim S on August 3, 2013 - 11:55 am

    Really worth full for beginners.

  8. #8 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 darshkalsariya@gmail.com

  9. #9 by androdeveloper.com on September 15, 2014 - 2:31 pm

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

  10. #10 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

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

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

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: