Change position of ZoomControls - android

I am working on a project related with Maps. The problem stands that the MapControls are positioned in right bottom. That position get covered with something else. So i want to remove the controls from there and place them somewhere else. I don't want to use custom positioning. Thanks in advance!

Create your own ZoomControls
<ZoomControls android:id="#+id/zoomcontrols"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
and you can set onclicklistener to that.
zoomControls = (ZoomControls) findViewById(R.id.zoomcontrols);
zoomControls.setOnZoomInClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mapController.zoomIn();
}
});
zoomControls.setOnZoomOutClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mapController.zoomOut();
}
});

Related

Android widget button animation

In Android I have a widget with a button:
<Button
android:id="#+id/btnPlayPause"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/ic_button_play"/>
I want to apply a very basic animation to it when the user presses it.
Simple animation means: scale a bit down, and come back to original size.
Is this somehow achievable?
but.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
but.animate().scaleY(2f).setDuration(200).withEndAction(new Runnable() {
#Override
public void run() {
but.animate().scaleY(1f);
}
});
}
});

Android ImageButton's image to be replaced with another image

I have an ImageButton and I want that onClick would replace it with another image (flip back and forth) and on a long press, would replace it to another image.
How can I do that?
I don't feel like reading long documentaries for this.
Set onClickListeners for your button then change the drawable. Since you don't have any code, the following is based on a dynamic ImageButton that only outlines how to perform the action you want. I suggest you define your ImageButton in your XML layout first and then use
iBtn = (ImageButton) findViewById(R.id.btnID);
ImageButton iBtn = new ImageButton(this);
iBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
iBtn.setImageDrawable(getResources().getDrawable(R.drawable.img1);
}
});
iBtn.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
iBtn.setImageDrawable(getResources().getDrawable(R.drawable.img2);
return true;
}
});
If you're going to learn Android (or any language or platform really), you should really get comfortable reading the documentation provided, as it will give you answers to many basic questions, such as how to use various methods and classes.
That aside, you need to set both an OnClickListener and an OnLongClickListener for your button. Then inside those listeners, you'll need to set the image using the setImageResource() method. That method requires a drawable image, which you should have saved in your drawable folder (if not, put it there!)
You didn't post your existing code, so here's a generic example.
ImageButton button = (ImageButton) findViewById(R.id.img_button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button.setImageResource(R.drawable.pic1);
}
});
button.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
button.setImageResource(R.drawable.pic2);
return true; // <-- This must be true.
}
});
You could read further about how to use any buttons in the button guide, you'll just be swapping for ImageButton where appropriate.
Add ImageButton to your layout :
<ImageButton
android:id="#+id/img_btn1"
android:src="#drawable/imgc"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
and then add this code to your Activity Oncreate() method
ImageButton imageButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageButton = (ImageButton) findViewById(R.id.img_btn1);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
imageButton.setImageResource(R.drawable.imga);
}
});
imageButton.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
imageButton.setImageResource(R.drawable.imgb);
return true;
}
});
}
change imga , imgb, imgc names according to your images taht are placed in drawable folder

Changing imagebutton image(from gallery) with longclick

I've searched the forums, but not have found any specific or understandable answers for my problem.
I'd like to change my Imagebutton image to a picture, selected from the gallery. Prefferrably the image should stay changed after closing the application.
My XML for the button is here:
<ImageButton
android:id="#+id/eat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:adjustViewBounds="true"
android:background="#drawable/eat"
android:clickable="true"
android:longClickable="true"
android:scaleType="fitCenter" />
The java code for playing the sound is here with the OnClick method.
ImageButton eat = (ImageButton) findViewById (R.id.eat);
eat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp1.start();
}
});
I would like to add the OnLongClick method here too, (since to OnClick is allready taken and the image replacing should be a little different), but havent found the right way. Can You please guide me a little bit?
You need to return true from image's onLongClickListener.
Like this:
eat.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
//do something
return true;
}
});
eat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp1.start();
}
});
This will not cause the image's onClickListener to be called as it means that the action has already been handled in longClickListener.

Android: Why is the zoom control like this?

Why does the zoom control show like this? It only happens on QVGA resolution smulator. It works perfectly on HVGA resolution.
This is the code that I used.
ZoomControls zoomControls = (ZoomControls) findViewById(R.id.zoomcontrols);
zoomControls.setOnZoomInClickListener(new View.OnClickListener() {
public void onClick(View v) {
mc.zoomIn();
}
});
zoomControls.setOnZoomOutClickListener(new View.OnClickListener() {
public void onClick(View v) {
mc.zoomOut();
}
});
zoomcontrols defined in xml as:
<ZoomControls android:id="#+id/zoomcontrols"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
Shouldn't be nothing wrong with your code. It could be a emulator bug. You should test it on a real device if you can.
It seems you are not the only one. Zoom controls on emulator running Android 1.6 with QVGA

Always show zoom controls on a MapView

Is there a way to always show the zoom controls on a MapView? I have added the zoom controls using
map=(MapView)findViewById(R.id.map);
map.setBuiltInZoomControls(true);
but the zoom controls fade in and out. I want them to always be visible.
Solved this by putting my own ZoomControls in my layout xml:
<ZoomControls android:id="#+id/zoomcontrols"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
and then setting up onClickListeners for then to handle the zoom:
zoomControls = (ZoomControls) findViewById(R.id.zoomcontrols);
zoomControls.setOnZoomInClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mapController.zoomIn();
}
});
zoomControls.setOnZoomOutClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mapController.zoomOut();
}
});
Intrications' solution does work, but you lose the built in functionality where the button disables when you have zoomed in or out all the way. You can retain this functionality by using the following instead:
localMapView.getZoomButtonsController().setAutoDismissed(false);
However, this does not let you place the controls wherer you want in the panel, and although it does not go away, it does not show up the first time until the user touches the screen once. I tried calling setVisible(true) on the controller, but that does not seem to work for some reason.

Categories

Resources