narispillai narispillai - 3 months ago 8
Java Question

Background image selecting in the app

What I wanted.
I'm currently having a problem setting the background for my app. What I wanted is a page with selections of wallpaper/background for the app user to choose. Once they chose the background the wanted by clicking the Image view, the whole app should use the image as their background.That's all.

What I've done.
I've created an activity that has 2 ImageView as the choices of wallpaper available and assigns the image view to set the background image when the user click on them. The problem now is I don't know how to save the setting and apply it to all other activity in my project.

XML

<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/settingscreen"
tools:context="com.example.naris.auin.SettingsActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Background Selector"
android:id="@+id/textViewBackgroundSelector"
android:layout_below="@+id/switchNightMode"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="38dp" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageViewBackground1"
android:layout_marginLeft="23dp"
android:layout_marginStart="23dp"
android:src="@drawable/rateicon"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageViewBackground2"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/textViewBackgroundSelector"
android:layout_toEndOf="@+id/textViewBackgroundSelector"
android:layout_marginLeft="36dp"
android:layout_marginStart="36dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>


Settings Activity

public class SettingsActivity extends AppCompatActivity {



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);

final RelativeLayout Settingscreen = (RelativeLayout) findViewById(R.id.settingscreen);





ImageView ImageViewBackground1 = (ImageView) findViewById(R.id.imageViewBackground1);
ImageView ImageViewBackground2 = (ImageView) findViewById(R.id.imageViewBackground2);

ImageViewBackground1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

Settingscreen.setBackgroundResource(R.drawable.faqicon);

}
});

ImageViewBackground2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

Settingscreen.setBackgroundResource(R.drawable.rateicon);

}
});

}

public boolean onKeyDown(int keyCode, KeyEvent event) {

if(keyCode == android.view.KeyEvent.KEYCODE_BACK)
{
startActivity(new Intent(SettingsActivity.this,
MainActivity.class));
finish();
}
return false;
};

}


Thanks in advance.

Answer
public class SettingsActivity extends AppCompatActivity {

private static final String PREF_NAME = "nextage_quiz";
private static final int PRIVATE_MODE = 0;

SharedPreferences getPrefs;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);

    getPrefs = this.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
    final RelativeLayout Settingscreen = (RelativeLayout) findViewById(R.id.settingscreen);

    ImageView ImageViewBackground1 = (ImageView) findViewById(R.id.imageViewBackground1);
    ImageView ImageViewBackground2 = (ImageView) findViewById(R.id.imageViewBackground2);

    ImageViewBackground1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            Settingscreen.setBackgroundResource(R.drawable.faqicon);
            getPrefs.edit().putInt("id", R.drawable.faqicon).apply();
        }
    });

    ImageViewBackground2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            Settingscreen.setBackgroundResource(R.drawable.rateicon);
            getPrefs.edit().putInt("id", R.drawable.faqicon).apply();
        }
    });
}

MainActivity:

public class MainActivity extends AppCompatActivity {

    private static final String PREF_NAME = "nextage_quiz";
    private static final int PRIVATE_MODE = 0;

    SharedPreferences getPrefs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getPrefs = this.getSharedPreferences(PREF_NAME, PRIVATE_MODE);

        ImageView background= (ImageView) findViewById(R.id.background);
        if(getPrefs.getInt("id",0) != 0) 
           background.setBackgroundResource(getPrefs.getInt("id",0));

    }

Upadate: Use these variables

        private static final String PREF_NAME = "nextage_quiz";
        private static final int PRIVATE_MODE = 0;

        SharedPreferences getPrefs;
Comments