0

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.

SripadRaj
  • 1,687
  • 2
  • 22
  • 33
narispillai
  • 77
  • 1
  • 10

2 Answers2

0

You should do that using Preferences. see the link for more details... https://developer.android.com/guide/topics/ui/settings.html

0
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;
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
  • I'm getting "error: cannot find symbol variable PREF_NAME & PRIVATE_MODE". What is the purpose of these two? and do we need to add this part of the code(MainActivity) to each and every other activity? Aren't there any other way to set the background using an XML? – narispillai Aug 29 '16 at 10:01
  • replace `PREF_NAME` it with "myprefs" – Sohail Zahid Aug 29 '16 at 10:02
  • Aren't compiling, though. C:\Users\..app\src\main\java\com\example\naris\auin\MainActivity.java Error:(80, 46) error: cannot find symbol variable PREF_NAME Error:(80, 57) error: cannot find symbol variable PRIVATE_MODE C:\Users\..app\src\main\java\com\example\naris\auin\SettingsActivity.java Error:(21, 46) error: cannot find symbol variable PREF_NAME Error:(21, 57) error: cannot find symbol variable PRIVATE_MODE Note: Some input files use or override a deprecated API. Note: Recompile with -Xlint:deprecation for details. :app:compileDebugJavaWithJavac FAILED – narispillai Aug 29 '16 at 10:15
  • is your issue fixed? – Sohail Zahid Aug 29 '16 at 10:51
  • private static final String PREF_NAME = "myprefs"; private static final int PRIVATE_MODE = 0; use these variables. – Sohail Zahid Aug 29 '16 at 10:52
  • Oh, my god!!It's working!Thank you so much.Been hitting my head on the keyboard for 2 days. Thank you so much. – narispillai Aug 29 '16 at 11:10