json leader json leader - 5 months ago 10
Android Question

How to add the photos to ReferrenceScreen on android?

Now create an Fragment subclass that will show your ImageView and provide a container that will hold your preferences:



public class More : Android.Support.V4.App.Fragment
{
private MainActivity mainActivity;
public override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);

// Create your fragment here
}

public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
// return inflater.Inflate(Resource.Layout.YourFragment, container, false);

//return base.OnCreateView (inflater, container, savedInstanceState);
return inflater.Inflate(Resource.Layout.MoreLayout,container,false);

}

[Obsolete("deprecated")]
public override void OnAttach (Android.App.Activity activity)
{
mainActivity = (MainActivity)activity;
base.OnAttach (activity);
}

public override void OnActivityCreated (Bundle savedInstanceState)
{
base.OnActivityCreated (savedInstanceState);
mainActivity.FragmentManager.BeginTransaction().Add(Resource.Id.PreferencesContainer, new ReferenceFragment()).Commit();


}


}


I want to set image inside PreferenceScreen as below picture
Settings

Create a PreferenceFragment subclass that will display just your preferences, I copy/pasted yours into a file Resources/layout/MyPrefs.xml and than load those in the Fragment's OnCreate:

public class ReferenceFragment : PreferenceFragment
{
public override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
AddPreferencesFromResource (Resource.Xml.preferecesSettings);
// Create your fragment here
}

public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
// return inflater.Inflate(Resource.Layout.YourFragment, container, false);

return base.OnCreateView (inflater, container, savedInstanceState);
}
}

Answer

You can do this via a PreferenceFragment.

Create a PreferenceFragment subclass that will display just your preferences, I copy/pasted yours into a file Resources/layout/MyPrefs.xml and than load those in the Fragment's OnCreate:

public class SettingsFragment : PreferenceFragment
{
    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        AddPreferencesFromResource(Resource.Layout.MyPrefs);
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return base.OnCreateView(inflater, container, savedInstanceState);
    }
}

Now create an Activity subclass that will show your ImageView and provide a container that will hold your preferences:

[Activity(Label = "SettingsActivity")]
public class SettingsActivity : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.PreferencesLayout);
        FragmentManager.BeginTransaction().Add(Resource.Id.PreferencesContainer, new SettingsFragment()).Commit();
    }
}

The layout that is being loaded in Activity that will show an image and your preferences:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView
        android:id="@+id/PreferencesImage"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:src="@drawable/PreferenceImage" />
    <FrameLayout
        android:id="@+id/PreferencesContainer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/PreferencesImage"/>
</RelativeLayout>    

Now just create an Intent and start the activity to show your custom preferences:

Intent intent = new Intent(this, typeof(SettingsActivity));
StartActivity(intent);

enter image description here

Comments