RelativeLayout to have the same width and height - android

I have a list of photos, each having some text in it. I would like each photo to horizontally fill the screen and would like to height to be the same as of the width
Trying to accomplish something like
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="self.width"
...
How could I accomplish this ?
Since I will be using a recycleview I can edit the height at runtime.

XML is not dynamic. Screen measurements have to be done in Java during onCreate.
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point(); display.getSize(size);
int width=size.x; int height=size.y;
LinearLayout.LayoutParams params= new LinearLayout.LayoutParams(width,width);
yourView.setLayoutParams(params);

find width of your layout programmatically, and set that value as height of your layout.
//get Width
int width=layout.getWidth();
//set height
layout.getLayoutParams().height=width;

should be like this,if you want height for full screen use match_parent also
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

Related

How to display an ImageView slightly outside a RelativeLayout or outside the screen? How to display a rubber on the topleft of the screen

I've setup a relative view with all my elements inside (buttons, images, etc...). It is the title page of my Android application.Now I would like to overlay "LITE" banner over the whole layout, in the upperleft corner.My problem is that the "LITE" banner image is an oblique red rubber, and that I need to set its topleft point to (-45,-45) on the screen to only display the part of the image I want (attached is the source image so you can understand what part of the image should be visible on the screen).
I have tried the AbsoluteLayout, the RelativeLayout, to move it programmatically with SetLeft and SetTop, but the negative values are not accepted.
Any idea ?
You can use Relative layout with the attribute android:clipToPadding="false" to get the desire effect.
example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clipToPadding="false"
android:background="#android:color/white"
android:paddingLeft="50dip"
>
<ImageView
android:id="#+id/myId"
android:layout_width="60dip"
android:layout_height="60dip"
android:layout_marginLeft="-70dp"
android:layout_marginTop="-20dp"
android:clipChildren="false"
android:src="#drawable/button_normal" />
</RelativeLayout>
result:
I'd like to share my experience of this affair with the community...
The idea was to display an oblique "LITE" rubber on the top-left corner of the main screen of my app.
Rod Algonquin's answer was fine. However, it did not completely solve my problem, because I had to adapt the picture's dimensions to the screen height...AND to the screen orientation. Nightmare. Even with a relative layout, it was nearly impossible, because the hidden parts of the image were never correctly aligned.
So I had to work differently: The picture had to be moved left and top, by 20%. How to do that?
In the layout.xml file :
Insert the ImageView inside a RelativeLayout
Give the relative layout an ID
Configure the ImageView to make it fit its container RelativeLayout's width and height (layout_width="wrap_content" and layout_height="wrap_content")
<RelativeLayout
android:id="#+id/accueil_litebannerlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/accueil_litebanner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/lite_banner" />
</RelativeLayout>
In your activity.java class file :
//get screen dimensions
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int ScreenWidth = size.x;
int ScreenHeight = size.y;
//set the desired height of the rubber, based on screen's height
int myLayoutWidthAndHeight=ScreenHeight/4;
//get rubber PNG image dimensions
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeResource(getResources(),
R.drawable.lite_banner,options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
//redux_factor has to be calculated, because if the image is reduced, then the translation has to be adapted
double redux_factor=1;
if (myLayoutWidthAndHeight<imageWidth) {
redux_factor=(double)myLayoutWidthAndHeight/imageWidth;
}
//determine by how many pixels left and top (same) the image will have to be translated
double translation_percents=.22;
double myCroppedMargin_double=imageWidth*translation_percents*redux_factor;
int myCroppedMargin=(int) Math.round(myCroppedMargin_double);
//get the image layout
RelativeLayout litebannerlayout=(RelativeLayout) this.findViewById(R.id.accueil_litebannerlayout);
//change its parameters (width, height, leftMargin, topMargin)
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(myLayoutWidthAndHeight,myLayoutWidthAndHeight);
params.setMargins(-myCroppedMargin, -myCroppedMargin, 0,0);
litebannerlayout.setLayoutParams(params);
Arghhh. It works...
You can use this sample code to move an imageView out of the screen, either based on a percentage, or a pixel count. This code can also be adapted to put the rubber/banner in the topright, bottomleft, bottomright corners.
OK, let's move on to something else...

Android Layout, view height is equal to screen size

in Android how to make a view have same height as its screen size, is it possible to achieve this with only the xml? or if it must use script, tell me how
Thanks.
Sorry for being not clear, and thanks for your reply
but i think, match_parent and fill_parent attribute is not reliable, because when i put the view inside one container or change the view container hierarchy, it won't work.
Here my complete xml layout.
The element i want to make the height sam with device screen is the last list view inside relative layout
No you cannot achieve this in XML only.
As Android supports multiple screen sizes, at runtime you need to check for each device size. The height for each device can be calculated like this:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int height = size.y;
With the above code, you will get the height of the screen and you need to set this height in dp to your view at runtime.
Do this in your activity:
// get view you want to resize
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main);
// get layout parameters for that view
ViewGroup.LayoutParams params = mainLayout.getLayoutParams();
// change height of the params e.g. 480dp
params.height = 480;
// initialize new parameters for my element
mainLayout.setLayoutParams(new LinearLayout.LayoutParams(params));
This can be possible from xml layout. To do this make the parent layout height and width fill_parent or match_parent and then set each child view width fill_parent or match_parent. Don't set any padding or margin to parent layout. Hope it will work. Here I am giving you an example:
<LinearLayout 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:orientation="vertical" >
<Button
android:id="#+id/btn_swap"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Left or Right" />
<SurfaceView
android:id="#+id/my_surface"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Attention: If you use ScrollView, you have to set fillViewport="true" otherwise it will not work. A Google engineer said about it before. Check it from here
Display screenDisplay = getActivity().getWindowManager().getDefaultDisplay();
LayoutHeight = screenDisplay.getHeight();
LinearLayout.LayoutParams listLayoutParams = new LinearLayout.LayoutParams(
LayoutHeight, LinearLayout.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(listLayoutParams);

Android - SlidingPaneLayout - set width on the Pane

I'm using a SlidingPaneLayout in my activity:
<android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myslidingpanelayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- menu left -->
<LinearLayout
android:id="#+id/menu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#8d305f"
android:orientation="vertical" >
...
</LineareLayout>
<!-- main page right-->
<LinearLayout
android:id="#+id/right_main"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#fff"
android:orientation="vertical" >
...
</LineareLayout>
</android.support.v4.widget.SlidingPaneLayout>
I want the menu to cover 3/4 of the page I want it to work on all the phones so I can't put for example
android:layout_width="300dp"
I want to calculate the screen width and set it to the left pane
Thank for your help
Thanks for you all I found this answer and it works with me:
int width;
int height;
if (android.os.Build.VERSION.SDK_INT >= 13){
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
}else {
Display display = getWindowManager().getDefaultDisplay();
width = display.getWidth(); // deprecated
height = display.getHeight(); // deprecated
}
if(width>0&&height>0){
LinearLayout layout = (LinearLayout)findViewById(R.id.menu);
// Gets the layout params that will allow you to resize the layout
LayoutParams params = layout.getLayoutParams();
// Changes the height and width to the specified *pixels*
params.height = height;
params.width = width*3/4;
}
Just looking up the doc for sliding pane, looks like it functions like a linear layout, and can use the
layout_weight
parameter to set a percentage based width since the parent viewgroup is match_parent
In the case of 3/4 = 75% you can
android:layout_weight="0.75"
From the android docs http://developer.android.com/reference/android/support/v4/widget/SlidingPaneLayout.html:
Like LinearLayout, SlidingPaneLayout supports the use of the layout parameter layout_weight on child views to determine how to divide leftover space after measurement is complete. It is only relevant for width. When views do not overlap weight behaves as it does in a LinearLayout.
When views do overlap, weight on a slideable pane indicates that the pane should be sized to fill all available space in the closed state. Weight on a pane that becomes covered indicates that the pane should be sized to fill all available space except a small minimum strip that the user may use to grab the slideable view and pull it back over into a closed state.
And from the LinearLayout docs http://developer.android.com/guide/topics/ui/layout/linear.html#Weight
Note: You will end up setting the layout_width parameter to 0dp since the view group will actually use the weight to lay the children out
Apart from Selecsosi's answer, which is correct, there is also this view I wrote to always display the second item as a pane (ignoring the default show-side-by-side-if-the-fit behaviour). It can, as the name shows, wrap around the sliding view.
You can implement the behaviour you're after by either using a lot of #dimen resources and switching them based on swXXXdp-(port|land) or just setting the sliding view's width at runtime (something I'm reasonably certain you can do with the default layout as well).

Android/Eclipse : Dealing with images' size

I am building an android application with eclipse and I'm struggling to master the image positioning in the screen. I have put an image in a relative layout, and I want the borders of the image to match perfectly the borders of the screen, but when I extend the image manually it never fits and even if I put padding :/
(see the right side is ok, but not the left, the problem is not even a problem about the dimensions of the screen)
Thank you for your help
If you want to do that, you need to take the dimensions of the screen using this code :
// pixels
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Then you need to set programmatically the dimensions of the picture using the width and height properties.
If you need that your image to be the backgroung of the activity you can use the background property.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rootRL"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background">
</RelativeLayout>
The "#drawable/your_image_name" is your image. The image should be the dimensions that google recomends for deferent sizes of screen.

How to adapt Cells of TableLayout to fit the screen?

I'm learning android development and I'm doing a minesweeper. So I use a tableLayout to display the board. But how can I do to adapt the width of the cells to fit the screen ?
Currently my cells are fixed so the grid expand beyond the side of the screen...
How can I take the entire screen width for my tableLayout ?
Thanks.
since you are creating your cells dynamically,
you can calculate the height and width of the screen using:
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
and then set your cell size accordingly.
This is done through using simple properties found inside of your layout file xml file. Specifically you want to set the
android:layout_width="fill_parent"
This will tell the layout to consume the ENTIRE width of its parent.

Categories

Resources