Using LibGDX with Android Preferences - java

I'm trying to use Andrioid's preferences system in conjunction with LibGDX's preferences system. They both use SharedPreferences as a backend, so I figure they should be able to work together, but when I try to load the data in LibGDX's preferences, I don't get any data back.
My Android preferences.xml file (I know it's short, it'll have much more later :P):
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference
android:key="framerate"
android:title="Set Framerate"
android:enabled="true"
android:persistent="true"
android:defaultValue="25" />
</PreferenceScreen>
Here is my PreferenceActivity:
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
public class WallpaperSettings extends PreferenceActivity {
#SuppressLint("NewApi")
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT < 11) {
addPreferencesFromResource(R.xml.preferences);
} else {
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class MyPreferenceFragment extends PreferenceFragment
{
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
}
When I call it from a subclass of com.badlogic.gdx.Game, I use
Preferences pref = Gdx.app.getPreferences("preferences");
pref.getInteger("framerate");
The number of keys within pref is 0.
Anyone have a clue as to how this might be fixed?

Thanks to http://www.badlogicgames.com/forum/viewtopic.php?f=11&t=6365#p32981 I was able to solve the problem.
Just a note, the code works for both Android 2.x and 3.0+.
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
public class WallpaperSettings extends PreferenceActivity {
#SuppressLint("NewApi")
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT < 11) {
addPreferencesFromResource(R.xml.preferences);
} else {
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class MyPreferenceFragment extends PreferenceFragment
{
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
getPreferenceManager().setSharedPreferencesName("preferences");
getPreferenceManager().setSharedPreferencesMode(0);
}
}
}

Related

How to read a method codes from another activity in my activity?

I want to call a method from another activity in my activity. I tried this codes but my app is crashed! :
SecondActivity:
package com.mycompany.myapp;
import android.app.*;
import android.os.*;
import android.widget.*;
public class SecondActivity extends Activity
{
public void toast()
{
Toast.makeText(getApplicationContext(),"hello",50).show();
}
}
MainActivity:
package com.mycompany.myapp;
import android.app.*;
import android.os.*;
import com.mycompany.myapp.*;
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SecondActivity s=new SecondActivity();
s.toast();
}
}
What the problem?! Please help me. Thanks.
I think you are confusing the Java class and Activity. If you want to declare the methods which don't need a layout and activity stuff, create a java class and have public methods.
1) MainActivity - Activity Class
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ToastClass toastClass = new ToastClass();
toastClass.toast(getApplicationContext(), "Hey dude!!");
}
}
2) ToastClass - java class
public class ToastClass {
public void toast(Context context, String msg) {
Toast.makeText(context ,"hello",Toast.LENGTH_SHORT).show();
}
}
Hope, it helps!
You do not instantiate activities, but starts them with an intent
Intent intentSecondActivity = new Intent(this,SecondActivity.class);
intentSecondActivity.putExtra("methodToStart","toast");
startActivity(intentScheduleActivity);
In the secondActivity read the extras and start the method:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.secondActivity);
String method = getIntent().getStringExtra("method");
if (method.equals("toast"){
toast();
}
}
Alternatively you might want to study fragments.

Invalid Method Declaration - Return type required

Having issues with the line I labelled. Tried multiple different suggestions but none seem to fix the issue, anyone has anymore suggestions?
package connect2you.com;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
findViewById(R.id.textViewSignup).setOnClickListener(this); // error on this line
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.textViewSignup:
startActivity(new Intent(this, SignUpActivity.class));
break;
}
}
}
Put your call tobfindViewById() inside a method. You can't run code outside of methods.

Disable screenshots in React Native

I know that you can not 100% stop the user from taking a screenshot if he insists to. But I read that you can still stop manual screenshots by setting LayoutParams.FLAG_SECURE in Java.
I tried adding it to my MainApplication file but getWindow() kept on throwing errors no matter what I do. So I moved that line of code to the MainActivity file and it worked without any errors.
Problem is, I can still normally take screenshots.
MainApplication:
package com.testapp;
import android.app.Activity;
import com.reactnativenavigation.NavigationApplication;
import com.facebook.react.modules.i18nmanager.I18nUtil;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
import android.support.annotation.Nullable;
/* custom modules */
import com.oblador.vectoricons.VectorIconsPackage;
import org.pgsqlite.SQLitePluginPackage;
import com.learnium.RNDeviceInfo.RNDeviceInfo;
import java.util.Arrays;
import java.util.List;
public class MainApplication extends NavigationApplication {
#Override
public boolean isDebug() {
return BuildConfig.DEBUG;
}
#Nullable
#Override
public List<ReactPackage> createAdditionalReactPackages() {
return Arrays.<ReactPackage>asList(
new SQLitePluginPackage(),
new VectorIconsPackage(),
new RNDeviceInfo()
);
}
#Override
public void onCreate() {
super.onCreate();
I18nUtil sharedI18nUtilInstance = I18nUtil.getInstance();
sharedI18nUtilInstance.allowRTL(getApplicationContext(), false);
}
}
MainActivity:
package com.testapp;
import android.widget.ImageView;
import com.reactnativenavigation.controllers.SplashActivity;
import android.os.Bundle;
import android.view.WindowManager.LayoutParams;
public class MainActivity extends SplashActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(LayoutParams.FLAG_SECURE, LayoutParams.FLAG_SECURE);
}
}
I did simply the following and it is working properly:
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE);
setContentView(R.layout.activity_main);
}
}

OnSharedPreferenceChangeListener Won't Work?

I am trying to change the application background color, and the font type via theme when a preference is changed in the prefs area.
So far I have the preferences working but when I put the listener in, it just does not get called. I am testing using toasts to see if it appears.
My Code:
package alertssystem.lsa13tafeproj.lsa13.resistorcalculator;
import android.app.ActionBar;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.widget.RelativeLayout;
import android.widget.Toast;
import java.util.prefs.PreferenceChangeEvent;
import java.util.prefs.PreferenceChangeListener;
public class Prefs extends PreferenceActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new PreferencesFragment()).commit();
}
#Override
public void onBackPressed()
{
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
public static class PreferencesFragment extends PreferenceFragment
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
}
There is no changeListener in above code as none of the solutions I found worked, can someone please tell me how I can implement this into this.
The user clicks on the ListPreference and it has the 2 options I have inserted, but nothing happens when new option is selected.
This works for me:
public class MyPreferenceFragment extends PreferenceFragment implements
OnSharedPreferenceChangeListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
PreferenceManager.getDefaultSharedPreferences(getActivity()).registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onDestroy() {
PreferenceManager.getDefaultSharedPreferences(getActivity()).unregisterOnSharedPreferenceChangeListener(this);
super.onDestroy();
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// Toast
}
}
Make sure you register your listener before changing the pref, and not unregistering it until you do.

java.lang.ClassNotFoundException: com.hamweather.aeris.maps.R$layout using Aeris Weather SDK

I am using the Hamweather Aeris Andorid SDK, and I am trying to implement the map view component. When I follow their online tutorials, I can not render the AerisMapView, and I get the error:
java.lang.ClassNotFoundException: com.hamweather.aeris.maps.R$layout.
Does anyone know where this is coming from/how to fix it?
My xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.hamweather.aeris.maps.AerisMapView
android:id="#+id/aerisfragment_map"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</com.hamweather.aeris.maps.AerisMapView>
</LinearLayout>
And my Activity:
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Window;
public class MapViewActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapview_activity);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MapFragment fragment = new MapFragment();
fragmentTransaction.add(R.id.frame_container, fragment);
fragmentTransaction.commit();
}
}
Finally, the fragment:
import android.location.*;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.model.LatLng;
import com.hamweather.aeris.communication.AerisCallback;
import com.hamweather.aeris.communication.EndpointType;
import com.hamweather.aeris.location.LocationHelper;
import com.hamweather.aeris.maps.AerisMapView;
import com.hamweather.aeris.maps.AerisMapView.AerisMapType;
import com.hamweather.aeris.maps.MapViewFragment;
import com.hamweather.aeris.maps.interfaces.OnAerisMapLongClickListener;
import com.hamweather.aeris.maps.interfaces.OnAerisMarkerInfoWindowClickListener;
import com.hamweather.aeris.maps.markers.AerisMarker;
import com.hamweather.aeris.model.AerisResponse;
import com.hamweather.aeris.response.EarthquakesResponse;
import com.hamweather.aeris.response.FiresResponse;
import com.hamweather.aeris.response.StormCellResponse;
import com.hamweather.aeris.response.StormReportsResponse;
public class MapFragment extends MapViewFragment implements OnAerisMapLongClickListener, AerisCallback,
OnAerisMarkerInfoWindowClickListener{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.single_tab_site_weather2,
container, false);
mapView = (AerisMapView) view.findViewById(R.id.aerisfragment_map);
mapView.init(savedInstanceState, AerisMapType.GOOGLE);
initMap();
setHasOptionsMenu(true);
return view;
}
/**
* Inits the map with specific setting
*/
private void initMap() {
mapView.moveToLocation(new LatLng(34.7, -86.7), 9);
mapView.setOnAerisMapLongClickListener(this);
mapView.setOnAerisWindowClickListener(this);
}
#Override
public void onResult(EndpointType endpointType, AerisResponse aerisResponse) {
}
#Override
public void onMapLongClick(double v, double v1) {
}
#Override
public void wildfireWindowPressed(FiresResponse firesResponse, AerisMarker aerisMarker) {
}
#Override
public void stormCellsWindowPressed(StormCellResponse stormCellResponse, AerisMarker aerisMarker) {
}
#Override
public void stormReportsWindowPressed(StormReportsResponse stormReportsResponse, AerisMarker aerisMarker) {
}
#Override
public void earthquakeWindowPressed(EarthquakesResponse earthquakesResponse, AerisMarker aerisMarker) {
}
}
Also, this is my first Q on stack exchange, so if I failed to adhere to a certain convention or etiquette, please let me know, and I'll try to fix it. Thanks.
I Managed to get it working. For anyone who has this issue, here were my steps:
I first switched from using the jars to using the gradle dependency. Per their website, add the following to your build.gradle(the module one)
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.android.gms:play-services:4.4.52'
compile 'com.hamweather:aeris-maps-library:1.0.0#aar'
}
Also be sure to add the following, in the andorid tag, which probably already exists in the file:
android {
...some other stuff...
dexOptions{
preDexLibraries = false
}
}
Double check that you have the proper API key permissions for google maps. Then it works. As far as I can tell, it had something to do with using the compiled jar, not the repo version.

Categories