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>
#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 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 by venky on November 7, 2012 - 12:57 pm
can u tell me , how to check the memory leaks in eclipse
#4 by sushantpatekar on January 8, 2013 - 4:59 pm
Good work
#5 by Joshua on February 10, 2013 - 5:12 am
Thank you so much, this was incredibly helpful!
#6 by Israel on April 19, 2013 - 3:10 pm
Can we use Two different shared preferences in a single class?
#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 by Mohamed Ibrahim S on August 3, 2013 - 11:55 am
Really worth full for beginners.
#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 darshkalsariya@gmail.com
#10 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
#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 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.