Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

button at the bottom of a LinearLayout

 
Richard Hayward
Ranch Hand
Posts: 116
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This seems the most simple thing, yet I can figure out how to do it.

In a LinearLayout, I have a Button and a class MyView that extends View. MyView occupies the space not used by the Button and just does some drawing in it's onDraw method.



It works ok, but I'd like the button at the bottom of the screen, despite the fact that it's declared before MyView in the xml.

Google brings loads of hits on how to do this, many of which must surely be way to complicated for such a simple problem. I've spent ages messing around with layout_weight, layout_gravity and various other attributes.

What is the simplest & cleanest way to achieve this?

Regards
Richard
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd like the button at the bottom of the screen, despite the fact that it's declared before MyView in the xml.

What are you trying to accomplish by that? LinearLayout displays views in the order they're defined. If you want to change that order, alter the layout file accordingly.
 
Richard Hayward
Ranch Hand
Posts: 116
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf

Ulf Dittmer wrote:
What are you trying to accomplish by that?


I did that because I want MyView to take all the remaining space once the Button has been created. If I reverse the order and declare MyView first, it seems to occupy all available screen space and the button cannot be seen.

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you are using the wrong layout. Use a RelativeLayout with the custom view set to above the button and the button set to align with the parents bottom
 
Richard Hayward
Ranch Hand
Posts: 116
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:Then you are using the wrong layout. Use a RelativeLayout with the custom view set to above the button and the button set to align with the parents bottom


But if I do that,



then the button is at the bottom, but MyView takes all the screen space, I can see it even behind (or maybe on top of), the button.
Changing MyView's layout_height attribute to either "fill_parent" or "wrap_content" still gives the same result.

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should put the button at the top of the layout file so it is sized and placed first. You will need to give the button an id. Then you use the correct layout attribute to align the custom view above the button

I forget the correct layout attribute to use but if you look up the api for RelativeLayout it should be clear.
 
Richard Hayward
Ranch Hand
Posts: 116
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve & Ulf,

Thanks for your help. I have it working now:



Above uses a SeekBar, instead of a Button but the problem & its solution is the same.

Richard
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic