how could I let a button on a imageview

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

how could I let a button on a imageview

Postby slashgill » Thu Jan 28, 2010 2:55 pm

hi,
i want a button on a imageview, but i got a error message.

it is my mail.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<ImageView
android:id="@+id/map_img"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/map"/>
</LinearLayout>

when i put a button on the ImageView, it became that:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<ImageView
android:id="@+id/map_img"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/map"/>
<Button android:text="@+id/Button01" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>

But i still cannot see the button,
how could i solve it? :?
thanks.
slashgill
Junior Developer
Junior Developer
 
Posts: 16
Joined: Mon Jan 25, 2010 5:14 am

Top

Postby icobasco » Thu Jan 28, 2010 3:46 pm

why don't implement the onClickListener on the ImageView and remove the button? :)
User avatar
icobasco
Senior Developer
Senior Developer
 
Posts: 108
Joined: Thu Jun 04, 2009 11:12 am
Location: Italy

Postby slashgill » Thu Jan 28, 2010 4:00 pm

Because i will use 3 buttons on the imageview....
slashgill
Junior Developer
Junior Developer
 
Posts: 16
Joined: Mon Jan 25, 2010 5:14 am

Postby icobasco » Thu Jan 28, 2010 4:04 pm

slashgill wrote:Because i will use 3 buttons on the imageview....


This is a nice reason :D

Don't use LinearLayout but RelativeLayout. You can put the imageview and then using alignTop, toLeftOf etc etc layout the buttons wherever you want on it.
User avatar
icobasco
Senior Developer
Senior Developer
 
Posts: 108
Joined: Thu Jun 04, 2009 11:12 am
Location: Italy

Postby jagtap.jj1 » Sat Jan 30, 2010 3:43 pm

Use FrameLayout
Take Help of this Tut http://mobiforge.com/designing/story/un ... -1-layouts
First FrameLayout will come inside frame layout ImageView will come and then RelativeLaout(Or any other Layout which fit the situation) having 3 buttons, The RelativeLayout having 3 button will get overlapped on imageview. So if the ImageView is big enough to give accomplish relativelayout it will resolve you issue.
java-android developer
User avatar
jagtap.jj1
Senior Developer
Senior Developer
 
Posts: 124
Joined: Fri Aug 22, 2008 11:52 am
Location: India(Mumbai)

Postby icobasco » Sun Jan 31, 2010 8:19 pm

jagtap.jj1 wrote:Use FrameLayout
Take Help of this Tut http://mobiforge.com/designing/story/un ... -1-layouts
First FrameLayout will come inside frame layout ImageView will come and then RelativeLaout(Or any other Layout which fit the situation) having 3 buttons, The RelativeLayout having 3 button will get overlapped on imageview. So if the ImageView is big enough to give accomplish relativelayout it will resolve you issue.


what!?
User avatar
icobasco
Senior Developer
Senior Developer
 
Posts: 108
Joined: Thu Jun 04, 2009 11:12 am
Location: Italy

Top

Postby BUKsAPPs » Sun Jan 31, 2010 9:22 pm

Does your image fill up the whole screen? I think the problem with the button not showing up is that it is actually after the image view, not inside it. The ImageView is entirely defined in one tag ending at the "/>" there is no "</ImageView>" so the button is not inside it. But I am not sure if moving the button inside the imageView is goign to work for you anyway. I am not sure if ImageView is a view container, but I can't test this right now.

There are 2 possible solutions that I can think of though.
Don't use an ImageView at all, and make your LinearLayout show the image with the property
android:background="@drawable/map". All the views and objects within the LinearLayout will be drawn on top of the image.

Another option that I have never tested, but I think is what jagtap is suggesting is to use a FrameLayout. All objects within a frameLayout are set a the top left corner. So if you add an imageView and an LayoutView both inside the frameLayout, they will sit on top of each other. Just make sure you add the imageView first and that the LayoutView(can be LinearLayout or RelativeLayout or any other view container) has a transparent background.

Again, I can't test this right now to get the exact code, but both options should be doable, for what you have described so far. There are strengths and weakness to both depending on the entire layout.
BUKsAPPs
Claustrum Lite - Area capture game
R:P:S Academy - play Rock/Paper/Scissors online
BUKsAPPs
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Sat Jan 16, 2010 9:01 pm
Location: USA

Postby icobasco » Sun Jan 31, 2010 9:31 pm

BUKsAPPs wrote:Does your image fill up the whole screen? I think the problem with the button not showing up is that it is actually after the image view, not inside it. The ImageView is entirely defined in one tag ending at the "/>" there is no "</ImageView>" so the button is not inside it. But I am not sure if moving the button inside the imageView is goign to work for you anyway. I am not sure if ImageView is a view container, but I can't test this right now.

Really wrong. ImageView is really not a container as you think. You need layouts for this purpose.

BUKsAPPs wrote:There are 2 possible solutions that I can think of though.
Don't use an ImageView at all, and make your LinearLayout show the image with the property
android:background="@drawable/map". All the views and objects within the LinearLayout will be drawn on top of the image.

yes, it could be a solution. I don't know where he wanna put his 3 buttons but i think that LinearLayout is not the right solution.
Again RelativeLayout or FrameLayout.
User avatar
icobasco
Senior Developer
Senior Developer
 
Posts: 108
Joined: Thu Jun 04, 2009 11:12 am
Location: Italy

Postby BUKsAPPs » Sun Jan 31, 2010 9:46 pm

icobasco wrote:
BUKsAPPs wrote:Does your image fill up the whole screen? I think the problem with the button not showing up is that it is actually after the image view, not inside it. The ImageView is entirely defined in one tag ending at the "/>" there is no "</ImageView>" so the button is not inside it. But I am not sure if moving the button inside the imageView is goign to work for you anyway. I am not sure if ImageView is a view container, but I can't test this right now.

Really wrong. ImageView is really not a container as you think. You need layouts for this purpose.

That is what I hinted at. I didn't think ImageView was a container, but wasn't positive. That is why I said I don't think it will work putting the button inside the imageView. I was trying to illustrate why the button currently isn't visible.
icobasco wrote:
BUKsAPPs wrote:There are 2 possible solutions that I can think of though.
Don't use an ImageView at all, and make your LinearLayout show the image with the property
android:background="@drawable/map". All the views and objects within the LinearLayout will be drawn on top of the image.

yes, it could be a solution. I don't know where he wanna put his 3 buttons but i think that LinearLayout is not the right solution.
Again RelativeLayout or FrameLayout.

I agree, for a complex case. That is why I mentioned 2 possible solutions. But the most simple case of 3 buttons on an image, making the image the background of the layout(whatever type it is) is a very quick and easy solution. Advanced button placement probably needs a relative layout, but the immediate problem I was trying to address was putting buttions on top of an image, and I did that yesterday with a single linearLayout and some buttons.

I think the more possible solutions the better, so I really like to hear all of the ideas out there, and alos like to mention the easy and complex that I can think of for a problem.
BUKsAPPs
Claustrum Lite - Area capture game
R:P:S Academy - play Rock/Paper/Scissors online
BUKsAPPs
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Sat Jan 16, 2010 9:01 pm
Location: USA

Postby jagtap.jj1 » Mon Feb 01, 2010 5:21 pm

what!?


What I am suggesting is the way (which I have implemented), to how one should put button on ImageView.
Thats it.
java-android developer
User avatar
jagtap.jj1
Senior Developer
Senior Developer
 
Posts: 124
Joined: Fri Aug 22, 2008 11:52 am
Location: India(Mumbai)

Postby icobasco » Mon Feb 01, 2010 5:24 pm

jagtap.jj1 wrote:
what!?


What I am suggesting is the way (which I have implemented), to how one should put button on ImageView.
Thats it.


My "what" :P was about the language, cause sorry I didn't catch you :(
User avatar
icobasco
Senior Developer
Senior Developer
 
Posts: 108
Joined: Thu Jun 04, 2009 11:12 am
Location: Italy

Postby icobasco » Mon Feb 01, 2010 7:40 pm

Didn't want to be rude :)
If so, just asking for pardon
User avatar
icobasco
Senior Developer
Senior Developer
 
Posts: 108
Joined: Thu Jun 04, 2009 11:12 am
Location: Italy

Postby jagtap.jj1 » Tue Feb 02, 2010 5:44 pm

Cool, Thats Ok.
java-android developer
User avatar
jagtap.jj1
Senior Developer
Senior Developer
 
Posts: 124
Joined: Fri Aug 22, 2008 11:52 am
Location: India(Mumbai)

Postby slashgill » Wed Feb 03, 2010 2:03 am

Thanks all.
Finally I use the LinearLayout and android:background to slove my problem.
In my case, the Image is a background, so useing android:background is an easy way.

And I don't use FrameLayout is because the Views is always anchored to the top left of the layout.
I have 3 buttons, i don't want to see them cover each others.
slashgill
Junior Developer
Junior Developer
 
Posts: 16
Joined: Mon Jan 25, 2010 5:14 am

Postby icobasco » Wed Feb 03, 2010 9:24 am

slashgill wrote:Thanks all.


You're welcome :)
User avatar
icobasco
Senior Developer
Senior Developer
 
Posts: 108
Joined: Thu Jun 04, 2009 11:12 am
Location: Italy

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Google [Bot] and 7 guests