Drawable item don't show up - java

I just started Android development and working on a little app related to google maps api. Am using the Google Map View for this and following this tutorial..
I've created a custom itemizedOverlay, which has a constructor like this(as told in the tutorial) -
public pujaItemizedOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
mContext = context;
}
I have a image file named sprite.png in the res/drawable/ forder. And here is my onCreate() function -
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.sprite);
pujaItemizedOverlay itemizedoverlay = new pujaItemizedOverlay(drawable, this);
GeoPoint point = new GeoPoint(19240000,-99120000);
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
The problem is, The image named sprite doesn't show up on the map.
One this I'd like to mention is that, according to the tutorial, they added a second paramete to the constructor of the custom itemizedOverlay of the class Context. But in their example, when they called that overlay, they provided only one parameter(check the tutorial page), like -
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);
Eclipse showed an obvious error in this line, so I added the second parameter as this to provide the current context. Am I doing it right here?
Update:
The image in question is here.

Found the solution here. It seems that if the mapview doesn't know how to align the image it won't show it at all.

Related

The java backend code to the xml code

I have a basic android application that has several pages accessible from the home page, anyways I change the layout by doing
public void main(View view) {
setContentView(R.layout.main);
}
and changing the layout to the main game page. Now I want to add characters to it and so I added protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint green = new Paint();
green.setColor(Color.GREEN);
Rect theRect = new Rect();
theRect.set(0,0,canvas.getWidth()/2,canvas.getHeight()/2);
canvas.drawRect(theRect, green);
}
but I only want to call it for the "main" or the scene/layout that the game is played in. It appears to not be called at all and that may be due to the fact that I do not completely understand the relationship between the xml and multiple java files and functions. I'm new to android so this may be a stupid question, I just couldn't find anything with multiple web searches, with no luck.
You can add your game view into activity class:
Class GameView extends View/SurfaceView
{
//-- contains onDraw() method
}
To add this game view into your activity, you have to do
get reference of layout in whioch you want to add this gameview.
eg: LinearLayout layout=(LinearLayout) findViewById(R.id.linearlayout);
Add GameView into this layout
eg: layout.addView(new GameView());
For more detail, you can get reference from http://www.edu4java.com/en/androidgame/androidgame3.html
Hope this helps you

How to make universal image loader load image buttons?

I'am sorry for the dumb question but I'am still a beginner.
I'am using universal image loader : https://github.com/nostra13/Android-Universal-Image-Loader
(Android, Eclipse)
and I don't know how to make it load my image buttons(nearly 30 imagebuttons per layout), I don't want to change anything in the image I just want it to display it as it is in the xml file.
So if you could tell me what code should I type that would be great, Thanks
for example if I have this ImageButton set up in my java file:
ImageButton staff = (ImageButton) findViewById(R.id.staff);
and my java file is called MainActivity and this image is saved in my drawable folder what should I do.
a part of my code:
public class MainActivity extends Activity implements OnClickListener {
SoundPool sp;
int soundId;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
soundId = sp.load(kh2hd.this, R.raw.btnclick, 1);
Picasso.with(this).load(R.drawable.staff).into(staff);
ImageButton staff = (ImageButton) findViewById(R.id.staffbtn);
staff.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.staffbtn:
sp.play(soundId, 1, 1, 0, 0, 1);
startActivity(new Intent("android.intent.action.INFO"));
break;
Download Picasso library from here
Add it to your build path. Import this library to your code file.
For loading from web url:
String url = "http://example.com"
Picasso.with(getApplicationContext()).load(url).into(imageView);
For loading from drawable:
Picasso.with(getApplicationContext()).load(R.drawable.imageFile).into(imageView);
Here, imageView is the name of variable you are using to represent ImageView. Eg.
ImageView imageView = (ImageView) findViewById(R.id.ImageView1);
If you are using ImageButton, change imageView variable to imageButton variable
ImageButton imageButton = (ImageButton)findViewById(R.id.imageButton);
Note that I am passing variable name.
#user3896367 i think below code is use full for you. if you upload image from drawable of web url using this code.
download universal image loader jar.
put jar in lib folder and add jar.
create application class.
public static void initImageLoader(Context context) {
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
context)
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.discCacheFileNameGenerator(
new Md5FileNameGenerator())
.tasksProcessingOrder(QueueProcessingType.LIFO)
.build();
ImageLoader.getInstance().init(config);
}
4.user imageloader like below syntext.
imageLoader = ImageLoader.getInstance();
options = new DisplayImageOptions.Builder()
.showImageOnLoading(
R.drawable.wallpaper_placeholder)
.showImageForEmptyUri(
R.drawable.wallpaper_placeholder)
.showImageOnFail(R.drawable.wallpaper_placeholder)
.displayer(new FadeInBitmapDisplayer(500))
.cacheInMemory(true).cacheOnDisk(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565).build();.
imageLoader.displayImage(
URL or ResourceID,
imageButton, options);
try this.

Add a Line/Image above ActionBar

I want to add a line above the action bar like in the "pocket"-app. How can i do this?
Here is a picture for example:
Thanks
tomtom
Taking advantage of an Activity's WindowManager, we can draw any view we want on top. Here's some (half-pseudo) code that should help:
// Create an instance of some View that does the actual drawing of the line
View customView = new CustomView(<some context>);
// Figure out the window we have to work with
Rect rect = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
// Make sure the view is measured before doing this
int requestedHeight = customView.getLayoutParams().height;
// setup the params of the new view we'll attach
WindowManager.LayoutParams wlp = new WindowManager.LayoutParams(
rect.width(), requestedHeight,
WindowManager.LayoutParams.TYPE_APPLICATION_PANEL,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
// set the parameters so we fit on the top left of the window
wlp.x = 0;
wlp.y = rect.top;
wlp.gravity = Gravity.TOP;
// finally add it to the screen
getWindowManager().addView(header, wlp);
The only thing to be careful is that you can't run that code from onCreate() or any lifecycle method of the Activity because the Window won't have been created yet (You'll get a BadTokenException). One way might be post a Runnable on the Window's DecorView so that your code to add the CustomView runs after the Window is created:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//...
getWindow().getDecorView().post(<Runnable that execs code above>);
}
As for the actual CustomView that will display that multi-coloured bar, I feel like that's a good exercise :-)
All you'd need to do is have the onDraw() method use canvas.drawRect() with specific x and widths.
Hope that helps.
What Pocket does
As for how Pocket actually does it. If you use HierarchyViewer on the Pocket app, you'll be able to determine that Pocket uses a custom class for their ActionBar. Since they already rebuild all the features of the ActionBar for their needs, in their case, adding the line is like adding a regular View to some ViewGroup.

Android overlaying GLSurfaceView with camera's SurfaceView

I'm looking to put an openGL model (via GLSurfaceView) on top of the camera, which has its own SurfaceView. Surely this must be possible, apps like Layar do this.
Here is what I am trying:
public class MainActivity extends Activity {
private CameraSurfaceView mCameraSurfaceView;
private GLSurfaceView mCubeGLSurfaceView;
private FrameLayout mFrameLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mFrameLayout = new FrameLayout(this);
mCameraSurfaceView = new CameraSurfaceView(this);
mCubeGLSurfaceView = new GLSurfaceView(this);
mCubeGLSurfaceView.setRenderer(new CubeRenderer());
//mCubeGLSurfaceView.setZOrderMediaOverlay(true);
mFrameLayout.addView(mCameraSurfaceView);
mFrameLayout.addView(mCubeGLSurfaceView);
setContentView(mFrameLayout);
}
}
Right now: just the camera shows up. I've read online about the SetZOrderMediaOverlay attribute for GLSurfaceView; setting it TRUE in the above code has the 3D cube show up with a black background (i.e. no camera preview shown).
References:
Here is the Cube OpenGL code I'm using: http://intransitione.com/blog/create-a-spinning-cube-with-opengl-es-and-android/
Here is the CameraSurfaceView code:
https://github.com/davefp/android-camera-api-example/blob/master/src/com/example/cameraexample/CameraSurfaceView.java
Other tests I've tried: changing the addView order of the respective surfaceviews doesn't help; it only shows the camera (I read somewhere changing the addView order may help).
Thanks for any help!
Since you state that only the camera preview is currently visible it may be that you have not ordered your views correctly. This is what I have done to create what you desire.
In the Activity create
instance of MySurfaceView derived from GLSurfaceView
instance of MyCameraPreview derived from SurfaceView
in my Activity's onCreate method I do the following:
mySurfaceView = new MySurfaceView(this);
addContentView(mySurfaceView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
myCameraPreview = new MyCameraPreview(this, myCamera);
addContentView(myCameraPreview, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Please note that using this method you do not use a layout xml.
This will create your two views on top of each other with the camera preview in the background.
Now you need to get the MySurfaceView to draw a transparent background so the camera preview will show behind the objects you render. If you are using OpenGL ES 2.0 add this to the constructor of your MySurfaceView class.
setEGLContextClientVersion(2);
setEGLConfigChooser(8,8,8,8,16,0);
getHolder().setFormat(PixelFormat.TRANSLUCENT);
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
I just worked through this process myself so I may have missed some details.

Using ItemizedOverlay and OverlayItem In Android Beta 0.9

Has anyone managed to use ItemizedOverlays in Android Beta 0.9? I can't get it to work, but I'm not sure if I've done something wrong or if this functionality isn't yet available.
I've been trying to use the ItemizedOverlay and OverlayItem classes. Their intended purpose is to simulate map markers (as seen in Google Maps Mashups) but I've had problems getting them to appear on the map.
I can add my own custom overlays using a similar technique, it's just the ItemizedOverlays that don't work.
Once I've implemented my own ItemizedOverlay (and overridden createItem), creating a new instance of my class seems to work (I can extract OverlayItems from it) but adding it to a map's Overlay list doesn't make it appear as it should.
This is the code I use to add the ItemizedOverlay class as an Overlay on to my MapView.
// Add the ItemizedOverlay to the Map
private void addItemizedOverlay() {
Resources r = getResources();
MapView mapView = (MapView)findViewById(R.id.mymapview);
List<Overlay> overlays = mapView.getOverlays();
MyItemizedOverlay markers = new MyItemizedOverlay(r.getDrawable(R.drawable.icon));
overlays.add(markers);
OverlayItem oi = markers.getItem(0);
markers.setFocus(oi);
mapView.postInvalidate();
}
Where MyItemizedOverlay is defined as:
public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
public MyItemizedOverlay(Drawable defaultMarker) {
super(defaultMarker);
populate();
}
#Override
protected OverlayItem createItem(int index) {
Double lat = (index+37.422006)*1E6;
Double lng = -122.084095*1E6;
GeoPoint point = new GeoPoint(lat.intValue(), lng.intValue());
OverlayItem oi = new OverlayItem(point, "Marker", "Marker Text");
return oi;
}
#Override
public int size() {
return 5;
}
}
For the sake of completeness I'll repeat the discussion on Reto's post over at the Android Groups here.
It seems that if you set the bounds on your drawable it does the trick:
Drawable defaultMarker = r.getDrawable(R.drawable.icon);
// You HAVE to specify the bounds! It seems like the markers are drawn
// through Drawable.draw(Canvas) and therefore must have its bounds set
// before drawing.
defaultMarker.setBounds(0, 0, defaultMarker.getIntrinsicWidth(),
defaultMarker.getIntrinsicHeight());
MyItemizedOverlay markers = new MyItemizedOverlay(defaultMarker);
overlays.add(markers);
By the way, the above is shamelessly ripped from the demo at MarcelP.info. Also, here is a good howto.
try :
Drawable defaultMarker = r.getDrawable(R.drawable.icon);
defaultMarker.setBounds(0, 0, defaultMarker.getIntrinsicWidth(),
defaultMarker.getIntrinsicHeight());
MyItemizedOverlay markers = new MyItemizedOverlay(defaultMarker);
overlays.add(markers);

Categories

Resources