0

I have a standard PreferencesActivity declared in the following way:

public class PreferencesActivity extends PreferenceActivity{
   ...
}

Then inside the activity I load my Fragment in the following way:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    getFragmentManager().beginTransaction()
       .replace(android.R.id.content, new GraphicsFragment())
       .commit();
}

And this is my Fragment:

public static class GraphicsFragment extends PreferenceFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.warrior_preference);
    }
}

Right now the Activity is completely covered by this Fragment, but what I need is to have an ImageView on top and then the Fragment activity loaded within a ScrollView or something similar. Is this possible?

enter image description here

Raffaeu
  • 6,694
  • 13
  • 68
  • 110

3 Answers3

1

You can try below. Create activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:id="@+id/activity_main_layout">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:src="@drawable/some image" />

    <FrameLayout android:id="@+id/fragment"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_below="@id/image"/>

</RelativeLayout>

In PreferencesActivity onCreate method use below write

    Fragment newFragment = new GraphicsFragment();
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.replace(R.id.fragment, newFragment).commit();;

Inside ImageView give your drawable image. Please go through below urls for PreferencesActivity creation.

What to use instead of “addPreferencesFromResource” in a PreferenceActivity

How do you create Preference Activity

Community
  • 1
  • 1
Mohammad Tauqir
  • 1,817
  • 1
  • 18
  • 53
  • It doesn't work. I get this error Unable to start activity ComponentInfo{xxx.PreferencesActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' – Raffaeu Dec 30 '15 at 16:39
  • Not working still same error Your content must have a ListView whose id attribute is 'android.R.id.list' This is a PreferencesActivity and not a standard activity – Raffaeu Dec 30 '15 at 16:46
  • 1
    @Raffaeu, I have update my answer and also check http://stackoverflow.com/questions/6647831/android-your-content-must-have-a-listview-whose-id-attribute-is-android-r-id-li or http://stackoverflow.com/questions/11050817/your-content-must-have-a-listview-whose-id-attribute-is-android-r-id-list – Mohammad Tauqir Dec 30 '15 at 16:56
  • I'll have a look at this – Raffaeu Dec 30 '15 at 16:56
  • This is the correct answer http://stackoverflow.com/questions/30139548/how-to-add-toolbar-in-preferenceactivity Preferences activity needs a listview to hold the preferences fragment and not a fragment or frame layout – Raffaeu Dec 30 '15 at 17:00
0

Yes this is possible. You need to place the fragment inside a specified view in the parent view.

You can also place the fragment directly into your XML file, if it is not necessary to inflate the fragment programmaticly.

Bassiuz
  • 101
  • 5
0

Yes what you're trying to achieve is completely possible. What can be done to achieve this is applying an imageView and a frameLayout in a layout:

<LinearLayout 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"
android:id="@+id/viewGroup"
android:orientation="vertical"
tools:context="com.android.empty.MainActivity">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="140dp"
    android:background="@drawable/c1"
    android:scaleType="centerCrop"/>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/frame"></FrameLayout>

</LinearLayout>

Now change, changing the id of inside the replace method from android.R.id.content to R.id.frame will do the trick.

getFragmentManager().beginTransaction()
            .replace(R.id.frame, new GraphicsFragment())
            .commit();

    //Do what ever you want to with imageview.

Actually, android.R.id.content gives the root view in return so the fragment completely covers the layout.

Nilesh Singh
  • 1,750
  • 1
  • 18
  • 30