Android dynamicly added button doesnt fit into layout - android

I have a RelativeLayout with 5 buttons :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#212121"
tools:context="com.example.test.MainActivity"
android:id="#+id/menu"
android:gravity="center">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="#+id/main_menu"
android:gravity="center">
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="#string/singleplayer_text"
android:id="#+id/singleplayer_button"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:onClick="initializeGame"
android:clickable="true"
android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/multiplayer_button"
android:layout_alignStart="#+id/multiplayer_button"
android:background="#drawable/button"
android:textColor="#212121" />
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="#string/multiplayer_text"
android:id="#+id/multiplayer_button"
android:layout_below="#+id/singleplayer_button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="#drawable/button"
android:textColor="#212121" />
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="#string/deck_name"
android:id="#+id/deck_button"
android:layout_below="#+id/multiplayer_button"
android:layout_alignLeft="#+id/multiplayer_button"
android:layout_alignStart="#+id/multiplayer_button"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="#drawable/button"
android:textColor="#212121" />
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="#string/options_text"
android:id="#+id/options_button"
android:layout_below="#+id/deck_button"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="#drawable/button"
android:textColor="#212121" />
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="#string/exit_text"
android:id="#+id/exit_button"
android:layout_below="#+id/options_button"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="#drawable/button"
android:textColor="#212121" />
</RelativeLayout>
Sometime in my game , i come to the point where i want to save it.And when it is saved, there should be another button added at my menu.
resume= new Button(this);
resume.setId((int) Math.random() * 10);
resume.setText("Resume");
resume.setTextColor(Color.parseColor("#212121"));
resume.setBackgroundResource(R.drawable.button);
int margin = (int) (8 * scale + 0.5f);
RelativeLayout main_menu = (RelativeLayout)findViewById(R.id.main_menu);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
lp.setMargins(0, margin, 0,margin);
main_menu.addView(resume, lp);
The problem is, that this new button resume covers the singleplayer button, but i want it on top of it and all buttons should still be centered.
Is this possible?

Hope this helps.
lp.addRule(RelativeLayout.BELOW, R.id.below_id);
Replace below_id with your specific id.

Adds a layout rule to be interpreted by the RelativeLayout. This
method should only be used for constraints that don't refer to another
sibling (e.g., CENTER_IN_PARENT) or take a boolean value (TRUE for
true or 0 for false). To specify a verb that takes a subject, use
addRule(int, int) instead.
According to your requirement doesnt fit into layout , You can remove
Remove this First Then rectify your addRule
Read RelativeLayout add rule
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"

Related

layout_weight in float and decimal

I change the width of a view dynamically in my code using:
final ImageButton ButtonTwo = (ImageButton)findViewById(R.id.callButton);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) ButtonTwo.getLayoutParams();
params.weight = 1.3f;
ButtonTwo.setLayoutParams(params);
The idea is that it matches another view in XML :
<ImageButton
android:layout_height="match_parent"
android:layout_width="0dp"
android:id="#+id/imageButton"
android:src="#drawable/softkeyboard"
android:clickable="true"
android:onClick="softkeyboardButton"
android:layout_gravity="center_horizontal"
android:layout_weight="1.3"
android:background="#drawable/buttonstates"
/>
They're not the same width - the first button is wider than the second one. I know that this is because one is float and one is decimal, but how can I make them both the decimal size, or should I just do trial and error on the float value? My params.weight only accepts float values.
EDIT:
Here's my XML code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
tools:context=".MainActivity">
<ListView
android:id="#+id/ListView_2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1">
</ListView>
<!--Let's set the height of the linearlayout below to 0dp. Apparently it's less work on resources,
as otherwise we would be drawing it twice, because the size is also set dynamically-->
<LinearLayout
android:id="#+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
>
<!--android:adjustViewBounds="true"-->
<!-- we want the text to begin in the centre of the edittext view
that's what gravity does-->
<EditText
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/editText"
android:inputType="phone"
android:gravity="center"
android:background="#d9d9d9"
android:layout_weight="4"
/>
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/callButton"
android:src="#drawable/call"
android:clickable="true"
android:onClick="softkeyboardButton"
android:background="#ffa62b"
android:layout_weight="2"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal" >
<CheckBox
android:layout_height="match_parent"
android:layout_width="0dp"
android:background="#ffa500"
android:text="New CheckBox"
android:id="#+id/checkBox"
android:layout_weight="5"
/>
<ImageButton
android:layout_height="match_parent"
android:layout_width="0dp"
android:id="#+id/imageButton"
android:src="#drawable/softkeyboard"
android:clickable="true"
android:onClick="softkeyboardButton"
android:layout_gravity="center_horizontal"
android:layout_weight="1.3"
android:background="#drawable/buttonstates"
/>
<View android:layout_height="fill_parent"
android:layout_width="2px"
android:background="#90909090"/>
<ImageButton
android:layout_height="match_parent"
android:layout_width="0dp"
android:id="#+id/imageButton2"
android:src="#drawable/settingsicon"
android:background="#drawable/buttonstates"
android:layout_gravity="center_horizontal"
android:layout_weight="1.3" />
</LinearLayout>
</LinearLayout>
Posting this as answer as advised. :)
If the idea is to just have them match the LayoutParams, why not just retrieve the LayoutParams of the ImageButton indicated in the xml, then set it as the LayoutParams of the ImageButton you want dynamically? Like so:
buttonTwo.setLayoutParams(buttonOne.getLayoutParams());
And to quote #Christophe Harris for other users:
I was having terrible trouble with the float - params.weight = 1.3f. The widths were never exactly the same, even though I was using your setLayoutParams = getLayoutParams. Probably something to do with floats never being accurate, I don't know. But then I changed it to params.width= 80, and it was exact.
You need to set layout_width to "0dp" for ButtonTwo for layout_weight to work correctly.
Hope this should work for you!

How can I toggle position of listview on button click

I have a list view with initially place above the button, but on clicking of the button I want button to be placed on top of listview. Below is the xml. Any help will be appreciated.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/parentView"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:id="#+id/likes_list"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:padding="20dp" />
<ListView
android:id="#+id/comments_list"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:padding="20dp" />
<Button
android:id="#+id/like_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/likes_list"
android:text="Likes"
android:onClick="onClickLikeButton" />
<Button
android:id="#+id/comment_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/likes_list"
android:layout_toRightOf="#+id/like_button"
android:text="Comments"
android:onClick="onClickCommentButton" />
In your code, use findViewById and get Button. Then, set LayoutParams as following:
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) button2.getLayoutParams();
rlp.addRule(RelativeLayout.ABOVE, R.id.listViewId);
button2.setLayoutParams(rlp);

HorizontalScrollView Not Scolling When Specified Width

I'm trying to have a HorizontalScrollView with 8 buttons which I can code fine. But I want to have the layout double the width of the screen so there are 4 buttons on the screen and the user has to scroll to see more (I'm don't want a "snap" scroll).
To do this I've manually change the width of the HorizontalScrollView to say "770dp" but whenever I specify the width it looks correct but does not scroll. Changing the width back to "wrap_content" and it works fine but does not look correct (5 or 6 buttons on the screen).
My xml code is below. This is just an extract - there are many more layouts/views on the screen.
I will be wanting to programmatically specify the width later the double the phone's screen size didn't want to move on to that until I worked out why the above isn't working.
I have included that code if anyone wants to lend a hand, but it is NOT related to the above non-scrolling problem.
Thanks in advance for any help you can give. Much appreciated.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:orientation="vertical"
tools:context="com.example.testScroll.MainActivity"
tools:ignore="MergeRootFrame" >
<HorizontalScrollView
android:id="#+id/topline_Buttons"
android:layout_width="770dp"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:orientation="horizontal" >
<Button
android:id="#+id/button_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="0dp"
android:layout_weight="1"
android:text="#string/button_1" />
<Button
android:id="#+id/button_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="0dp"
android:layout_weight="1"
android:text="#string/button_2" />
<Button
android:id="#+id/button_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="0dp"
android:layout_weight="1"
android:text="#string/button_3" />
<Button
android:id="#+id/button_4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="0dp"
android:layout_weight="1"
android:text="#string/button_4" />
<Button
android:id="#+id/button_5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="0dp"
android:layout_weight="1"
android:text="#string/button_5" />
<Button
android:id="#+id/button_6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="0dp"
android:layout_weight="1"
android:text="#string/button_6" />
<Button
android:id="#+id/button_7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="0dp"
android:layout_weight="1"
android:text="#string/button_7" />
<Button
android:id="#+id/button_8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="0dp"
android:layout_weight="1"
android:text="#string/button_8" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
Here is what I have found on SO HERE to programmatically set the width. ALthough I haven't got it working yet
//Set HorizontalScrollView double screen width
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(metrics.widthPixels*2, LayoutParams.MATCH_PARENT);
LinearLayout topline_Buttons = (LinearLayout) findViewById(R.id.topline_Buttons);
topline_Buttons.setLayoutParams(params);
I think trying to increase the size of the scroll view is the wrong way to approach this. If you want the layout that the scroll view wraps to be larger, then change the height of the layout.
Remember that the buttons are in the LinearLayout, and that is currently set to wrap content, so it will only take the space needed to display its children (the buttons).
Try this (with the rest of your code of course):
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none">
<LinearLayout
android:id="#+id/topline_Buttons"
android:layout_width="770dp"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:orientation="horizontal" >
// try this way,hope this will help you..
Note : if fix HorizontalScrollView layout_width then it's not Scrollable so you have gave layout_width "wrap_content"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:orientation="vertical">
<HorizontalScrollView
android:id="#+id/topline_Buttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/button_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="button_1" />
<Button
android:id="#+id/button_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="button_2" />
<Button
android:id="#+id/button_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="button_3" />
<Button
android:id="#+id/button_4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="button_4" />
<Button
android:id="#+id/button_5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="button_5" />
<Button
android:id="#+id/button_6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="button_6" />
<Button
android:id="#+id/button_7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="button_7" />
<Button
android:id="#+id/button_8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="button_8" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
**Also remove this peace of code from Activity**
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(metrics.widthPixels*2, LinearLayout.LayoutParams.MATCH_PARENT);
topline_Buttons.setLayoutParams(params);
Ok I've worked it out! Or at least one way of doing it...
If there is a better way let me know.
Instead of specifying the width of the LinearLayout or HorizontalScrollView I needed to specify the width of the buttons inside to 1/4th of the screensize. The HorizontalScrollView should fill the whole page, the LinearLayout wrap content and so the buttons make enough space for themselves.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:orientation="vertical">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:orientation="horizontal" >
<Button
android:id="#+id/button_1"
android:layout_width="92dp" // Specify width here manually
android:layout_height="wrap_content"
android:text="1" />
... etc
That worked but obviously wouldn't look right with many screen sizes, so I specified it programmatically:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screenWidth = size.x / 4;
button_1 = (Button)findViewById(R.id.button_1);
button_1.setLayoutParams(new LinearLayout.LayoutParams(screenWidth, LayoutParams.WRAP_CONTENT));

Android - Make AlertDIalog buttons a uniform size

I have an alert dialog with a positive and negative button added programatically. In the dialog layout there are also two buttons, above the AlertDialog's native buttons.
When these two are right next to each other, I realized that in this dialog, the native positive/negative/neutral buttons are not equally weighted. The content of their text determines their horizontal weight, rather than each taking up 50% (or 33% if 3 buttons) of the dialog. So in my case, where the negative button's text is longer than that positive button's text, we get something that looks like the image I posted above.
I can't find a way to fix this, does anyone have an idea?
Thanks!
XML as requested:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="#+id/rcd_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="8dp"
android:text="#string/rating_dialog_text"
android:textSize="14dp"
android:textColor="#android:color/white"
android:gravity="center"/>
<RatingBar android:id="#+id/rcd_rating_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/rcd_msg"
android:layout_margin="8dp"
android:paddingBottom="10dp"
android:numStars="5"
android:rating="0"
android:layout_centerHorizontal="true"/>
<RelativeLayout android:layout_height="56dp"
android:layout_width="match_parent"
android:layout_below="#id/rcd_rating_bar">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout android:id="#+id/rcd_image"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:background="#color/selectable_transparent"
android:layout_weight="1">
<TextView android:id="#+id/rcd_image_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:paddingLeft="0dp"
android:gravity="center"
android:text="#string/add_image"
android:textColor="#android:color/white"
android:textSize="16sp"
android:clickable="true"/>
<ImageView android:id="#+id/rcd_image_img"
android:layout_height="32dp"
android:layout_width="32dp"
android:layout_centerVertical="true"
android:layout_margin="8dp"
android:layout_toLeftOf="#id/rcd_image_text"
android:scaleType="centerInside"
android:src="#android:drawable/ic_menu_camera"/>
</RelativeLayout>
<RelativeLayout android:id="#+id/rcd_comment"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:background="#color/selectable_transparent"
android:layout_weight="1">
<TextView android:id="#+id/rcd_comment_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:paddingRight="6dp"
android:gravity="center"
android:text="#string/add_comment"
android:textColor="#android:color/white"
android:textSize="16sp"
android:clickable="true"/>
<ImageView android:id="#+id/rcd_comment_img"
android:layout_height="32dp"
android:layout_width="32dp"
android:layout_centerVertical="true"
android:layout_margin="4dp"
android:layout_toRightOf="#id/rcd_comment_text"
android:scaleType="centerInside"
android:src="#drawable/ic_menu_comment"/>
</RelativeLayout>
</LinearLayout>
<LinearLayout android:layout_width="1dp"
android:layout_height="38dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:layout_margin="10dp"
android:background="#color/transparent_white" />
</RelativeLayout>
</RelativeLayout>
Figured out a solution... don't know why I didn't think of it before.
I do the following:
mRateConcertDialog.setOnShowListener(new OnShowListener() {
#Override
public void onShow(DialogInterface d) {
Button posButton = mRateConcertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
Button negButton = mRateConcertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
LayoutParams posParams = (LayoutParams) posButton.getLayoutParams();
posParams.weight = 1;
posParams.width = LayoutParams.MATCH_PARENT;
LayoutParams negParams = (LayoutParams) negButton.getLayoutParams();
negParams.weight = 1;
negParams.width = LayoutParams.MATCH_PARENT;
posButton.setLayoutParams(posParams);
negButton.setLayoutParams(negParams);
}
});
Basically, you have to manipulate the size of the buttons AFTER the dialog is shown. So create an onShowListener and you can just set weights and widths that way. Thanks for the comments, I hope this can help someone in the future.

android interface error ImageButton

Hi i have an app that is a gallery of images.
The problem is that, i have buttons to move between images on the center of the app and i want them at 75% of the margin of top.
The question is : Can i put it on % of the screen instead of dp?
The error is that if i use margin_bottom it crashes.
Here is my code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/color_fondo"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/barra"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:scaleType="fitXY"a
android:src="#drawable/cabecera" />
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/barra"
android:paddingTop="#dimen/activity_vertical_margin"
android:scaleType="fitXY"
android:src="#drawable/ic_launcher" />
<ProgressBar
android:id="#+id/pb1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/img1"
android:layout_centerHorizontal="true"
android:visibility="invisible" />
<ImageButton
android:id="#+id/imgbtnshare1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#null"
android:paddingBottom="5dp"
android:src="#drawable/compartir4" />
<ImageButton
android:id="#+id/imgbtn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/imgbtnshare1"
android:layout_alignParentLeft="true"
android:layout_marginBottom="100dp"
android:background="#null"
android:src="#drawable/izquierda42" />
<ImageButton
android:id="#+id/imgbtn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/imgbtn2"
android:background="#null"
android:src="#drawable/derecha42" />
</RelativeLayout>
You may get the Layout size and then calculate your buttons top margin as proportion of that:
NOTE: You cannot get the layout size in onCreate() since it's not set yet. Do it in onWindowFocusChanged method of your Activity.
RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.YourXML);
#Override
public void onWindowFocusChanged (boolean hasFocus) {
// The layout has been drawn here
// so the Height and Width have been computed
int width = relativeLayout.getWidth();
int height = relativeLayout.getHeight();
}
int topMargin = height * 0.75;
And set it programmatically:
LayoutParams layoutParams = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
layoutParams.setMargins(left, top, right, bottom);
yourbutton.setLayoutParams(layoutParams);

Categories

Resources