[Q] Need some help with GLSurfaceView - Android Q&A, Help & Troubleshooting

sorry if this is in the wrong place please move if thats true!
`basically im trying to make a app with a dual layer background which scrolls at different speeds but makes up the same background: for example: running water with leafs floating past.....scrolling stars with debris floating by. water equals background1' leafs equals background2 (blended together) space with stars scrolling equals background1 some debris floating equals background2..thats what im doing but im stuck here. i have everything in place, i cannot figure this bit out, preferably if someone can help me with this can u put an example or the solution please...
To this point, you have created the AGame activity, but you now need something for it to display. Let’s create a new class named AGameView:
public class AGameView{
Now, modify this class to extend GLSurfaceView.
i have done this to this class....
import android.opengl.GLSurfaceView;
public class AGameView extends GLSurfaceView {
With the class that extends the GLSurfaceView created, you can add a reference to it in your AGame activity. In the previous chapter, you set the value for setContentView() in your (application) activity to a layout. As of right now, the setContentView() value for the AGame activity is not set, or it is set to a default main layout. However, you can set this value to a GLSurfaceView. Setting the AGame setContentView() to the AGameView that you just created will let you begin to work with and display OpenGL.
Open the AGame activity, and create an instance of the AGameView GLSurfaceView that you just created.
i also have this done this:
import android.app.Activity;
import android.os.Bundle;
public class AGame extends Activity {
private AGameView gameView;
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView();
Now, instantiate the AGameView, and set the setContentView() to the new instance.
done this too..
import android.app.Activity;
import android.os.Bundle;
public class AGame extends Activity {
private AGameView gameView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gameView = new AGameView(this);
setContentView(gameView);
have i missed something??????? thanks for any help col

Related

[Q] Importing and drawing moving images.

I am making an app that currently only make squares move on a "road" and make random desicions on where to go. Now I want to replace them with a image of a car.
I'm currently using the "onDraw(Canvas canvas)" method. confused
So how do I import images to the app.
And how do I do If I want it to animate with 2-3 images.
Easyer explained. I want to replace the drawRect with drawImage or drawBitmap and make it work. I want to load the image from the drawable folder.
Code:
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(android.graphics.Color.RED);
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(square1.x, square1.y, square1.x + 20, square1.y + 20,
paint);
paint.setColor(android.graphics.Color.BLUE);
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(square2.x, square2.y, square2.x + 20, square2.y + 20, paint);
paint.setColor(android.graphics.Color.GREEN);
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(square3.x, square3.y, square3.x + 20, square3.y + 20,
paint);
paint.setColor(android.graphics.Color.BLACK);
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(square4.x, square4.y, square4.x + 20, square4.y + 20,
paint);
paint.setColor(android.graphics.Color.YELLOW);
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(square5.x, square5.y, square5.x + 20, square5.y + 20,
paint);
Generally the easiest way to animate a limited number of frames on any system is using clip rectangles and a single image that contains the frames side by side... depending on the system this doesn't even create a whole let of overhead if you use an uncompressed format like TGA or BMP, laid out vertically. Is there anything specific you need to know about that... or is your problem with the timing? You could just use the nanotime and decide which frame to draw based on y=(time%fullAnimationTime)/frameTime*tileHeight
That will be my problem in the future.
But right now I don't know how to even import and use images properly.
I'm very new at programming for android. Have only programmed a little java before
Just read up a little on this... seems AnimationDrawable actually does exactly what you need, just look it up in the reference.
If you still want to do the side-by-side thing I suggested first, here's some code to get you started, which uses assets to access a BMP. You still need a timer to actually cause the drawing, but I guess you've already set that up:
Code:
Bitmap animBmp=null;
try {
InputStream animStream=null;
animStream=this.getAssets().open("anim.bmp");
BufferedInputStream animBufStream = new BufferedInputStream(animStream);
animBmp = BitmapFactory.decodeStream(animBufStream);
animBufStream.close();
animStream.close();
} catch (IOException e) {
e.printStackTrace();
}
class AnimView extends View{
public int frameTime=100;
public int frames=-1;
public int frameWidth=0;
public int frameHeight=0;
public Bitmap animBmp=null;
public Paint paint=null;
private int lastFrame=-1;
public AnimView(Context ctx, Bitmap bmp,int frms) {
super(ctx);
animBmp=bmp;
frames=frms;
frameWidth=bmp.getWidth();
frameHeight=bmp.getHeight()/frames;
paint=new Paint();
}
protected void onDraw(Canvas c){
Log.i("draw", "draw");
int frame=(int)((SystemClock.elapsedRealtime()%(frameTime*frames))/frameTime);
if(frame!=lastFrame){
c.drawBitmap(animBmp, new Rect(0,frame*frameHeight,frameWidth,(frame+1)*frameHeight), new Rect(0,0,frameWidth,frameHeight),paint);
lastFrame=frame;
}
}
};
AnimView v=new AnimView(this,animBmp,3);
It says that
"animStream=this.getAssets().open("anim.bmp");"
The method getAssets() is undefined for the type MyTimedView
I am a real noob at this. Where am I suppose to set my test.bmp? (The image is named test) Which variables should I change for it to understand that it always should use the "test" image?
Also. I have a image of a house that I want to be placed on the screen. And this that you gave me doesn't help me there I think?
Thanks for all your help!!
getAssets operates on the Activity, not the View.
Change this line to your bitmap's path:
animStream=this.getAssets().open("anim.bmp");
in your case
animStream=this.getAssets().open("test.bmp");
You'll also have to change this line to match the actual number of frames:
AnimView v=new AnimView(this,animBmp,3);
And lastly, the file (test.bmp) should be inside the ASSETS directory of your eclipse project.
Ok I have done everything you have said. I made 3 images and stored them in assets. Named test1, test2, test3. But now I only get a white screen. I had some stuff from the beginning but they are now gone. And I don't get a image or anything.
Even more complex. How do I set the X and Y values to the animation?
It's supposed to be one image with the frames ontop of each other. Give me a second... I'll pack it into a project and attach it.
Here you go. I added a few comments:
Code:
package net.tapperware.simpleanimation;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
public class SimpleAnimationActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//This will hold our single bitmap which will contain all frames stacked ontop of each other
Bitmap animBmp=null;
try {
//First, load the file: This is supposed to be in the assets folder of the project
InputStream animStream=this.getAssets().open("anim.bmp");
//BitmapFactory needs a BufferedInputStream, so let's just convert our InputStream
BufferedInputStream animBufStream = new BufferedInputStream(animStream);
//OK, now we decode it to an Android-Bitmap
animBmp = BitmapFactory.decodeStream(animBufStream);
//And finally, we clean up
animBufStream.close();
animStream.close();
} catch (IOException e) {
e.printStackTrace();
}
//This will be used to operate the loop... looping on Android needs a Runnable, and this class provides it. When
//run, it will invalidate the view, which causes onDraw to be called.
class AnimLoop implements Runnable{
private View view;
AnimLoop(View v){ view=v; }
@Override public void run() { view.invalidate(); }
}
//And here the actual view
class AnimView extends View{
//Time between frames
public int frameTime=100;
//Number of frames
public int frames=-1;
//Width of each frame
public int frameWidth=0;
//Height of each frame
public int frameHeight=0;
//The previous frame number shown
public int lastFrame=0;
//This is our Runnable that will call invalidate()
public AnimLoop loop=null;
//Of course, the actual Bitmap
public Bitmap animBmp=null;
//Just a default Paint object, since it's required by drawBitmap
public Paint paint=null;
public AnimView(Context ctx, Bitmap bmp,int frms) {
super(ctx);
//Assign the args to our attributes
animBmp=bmp;
frames=frms;
//Since the images are stacked ontop of each other, each frame is widthOfBitmap * (heightOfBitmap / numberOfFrames )
frameWidth=bmp.getWidth();
frameHeight=bmp.getHeight()/frames;
//And finally, our dummy height
paint=new Paint();
//And the Runnable that will cause invalidate to be called
loop=new AnimLoop(this);
}
protected void onDraw(Canvas c){
//Draw the rect that corresponds to the current frame
c.drawBitmap(
animBmp
//We need to move down by frame number*the frame index
,new Rect(0,lastFrame*frameHeight,frameWidth,(lastFrame+1)*frameHeight)
//This is our target rect... draw wherever you want
,new Rect(0,0,frameWidth,frameHeight)
//And our standard paint settings
,paint);
//Increase the frame number by one, if it's >= the total number of frames, make it wrap back to 0
lastFrame=(lastFrame+1)%frames;
//And ask for the next frame to be drawn in frameTime milliseconds.
this.getHandler().postDelayed(loop, frameTime);
}
};
//Arguments: Activity/Context , Bitmap, Frame Count
AnimView v=new AnimView(this,animBmp,3);
setContentView(v);
}
}
[2011-08-21 01:53:39 - SimpleAnimation] ERROR: Application requires API version 10. Device API version is 8 (Android 2.2).
[2011-08-21 01:53:39 - SimpleAnimation] Launch canceled!
Right now I feel like a 10 year old that doesn't understand how to spell.
Any quickfix to make it 2.2 compatible?
Yep, go to Project /Properties /Android and set the checkmark somewhere else... the code is not using any of it, I just set it to something. Any 2.x should be fine, probably even 1.x
Thanks for all your awsome work!
But I have 1 problem. The picture. anim.bmp never gets placed on the screen. I have altered the x and y values but it seems like it's never getting loaded. I have changed all that I thought needed to be changed.
Lets keep this simple. After I am done with the above. I want to make 1 image on a specific x and y. And I want it to be clickable. Nobody really goes into detail about how to even get a picture into the app. Everyone believes it's so simple...
That's... troubling. You mean the unchanged project right? It works perfectly here and I can't see anything that would cause issues with the code, so it must be the project setup. Could you open the compiled APK with WinRAR or 7zip and verify that there is indeed a folder named assets, containing anim.bmp?
It's supposed to appear at 0,0... to move it, you'd have to change this line
,new Rect(0,0,frameWidth,frameHeight)
to
,new Rect(desiredX,desiredY,desiredX+frameWidth,desiredY+frameHeight)
I'm not really qualified to tell you how to work with resources... I just plain don't like the system, so I usually work around it and use assets instead, which are simply put into the assets folder and that's the end of it (BUT, they don't have any automatic resolution handling, so you have to implement that yourself).
Yay it worked! If I would want to use this code for my app, do I create a new class for it or just dump everything in activitys? Because right now when I dumped it in activitys it just made a white screen. No animation or anything. It's just printing out the backround.
Phew! Your code will usually consist of a bunch of classes which inherit from View, which should each be contained in their own file. This AnimView will just be one more class among your collection.
According to the guidelines, you should then reference your Classes in res/layout/*.xml, but I've never done that (this strange, broken thing that Android calls XML gives me a headache), so well... you've got to find out how to do that without my help.
Why does it still give me a white screen? It is now back to the original and no new view is implemented.
I know my code looks horrible.
You load R.layout.main... but from what I can tell, there's nothing in there?
It is suppose to be loaded I think. If you mean when it is activitys.
And yes it isn't that much code. I recently started.
Where?:
StepByStepActivity:
Code:
public class StepByStepActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
(...)
setContentView(R.layout.main);
}
}
This loads R.layout.main... as far as I can tell, that's the only thing being executed on load, aside from a few config changes.
res/layout/main.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
(...) android:id="@+id/mainlayout" (...)>
</LinearLayout>
That defines a linear layout.... but where is its content
This is the content. 5 squares and some lines drawed so it would be easyer to see where they are suppose to go.
Code:
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(android.graphics.Color.RED);
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(square1.x, square1.y, square1.x + 20, square1.y + 20,
paint);
paint.setColor(android.graphics.Color.BLUE);
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(square2.x, square2.y, square2.x + 20, square2.y + 20, paint);
paint.setColor(android.graphics.Color.GREEN);
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(square3.x, square3.y, square3.x + 20, square3.y + 20,
paint);
paint.setColor(android.graphics.Color.BLACK);
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(square4.x, square4.y, square4.x + 20, square4.y + 20,
paint);
paint.setColor(android.graphics.Color.YELLOW);
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(square5.x, square5.y, square5.x + 20, square5.y + 20,
paint);
paint.setStrokeWidth(3);
paint.setColor(android.graphics.Color.BLACK);
paint.setStyle(Paint.Style.FILL);
canvas.drawLine(95, 200, 650, 200, paint);
// 6 Vågrät
paint.setStrokeWidth(3);
paint.setColor(android.graphics.Color.BLACK);
paint.setStyle(Paint.Style.FILL);
canvas.drawLine(100, 200, 100, 480, paint);
// 5 lodrät
paint.setStrokeWidth(3);
paint.setColor(android.graphics.Color.BLACK);
paint.setStyle(Paint.Style.FILL);
canvas.drawLine(650, 200, 650, 480, paint);
// 4 lodrät
paint.setStrokeWidth(3);
paint.setColor(android.graphics.Color.BLACK);
paint.setStyle(Paint.Style.FILL);
canvas.drawLine(600, 0, 600, 200, paint);
// 3 lodrät
paint.setStrokeWidth(3);
paint.setColor(android.graphics.Color.BLACK);
paint.setStyle(Paint.Style.FILL);
canvas.drawLine(340, 200, 340, 480, paint);
// 2 lodrät
paint.setStrokeWidth(3);
paint.setColor(android.graphics.Color.BLACK);
paint.setStyle(Paint.Style.FILL);
canvas.drawLine(180, 0, 180, 200, paint);
// 1 lodrät
}

[Q] Convert TimeZone to Text

Im trying to convert the results of timezone to text for use in textView. I am new ro android devloping, so i need some help.
Here is my current code:
Code:
TimeZone tz = TimeZone.getDefault();
TextView tv = new TextView(this);
tv.setText(tz);
but it gives me this error:
Code:
The method setText(CharSequence) in the type TextView is not applicable for the arguments (TimeZone)
.
I understand the error, but I do not know how to convert the Timezone to "Eastern Standard Time" or "Pacific Standard Time" or whatever the case may be.
remember im a noob.. so please make it easy and actually show me the code. (im also new to java)
Try this:
Code:
tv.setText(tz.getDisplayName());
Whenever you are stuck its always worth looking through the android reference for the classes you are working with. You'll almost always find a solution there
http://developer.android.com/reference/java/util/TimeZone.html
that fixed it.. Thank you so much!
Yeah but like i say I'm new at java and android. So i can't understand most of it.
alexander7567 said:
that fixed it.. Thank you so much!
Yeah but like i say I'm new at java and android. So i can't understand most of it.
Click to expand...
Click to collapse
No worries. Just continue making some practice apps. And you'll get better at it!
ok my next question is.. I want this to update every 30 seconds. I thought about using a Thread.sleep(10000);, but i realize this would not work because android would detect it being an infinite loop. So how could I create some kind of a thread i believe its called to poll the timezone every 10 seconds?
Where exactly are you trying to set the text view? Is it in a while loop?
no.. the textview simply shows the timezone. I live on the timezone line and i never know what time it really is.. So i was making a app and soon a widget that shows me what timezone I'm in. It's just a learning app for me.
So i need it to poll the timezone every 30 seconds and update the textview to the current time zone.
btw.. Looked at your app and i think its really neat that you can make a game like that.. Did you have to make all the graphics or what?
I am guessing you are making the text view in the onCreate method.
Do this:
Before your activity's onCreate method, make three class members like so
Code:
private TextView textView;
private TimeZone timeZone;
private Timer timer;
Then in the onCreate method do this:
Code:
timer = new Timer();
textView = new TextView(this);
timeZone = TimeZone.getDefault();
TimerTask timerTask = new TimerTask()
{
@Override
public void run()
{
textView.setText(timeZone.getDisplayName());
}
};
timer.schedule(timerTask, 0, 30000);
Then when you wish to stop the timer do this (in the onDestroy method maybe?)
Code:
timer.cancel();
I hope you are familiar with Java's Anonymous Inner Classes. If not, I'd highly recommend first reading and getting familiar with the various Java features and techniques before continuing Android.
---------- Post added at 02:51 AM ---------- Previous post was at 02:48 AM ----------
Thank you!
Yes I made all the graphics. Except for the coin . That was made by my cousin.
i did what u said, and then i put in textView.setTextColor(929) in the time to test and see if it is actually updating, and i found out it is never running the "task"... Know what could be going on?
Sorry about the old code. I did not test it before posting it.
Anyways I've tested the following code and it works fine for me. The timerTask is called every 30 seconds. You can verify this by looking at LogCat.
Code:
public class TestActivity extends Activity
{
private TimeZone timeZone;
private TextView textView;
private Timer timer;
private Handler handler;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
textView = new TextView(this);
timeZone = TimeZone.getDefault();
timer = new Timer();
handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
textView.setText(timeZone.getDisplayName());
}
};
TimerTask timerTask = new TimerTask()
{
@Override
public void run()
{
handler.sendEmptyMessage(0);
Log.d("TestActivity", "Timer Task Called");
}
};
timer.schedule(timerTask, 0, 30000);
setContentView(textView);
}
@Override
public void onDestroy()
{
super.onDestroy();
timer.cancel();
}
}
Let me know if this works for you
What you gave me works. Thank you! I have done a lot of googling on that but there is nothing more helpful than a personalized answer.
So i think i have one last question. when you used "setContentView(textView);", it sets the whole screen to that text. And i realize this is a really noob question. But i am also trying to display other things in this app. It hides everything else.
So here is my sad atempt to fix it::
Code:
package com.alexriggs.android.timezoneclock;
import java.util.TimeZone;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
public class TimeZoneClockActivity extends Activity
{
private TimeZone timeZone;
private TextView t;
private Timer timer;
private Handler handler;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
t=new TextView(this);
t=(TextView)findViewById(R.id.textView1);
timeZone = TimeZone.getDefault();
timer = new Timer();
handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
t.setText(timeZone.getDisplayName());
}
};
TimerTask timerTask = new TimerTask()
{
@Override
public void run()
{
handler.sendEmptyMessage(0);
Log.d("TimeZoneClock", "Timer Task Called");
}
};
timer.schedule(timerTask, 0, 30000);
}
@Override
public void onDestroy()
{
super.onDestroy();
timer.cancel();
}
}
I show no compiler errors, but when i run the app i get a force close. I run it in debug mode and the error is around "t.setText(timeZone.getDisplayName());". I have tried googling but i cant seem to find the answer.
Sorry to keep bugging you, but i learn best by doing and not reading.
are there hardcode in this?
Well when you want to load up a view from an xml file you must do this:
Code:
setContentView(R.layout.main);
Where R.layout.main is the resource id of the xml file you wish to load. Only after that you can get the TextView (or any other component that is a part of that xml) using findViewById. And you don't need to do textView = new TextView(this); cause the TextView is already created when using xml, just grab a reference to it using findViewById.
So your code should look like this:
Code:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // Or whatever your layout xml's name is
t=(TextView)findViewById(R.id.textView1);
timeZone = TimeZone.getDefault();
timer = new Timer();
// The rest remains the same
Try it and let me know.
---------- Post added at 12:18 AM ---------- Previous post was at 12:16 AM ----------
randommmm said:
are there hardcode in this?
Click to expand...
Click to collapse
Sorry, but I'm not sure I understand what you mean to ask.
wow.. That was so simple i now feel stupid lol.
Yes that fixed it. Thank you so much for your help.
Ok.. new question for that same program.. I am now attempting to create a widget for this that simply displays a text view that shows the timezone. My problem is it simply show 'Large Text". It is not updating the text view to the timezone. Here is my code in AppWidget.java.
Code:
package com.alexriggs.android.timezoneclock;
import java.util.TimeZone;
import java.util.Timer;
import java.util.TimerTask;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
public class AppWidget extends AppWidgetProvider {
private TimeZone timeZone;
private TextView t;
private Timer timer;
private Handler handler;
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
t=new TextView(context);
timeZone = TimeZone.getDefault();
timer = new Timer();
handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
t.setText(timeZone.getDisplayName());
}
};
TimerTask timerTask = new TimerTask()
{
@Override
public void run()
{
handler.sendEmptyMessage(0);
Log.d("TestActivity", "Timer Task Called");
}
};
timer.schedule(timerTask, 0, 30000);
}
}
BTW... Seems like this is the hardest language I have ever tried to learn.
I realize im missing setContentView(R.layout.widget), but everytime i set it i get "The method setContentView(int) is undefined for the type AppWidget". Could that be thte issue?
Hello again
I'm not really familiar with making widgets myself, so I cannot really say what you are doing wrong. I found a link to this tutorial on making a simple widget. I think you should try and follow it and see if you can get something out of it.
Let me know if it is helpful for you or not
http://www.vogella.com/articles/AndroidWidgets/article.html
It was a lot more helpful than most things out there.
But i still am not able to understand it the best.
What i am wanting to accomplish is simply make a text view widget that displays the timezone (I.E. EST, CST, PST) and make it refresh every 30 seconds.
It isn't very hard to do in a normal app, but a widget seems impossible to me.
Is there anyone that can help me with This?

[Q] OnClickListener vs. android:OnClick

I'm looking at both a book and the beginning projects on developers.android.com and have a question. Google is utilizing the android:nClick to handle the click event which then points to a public method for execution....but the book is setting up click listeners by setting a View with the buttons ID and then executing the setOnClickListener. OnClickListenver has one method called OnClick which is then executed.
//book
public class MyGame extends Activity implements OnClickListener {
View continueButton = findViewById(R.id.continue_button);
continueButton.setOnClickListener(this);
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
View exitButton = findViewById(R.id.exit_button);
exitButton.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
}
}
My question. Which is the better way to handle the Button Click? In looking at what google does in the XML, it seems a helluva lot cleaner....but curious on what the advantage is for the other way (and I cannot think of any). Search of the site didn't yield anything.
I dunno if the methods have advantages between them, however, I prefer the androidnClick method since the code looks clearer
You usually want to use the onClick method, as there is no benefit to implementing the OnClickListener (only more effort and possible performance loss).
If you want things like long clicks or other events, you will have to implement that (or define an inline listener), as there is no xml tag for those.
I think the OnClickListener is actually only there for consistency with these other classes

[GUIDE] Customizing Sony W. Button behaviour

I recently got an SONY NWZ-F806 Walkman with Android. I bought it to listen to Spotify and everything works fine but the fact that you cannot control Spotify with the the dedicated W. button on the side of the device.
So I googled and found a solution where you had to redirect the W. button to the search button which was not satisfying to me. So I decided to investigate deeper into this and with a little help of adb and dumpsys I found the Intent that was fired when the W. button is pressed "com.sony.walkman.intent.action.WALKMAN_BUTTON". Than I found the service which was responsible for the default behavior "W.control".
So to redirect the W. button to Spotify I had to do the following things:
1) Disable the default W. behavior
To do this you simply have to deactivate the W.control service
Systemsettings > Apps > All > W.control > Disable
2) To listen to the W. button a BroadcastReceiver has to be implemented which receives to the Intent "com.sony.walkman.intent.action.WALKMAN_BUTTON" and sends a Intent to Spotify
AndroidManifest.xml
Code:
<application>
<receiver android:name=".WDotReceiver" >
<intent-filter>
<action android:name="com.sony.walkman.intent.action.WALKMAN_BUTTON" />
</intent-filter>
</receiver>
</application>
WDotReceiver.java
Code:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
public class WDotReceiver extends BroadcastReceiver {
private static final String KEY_EVENT_KEY = "android.intent.extra.KEY_EVENT";
private static final String SPOTIFY_PLAY_KEY = "com.spotify.mobile.android.ui.widget.PLAY";
private static final String SPOTIFY_NEXT_KEY = "com.spotify.mobile.android.ui.widget.NEXT";
@Override
public void onReceive(Context arg0, Intent arg1) {
Bundle extrasBundle = arg1.getExtras();
if(extrasBundle != null)
{
if(extrasBundle.containsKey(KEY_EVENT_KEY))
{
KeyEvent keyEvent = extrasBundle.getParcelable(KEY_EVENT_KEY);
if(keyEvent.getAction() == KeyEvent.ACTION_DOWN && keyEvent.getRepeatCount() == 0)
{
arg0.sendBroadcast(new Intent(SPOTIFY_PLAY_KEY));
}
else if(keyEvent.getAction() == KeyEvent.ACTION_DOWN && keyEvent.getRepeatCount() == 1)
{
arg0.sendBroadcast(new Intent(SPOTIFY_NEXT_KEY));
}
}
}
}
}
If you press the W. button for a short time a Play/Pause Intent is send and if you press the button longer Spotify will skip to the next track.
To me this increases the usability of the device tremendously.
Kind regards
Mathias
wrong forum bro

Google Maps Android changing markers.

Hello! I am working on a Google Maps Android API project and am having a difficult time. I have several markers on my map, which is an indoor location. I was able to change the color of the markers, but would like to change the icons as well. I've looked over the documentation from google and am trying to implement it but it is not working and I cannot go any further until it does. Id like to use custom images, but for now I want to just see it work. Im trying to use this code:
Code:
.icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow))
. I'm getting an error in Android studio saying that it can't resolve 'arrow' symbol. my full code is here: It doesn't seem like I'm missing any imports, so I'm not sure why this doesn't work.
Code:
package com.aa.maps;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
public Bitmap resizeMapIcons(String iconName, int width, int height){
Bitmap imageBitmap = BitmapFactory.decodeResource(getResources(),getResources().getIdentifier(iconName, "drawable", getPackageName()));
Bitmap resizedBitmap = Bitmap.createScaledBitmap(imageBitmap, width, height, false);
return resizedBitmap;
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Milwaukee, Wisconsin.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// Add a marker in Milwaukee and move the camera
LatLng mpm = new LatLng(43.0408468, -87.921474);
LatLng planetarium = new LatLng(43.040622, -87.920798);
LatLng theater = new LatLng(43.040497, -87.920640);
LatLng marketplace = new LatLng(43.040779, -87.921717);
mMap.addMarker(new MarkerOptions().position(mpm).title("Milwaukee Public Museum").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE))).showInfoWindow();
mMap.addMarker(new MarkerOptions().position(planetarium).title("The Daniel M. Soref National Geographic Planetarium").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
mMap.addMarker(new MarkerOptions().position(theater).title("The Daniel M. Soref National Geographic Theater").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
mMap.addMarker(new MarkerOptions().position(marketplace).title("Museum Marketplace"));
mMap.addMarker(new MarkerOptions().position(marketplace).title("Museum Marketplace").icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow)));
}
}

Categories

Resources