I'm new to android develop. i just stated this week.
i dont know if im asking stupid question but i want to learn.
i have created an app just click a button change the background color.
my layout is this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.seluhadu.colorpicker.MainActivity">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="136dp"
tools:layout_editor_absoluteY="1dp"
android:elevation="8dp"
android:layout_alignBaseline="#+id/button3"
android:layout_alignBottom="#+id/button3"
android:layout_alignParentEnd="true"
android:layout_marginEnd="52dp"/>
</RelativeLayout>
and i have a class "java" like this
public class MainActivity extends AppCompatActivity {
Button button;
RelativeLayout relativeLayout;
Integer[] colors = {
R.color.color1,
R.color.color2,
R.color.color3,
R.color.color4,
R.color.color5,
R.color.color6,
R.color.color7,
R.color.color8,
R.color.color9,
R.color.color10,
R.color.color11,
R.color.color12,
R.color.color13,
R.color.color14,
R.color.color15,
R.color.color16,
R.color.color17,
R.color.color18,
R.color.color19,
R.color.color20
};
Random random;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
random = new Random();
relativeLayout = (RelativeLayout) findViewById(R.id.background);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
relativeLayout.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),
colors[random.nextInt(colors.length)]));
}
});
}
it just change the background color random.
when it change the color if i rotat the screen it change to default back.
my question is how to make a onSaveInstanceState and onRestoreInstanceState method to save the change color even if i reopen the app to stay the color that i change.
OR how can i do like that in other way?
thank you for your help!
When your activity is recreated after it was previously destroyed, you can recover your saved state from the Bundle that the system
passes your activity. Both the onCreate() and onRestoreInstanceState()
callback methods receive the same Bundle that contains the instance
state information.
Because the onCreate() method is called whether the system is creating
a new instance of your activity or recreating a previous one, you must
check whether the state Bundle is null before you attempt to read it.
If it is null, then the system is creating a new instance of the
activity, instead of restoring a previous one that was destroyed.
public class MainActivity extends AppCompatActivity {
Button button;
RelativeLayout relativeLayout;
Integer[] colors = {
R.color.color1,
R.color.color2,
R.color.color3,
R.color.color4,
R.color.color5,
R.color.color6,
R.color.color7,
R.color.color8,
R.color.color9,
R.color.color10,
R.color.color11,
R.color.color12,
R.color.color13,
R.color.color14,
R.color.color15,
R.color.color16,
R.color.color17,
R.color.color18,
R.color.color19,
R.color.color20
};
Random random;
Boolean backgroundChanged = false;
int colorIndex;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore background color from saved state
backgroundChanged = savedInstanceState.getBoolean("backgroundChanged", false);
colorIndex = savedInstanceState.getInt(colorIndex, 0);
}
random = new Random();
relativeLayout = (RelativeLayout) findViewById(R.id.background);
if(backgroundChanged) {
relativeLayout.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), colors[colorIndex]));
}
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
backgroundChanged = true;
colorIndex = random.nextInt(colors.length);
relativeLayout.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), colors[colorIndex]));
}
});
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putBoolean("backgroundChanged", backgroundChanged);
if(backgroundChanged) {
savedInstanceState.putInt("colorIndex", colorIndex);
}
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
Update
Here's my code below, plugged into your posted example:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
random = new Random();
relativeLayout = (RelativeLayout) findViewById(R.id.background);
button = (Button) findViewById(R.id.button);
SharedPreferences settings = getSharedPreferences("myPrefs", 0);
int colorIndex = settings.getInt("colorIndex", 0);
updateBackground(colorIndex);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int colorIndex = random.nextInt(colors.length);
SharedPreferences settings = getSharedPreferences("myPrefs", 0);
settings.edit().putInt("colorIndex", colorIndex).apply();
updateBackground(colorIndex);
}
});
}
private void updateBackground(int colorIndex) {
relativeLayout.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), colors[colorIndex]));
}
If you have to store the color even through app restarts, you should use SharedPreferences instead of using onSaveInstanceState().
In your onClick(), when you generate the random index for your color array, write that value to your SharedPreferences object. In onCreate() when you set everything up, read the value out of the SharedPreferences.
To write:
int colorIndex = random.nextInt(colors.length);
SharedPreferences settings = getSharedPreferences("myPrefs", 0);
settings.edit().putInt("colorIndex", colorIndex).apply();
To read:
SharedPreferences settings = getSharedPreferences("myPrefs", 0);
int colorIndex = settings.getInt("colorIndex", 0);
Related
I have an application that allows my users to customize the background of my application using buttons. My app works like this: First it will lead them to my main activity, where there is a button that they can press to customize the background. When they pressed that button, it will lead them to a dialog fragment that will give users an option to choose which background image they want. I'm able to change my background, however, the shared preference is not functioning correctly. When I close my app and open it, it changes back to my default background, but when I press the dialog fragment button, it then updates the background to whatever they chose.
So basically, the background only updates when I open the button that offers the background images.
I'm not sure if I explained it well so here is a gif of my
problem
The background only updates when I press the terrain button, does anyone have an idea to fix this? I'm still very new to android and java so I'm not sure if I'm just missing something...
PopupTheme.java
public class PopupTheme extends DialogFragment implements View.OnClickListener {
private ImageButton btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10;
private static final String BG_NAME = "bgName";
private static final String BG_KEY = "bg";
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.activity_popup_theme, container, false);
btn1 = view.findViewById(R.id.btn1);
btn2 = view.findViewById(R.id.btn2);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
if (getBackground() != R.drawable.bgscreen1 ){
MainActivity.mainLayout.setBackgroundResource(getBackground());
}
return view;
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn1:
MainActivity.mainLayout.setBackgroundResource(R.drawable.bgscreen1);
Toast.makeText(getContext(), "Clicked", Toast.LENGTH_SHORT).show();
storeBackground(R.drawable.bgscreen1);
break;
case R.id.btn2:
MainActivity.mainLayout.setBackgroundResource(R.drawable.bgscreen2);
Toast.makeText(getContext(), "Clicked", Toast.LENGTH_SHORT).show();
storeBackground(R.drawable.bgscreen2);
break;
}
}
public void storeBackground(int background) {
SharedPreferences sharedPreferences = getActivity().getSharedPreferences(BG_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit(); //accessing the shared pref
editor.putInt(BG_KEY, background);
editor.apply();
}
// getting the background
public int getBackground() {
SharedPreferences sharedPreferences = getActivity().getSharedPreferences(BG_NAME, Context.MODE_PRIVATE);
int selectedBG = sharedPreferences.getInt(BG_KEY, R.drawable.bgscreen1);
return selectedBG;
}
}
Main Activity.java
public class MainActivity extends AppCompatActivity {
private Button btnWatch, btnReadStory, btnFavorites, btnAbout, btnListen;
private ImageButton btnTheme;
static ConstraintLayout mainLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnTheme = findViewById(R.id.btnTheme);
mainLayout = findViewById(R.id.layoutMain);
btnTheme.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PopupTheme popupTheme = new PopupTheme();
popupTheme.show(getSupportFragmentManager(), "Popup Theme");
}
});
}
}
The problem is that you update background of your MainActivity only if you opened the PopupTheme dialog, So you need to move getBackground and update code from PopupTheme to the onCreate method on MainActivity so the code will executed when the user launch the app
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnTheme = findViewById(R.id.btnTheme);
mainLayout = findViewById(R.id.layoutMain);
int background = getBackground();
if (background != R.drawable.bgscreen1 ){
mainLayout.setBackgroundResource(background);
}
// Other code on onCreate
}
// Move getBackground method here from PopupTheme
Note: you can use -1 as default value of background and check if it's -1 that mean the user use the default background
I am trying to save my programmatically created CardView and TextView using OnSaveInstanceState and OnRestoreInstanceState as in the sample code.
While debugging, I notice that values are saved but when I quit the app and reopen it, nothing is being restored.
What am I doing wrong?
**#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString("key", userInput.getText().toString());
savedInstanceState.putInt("LayoutId", mConstraintLayout.getId());
super.onSaveInstanceState(savedInstanceState);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
savedInstanceState.getString("key");
savedInstanceState.getInt("LayoutId");
}**
This are the methods I am using after 'OnCreate'
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = getApplicationContext();
mConstraintLayout = findViewById(R.id.top_left);
addButton = findViewById(R.id.add);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View popup = findViewById(R.id.popup);
popup.setVisibility(View.VISIBLE);
}
});
mButton = (Button) findViewById(R.id.create);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final CardView sticker = new CardView(mContext);
CardView.LayoutParams params = new CardView.LayoutParams(
CardView.LayoutParams.WRAP_CONTENT,
CardView.LayoutParams.WRAP_CONTENT
);
params.height=500;
params.width=500;
CheckBox privateCalendar = findViewById(R.id.checkPrivate);
CheckBox workCalendar = findViewById(R.id.checkWork);
CheckBox holidayCalendar = findViewById(R.id.checkHoliday);
if (privateCalendar.isChecked()){
sticker.setCardBackgroundColor(Color.parseColor("#FFCC00"));
}
else if (workCalendar.isChecked()){
sticker.setCardBackgroundColor(Color.parseColor("#FF8080"));
}
else if (holidayCalendar.isChecked()){
sticker.setCardBackgroundColor(Color.parseColor("#66B3FF"));
}
else{
sticker.setCardBackgroundColor(Color.parseColor("#FFCC00"));
}
sticker.setId(CardView.generateViewId());
sticker.getId();
sticker.setTag(CARD_VIEW_TAG);
sticker.setLayoutParams(params);
sticker.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
ClipData.Item item = new ClipData.Item((CharSequence) sticker.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData data = new ClipData(sticker.getTag().toString(), mimeTypes, item);
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(sticker);
sticker.startDrag(data
, shadowBuilder
, sticker
, 0
);
sticker.setVisibility(View.INVISIBLE);
return true;
}
});
TextView tv = new TextView(mContext);
tv.setLayoutParams(params);
tv.setBackgroundColor(Color.TRANSPARENT);
tv.setGravity(Gravity.CENTER);
userInput = findViewById(R.id.editText);
tv.setText(userInput.getText());
tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
tv.setTextColor(Color.BLACK);
sticker.addView(tv);
mConstraintLayout.addView(sticker);
View popup = findViewById(R.id.popup);
popup.setVisibility(View.GONE);
}
});
}
SavedInstanceState and RestoreInstanceState will work if activity is recreated due to config change like orientation change or android system killed that activity due to memory issue, Then only savedInstanceState and restoreInstanceState comes in role.
If you finish activity by yourself and start activity again then there is no use of these methods.
Try to save data in SharedPreference instead savedInstanceState.
SharedPreferences pref =
getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();
For Storing data:-
editor.putString("key", userInput.getText().toString());
editor.putInt("LayoutId", mConstraintLayout.getId());
editor.apply();
For retriving data:-
editor.getString("key", null); // getting String
editor.getInt("LayoutId", 0); // getting Integer
First of all if by "close the app" you mean destroy it, this will never work for you.
Second of all in onRestoreInstanceState you don't do anything with those values, you simply read them but don't store them anywhere.
If you mean to recreate the old form after switching back and forth between apps, with your app being destroyed in the background then, you need to only set values in onSaveInstanceState like you did, and in onCreate(Bundle savedInstance), you should do something like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstance == null) {
//read values from getIntent()
key = getIntent().getString("key);
LayoutId = getIntent().getInt("LayoutId");
} else {
//read values from savedInstanceState
key = savedInstanceState.getString("key");
LayoutId = savedInstanceState.getInt("LayoutId");
}
}
Trying to create notes application. I am trying to add dynamic text view after button click and it should be added to layout, one after another. In my code, only one text view is getting added successfully but from second time, app got crashed. Please help
XML Code:
<EditText
android:id="#+id/text_thingsToDo"
android:layout_width="#dimen/ThingsToDo_EditText_Width"
android:layout_height="#dimen/ThingsToDo_EditText_Height"
android:layout_margin="#dimen/Standardize_Margin"
android:background="#color/ThingsToDo_EditText_Color"/>
<Button
android:id="#+id/save_thingsToDo"
android:layout_toRightOf="#+id/text_thingsToDo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ThingsToDo_Save_Button_Text"
android:layout_marginRight="#dimen/Standardize_Margin"
android:layout_marginTop="#dimen/Standardize_Margin"
android:layout_marginBottom="#dimen/Standardize_Margin"
android:background="#color/ThingsToDo_Save_Button"
android:textColor="#color/White"
android:layout_centerVertical="true"/>
Java Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_things_to_do);
final EditText editText = (EditText) findViewById(R.id.text_thingsToDo);
Button button = (Button) findViewById(R.id.save_thingsToDo);
final TextView notesView = new TextView(this);
final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.rootView_ThingsToDo);
final ArrayList<String> arrayList = new ArrayList<String>();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
notesView.setText(editText.getText().toString());
linearLayout.addView(notesView);
}
});
}
Perhaps create a new TextView every time you press the button? Try to declare the TextView as a class member and then in your OnClickListener do this:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
notesView = new TextView(this);
notesView.setText(editText.getText().toString());
linearLayout.addView(notesView);
}
});
Hopefully this will fix your problem!
I'm quite new to Android Prorgramming and have a problem understandig SharedPreferences.
I have a Main Activity and a Settings Activity.
I want to be able to change a SharedPreference (here called firstWaitTime) in the Settings Activity and work with it in the Main Activity.
I can Set the new value in the settings, but in the Main Activity i only see the new value when i close and reopen the app.
I Think i would need a OnPrefrenceChanged Listender but i have no idea how to implement it...
The Main one looks like this:
public class MainActivity extends ActionBarActivity {
//Radio Button
private int stufe;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences settings = this.getSharedPreferences("settings", Context.MODE_PRIVATE);
int firstWaitTime = settings.getInt("firstWaitTime", 12);
int secondWaitTime = settings.getInt("secondWaitTime", 8);
int thirdWaitTime = settings.getInt("thirdWaitTime", 6);
//TEST
final TextView test = (TextView) findViewById(R.id.test);
test.setText("" + firstWaitTime);
}
The Settings Activity looks like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
final SharedPreferences settings = this.getSharedPreferences("settings", MODE_PRIVATE);
//BUTTON
final EditText edit1 = (EditText) findViewById(R.id.edit1);
Button save=(Button) findViewById(R.id.savebtn);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//prefs = getSharedPreferences(prefName, MODE_PRIVATE);
//SharedPreferences.Editor editor = prefs.edit();
SharedPreferences.Editor editor = settings.edit();
//---save the values in the EditText view to preferences---
editor.putInt("firstWaitTime", Integer.parseInt(edit1.getText().toString()));
//---saves the values---
editor.commit();
Toast.makeText(getBaseContext(), "Saved", Toast.LENGTH_SHORT).show();
}
});
}
What is the best way to achieve this?
onCreate() will not be called as Android hasn't killed the Activity. Try putting your code in onResume() instead. For more info read up on the Android Activity lifecycle.
So when I try and run my code in the emulator, the app background pops up then closes giving me the dialog, "Unfortunately, Callisto has stopped working"
I have no idea what is wrong other than it gives me a null pointer exception (line 49) but there is nothing at line 49
XML
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/callisto_heading" />
<Button
android:id="#+id/bClasses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Classes"
android:layout_gravity="center"
android:onClick=""
/>
<Button
android:id="#+id/bSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Settings"
android:layout_gravity="center"
android:onClick=""
/>
</LinearLayout>
Java
package android.callisto.com;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
public class CallistoActivity extends Activity {
/** Called when the activity is first created. */
Button settings_button;
Button classes_button;
Button home_button;
CheckBox notif_cb;
CheckBox math_cb;
CheckBox science_cb;
CheckBox ss_cb;
CheckBox english_cb;
CheckBox language_cb;
boolean notif,math,science,english,ss,language;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//settings layout
notif_cb = (CheckBox) findViewById(R.id.cbNotif);
math_cb = (CheckBox) findViewById(R.id.cbMath);
science_cb = (CheckBox) findViewById(R.id.cbScience);
ss_cb = (CheckBox) findViewById(R.id.cbSS);
english_cb = (CheckBox) findViewById(R.id.cbEnglish);
language_cb = (CheckBox) findViewById(R.id.cbLang);
home_button = (Button) findViewById(R.id.bHome);
notif = true;
math = true;
science = true;
english = true;
ss = true;
language = true;
home_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
setContentView(R.layout.main);
}
});
//notifications
//main layout
settings_button = (Button) findViewById(R.id.bSettings);
classes_button = (Button) findViewById(R.id.bClasses);
settings_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
setContentView(R.layout.settings);
}
});
}
}
FYI the app was loading last night.. Thanks for any help and please remember I am new to Android programming. Thank you.
the problem is here
home_button = (Button) findViewById(R.id.bHome);
in layout file there is no bHome
The problem probabaly is, that you are using the setContentView() for displaying different screens. You should not use this method for changing screens,you should use a different activitys for this.
Usually, you only set the contentView ONCE in the oncreate, per activity.
Your R.layout.main layout probabaly does not contain the 'home' button, but the r.layout.settings layout does.
First you are loading the main layout at this line: setContentView(R.layout.main); But since this layout file does NOT contain the home button, findViewById(R.id.bHome); will return null. After that, calling a method on this returned value, home_button.setOnClickListener(); in your case, will cause a NullPointerException.
What you should do is the following:
Create an activity for your main layout:
public class CallistoMainActivity extends Activity {
/** Called when the activity is first created. */
Button settings_button;
Button classes_button;
Button home_button;
CheckBox notif_cb;
CheckBox math_cb;
CheckBox science_cb;
CheckBox ss_cb;
CheckBox english_cb;
CheckBox language_cb;
boolean notif,math,science,english,ss,language;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//main layout
settings_button = (Button) findViewById(R.id.bSettings);
classes_button = (Button) findViewById(R.id.bClasses);
settings_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(getApplicationContext(),
CallistoSettingsActivity.class));
}
});
}
And an activity for your Settings layout:
public class CallistoSettingsActivity extends Activity {
Button home_button;
CheckBox notif_cb;
CheckBox math_cb;
CheckBox science_cb;
CheckBox ss_cb;
CheckBox english_cb;
CheckBox language_cb;
boolean notif,math,science,english,ss,language;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
//settings layout
notif_cb = (CheckBox) findViewById(R.id.cbNotif);
math_cb = (CheckBox) findViewById(R.id.cbMath);
science_cb = (CheckBox) findViewById(R.id.cbScience);
ss_cb = (CheckBox) findViewById(R.id.cbSS);
english_cb = (CheckBox) findViewById(R.id.cbEnglish);
language_cb = (CheckBox) findViewById(R.id.cbLang);
home_button = (Button) findViewById(R.id.bHome);
notif = true;
math = true;
science = true;
english = true;
ss = true;
language = true;
home_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
//You could either use finish() to finish the current activity, and return to the previous CallistoMainActivity on the stack.
//Or you could simply start the main activity again by using the startActivity() method you see in the onclicklistener on the settings button.
}
});
}
What happens now, is when you click the settings button in the main activity, the settings activity will be shown. When you click the home button in the settings activity, the settings activity will be finished, and the user will be returned to the previous activity on the stack; which is the main activity.
Also do not forget to define the settingsactivity in your AndroidManifest.xml
EDIT: Another thing you should note, that if there is an error 'at line x' but there is nothing at line x in your code, then the code running on your Android device, is not the same as the code you are looking at in your editor. So that might be the reason it was running last night, but not anymore.