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>

, , , , , , ,

12 Comments