How to resize nestedlinearlayout in xml - android

I am trying to resize layout(l2) which is already in other layout(l1).Please let me know how to achieve it.
I want to do it xml.I dont want hardcode dp values such as 50 dp for l1 and 30 dp2.
Generally we use width 0r height as 0 dp and layout weight to partition according to our requirement.Is there any similar way like it.

One possible approach is nested linearlayouts with transparent views inside them (they do not show anything, just take place); layout_width="0" layout_weight="1" will do the job.

is this ok?
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
>
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:padding="10dp"
></RelativeLayout>

Related

ImageButton resizes automatically in gridlayout

I'm asking this (propably) pretty easy question since I can't figure it out by myself and googling in for more then an Hour doesn't give me proper results.
So the question is: How can I make my items(generally those are only 16 image buttons) so they can stay in scale (comparing to gridlayout) when my picture is really large (So in short way: I load 16xImages as src on buttonImages, each Image is 640x640, and I want to have those 16 buttons in grid layout scaled comparing to grid layout width/height and column/row count). I hope this is possible to write in XML since I don't want to refactor my java code :/
Ps. I'm newbie to Android programmig
Ps2.Here is my GridLayout with button example , but it's completly wrong :/
<GridLayout
android:id="#+id/GridLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="4"
android:rowCount="4"
tools:context=".GridXMLActivity"
android:layout_below="#+id/Tx1"
android:layout_above="#+id/restart"
android:layout_marginBottom="50sp"
android:layout_marginTop="50sp"
>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:src="#drawable/clear"
android:id="#+id/But1"
android:scaleType="fitXY"
android:onClick="but1"
/>
[EDIT]
I've even made such layout , but there are some problems. 1st is that my grid layout doesn't scale at all!(So when I take screen with lower or higher resolution it won't fit). That's why I wanted to make this button some kind of resizeable (in this screen each button has width and height at 60sp which I know it shouldn't)
Another problem is that I'm working on button backgrounds and I wanted to make it src.
Genereally I want to achive same thing like on the screen , but making it other/more flexible way (So the resolution and orientation won't affect my gui and sizes of it's element). If you need whole XML file I'll post it here but it's a bit mess since I've copy-paste 16 times Image Button
If you want something to scale, keeping aspect ratio and you have one dimension set to match_parent, the other must be wrap_content.
So, presuming you want the button to always fit the width but stay at the correct aspect:
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
... />
To make all items fit in grid rather than all items fit their images, you should use weight. Weight with grid layout requires the support library before API21
<GridLayout ...
android:columnCount="2">
<ImageButton
app:layout_gravity="fill"
app:layout_columnWeight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
... />
...

put buttons on an image which looks the same in different displays

I have a map and wanna put some Buttons on it as pointers. Here is my
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/karte"
android:background="#drawable/my_map"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/btnSP1"
android:layout_width="#dimen/pinpoint_size"
android:layout_height="#dimen/pinpoint_size"
android:scaleType="fitXY"
android:background="#drawable/pinpoint"
android:tag="001"
android:layout_marginLeft="50px"
android:layout_marginTop="90px"
android:onClick="spClick"/>
<ImageButton
android:id="#+id/btnSP2"
android:layout_width="#dimen/pinpoint_size"
android:layout_height="#dimen/pinpoint_size"
android:scaleType="fitXY"
android:background="#drawable/pinpoint"
android:tag="002"
android:layout_marginLeft="100px"
android:layout_marginTop="250px"
android:onClick="spClick"/>
</RelativeLayout>
I have set layout_margin in px or dp or progremmatically in percentage of the display_size:
RelativeLayout.LayoutParams param =(RelativeLayout.LayoutParams)(getActivity().findViewById(id[i])).getLayoutParams();
param.setMargins((int)(xFactor[i]*displySize.x),(int)(yFactor[i]*displySize.y),0,0);
but in all of these methods Buttons change their position when I use different display size.
Is there any idea how to fix Buttons to make their position independent of the display size.
Normally DP or DIP means in android Density-independent Pixels so if you attribute a position to an object obviously it will be fixed.
From your code i see that you should get the equal of your values in DP with this way.
RelativeLayout.LayoutParams param =(RelativeLayout.LayoutParams(getActivity().findViewById(id[i])).getLayoutParams();
param.setMargins(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,(int)(xFactor[i]*displySize.x), context.getResources().getDisplayMetrics()),TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,(int)(yFactor[i]*displySize.y), context.getResources().getDisplayMetrics()),0,0);
I hope that will help you

Android - Put a button over an ImageView to the exact spot

I have an ImageView (human body) and buttons over this image.
Is it possible somehow easily create this in RelativeLayout for the most different screen sizes? Or should I create different layouts for different screen sizes?
How would you proceed? What do you recommend?
Have a look at the percent library.
It would be perfect for something like this, and would work for every device. You'd have to work out the percentage width, height, and margin for each PercentRelativeLayout relative to your person image.
Here's an example:
<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
app:layout_widthPercent="25%"
android:layout_height="100dp"
app:layout_marginLeftPercent="5%"
android:background="#ff0000" />
</android.support.percent.PercentRelativeLayout>
and remember to
compile 'com.android.support:percent:23.0.0'
This is an example for an imageview with clickeable areas.
https://blahti.wordpress.com/2012/06/26/images-with-clickable-areas/
I hope it will helps to you.
Yes it is possible with Relative layout but its difficult.
for this you have to focus on
different images with perfect cutting.
put images in all drawable.
Create Relative layout and starting with image-view at one side to end side as reference wise.
Margin add in different dimen.xml

How do I set the height of an element to the same as another element?

I'm just starting out with android and trying to make a simple user interface.
I'm using TableLayout and each TableRow is supposed to contain an image and several buttons next to it. I set the width and height of the buttons to wrap_content, which is exactly what I want. Now I want the image to have exactly the same height as the buttons. The image is way bigger, so wrap_content doesn't work.
Obviously I can just adjust the dp value of the image height until it fits, but there has to be an easier way and I guess that pretty much defeats the purpose of using wrap_content in the first place...
Set the height of the image to match_parent. Your buttons are doing all the height-sizing work having the wrap_content set. When the row gets sized it uses the button height, as they say "you will be as tall as our content", and then when the image is going to be sized, it will use the available height, as specified before by the buttons. Even if the buttons are smaller than the image, the image will still take the available space.
Try this
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>

android:layout_width along with layout weight usage

why should i put android:layout_width="0px" when i use android:layout_weight property? For example following is my main.xml file , and in that i used android:layout_width="wrap_content", and everything works fine, so why android:layout_width="0px" should be used when i am using the layout_weight property?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText" android:hint="enter your name"
android:layout_weight="3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:id="#+id/button"
android:layout_weight="0"/>
</LinearLayout>
and this is how my layout looks:
You certainly don't have to. Additionally weight=0 doesn't make much sense. The weight parameter controls what part of the remaining space in the layout the widget occupies. So setting width=0 effectively tells it to take up only the remaining space. If you set width=30, it will occupy 30 px|dp + all the remaining space. Setting 0 as the width makes it easier to get a predictable result on different screen sizes.
A common pattern is to have two widgets with width=0 and equal weight to make them equally sized inside the parent container, where you don't care about the actual size (width or height).
Layout weight itself is used to give appropriate width as per weight property.
Check this
This attribute assigns an "importance" value to a view in terms of how
much space is should occupy on the screen. A larger weight value
allows it to expand to fill any remaining space in the parent view.
So eclipse suggests to give width as 0px
layout_weight you can specify a size ratio between multiple views.
E.g. you have a Tabelview and a image which should show some additional information to the layout. The tabel should use 3/4 of the screen and image should use 1/4 of the screen. Then you will set the layout_weight of the tabelview to 3 and the layout_weight of the image to 1.
To get it work you also have to set the height or widthto 0px.
http://developer.android.com/guide/topics/ui/declaring-layout.html#CommonLayouts

Categories

Resources