Drawing to a canvas using a custom method? - java

I'm coding for an android 2-D game and I'm having some trouble.
Currently, in my package, I have a thread which draws on an instance of a canvas. I'm drawing Drawable bitmaps from resources. What I would like to do is keep the thread handing the drawing of the background image. But have instances of custom objects able draw to that same canvas using drawable objects. This seems possible logically, but I can't get it to work. Any time I attempt to retrieve the resources in my custom class the app crashes on start-up.
Here were a couple of my efforts: (please don't laugh if i've done something stupid, i'm trying.)
public class Worker{
//Get drawables
// Resources res = getResources();
// Drawable man1 = res.getDrawable(R.drawable.workertest);
// Context mContext;
// Resources res = mContext.getResources();
// Drawable man1 = mContext.getResources().getDrawable(R.drawable.workertest);
// Drawable man2 = mContext.getResources().getDrawable(R.drawable.workertest1);
// Drawable man1 = res.getDrawable(R.drawable.workertest);
// Drawable man2 = res.getDrawable(R.drawable.workertest1);
}
As you can see I've tried a couple different methods, and I've also tried adding (extends activity) to my class, but I can't figure this out.
This code doesn't address actually drawing to the canvas, I have yet to cross that bridge
Edit: Here is the Activity. As you can see it calls a thread in lunarview. That thread instantiates the object that needs to retrieve the Drawable. how in gods name do i get the application context to the object? or is there another way to do it? And yes, this is the code from the android sample codes.
public class LunarLander extends Activity {
private static final int MENU_EASY = 1;
private static final int MENU_HARD = 2;
private static final int MENU_MEDIUM = 3;
private static final int MENU_PAUSE = 4;
private static final int MENU_RESUME = 5;
private static final int MENU_START = 6;
private static final int MENU_STOP = 7;
/** A handle to the thread that's actually running the animation. */
public static LunarThread mLunarThread;
/** A handle to the View in which the game is running. */
public LunarView mLunarView;
/**
* Invoked during init to give the Activity a chance to set up its Menu.
*
* #param menu the Menu to which entries may be added
* #return true
*/
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, MENU_START, 0, R.string.menu_start);
menu.add(0, MENU_STOP, 0, R.string.menu_stop);
menu.add(0, MENU_PAUSE, 0, R.string.menu_pause);
menu.add(0, MENU_RESUME, 0, R.string.menu_resume);
menu.add(0, MENU_EASY, 0, R.string.menu_easy);
menu.add(0, MENU_MEDIUM, 0, R.string.menu_medium);
menu.add(0, MENU_HARD, 0, R.string.menu_hard);
return true;
}
/**
* Invoked when the user selects an item from the Menu.
*
* #param item the Menu entry which was selected
* #return true if the Menu item was legit (and we consumed it), false
* otherwise
*/
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_START:
mLunarThread.doStart();
return true;
case MENU_STOP:
mLunarThread.setState(LunarThread.STATE_LOSE,
getText(R.string.message_stopped));
return true;
case MENU_PAUSE:
mLunarThread.pause();
return true;
case MENU_RESUME:
mLunarThread.unpause();
return true;
case MENU_EASY:
mLunarThread.setDifficulty(LunarThread.DIFFICULTY_EASY);
return true;
case MENU_MEDIUM:
mLunarThread.setDifficulty(LunarThread.DIFFICULTY_MEDIUM);
return true;
case MENU_HARD:
mLunarThread.setDifficulty(LunarThread.DIFFICULTY_HARD);
return true;
}
return false;
}
/**
* Invoked when the Activity is created.
*
* #param savedInstanceState a Bundle containing state saved from a previous
* execution, or null if this is a new execution
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// tell system to use the layout defined in our XML file
setContentView(R.layout.lunar_layout);
// get handles to the LunarView from XML, and its LunarThread
mLunarView = (LunarView) findViewById(R.id.lunar);
mLunarThread = mLunarView.getThread();
// give the LunarView a handle to the TextView used for messages
mLunarView.setTextView((TextView) findViewById(R.id.text));
if (savedInstanceState == null) {
// we were just launched: set up a new game
mLunarThread.setState(LunarThread.STATE_READY);
Log.w(this.getClass().getName(), "SIS is null");
} else {
// we are being restored: resume a previous game
mLunarThread.restoreState(savedInstanceState);
Log.w(this.getClass().getName(), "SIS is nonnull");
}
}
/**
* Invoked when the Activity loses user focus.
*/
#Override
protected void onPause() {
super.onPause();
mLunarView.getThread().pause(); // pause game when Activity pauses
}
/**
* Notification that something is about to happen, to give the Activity a
* chance to save state.
*
* #param outState a Bundle into which this Activity should save its state
*/
#Override
protected void onSaveInstanceState(Bundle outState) {
// just have the View's thread save its state into our Bundle
super.onSaveInstanceState(outState);
mLunarThread.saveState(outState);
Log.w(this.getClass().getName(), "SIS called");
}
public boolean onTouchEvent(MotionEvent event){
int clickX = Math.round(event.getX()/10)*10;
int clickY = Math.round(event.getY()/10)*10;
LunarView.xCo = clickX;
LunarView.yCo = clickY;
return true;
}

As Greg suggested, the problem is in getting the right Context to retrieve the resources.
An easy way to solve this is to pass the main Activity as an argument in the Worker class constructor, and save it as a private Context field. Something like:
public class Worker{
private Context mContext;
public Worker (Context context) {
mContext = context;
Resources res = mContext.getResources();
// all the rest of your code
}
}
You will use it in your main activity as:
Worker worker = new Worker(this);

Related

How to create a Listener to Preferences changes in Preferences activity?

I have Preferences activity in my app that has ListPreference so the user can choose language for the app.
The app displays the new language just after the user close the Preferences activity.
I want to create a listener to the ListPreference so the app will restart when the Listener is triggers (just after the user choose a language/choose item from the ListPreference).
How can I do that?
SettingsActivity:
public class SettingsActivity extends AppCompatPreferenceActivity {
/**
* A preference value change listener that updates the preference's summary
* to reflect its new value.
*/
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference.setSummary(
index >= 0
? listPreference.getEntries()[index]
: null);
} else {
// For all other preferences, set the summary to the value's
// simple string representation.
preference.setSummary(stringValue);
}
return true;
}
};
/**
* Binds a preference's summary to its value. More specifically, when the
* preference's value is changed, its summary (line of text below the
* preference title) is updated to reflect the value. The summary is also
* immediately updated upon calling this method. The exact display format is
* dependent on the type of preference.
*
* #see #sBindPreferenceSummaryToValueListener
*/
private static void bindPreferenceSummaryToValue(Preference preference) {
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
// Trigger the listener immediately with the preference's
// current value.
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupActionBar();
setTitle(R.string.action_settings);
}
/**
* Set up the {#link android.app.ActionBar}, if the API is available.
*/
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
// Show the Up button in the action bar.
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
if (!super.onMenuItemSelected(featureId, item)) {
NavUtils.navigateUpFromSameTask(this);
}
return true;
}
return super.onMenuItemSelected(featureId, item);
}
/**
* This method stops fragment injection in malicious applications.
* Make sure to deny any unknown fragments here.
*/
protected boolean isValidFragment(String fragmentName) {
return PreferenceFragment.class.getName().equals(fragmentName)
|| GeneralPreferenceFragment.class.getName().equals(fragmentName);
}
/**
* This fragment shows general preferences only. It is used when the
* activity is showing a two-pane settings UI.
*/
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class GeneralPreferenceFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
setHasOptionsMenu(true);
// Bind the summaries of EditText/List/Dialog/Ringtone preferences
// to their values. When their values change, their summaries are
// updated to reflect the new value, per the Android Design
// guidelines.
bindPreferenceSummaryToValue(findPreference("example_text"));
bindPreferenceSummaryToValue(findPreference(getString(R.string.language_shared_pref_key)));
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
getActivity().finish();
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
}
pref_general.xml:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:defaultValue="#string/language_code"
android:entries="#array/pref_languages_list_titles"
android:entryValues="#array/pref_languages_list_values"
android:key="#string/language_shared_pref_key"
android:negativeButtonText="#null"
android:positiveButtonText="#null"
android:title="#string/pref_title_language" />
</PreferenceScreen>
Thank you!!!
Here is some real quick sample code for a shared prefs chaneg listener I have set-up in one of my projects; it's located in the onCreate of a Service but obviously can detect changes to my shared prefs that originate from anywhere in my app.
private SharedPreferences.OnSharedPreferenceChangeListener listener;
//Loads Shared preferences
prefs = PreferenceManager.getDefaultSharedPreferences(this);
//Setup a shared preference listener for hpwAddress and restart transport
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if (key.equals(/*key for shared pref you're listening for*/) {
//Do stuff; restart activity in your case
}
};
prefs.registerOnSharedPreferenceChangeListener(listener);
One possible solution is to keep everything together, and implement the OnSharedPreferenceChangeListener interface inside your own GeneralPreferenceFragment.
You can do that by declaring that GeneralPreferenceFragment both inherits from PreferenceFragment, and implements SharedPreferences.OnSharedPreferenceChangeListener interface. This means that you need to define override for onSharedPreferenceChanged in GeneralPreferenceFragment.
Because preferences can change only if the fragment in question is active, you can register your fragment as listener in onResume for a fragment (via registerOnSharedPreferenceChangeListener), and unregister it on onStop.
The following example code is in Kotlin, not in Java, but it should be fairly easy to translate it.
class SettingsActivity : AppCompatActivity() {
// ...
class SettingsFragment : PreferenceFragmentCompat(),
SharedPreferences.OnSharedPreferenceChangeListener
{
// ...
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?,
key: String?) {
if (key == "<something>")
// do something
}
override fun onResume() {
super.onResume()
preferenceScreen.sharedPreferences
?.registerOnSharedPreferenceChangeListener(this)
}
override fun onPause() {
super.onPause()
preferenceScreen.sharedPreferences
?.unregisterOnSharedPreferenceChangeListener(this)
}
}

Android AlertDialog does not show Adapter names

I made a AllertDialog that's supposed to display a list of apps, when I click on one of the alerdialog members it does return the right String from the ArrayAdapter.
Here's how it looks.
public class AppList {
private Activity main_activity;
private AlertDialog app_list_dialog;
private ArrayList<RemoteAppDetail> remoteAppDetails;
public AppList(Activity activity){
this.main_activity = activity;
// init();
}
/**
* Initialize the Dialog, this needs to be called before .show()/.hide().dispose();
*/
public void init(){
this.remoteAppDetails = ConnectionHandler.getLibrary();
AlertDialog.Builder app_list_builder = new AlertDialog.Builder(main_activity);
app_list_builder.setIcon(R.drawable.ic_input_add);
app_list_builder.setTitle("Installable/Updatable Apps: ");
final ArrayAdapter<String> app_list = new ArrayAdapter<String>(main_activity, R.layout.simple_list_item_single_choice);
final ArrayList<String> app_name_list = getInstallableApps();
app_list.addAll(app_name_list);
if(!app_list.isEmpty()){
//Add the app_list adapter (The button list)
app_list_builder.setAdapter(app_list, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("DIALOG SELECTED", app_list.getItem(which));
}
});
}else{
app_list_builder.setMessage("There are no apps available for download/update.");
}
//Close Button AppList
app_list_builder.setNegativeButton("Close",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
this.app_list_dialog = app_list_builder.create();
}
/**
* Show the list.
*/
public void show(){
if(this.app_list_dialog != null){
app_list_dialog.show();
}
}
/**
* Hide the list.
*/
public void hide(){
if(this.app_list_dialog != null){
app_list_dialog.hide();
}
}
/**
* Disposes the list, it will need to be reinitialized of you change your mind after disposing of the list.
*/
public void dispose(){
if(this.app_list_dialog != null){
app_list_dialog.dismiss();
}
}
/**
* Gets a list of apps that can be installed
* #return Arraylist installable apps
*/
private ArrayList<String> getInstallableApps(){
ArrayList<String> installable_apps_list = new ArrayList<String>();
if(this.remoteAppDetails != null){
for(RemoteAppDetail rea : this.remoteAppDetails){
BasicNameValuePair remote_app_info = new BasicNameValuePair(rea.filename, String.valueOf(rea.version));
BasicNameValuePair local_app_info = getLocalAppInfo(rea.filename);
if(local_app_info != null){
if(isRemoteVersionNewer(remote_app_info, local_app_info)){
installable_apps_list.add(rea.filename);
}
}else{
if(rea.unlock_status == 0){
installable_apps_list.add(rea.filename);
}
}
}
}
return installable_apps_list;
}
/**
* Check if the app exists locally.
* #param app_name_remote
* #return Local app info as BasicNameValuePair
*/
private BasicNameValuePair getLocalAppInfo(String app_name_remote){
List<PackageInfo> packs = main_activity.getPackageManager().getInstalledPackages(0);
for(PackageInfo pi : packs){
String app_name_local = pi.applicationInfo.loadLabel(main_activity.getPackageManager()).toString();
String app_version = String.valueOf(pi.versionCode);
if(app_name_remote == app_name_local){
return new BasicNameValuePair(app_name_local, app_version);
}
}
return null;
}
/**
* Compare local and remote app info.
* #param remote_app_info
* #param local_app_info
* #return Return whether or not the remote version is newer as Boolean.
*/
private boolean isRemoteVersionNewer(BasicNameValuePair remote_app_info, BasicNameValuePair local_app_info){
if(remote_app_info.getName().contains(local_app_info.getName())){
if(remote_app_info.getValue() == local_app_info.getValue()){
return true;
}
}
return false;
}
So I would really like to know what I did wrong and if this is enough information.
UPDATE 1:
This is how I call this class:
Applist main_app_list = new Applist(this);
main_app_list.init();
main_app_list.show();
and it's called like that in my main activity's onCreate method.
Someone suggested my getInstallableApps() might return empty value's,
However if you inspect the code carefully this is not a possibility. Because if they there were empty it wouldn't even have any adapter members.
Either way I added some test value's, and it remains to do the exact same thing:
final ArrayAdapter<String> app_list = new ArrayAdapter<String>(main_activity, R.layout.simple_list_item_single_choice);
final ArrayList<String> app_name_list = getInstallableApps();
// app_list.addAll(app_name_list);
app_list.add("TEST");
app_list.add("TEST2");
The rest of the code remains the same.
UPDATE 2:
When I touch one of the members, it show's their name for a brief second, or when I hold it down it show's them for as long as I hold it.
Debug your getInstallableApps() method, you will see that it's entering the for statement because there are two elements in this.remoteAppDetails, but inside wether the elements you take data from are not called properly or empty (remote_app_info,local_app_info), or the filename field is empty. Once you find whats is placing empty values to you array you can fix it.
So I just ended up forcing the color to fix this since a custom layout was to much hassle for just this.
app_list = new ArrayAdapter<String>(main_activity, R.layout.simple_list_item_single_choice){
public View getView(int position, View convertView, android.view.ViewGroup parent) {
TextView textView = (TextView) super.getView(position, convertView, parent);
textView.setTextColor(Color.BLACK);
return textView;
}
};
For future reference when you think it doesn't show adapter names, just hold down on one of the members to see if it show's the member name, if it does you know you got a font problem.

How to set/change Android Studio 'Fullscreen Activity' background?

So I created a new activity using the 'Fullscreen Activity' template in Android Studio. I now want to change the background which is set to android:background="#0099cc" by default in its generated XML file. However, I've tried the 'solution' on one of the more popular, similar questions but got a error.
How to set background color of an Activity to white programmatically?
The error I get is:
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0xffffffff
at android.content.res.Resources.getValue(Resources.java:2060)
at android.content.res.Resources.getDrawable(Resources.java:1625)
at android.view.View.setBackgroundResource(View.java:16251)
which happens at the root.setBackgroundResource(Color.WHITE); line. Below is the entirety of the java code with the only two things added to the Activity template being the onStart method and a setActivityBackgroundColor() method.
package org.test.app;
import org.test.app.util.SystemUiHider;
import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import org.test.app.R;
/**
* An example full-screen activity that shows and hides the system UI (i.e.
* status bar and navigation/system bar) with user interaction.
*
* #see SystemUiHider
*/
public class FullscreenActivityTest extends Activity {
/**
* Whether or not the system UI should be auto-hidden after
* {#link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
*/
private static final boolean AUTO_HIDE = true;
/**
* If {#link #AUTO_HIDE} is set, the number of milliseconds to wait after
* user interaction before hiding the system UI.
*/
private static final int AUTO_HIDE_DELAY_MILLIS = 3000;
/**
* If set, will toggle the system UI visibility upon interaction. Otherwise,
* will show the system UI visibility upon interaction.
*/
private static final boolean TOGGLE_ON_CLICK = true;
/**
* The flags to pass to {#link SystemUiHider#getInstance}.
*/
private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;
/**
* The instance of the {#link SystemUiHider} for this activity.
*/
private SystemUiHider mSystemUiHider;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen_activity_test);
final View controlsView = findViewById(R.id.fullscreen_content_controls);
final View contentView = findViewById(R.id.fullscreen_content);
// Set up an instance of SystemUiHider to control the system UI for
// this activity.
mSystemUiHider = SystemUiHider.getInstance(this, contentView, HIDER_FLAGS);
mSystemUiHider.setup();
mSystemUiHider
.setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
// Cached values.
int mControlsHeight;
int mShortAnimTime;
#Override
#TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public void onVisibilityChange(boolean visible) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
// If the ViewPropertyAnimator API is available
// (Honeycomb MR2 and later), use it to animate the
// in-layout UI controls at the bottom of the
// screen.
if (mControlsHeight == 0) {
mControlsHeight = controlsView.getHeight();
}
if (mShortAnimTime == 0) {
mShortAnimTime = getResources().getInteger(
android.R.integer.config_shortAnimTime);
}
controlsView.animate()
.translationY(visible ? 0 : mControlsHeight)
.setDuration(mShortAnimTime);
} else {
// If the ViewPropertyAnimator APIs aren't
// available, simply show or hide the in-layout UI
// controls.
controlsView.setVisibility(visible ? View.VISIBLE : View.GONE);
}
if (visible && AUTO_HIDE) {
// Schedule a hide().
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
}
});
// Set up the user interaction to manually show or hide the system UI.
contentView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (TOGGLE_ON_CLICK) {
mSystemUiHider.toggle();
} else {
mSystemUiHider.show();
}
}
});
// Upon interacting with UI controls, delay any scheduled hide()
// operations to prevent the jarring behavior of controls going away
// while interacting with the UI.
findViewById(R.id.dummy_button).setOnTouchListener(mDelayHideTouchListener);
}
protected void onStart() {
super.onStart();
setActivityBackgroundColor();
}
public void setActivityBackgroundColor() {
setContentView(R.layout.activity_fullscreen_activity_test);
View root = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
root.setBackgroundResource(Color.WHITE);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Trigger the initial hide() shortly after the activity has been
// created, to briefly hint to the user that UI controls
// are available.
delayedHide(100);
}
/**
* Touch listener to use for in-layout UI controls to delay hiding the
* system UI. This is to prevent the jarring behavior of controls going away
* while interacting with activity UI.
*/
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (AUTO_HIDE) {
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
return false;
}
};
Handler mHideHandler = new Handler();
Runnable mHideRunnable = new Runnable() {
#Override
public void run() {
mSystemUiHider.hide();
}
};
/**
* Schedules a call to hide() in [delay] milliseconds, canceling any
* previously scheduled calls.
*/
private void delayedHide(int delayMillis) {
mHideHandler.removeCallbacks(mHideRunnable);
mHideHandler.postDelayed(mHideRunnable, delayMillis);
}
}
setBackgroundResource takes a resource, not a color (even though both are integers). You probably want to use View.setBackgroundColor if you want to set it to a color in code.

Flip Images in a viewpager

I have a viewpager for a fullscreen image slider with horizontal swipe. What I am trying to do is put an image behind every image on the slider. Meaning, a user can click on a 'flip' button to reveal another image.
Below is my code however I have two issues:
The first time I click the flip button, it flips the image but only
to reveal the same image. However, when I click it the second time
it works :).
The flip button only works on the first image. When I swipe
right or left the flip button doesn't work anymore. Any help?
Note: I am using a fullscreenActivity theme which hides the UI control until the user clicks on the image.
FullscreenActivity.java
package me.zamaaan.wallpaper;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import me.zamaaan.wallpaper.util.SystemUiHider;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import static me.zamaaan.wallpaper.HeavyLifter.FAIL;
import static me.zamaaan.wallpaper.HeavyLifter.SUCCESS;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import java.util.ArrayList;
import java.util.List;
/** A helper class that will do the heavy work of decoding images and actually setting the wallpaper */
/**
* An example full-screen activity that shows and hides the system UI (i.e.
* status bar and navigation/system bar) with user interaction.
*
* #see SystemUiHider
*/
public class FullscreenActivity extends Activity implements OnClickListener, AnimationListener{
/**
* Whether or not the system UI should be auto-hidden after
* {#link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
*/
private static final boolean AUTO_HIDE = true;
private HeavyLifter chuckNorris;
private Animation animation1;
private Animation animation2;
private boolean isBackOfCardShowing = true;
/*
an array with the ids of the images for the viewpager
*/
public Integer[] mImageIds = {
R.drawable.background1, R.drawable.background2,
R.drawable.background3, R.drawable.background4,
R.drawable.background1, R.drawable.background2,
R.drawable.background1, R.drawable.background2,
R.drawable.background3, R.drawable.background4,
R.drawable.background1, R.drawable.background2,
R.drawable.background3
};
/*
an array with the ids of the images for the back flip of each image in the viewpager
*/
public Integer[] mBackIds = {
R.drawable.splash, R.drawable.splash,
R.drawable.splash, R.drawable.splash,
R.drawable.splash, R.drawable.splash,
R.drawable.splash, R.drawable.splash,
R.drawable.splash, R.drawable.splash,
R.drawable.splash, R.drawable.splash,
R.drawable.splash
};
/**
* If {#link #AUTO_HIDE} is set, the number of milliseconds to wait after
* user interaction before hiding the system UI.
*/
private static final int AUTO_HIDE_DELAY_MILLIS = 3000;
/**
* If set, will toggle the system UI visibility upon interaction. Otherwise,
* will show the system UI visibility upon interaction.
*/
private static final boolean TOGGLE_ON_CLICK = true;
/**
* The flags to pass to {#link SystemUiHider#getInstance}.
*/
private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;
/**
* The instance of the {#link SystemUiHider} for this activity.
*/
private SystemUiHider mSystemUiHider;
/**
* The pager widget, which handles animation and allows swiping horizontally to access previous
* and next wizard steps.
*/
private ViewPager mPager;
/**
* The pager adapter, which provides the pages to the view pager widget.
*/
private PagerAdapter mPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
animation1 = AnimationUtils.loadAnimation(this, R.anim.to_middle);
animation1.setAnimationListener(this);
animation2 = AnimationUtils.loadAnimation(this, R.anim.from_middle);
animation2.setAnimationListener(this);
findViewById(R.id.flip).setOnClickListener(this);
// Loop through the ids to create a list of full screen image views
ImageAdapter imageAdapter = new ImageAdapter(this);
List<ImageView> images = new ArrayList<ImageView>();
for (int i = 0; i < imageAdapter.getCount(); i++) {
ImageView imageView = new ImageView(this);
imageView.setId(i);
imageView.setImageResource(imageAdapter.mThumbIds[i]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
images.add(imageView);
}
final View controlsView = findViewById(R.id.fullscreen_content_controls);
final View contentView = findViewById(R.id.view_pager);
// Set up an instance of SystemUiHider to control the system UI for
// this activity.
mSystemUiHider = SystemUiHider.getInstance(this, contentView, HIDER_FLAGS);
mSystemUiHider.setup();
mSystemUiHider.setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
// Cached values.
int mControlsHeight;
int mShortAnimTime;
#Override
#TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public void onVisibilityChange(boolean visible) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
// If the ViewPropertyAnimator API is available
// (Honeycomb MR2 and later), use it to animate the
// in-layout UI controls at the bottom of the
// screen.
if (mControlsHeight == 0) {
mControlsHeight = controlsView.getHeight();
}
if (mShortAnimTime == 0) {
mShortAnimTime = getResources().getInteger(
android.R.integer.config_shortAnimTime);
}
controlsView
.animate()
.translationY(visible ? 0 : mControlsHeight)
.setDuration(mShortAnimTime);
} else {
// If the ViewPropertyAnimator APIs aren't
// available, simply show or hide the in-layout UI
// controls.
controlsView.setVisibility(visible ? View.VISIBLE
: View.GONE);
}
if (visible && AUTO_HIDE) {
// Schedule a hide().
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
}
});
// Set up the user interaction to manually show or hide the system UI.
contentView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (TOGGLE_ON_CLICK) {
mSystemUiHider.toggle();
} else {
mSystemUiHider.show();
}
}
});
// Upon interacting with UI controls, delay any scheduled hide()
// operations to prevent the jarring behavior of controls going away
// while interacting with the UI.
findViewById(R.id.btnSetAsWallpaper).setOnTouchListener(
mDelayHideTouchListener);
findViewById(R.id.btnSaveWallpaper).setOnTouchListener(
mDelayHideTouchListener);
// Finally create the adapter
ImagePagerAdapter imagePagerAdapter = new ImagePagerAdapter(images);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
viewPager.setAdapter(imagePagerAdapter);
// Set the ViewPager to point to the selected image from the previous activity
// Selected image id
int position = getIntent().getExtras().getInt("id");
viewPager.setCurrentItem(position);
//set title of Image
this.setTitle(imageAdapter.mThumbTitles[position]);
// Load are heavy lifter (goes and does work on another thread), to get a response after the lifters thread
// has finished we pass in a Handler that will be notified when it completes
chuckNorris = new HeavyLifter(this, chuckFinishedHandler);
}
#Override
public void onClick(View v) {
v.setEnabled(false);
((ImageView)findViewById(getIntent().getExtras().getInt("id"))).clearAnimation();
((ImageView)findViewById(getIntent().getExtras().getInt("id"))).setAnimation(animation1);
((ImageView)findViewById(getIntent().getExtras().getInt("id"))).startAnimation(animation1);
}
#Override
public void onAnimationEnd(Animation animation) {
int i = getIntent().getExtras().getInt("id");
if (animation==animation1) {
if (isBackOfCardShowing) {
((ImageView)findViewById(i)).setImageResource(mImageIds[i]);
} else {
((ImageView)findViewById(i)).setImageResource(mBackIds[i]);
}
((ImageView)findViewById(getIntent().getExtras().getInt("id"))).clearAnimation();
((ImageView)findViewById(getIntent().getExtras().getInt("id"))).setAnimation(animation2);
((ImageView)findViewById(getIntent().getExtras().getInt("id"))).startAnimation(animation2);
} else {
isBackOfCardShowing=!isBackOfCardShowing;
findViewById(R.id.flip).setEnabled(true);
}
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Trigger the initial hide() shortly after the activity has been
// created, to briefly hint to the user that UI controls
// are available.
delayedHide(100);
}
/**
* Touch listener to use for in-layout UI controls to delay hiding the
* system UI. This is to prevent the jarring behavior of controls going away
* while interacting with activity UI.
*/
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (AUTO_HIDE) {
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
return false;
}
};
Handler mHideHandler = new Handler();
Runnable mHideRunnable = new Runnable() {
#Override
public void run() {
mSystemUiHider.hide();
}
};
/**
* Schedules a call to hide() in [delay] milliseconds, canceling any
* previously scheduled calls.
*/
private void delayedHide(int delayMillis) {
mHideHandler.removeCallbacks(mHideRunnable);
mHideHandler.postDelayed(mHideRunnable, delayMillis);
}
/**
* Called from XML when the save wallpaper button is pressed
* Thie retrieves the id of the current image from our list
* It then asks chuck to save it as a wallpaper!
* The chuckHandler will be called when this operation is complete
* #param v
*/
public void saveWallpaper(View v) {
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
// Show a toast message on successful save
if(chuckNorris.saveResourceAsWallpaper(imageAdapter.mThumbIds[position])){
Toast.makeText(FullscreenActivity.this, "Image Saved to ZamaaanWallpaper folder", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(FullscreenActivity.this, "Image couldn't be saved, try again", Toast.LENGTH_SHORT).show();
}
}
/**
* Called from XML when the set wallpaper button is pressed
* Thie retrieves the id of the current image from our list
* It then asks chuck to set it as a wallpaper!
* The chuckHandler will be called when this operation is complete
* #param v
*/
public void setAsWallpaper(View v) {
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
chuckNorris.setResourceAsWallpaper(imageAdapter.mThumbIds[position]);
}
/**
* This is the handler that is notified when are HeavyLifter is finished doing an operation
*/
private Handler chuckFinishedHandler = new Handler(){
#Override
public void handleMessage(Message msg) {
switch(msg.what){
case SUCCESS:
Toast.makeText(FullscreenActivity.this, "Wallpaper set", Toast.LENGTH_SHORT).show();
break;
case FAIL:
Toast.makeText(FullscreenActivity.this, "Wallper NOT set, try again", Toast.LENGTH_SHORT).show();
break;
default:
super.handleMessage(msg);
}
}
};
}
I fixed my issues. I simply had to :
Change the boolean isBackOfCardShowing to an array of boolean for each page and intialize it to false.
Use viewpager.getCurrentItem() to get the position of the current page instead of what I was doing.

Errors for no reason

I tried to run the code to test if the program works, but it doesn't seem to do anything. I am getting an error that the fullscreen_contents_controls can't be resolved (line 54) and a syntax error when I can't find one (line 100). Can you help debug it?
Here are the errors:
Description Resource Path Location Type
fullscreen_content_controls cannot be resolved or is not a field Opening.java /MagicSquaresGame/src/com/mtprogramming/magicsquaresgame line 54 Java Problem
Description Resource Path Location Type
Syntax error, insert "}" to complete MethodBody Opening.java /MagicSquaresGame/src/com/mtprogramming/magicsquaresgame line 100 Java Problem
Code:
/**
* An example full-screen activity that shows and hides the system UI (i.e.
* status bar and navigation/system bar) with user interaction.
*
* #see SystemUiHider
*/
public class Opening extends Activity {
/**
* Whether or not the system UI should be auto-hidden after
* {#link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
*/
private static final boolean AUTO_HIDE = false;
/**
* If {#link #AUTO_HIDE} is set, the number of milliseconds to wait after
* user interaction before hiding the system UI.
*/
private static final int AUTO_HIDE_DELAY_MILLIS = 3000;
/**
* If set, will toggle the system UI visibility upon interaction. Otherwise,
* will show the system UI visibility upon interaction.
*/
private static final boolean TOGGLE_ON_CLICK = true;
/**
* The flags to pass to {#link SystemUiHider#getInstance}.
*/
private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;
/**
* The instance of the {#link SystemUiHider} for this activity.
*/
private SystemUiHider mSystemUiHider;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_opening);
final View controlsView = findViewById(R.id.fullscreen_content_controls);
final View contentView = findViewById(R.id.fullscreen_content);
// Set up an instance of SystemUiHider to control the system UI for
// this activity.
mSystemUiHider = SystemUiHider.getInstance(this, contentView,
HIDER_FLAGS);
mSystemUiHider.setup();
mSystemUiHider
.setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
// Cached values.
int mControlsHeight;
int mShortAnimTime;
#Override
#TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public void onVisibilityChange(boolean visible) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
// If the ViewPropertyAnimator API is available
// (Honeycomb MR2 and later), use it to animate the
// in-layout UI controls at the bottom of the
// screen.
if (mControlsHeight == 0) {
mControlsHeight = controlsView.getHeight();
}
if (mShortAnimTime == 0) {
mShortAnimTime = getResources().getInteger(
android.R.integer.config_shortAnimTime);
}
controlsView
.animate()
.translationY(visible ? 0 : mControlsHeight)
.setDuration(mShortAnimTime);
} else {
// If the ViewPropertyAnimator APIs aren't
// available, simply show or hide the in-layout UI
// controls.
controlsView.setVisibility(visible ? View.VISIBLE
: View.GONE);
}
if (visible && AUTO_HIDE) {
// Schedule a hide().
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
}
});
// Set up the user interaction to manually show or hide the system UI.
// Upon interacting with UI controls, delay any scheduled hide()
// operations to prevent the jarring behavior of controls going away
// while interacting with the UI.
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Trigger the initial hide() shortly after the activity has been
// created, to briefly hint to the user that UI controls
// are available.
delayedHide(100);
}
/**
* Touch listener to use for in-layout UI controls to delay hiding the
* system UI. This is to prevent the jarring behavior of controls going away
* while interacting with activity UI.
*/
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (AUTO_HIDE) {
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
return false;
}
};
Handler mHideHandler = new Handler();
Runnable mHideRunnable = new Runnable() {
#Override
public void run() {
mSystemUiHider.hide();
}
};
/**
* Schedules a call to hide() in [delay] milliseconds, canceling any
* previously scheduled calls.
*/
private void delayedHide(int delayMillis) {
mHideHandler.removeCallbacks(mHideRunnable);
mHideHandler.postDelayed(mHideRunnable, delayMillis);
}
}
Have you made sure that "fullscreen_content_controls" exists in your xml layout?
Your controlsView should have an id attribute
<controlsView
android:id='full_content_controls'
...
>

Categories