I want on the initialization of my activity in android to set the position of some imageView's
by code.
Lets say I have five cards displayed on the screen, all placed in (0,0) by me in the XML.
I want to calculate the screen size (easy to do) and then place the first card at 0.2height , 0.2 width the second one 0.4height, 0.4 width, ETC.
I want to do it through code so i could change some constants in the future and the rest of the changes will apply automatically
Thanks.
Create a LinearLayout container view in your layout, and then use code to add your new View to it
<LinearLayout
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
And then:
((ViewGroup)findViewById(R.id.container)).addView(myImageView);
try to use "findViewById()" to inflate some parental layouts,
"new ImageView()" to construct new Views and ".add(myImageView)" to add them.
In your code u can use all kinds of setters for the imageview...
(btw: more details would be great, the question is kinda ambiguous)
Related
I have a special requirement in one of my project. I have an ArrayList of items and I want to show the Views like in the image below.
All the brown boxes are View's (TextView or LinearLayout or Button). I want to add them in the order they are numbered. Their width depends on the content inside them i.e. length of text in the case of TextView.
When I add a new view, I want it to be on the right side of the previous view if their is space otherwise it should go in the next line/row.
How can I accomplish this?
I like to use FlowLayout. Super simple to use and doesn't require you re-inventing the wheel.
Just change your root view in the xml file to this:
<org.apmem.tools.layouts.FlowLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
And thats all it takes!
I used this third party library to solve this.
If i try to make the thing u see in the screenshot it just falls back to original size. I can't change sizes of any view Objects. Anybody knows a fix?
http://imgur.com/2S1xoLP
In Eclipse (as you're using the designer), you can set the Width and Height of a View, within the Layout Parameters section of the Properties pane. This can be set to wrap_content, match_parent or fill_parent.
You can also do this within the XML markup of the activity you're editing. Click the .xml tab at the bottom of your designer, and you'll see all of the XML that makes up your activity. Once in there, find the problematic view and add:
android:layout_width="match_parent"
android:layout_height="match_parent"
Edit
Also, when inside of a RelativeLayout, it's possible that Eclipse will add default padding values, so regardless of what you set, your View's wont reach the parent layout's edge, until you remove them. Just FYI!
I've created a custom dialog builder that contains 2 buttons.
Depending on the dialog's setup, I may choose to hide one of the buttons completely, using Window.GONE.
Ideally what I want to happen is:
1. If there is only one button, then fill the layout with it
2. If there are two buttons, then split up the space in the layout equally with these
Is it possible to do this without having to work out the width of the dialog, the number of buttons and then set the sizes manually?
I was hoping there may be a neater way to perform this
ok, here is how I would do it:
<LinearLayout layout_width:fill_parent layout_height:wrap_content>
<Button
layout_width=fill_parent
layout_height=wrap_content
layout_weight=1/>
<Button
layout_width=fill_parent
layout_height=wrap_content
layout_weight=1/>
</LinearLayout>
The trick is to put both elements a width of fill_parent and a weight of 1. If they are both drawn, they will each take up half the screen. If you use View.Gone, one of them will disappear and the other should take up all the space.
yes of cource put your views means button in linear layout and give yor buttons equal layout_weight will solve your problem.
How do I go about implementing a button bar with buttons of different shapes and heights? As an example (please excuse my poor drawing skills for this quick mockup):
The example bar has 3 buttons, with the middle button (3) a different shape and height than the other 2 buttons (1,2). The button bar will be a view that is included and merged into other views so as to seem to float on top of the parent view.
I was thinking of implementing buttons 1 and 2 into a layout, and then button 3 as another layout that I then merge with the first two button's layout.
like my previous comrades said, you need some kind of layout or container that can have a background (if you wish for button #3 to hoover above it) then use relative layout for mixing the two the disadvantage of this other than complexity is that youcannot relate to the other two buttons since they reside in a different layout.
More elegant solution may be to have a special background drawable that can:
have a method setCurrentHeight() that will specify the height the actual viewable section should have the rest will be filled with transparent color.
override it's own draw so just before it's drawing it will have a callback called, call back you can register yourself to.
then you can register the callback in your activity to take the current position of the #3 button and set the height accordingly, this way you are still with one layout with special drawable as background.
A customized LevelDrawable might do the trick.
I would layout this bar as follows:
A RelativeLayout as a container for the rest, with height set to wrap_content and alignparentbottom = true
An ImageView for the bar
2 Buttons with a transparent background (Button 1 and 2)
Button 3 is a custom Button with a custom Image of a trapezoid as background
So you will have a Layout similar to this:
<RelativeLayout
...>
<ImageView
.../>
<Button
... Button 1 />
<Button
... Button 2 />
<Button
... Button 3 />
</RelativeLayout>
I don't exactly know that this will work, and I can't test it, but you might give something like this a try; I believe it can all be done elegantly through XML.
Have a RelativeLayout (id:mainLayout) that will contain all of your views, wrap_content for both dimensions.
Have a blank View as your first child that will serve as your background bar
Set the View's background color/image to what you want; layout_width to fill_parent; layout_height to wrap_content; layout_alignTop="#id/leftButton"; layout_alignBottom="#id/leftButton".
Add an ImageButton for your center button (id:bigButton), wrap_content for both dimensions; layout_centerInParent="true".
Add an ImageButton for your left button (id:leftButton), wrap_content for both dimensions; layout_toLeftOf="#id/bigButton"; layout_centerInParent="true".
Add an ImageButton for your right button (id:rightButton), wrap_content for both dimensions; layout_toRightOf="#id/bigButton"; layout_centerInParent="true".
In my head, I believe this works, but I could be off. Regardless, something to think about, and I hope it helps you find a solution. :)
Better you can tablelayout with different button styles or relative layout for button "3"
I'm having trouble developing the UI for my first lame "game".
Here is a screenshot.
I'm using a LinearLayout that contains a TableLayout with TableRows. It seems so tedious and hard to control the position of elements.
For example, to get things to line up, I've inserted empty TextViews to "push" other elements into place.
I've also added padding to the buttons to get them to be the size I want.
Is there a better way of doing this?
Thanks!
You definitely want to be using a Relative Layout for this.
You would be able to specify where each button is in relation to other buttons.
Absolutely AVOID developing your UIs the way you are currently trying. The TextViews will be different sizes for different distributions of Android, and will likely only look right on the device you tested them for.
EDIT:
If you need empty space, use the XML attribute android:weightSum="x" in the parent view and android:layout_weight="y" in the child. This will make the child take up (y/x) of the space allotted to it in the layout_height and layout_width.
EDIT:
I think another good bit of advice for this would be to use individual layouts for things like your "direction" buttons. You'll be able to handle where they are on the screen as a group, instead of having to move each individually.
You should use RelativeLayout to solve this problem. I've gone through a similar problem once...
I didn't use the the Android's default buttons, for I had my own images for the pressed and unpressed behaviors...
Let suppose you want to place the east "button". You could use a function like:
public void addEastImageView(RelativeLayout myBackgroundLayout, ImageView center, ImageView east, int leftPadding, int topPadding, int rightPadding, int bottomPadding){
RelativeLayout.LayoutParams rightSide = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rightSide.addRule(RelativeLayout.RIGHT_OF, center.getId());
east.setPadding(leftPadding, topPadding, rightPadding, bottomPadding);
myBackgroundLayout.addView(east, rightSide);
}
The ImageView called "center" would be the one you called "i" in your image. The padding parameters would allow you to control the distance between the ImageViews. You can create functions like this one to add the "west", "south" and "north" buttons also: you just have to change the parameter "RelativeLayout.RIGHT_OF" to "RelativeLayout.LEFT_OF", "RelativeLayout.BELLOW" and "RelativeLayout.ABOVE" accordingly.
If you want some behavior for your ImageViews, you just have to set it in the setOnClickListener. You can then change your ImageView's "image" with setBackgroundResource, for example, and set the others logic behaviors you want.
Hope it helps :D
Use an AbsoluteLayout - it lets you state exactly where to put every element
http://mobiforge.com/designing/story/understanding-user-interface-android-part-1-layouts
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
android:layout_width="188px"
android:layout_height="wrap_content"
android:text="Button"
android:layout_x="126px"
android:layout_y="361px"
/>
<Button
android:layout_width="113px"
android:layout_height="wrap_content"
android:text="Button"
android:layout_x="12px"
android:layout_y="361px"
/>