OnClick Not Firing? - java

I'm new to android and having trouble getting some simple things going!
Here is some basic code:
MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Lets go ahead and set our buttons up.
ServerButton = (Button)this.findViewById(R.id.ServerButton);
ClientButton = (Button)this.findViewById(R.id.ClientButton);
BTServer = new BluetoothServerService(this, mHandler);
}
#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;
}
public void ServerButtonClick(View v){
Log.d("BLUETOOTH MAIN ACTIVITY", "It got here");
BTServer.start();
}
private final Handler mHandler = new Handler(){
#Override
public void handleMessage(Message msg){
switch (msg.what){
case HANDLER_CHANGE_SERVER_STATUS:
switch(msg.arg1){
case SERVER_STATUS_OFF:
CURRENT_SERVER_STATUS = SERVER_STATUS_OFF;
//We should probably add some logging, and the change to the button
break;
case SERVER_STATUS_ON:
CURRENT_SERVER_STATUS = SERVER_STATUS_ON;
//Again lets add some logging, etc.
break;
case SERVER_STATUS_ON_CONNECTED:
CURRENT_SERVER_STATUS = SERVER_STATUS_ON_CONNECTED;
//Again lets add some logging, etc.
break;
}
}
return;
}
};
My XML Looks like this:
<Button
android:id="#+id/ServerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="146dp"
android:layout_marginLeft="64dp"
android:text="Connect to Server" />
<Button
android:id="#+id/ClientButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:text="Start Server"
android:onClick="ServerButtonClick"/>
In my logs, I see the logs saying it creates the BTServer object, but I can't get any logs to show up when I click the button.
Any suggestions?
Thanks!

define a click action for ClientButton inside onCreate method
ClientButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
also for ServerButton define a click action like the above as
ServerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});

For me, I find it's best to have your Activity implement android.view.View.OnClickListener, then set your Button's onClickListener to your Activity.
Here's an example Activity class:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Home extends Activity implements OnClickListener
{
#Override
public void onCreate (Bundle savedInstanceState)
{
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
Button serverButton = (Button)this.findViewById (R.id.ServerButton);
serverButton.setOnClickListener (this);
Button clientButton = (Button)this.findViewById (R.id.ClientButton);
clientButton.setOnClickListener (this);
}
#Override
public void onClick(View button)
{
int buttonId = button.getId ();
if (buttonId == R.id.ServerButton)
{
// Do server stuff.
}
if (buttonId == R.id.ClientButton)
{
// Do client stuff.
}
}
}

In Android when you define the android:onClick="someMethod" attribute it only implements the OnClickListener for you.
So it is your responsibility to define the onCLickListener Method and call your ServerButtonClick method from that.
Add this method in your Activity OnCreate Method. It would work.
ServerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ServerButtonClick();
}
});
Also, android:onClick method works only if API level > 4 onwards. so if you're targeting Minimum SDK version < 1.6 please do not use it

Related

App button as keyboard press

I'm trying to map a button in my fragment layout as a keypress on a keyboard.
This is my button press:
Button down = (Button)(myView.findViewById(R.id.btnDOWN));
down.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.i(TAG, "Down pressed");
return false;
}
});
I wasn't able to find any library which would allow me to do so, all I've found was reverse case, when a keyboard or controller press would be sent to device.
What I'm trying to generally do here, is an app that acts like a controller for RetroPie by using the phone as a bluetooth keyboard, with only the keys necessary for a specific controller.
The method you're using is not correct. you should use the setOnClickListener:
Here an example from Android official guide:
public class MyActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final Button button = findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
}
}
If you're looking for continuos aka long click, you should change it to setOnLongClickListener:
down.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return true;
}
});
It wasn't hard to find in this case the solution on this site

how to use onClickListener with my own class

I have created my own class to mimick the Snackbar, lets call it CustomSnackbar. What I am trying to achieve is to customize the snackbar and be able to call CustomSnackbar from my main activity and for the usage to be very similar to calling the standard Snackbar. For the purpose of demonstrating my example without all the bulk code, here is my CustomSnackbar class:
package com.wizzkidd.myapp.helpers;
import android.content.Context;
import android.support.design.widget.Snackbar;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class CustomSnackbar {
Context _context;
Snackbar snackbar;
public CustomSnackbar(Context context) {
this._context = context;
}
public void make(View view, CharSequence text, int duration) {
snackbar = Snackbar.make(view, "", duration);
Snackbar.SnackbarLayout snackbarLayout = (Snackbar.SnackbarLayout) snackbar.getView();
TextView textView = (TextView) snackbarLayout.findViewById(android.support.design.R.id.snackbar_text);
textView.setVisibility(View.INVISIBLE); //hide the default snackbar textview
//Create my own textview instead
TextView myTextView = new TextView(_context);
myTextView.setText(text);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); //Create layout params for some text
myTextView.setLayoutParams(params); //Apply the text layout params
snackbarLayout.addView(myTextView); //Add my text to the main snackbar layout. (Other widgets will also be added)
}
public void setAction(CharSequence text) {
snackbar.setAction(text, new View.OnClickListener() {
#Override
public void onClick(View view) {
//do something
Log.v("TAG", "You clicked the action");
}
});
}
public void show() {
snackbar.show();
}
}
In my MainActivity, I am using the class like this:
CustomSnackbar customSnackbar = new CustomSnackbar(activity);
customSnackbar.make(view, "This is my snackbar", Snackbar.LENGTH_INDEFINITE);
customSnackbar.setAction("HIDE");
customSnackbar.show();
You can see that I am using my .setAction method to pass a string/charsequence but I am unsure how to handle the onClickListener in the same call instead of handling the onClickListener inside the class
Please ignore the fact that the class may appear pointless (but this is because I have simplified it for the purpose of this question). I'm not sure that I am creating this class correctly, so any additional advice or suggestions would be appreciated. Thanks.
declare somewhere else your OnClickListener, e.g. in Activity in which you are calling your methods, and pass it to your class
final View.OnClickListener ocl = new View.OnClickListener() {
#Override
public void onClick(View view) {
//do something
Log.v("TAG", "You clicked the action");
}
}
CustomSnackbar customSnackbar = new CustomSnackbar(activity);
customSnackbar.make(view, "This is my snackbar", Snackbar.LENGTH_INDEFINITE);
customSnackbar.setAction("HIDE", ocl);
customSnackbar.show();
and inside your custom class:
public void setAction(CharSequence text, final View.OnClickListener ocl) {
snackbar.setAction(text, ocl);
}
now inside OnClickListener you may call methods from Activity
you have to provide it as parameter to your setAction method. E.g.
public void setAction(CharSequence text, final View.OnClickListener listener) {
and either pass the provided instance, or proxy the call to the other object
Do a class extends Snackbar.
Then addMouseListener(classWithExtendedSnackbar)
I am exactly not clear about your question. But,if you want to make a custom Snackbar which will display all your message, and give functionality on click of it. Then you can try this code.
just call this method to make a snackbar.
//Here I am sending one as code
showMessage("Snackbar Opened","Close",1)
//send different code based on which you can set something to be done, or identify a button click
private void showMessage(String msg, String action_name, final int code) {
progress.cancel();
final Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), msg, Snackbar.LENGTH_INDEFINITE);
snackbar.setAction(action_name, new View.OnClickListener() {
#Override
public void onClick(View v) {
clearDataForListView();
if (code == 1) {
//do something if code is one/if a particular button is clicked
//you can dismiss anwhere else also.
snackbar.dismiss();
} else if (code == 2) {
//do something if another button is pressed
snackbar.dismiss();
}
}
});
snackbar.show();
}

Android Studio 'Run app' dialogue doesn't appear in one project but will in another

I'm toying with Android Studio making a very simple very stupid app to learn about saving key preferences and I ran into a weird obstacle. I'll try to provide as much as I can since it may be hard to reproduce this bug but honestly both apps I'm running are super basic and there no compile errors.
Specs:
No emulator, I'm running a Samsung Galaxy Tablet. Windows 7, Android Studio 1.2, Gradle 2.2.1.
In the question title, I mean that I have a project named Kitty (pretty much hello world and a button). I click Run->'Run app'->(Dialogue box opens)->OK->Within moments the app launches on my tablet.
^^^THIS IS THE BEAUTIFUL SCREEN I WANT TO SEE ON Sharedpreferences, but it's only on kitty.
Now I started another project called SharedPreferences (gist: two checkboxes ask you "do you like chocolate" and "do you like luigi" and you check one none or both and press save. Two textviews underneath will update to say if you like those things and even later if you reopen the app the textviews will remember Chocolate Luigi preferences). It is just a main_activity.
I don't think I changed any settings or project preferences between the two and neither give me an error. MainActivity.java OUTDATED ORIGINAL SEE EDIT:
package gaga.sharedpreferences;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
public final class setup extends MainActivity {
public void setup () {
//Nothing to see here!
}
// Define the File of Prefs; created if nonexistent
public static final String PREFS_NAME = "MyPrefsFile";
// Start up
#Override
public void onCreate(Bundle state) {
super.onCreate(state);
// Restore preferences on Startup
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean Chocolate = settings.getBoolean("checkChocolate", false);
boolean Luigi = settings.getBoolean("checkLuigi", false);
// Function set to Whatever
// setSilent(silent);
/* Note:
* CheckedTextView and CheckBox::isChecked()
* CheckBox::setChecked()
* */
CheckBox checkHandleChocolate = (CheckBox) findViewById(R.id.checkChocolate);
CheckBox checkHandleLuigi = (CheckBox) findViewById(R.id.checkLuigi);
// What was the preference? On Start set it to the bool it left off in
checkHandleChocolate.setChecked(Chocolate);
checkHandleLuigi.setChecked(Luigi);
// Change report text on Start
TextView buttonHandleChocolate = (TextView) findViewById(R.id.chocolate);
TextView buttonHandleLuigi = (TextView) findViewById(R.id.luigi);
if(Chocolate)
buttonHandleChocolate.setText("I do prefer Chocolate");
else
buttonHandleChocolate.setText("I do not prefer Chocolate");
if(Luigi)
buttonHandleLuigi.setText("I do prefer Luigi");
else
buttonHandleLuigi.setText("I do not prefer Luigi");
}
public void saveChocolate(Boolean c) {
// All objects from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("Chocolate", c);
// Commit the edits
editor.commit();
}
public void saveLuigi(Boolean l) {
// All objects from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("Chocolate", l);
// Commit the edits
editor.commit();
}
}
#Override
protected void onStop(){
super.onStop();
// Objects are from android.context.Context
//Normally I'd put the edit commits here, but that's not true
}
// Clicks on Done
public void userDone (View view) {
// View is which widget
boolean checked = ((CheckBox) view).isChecked();
// Which checkbox was clicked
switch(view.getId()) {
case R.id.checkChocolate:
setup instance1 = new setup();
instance1.saveChocolate(checked);
// No break; continue along
case R.id.checkLuigi:
setup instance2 = new setup();
instance2.saveLuigi(checked);
break;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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, 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);
}
}
Red parts of logcat:
06-02 20:49:57.245 25557-25557/? I/SDP.PUB_CRYPTOD﹕ Starting
06-02 20:49:57.245 25557-25557/? I/SDP.PUB_CRYPTOD﹕ Socket created with fd:-1
06-02 20:49:57.245 25557-25557/? E/SDP.PUB_CRYPTOD﹕ Failed to open the netlink socket with error: Protocol not supported
06-02 20:49:57.245 25557-25557/? E/SDP.PUB_CRYPTOD﹕ Exiting
06-02 20:49:59.995 2866-3012/? V/AlarmManager﹕ waitForAlarm result :8
06-02 20:50:02.280 25633-25633/? I/SDP.PUB_CRYPTOD﹕ Starting
06-02 20:50:02.280 25633-25633/? I/SDP.PUB_CRYPTOD﹕ Socket created with fd:-1
06-02 20:50:02.280 25633-25633/? E/SDP.PUB_CRYPTOD﹕ Failed to open the netlink socket with error: Protocol not supported
06-02 20:50:02.280 25633-25633/? E/SDP.PUB_CRYPTOD﹕ Exiting
Thanks for any help. I haven't seen this issue while prowling the internet so it might be excessively noob.
EDIT: Rewritten with the only onCreate in the larger MainActivity class
package gaga.sharedpreferences;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
public final class setup extends MainActivity {
public void setup () {
//Nothing to see here!
}
// Define the File of Prefs; created if nonexistent
public static final String PREFS_NAME = "MyPrefsFile";
// Start up
public void onCreateSubclass() {
// super.onCreate(state);
// Restore preferences on Startup
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean Chocolate = settings.getBoolean("checkChocolate", false);
boolean Luigi = settings.getBoolean("checkLuigi", false);
// Function set to Whatever
// setSilent(silent);
/* Note:
* CheckedTextView and CheckBox::isChecked()
* CheckBox::setChecked()
* */
CheckBox checkHandleChocolate = (CheckBox) findViewById(R.id.checkChocolate);
CheckBox checkHandleLuigi = (CheckBox) findViewById(R.id.checkLuigi);
// What was the preference? On Start set it to the bool it left off in
checkHandleChocolate.setChecked(Chocolate);
checkHandleLuigi.setChecked(Luigi);
// Change report text on Start
TextView buttonHandleChocolate = (TextView) findViewById(R.id.chocolate);
TextView buttonHandleLuigi = (TextView) findViewById(R.id.luigi);
if(Chocolate)
buttonHandleChocolate.setText("I do prefer Chocolate");
else
buttonHandleChocolate.setText("I do not prefer Chocolate");
if(Luigi)
buttonHandleLuigi.setText("I do prefer Luigi");
else
buttonHandleLuigi.setText("I do not prefer Luigi");
}
public void saveChocolate(Boolean c) {
// All objects from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("Chocolate", c);
// Commit the edits
editor.commit();
}
public void saveLuigi(Boolean l) {
// All objects from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("Chocolate", l);
// Commit the edits
editor.commit();
}
}
#Override
protected void onStop(){
super.onStop();
// Objects are from android.context.Context
//Normally I'd put the edit commits here, but that's not true
}
// Clicks on Done
public void userDone (View view) {
// View is which widget
boolean checked = ((CheckBox) view).isChecked();
// Which checkbox was clicked
switch(view.getId()) {
case R.id.checkChocolate:
setup instance1 = new setup();
instance1.saveChocolate(checked);
// No break; continue along
case R.id.checkLuigi:
setup instance2 = new setup();
instance2.saveLuigi(checked);
break;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setup startInstance = new setup();
startInstance.onCreateSubclass();
}
#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, 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);
}
}
In Android Studio you need create a Run Configuration in your project.
Go to this link to left run icon
And click on Edit Configuration, after in the windows configure like this:
And save it, for test click run icon.
It appears that you have two onCreate methods. Try removing the second one and running it again.

How to enter two activity links in another activity?

I know my question might be stupid but I am new in Android App development and the Eclipse things but reached to e problem that can't find solution in internet.
I am making multi-activity application and reached to a point where when i have two buttons in one of the activities and want each of them to lead to different other activities, the application crashes. When I lead them both to one activity, everything is fine. Here is my code and hope really my question not to be so stupid as I am thinking.
public class Home extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Button myButton = (Button) findViewById(R.id.tables);
myButton.setOnClickListener(goToTables);
Button mySecondButton = (Button) findViewById(R.id.reservations);
mySecondButton.setOnClickListener(goToMenu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
private OnClickListener goToTables = new OnClickListener(){
#Override
public void onClick(View v) {
doButton();
}};
private void doButton()
{
startActivity(new Intent(this, Tables.class));
}
private OnClickListener goToMenu = new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
doSecondButton();
}};
private void doSecondButton()
{
startActivity(new Intent(this, Menu.class));
}
}
The goToTables works perfectly but I am missing something important to change in goToMenu. My other activities are: Tables and Menu. Can somebody please tell me where I am wrong? Thanks in advance!
android:onClick="dobutton" try adding this in your button tag in xml code rather then using onclicklistner.
Try changing the name of your Menu activity or add the full name path of Menu.class in your intent, eg. com.myapp.Menu.class

New Preferences not loading when back button is pressed

I have this preferences class (below) that saves two ListPreferences, but if the ListPreferences are changed and the back button is pressed, the changes don't take affect unless the application is restarted. Did I miss something? Have been looking everywhere, but just can't seem to find an answer the fits or works. Please help.
public class Preferences extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
#Override
public void onPause() {
super.onPause();
}
#Override
public void onResume() {
super.onResume();
}
}
Application Code
public class Quotes extends Activity implements OnClickListener {
ProgressDialog dialog;
private WebView webview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String q = SP.getString("appViewType","http://www.google.com");
String c = SP.getString("appRefreshRate","20");
webview = (WebView) findViewById(R.id.scroll);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new QuotesWebView(this));
webview.loadUrl(q);
ScheduledExecutorService timer = Executors.newSingleThreadScheduledExecutor();
timer.scheduleAtFixedRate(new Runnable() {
#Override
public void run() {
webview.reload();
}
}, 10, Long.parseLong(c),TimeUnit.SECONDS);
findViewById(R.id.refresh).setOnClickListener(this);
}
#Override
public void onPause(){
super.onPause();
}
#Override
public void onResume(){
super.onResume();
}
public void onClick(View v){
switch(v.getId()){
case R.id.refresh:
webview.reload();
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItem about = menu.getItem(0);
about.setIntent(new Intent(this, About.class));
MenuItem preferences = menu.getItem(1);
preferences.setIntent(new Intent(this, Preferences.class));
return true;
}
}
You need to somehow reload your preferences when the preferences activity finishes. I thought Dirol's suggestion of loading them in onResume() instead of onCreate() was excellent; have you tried it? Or am I misunderstanding the problem as well.
In my own case, I launched the preferences activity with startActivityForResult() and then on the activity result callback, I reloaded the preferences.
Code snippets:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_PREFERENCES:
Intent intent = new Intent().setClass(this, CalcPreferences.class);
startActivityForResult(intent, MENU_PREFERENCES);
break;
default: return super.onOptionsItemSelected(item);
}
return true;
}
#Override
protected void onActivityResult(int req, int result, Intent data) {
switch( req ) {
case MENU_PREFERENCES:
SharedPreferences sp =
PreferenceManager.getDefaultSharedPreferences(this);
updatePreferences(sp);
break;
default:
super.onActivityResult(req, result, data);
break;
}
}
#Override
protected void updatePreferences(SharedPreferences sp) {
super.updatePreferences(sp);
keyclick = sp.getBoolean("keyclick", keyclick);
}
Anyway, this is what works for me. I may try moving my updatePreferences() call to onResume() myself to see if that works too.
Try overriding the onBackPressed() method.
If your "Up" button (top left <-) provides the correct result, then you can set the Back button to behave like the Up button.
#Override
public void onBackPressed() {
super.onBackPressed();
NavUtils.navigateUpFromSameTask(this);
}
You load preferences only on onCreate() method. That method called only when a fresh activity starts up. The addPreferencesFromResource inflates the xml file into the preferences, so you only get the info, which is already has been stored in the xml at the moment addPreferencesFromResource was called, not after.
Try to move that method to onResume. But watch for the memory leak. I don't know exactly what the addPreferencesFromResource do, but from the documentation - I would be very suspicious about that method activity.
I had the same problem and solved it as follows:
The main activity class implements OnSharedPreferenceChangeListener:
public class Activity_name extends Activity implements OnSharedPreferenceChangeListener {
...
}
Inside the main activity class the onSharedPreferenceChanged is run whenever a preference entry changes. I simply update all my variables from the preferences as i did in onCreate:
#Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
<read all preferences as you did in onCreate()>
}
This does the trick and I hope it saves you some time in searching for a solution.
I've had the same problem...
Try to create preference instance and load its data in every class and every activity where you need it.
It worked for me...Hope it helps.
You will need to reload your view or whatever object which uses those preferences, preferably when preference activity closes.
Preference activities do not change nothing but an internal file with your preferences(key=value list). When it is changed, preferenceActivity calls onPreferenceChaged() and nothing more. It doesn't refresh your stuff by itself. You need to reload prefs and to reuse them in onResume() method or equivalent.

Categories