Search code examples
androidbuttonpreferenceactivity

Create button in extends PreferenceActivity


I am making an app that you can make and remove tabs. I want to make 2 buttons that you can create a tab and remove a tab. I want the buttons to be in the same activity as where the settings are, but it seems not to work.

Here is my AppPreferences:

public class AppPreferences extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);

}
public void createtab(View view){

    Toast.makeText(AppPreferences.this, "Button 1", Toast.LENGTH_SHORT).show();

}
public void removetab(View view){

    Toast.makeText(AppPreferences.this, "Button 2", Toast.LENGTH_SHORT).show();

}

and this is my layout:

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Create Tab"
    android:id="@+id/createtab"
    android:onClick="createtab"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Remove Tab"
    android:id="@+id/removetab"
    android:onClick="removetab"
    android:layout_alignBottom="@+id/createtab"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    />

Thanks for reading and I hope you can help me!


Solution

  • According to How to add a button to a PreferenceScreen (Android), you can do it with something like this:

    <PreferenceScreen android:title="@string/login" android:key="Login">
        <EditTextPreference android:persistent="true" android:title="@string/username" android:key="Username"></EditTextPreference>
        <EditTextPreference android:title="@string/password" android:persistent="true" android:password="true" android:key="Password"></EditTextPreference>
        <Preference android:layout="@layout/loginButtons" android:key="loginButtons"></Preference>
    

    and the layout file (layoutButtons.xml) looks that way:

     <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:weightSum="10" 
        android:baselineAligned="false" android:orientation="horizontal">
        <Button android:text="Login" android:layout_width="fill_parent"
            android:layout_weight="5" android:layout_height="wrap_content"
            android:id="@+id/loginButton" android:layout_gravity="left"></Button>
        <Button android:text="Password?" android:layout_width="fill_parent"
            android:layout_weight="5" android:layout_height="wrap_content"
            android:id="@+id/forgottenPasswordButton"></Button>
    </LinearLayout>
    

    you can add OnClick..