SharedPreference values not available between activities - android

I have 2 activities. One is the main activity. One is the Preference activity. I'm trying to change the background color of the activity based on the color selected in a RadioGroup in the Preference activity.
This is the class file for the main activity.
package com.example.mycsimodules;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ArrayAdapter;
public class ModList extends ActionBarActivity {
SharedPreferences savedData;
private String[] moduleArray = { "COMP 41600", "COMP 41620", "COMP 47330","COMP 30160", "COMP 30500", "COMP 40725", "COMP 41100", "COMP 41110" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mod_list);
savedData=getSharedPreferences("MyPrefs",0);
String colorMine=savedData.getString("color", "deflt");
TextView head=(TextView) findViewById(R.id.mymodlist);
head.setText(colorMine);
ArrayAdapter<String> moduleAdapter = new ArrayAdapter<String>(this, R.layout.activity_list_view, R.id.list1, moduleArray);
final ListView list = (ListView) findViewById(R.id.mod_list);
list.setAdapter(moduleAdapter);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mod_list, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
Intent i=new Intent(this, Preferences.class);
startActivity(i);
return true;
}
return super.onOptionsItemSelected(item);
}
This is the class file for the Preference class.
package com.example.mycsimodules;
import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class Preferences extends ActionBarActivity implements OnClickListener{
RadioGroup colorList;
Button saveButton;
SharedPreferences savedData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferences);
colorList=(RadioGroup) findViewById(R.id.colorRadioGroup);
saveButton=(Button) findViewById(R.id.bSave);
saveButton.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.preferences, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressLint("NewApi") #Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==saveButton){
RadioButton myColor=(RadioButton) findViewById(colorList.getCheckedRadioButtonId());
String colorSelected=myColor.getText().toString();
TextView label=(TextView) findViewById(R.id.the_color_is);
label.setText(colorSelected);
savedData=getSharedPreferences("MyPref", 0);
SharedPreferences.Editor editor=savedData.edit();
editor.putString("color", colorSelected);
editor.apply();
editor.commit();
}
}
}
I haven't actually done the background color part. I'm just trying to display the value in a TextView currently. But it shows only the default value all the time. What I'm trying to do is.
Click on Settings.
Click on RadioButton for color desired.
Click on Save button.
Click on back key to return to main activity.
I'm pretty new to this and might be doing something wrong. But I just can't figure out what it is.

savedData=getSharedPreferences("MyPrefs",0);
it's diferent from
savedData=getSharedPreferences("MyPref", 0);
Use the same file when you open your SharedPreferences

Related

Locating style animation for ImageView

I'm trying to create an animation for an ImageView which shows like picture below (collected on Stackoverflow).
I want to do this because I want it shows when my location is locating. I've tried to find keywords but i couldn't found anything. Thanks.
You can use Wavedrawable Library.
it is available in GitHub with sample projects.
https://github.com/Alexrs95/WaveDrawable
compile 'me.alexrs:wave-drawable:1.0.0'
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Interpolator;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import me.alexrs.wavedrawable.WaveDrawable;
public class MainActivity extends Activity {
private WaveDrawable waveDrawable;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView) findViewById(R.id.image);
waveDrawable = new WaveDrawable(Color.parseColor("#8e44ad"), 500);
imageView.setBackgroundDrawable(waveDrawable);
interpolator = new LinearInterpolator();
waveDrawable.setWaveInterpolator(interpolator);
waveDrawable.startAnimation();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

app chrashing on imageview rotation

Hi guys i am new on android developing and i am trying to rotate an imageview.
The problem comes when (obviously) i push the button and the app close itself
Code java:
package comad.exampleaaaa.vittoriodaadntico.bottiglia3;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity6 extends AppCompatActivity {
public ImageView bottiglia;
public Button gira;
#Override
protected void onCreate(Bundle savedInstanceState) {
gira=(Button)findViewById(R.id.btn_roll);
bottiglia=(ImageView)findViewById(R.id.bottiglia);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main6);
}
public void rollOnClick(View v){
RotateAnimation anim=new RotateAnimation(0,360,20,20,20,20);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(700);
bottiglia.startAnimation(anim);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_activity6, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
error log :
http://imgur.com/H2Phsvf
You call methods in wrong order:
#Override
protected void onCreate(Bundle savedInstanceState) {
gira=(Button)findViewById(R.id.btn_roll); // This
bottiglia=(ImageView)findViewById(R.id.bottiglia); // And this
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main6); // Should come after this
}
Here you search for views before you set content view. You should set your view, and only then search for widgets. Otherwise there is no view in which your widgets could be found, so both gira and bottiglia are nulls:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main6);
gira=(Button)findViewById(R.id.btn_roll);
bottiglia=(ImageView)findViewById(R.id.bottiglia);
}

How to share action bar and items on it between activities

This is probably extremely simple, but I have two activities that both need the same items in the action bar, but when I start the second activity (the first is main) the action overflow menu disappears. I understand that the action bar is unique to the activity its created in, but I want to know how to use the same action bar in various activities. Thanks
Edit: here is some code that might help with my second issue of action bar items not working out of the first activity:
package com.example.wfhsregistry;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MenuActivity extends Activity{
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.menuSettings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And here is my first class that starts my first activity:
package com.example.wfhsregistry;
import java.lang.reflect.Field;
import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.ViewConfiguration;
public class Main extends MenuActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionbar = getActionBar();
setContentView(R.layout.splash);
Thread logotimer = new Thread() {
public void run() {
try {
sleep(5000);
Intent menuIntent = new Intent(
"com.example.wfhsregistry.MENU");
startActivity(menuIntent);
} catch (Exception e) {
e.printStackTrace();
} finally {
finish();
}
}
};
logotimer.start();
}
public void getOverflowMenu() {
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class
.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
You could use inheritance.
Create a new Activity lets call it MenuActivity and place all the options logic in.
MenuActivity extends Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
...
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
...
}
And now your 2 activities just need to extend it (so they will inherit MenuActivity's logic unless you override its method without calling super)
MainActivity extends MenuActivity
And
Activity2 extends MenuActivity

How can i switch activity using Button?

How can i switch activity using Button? this is my problem:
package net.example.finals;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment implements OnClickListener {
Button bq;
Button bw;
Button be;
Button br;
Button bt;
Button by;
Button bu;
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_second,
container, false);
bq=(Button)rootView.findViewById(R.id.btn1);
bq.setOnClickListener(this);
bw=(Button)rootView.findViewById(R.id.btn2);
bw.setOnClickListener(this);
be=(Button)rootView.findViewById(R.id.btn3);
be.setOnClickListener(this);
br=(Button)rootView.findViewById(R.id.btn4);
br.setOnClickListener(this);
bt=(Button)rootView.findViewById(R.id.btn5);
bt.setOnClickListener(this);
by=(Button)rootView.findViewById(R.id.btn6);
by.setOnClickListener(this);
bu=(Button)rootView.findViewById(R.id.btn7);
bu.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//i dont know what to put here
case R.id.btn1:
if(bq.callOnClick()==true){
Intent myOwnIntent = new Intent(getActivity(), Second.class);
startActivity(myOwnIntent);
}
break;
}
}
}
Why did you use "if(bq.callOnClick()==true)" this?
Android use a lots of java code, first you need to learn java properly.
AND Google too
#Override
public void onClick(View v) {
int id = v.getId();
switch(id){
case R.id.btn1:
Intent myOwnIntent = new Intent(YourActivityName.this, Second.class);
startActivity(myOwnIntent);
break;
}
}
Try this one.
How to start new activity on button click
Start another activity by clicking a button
Android eclipse how to start a new activity on button click

Android Switch widget force close

Trying to create a switch in android with default ON state and with OnCheckedChangeListener. But application is force closing. What is wrong? please help. New in android development.
package com.tiumca2013.smarthousecontrol;
import android.app.Activity;
import android.app.Fragment;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
import android.widget.Toast;
public class MainActivity extends Activity{
private Switch switch1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switch1 = (Switch) findViewById(R.id.Switch1);
switch1.setChecked(true); // force close
switch1.setOnCheckedChangeListener(new OnCheckedChangeListener() // force close again
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(isChecked)
Toast.makeText(getApplicationContext(),"switch 1 ON",Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(),"switch 1 OFF",Toast.LENGTH_SHORT).show();
}
});
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}

Categories

Resources