I created the following user settings screen. 
I want to add a listener on Button1 and Button2. How can I do it?
I use the following code to create the settings screen above.
DemoPref:
import android.app.Activity; import android.content.Intent; import android.os.Bundle; public class DemoPref extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); startActivity(new Intent(this, MyPref.class)); } }
MyPref:
import android.os.Bundle; import android.preference.PreferenceActivity; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MyPref extends PreferenceActivity{ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle);
Res \ location \ setting.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:text="TextView1" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <EditText android:text="EditText2" android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:text="TextView2" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <EditText android:text="EditText2" android:id="@+id/editText2" android:layout_height="wrap_content" android:layout_width="wrap_content"></EditText> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:text="Button1" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:text="Button2" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout> </LinearLayout>
Res \ XML \ pref.xml:
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceScreen android:title="My Setting" android:layout="@layout/setting" android:summary="This is my setting."></PreferenceScreen> </PreferenceScreen>
Vivek
source share