Handle ImageButtons with different image sizes - android

My layout contains 3 ImageButtons, arranged vertical in a LinearLayout.
Now I want that each of them has the same height and together fill the device's screen height.
Works fine with the attribute android:layout_weight="1". But if the image of one ImageButton is too big, it won't work (this button is higher than the others), despite setting android:scaleType="center_inside".
Any advices/tricks?
If you need any code, let me know. But there is nothin special.

If you have given weights correctly than this should work. The size of the image doesn't matter than. Just one thing to keep in mind while using weights is that attribute for which you are giving the weight(height/width) should be assigned value "0dp" in the xml, only then the weights will work correctly. Here is the example code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:weightSum="3">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:src="#drawable/drawable1"
android:layout_weight="1"
android:scaleType="centerInside"
/>
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:src="#drawable/drawable2"
android:scaleType="fitXY" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:src="#drawable/drawable3"
android:scaleType="fitXY" />
</LinearLayout>
Just use this xml and replace the drawables according to your needs. Let me know if any issues.

Related

How to resize RelativeLayout/LinearLayout and content inside those layers programmatically, based on screen size

I am developing an Android application, for very first time.I am coming from web environment, and i am not sure if i can transfer my skills over Android UI as well.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="#android:color/background_light"
android:gravity="center_vertical|center_horizontal">
<LinearLayout
android:id="#+id/SignUpHolder"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_light"
android:gravity="center"
android:orientation="horizontal"
android:padding="15dp">
<ImageView
android:id="#+id/imageView2"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="#string/Yeah"
app:srcCompat="#mipmap/ic_launcher" />
<TextView
android:id="#+id/someTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="#string/sign_up_with_string_sign_up_with_facebook"
android:textSize="25sp" />
</LinearLayout>
</RelativeLayout>
As you can see
android:layout_width="350dp"
layout_width is set hardcoded by 350dp.
By this, this LinearLayer is not touching the screen of the phone and will be in every kind of mobile screen.
I am looking for a way to cut the width of this LinearLayer by 20-50dp, but do not be way to small so ImageView and TextView to be cutted off.
Is there a way to get the phone screen width, calculate it and set this Layer dynamically by some dp?
An easy way to achieve what I think you want would be to combine a match_parent width with horizontal margins. Something like this:
<LinearLayout
android:id="#+id/SignUpHolder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#android:color/holo_blue_light"
android:gravity="center"
android:orientation="horizontal"
android:padding="15dp">
This will cause your LinearLayout to be as wide as whatever parent it is inside of... minus 20dp on each side.
You can add margin. Instead of hard coding the width, use this in your linear layout tag
android:layout_margin="30dp"
android:layout_width="wrap_content"

ImageViews in LinearLayout - messing up the scale

I been struggeling with this for a whole day now. I want 3 Image Views in my LinearLayout. As you can see the scale is not correct at all. All of my icons have a size of 24x24 px. Been experimenting with different propertiers inside of my ImageViews.
Here is my XML-code:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="45dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/tList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:background="#drawable/ic_home_black_24px" />
<ImageView
android:id="#+id/ist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:background="#drawable/ic_help_black_24px"
/>
<ImageView
android:id="#+id/ations"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:background="#drawable/ic_settings_black_24px"
/>
</LinearLayout>
I pasted your code on a layout file in my Android Studio, an it seems that the weight and scaleType attributes are messing up your view. This is how I declared a 24 by 24 ImageView:
<ImageView
android:id="#+id/ations"
android:layout_width="24dp"
android:layout_height="24dp"
android:background="#drawable/your_drawable"
/>
You can check full doc about weight here, but what is basically saying is that if you put weight on several attributes under a LinearLayout, you're adding a priority to grow when the screen does. Since the 3 ImageViews have 1 as weight, they grow with the same priority, and since the fill_parent is being called also, they will force to fit in the parent layout params, looking oddly.
Change all of your ImageView's width to "0dp" and replace background to src like this :
<ImageView
android:id="#+id/ations"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/ic_settings_black_24px"
/>

Android LinearLayout horizontally or vertically oriented items fill remaining space

I wonder if it is possible to have two items in a LinearLayout one wraps its content and the other fills the remaining horizontal space. I do this frequently in WPF (.NET) by specifying
HorizontalAlignment="Stretch".
For example:
<ImageView
android:layout_width="wrap_content"
android:layout_height="24dp"
android:background="#0000FF"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="24dp"
android:background="#FF0000"/>
When I do this the second ImageView fill the whole horizontal space as I expected. I tried to set both wrap_content and use gravity such as android:gravity="start" and android:gravity="fill_horizontal" for the second one it did not worked.
NOTE: I can achieve something similar by specifying weight attribute. But this is providing a division according to the percent value. This is not I want.
You can do this using the weight attribute itself. Try the following method
<ImageView
android:layout_width="wrap_content"
android:layout_height="24dp"
android:background="#0000FF"/>
<ImageView
android:layout_width="0dp"
android:layout_height="24dp"
android:layout_weight="1"
android:background="#FF0000"/>
include this in your horizontal linear layout
You need to use a Relative layout for this. Also you either need to specify a width for first image view or it should contain an image otherwise, how can the imageview wrap the content.
You can use like below :
<?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" >
<ImageView
android:layout_alignParentLeft="true"
android:id="#+id/imgFirst"
android:layout_width="50dp"
android:layout_height="24dp"
android:background="#0000FF" />
<ImageView
android:layout_width="match_parent"
android:layout_height="24dp"
android:layout_alignParentRight="#+id/imgFirst"
android:layout_toRightOf="#+id/imgFirst"
android:background="#FF0000" />
</RelativeLayout>

Android Single Layout to FIt All Screen Sizes

I am updating my Android app and realized that I have created a layout for every possible screen size (layout-small, layout-large, etc...) It would be a huge pain to go through every XML file and manually make a small change. I am attempting to create a single XML file to support all screen sizes. After reviewing the Android documentation and other questions on stackoverflow, it seems LinearLayout is the best choice as you can provide a weightSum and layout_weight for each item in the layout. This is not working as expected (see below code and images). I am doing this correctly? Do I have to go back to creating a RelativeLayout for every possible screen size?
My images are an a single drawable folder and my layouts are in a single layout folder.
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg"
android:gravity="center"
android:orientation="vertical"
android:weightSum="100" >
<ImageButton
android:id="#+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#drawable/image0"
android:background="#null"
android:layout_weight="30" />
<ImageButton
android:id="#+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#drawable/image1"
android:background="#null"
android:layout_weight="30" />
<ImageButton
android:id="#+id/key"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="30"
android:background="#null"
android:src="#drawable/image0_key" />
<TextView
android:id="#+id/tvScore"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Score: 0"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_weight="10"
android:layout_gravity="left" />
</LinearLayout>
Resulting View (overflow of items and layout not consistent for screen sizes)
Nexus One:
Tablet:
EDIT:
I have added the following drawable-____ folders. It produces the same result.
You might want to consider creating compatibility layout folders. :)
Yes we have a solution for the same by using android's percent layout we can now use app:layout_heightPercent and app:layout_widthPercent to fit all the screens using single layout.
compile 'com.android.support:percent:23.0.0'
Why use LinearLayout weight property now we have simpler solution.
Demo HERE !
GitHub project HERE !
Consider a simple sample
<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">
<TextView
android:id="#+id/fifty_huntv"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#ff7acfff"
android:text="20% - 50%"
android:textColor="#android:color/white"
app:layout_heightPercent="20%"
app:layout_widthPercent="50%" />
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_toRightOf="#id/fifty_huntv"
android:background="#ffff5566"
android:text="80%-50%"
app:layout_heightPercent="80%"
app:layout_widthPercent="50%"
/>
</android.support.percent.PercentRelativeLayout>
Hope it's useful for someone :-)
Use Below layout for arranging your ImageButton and TextView. It works for all screen size Layouts.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="3" />
<ImageButton
android:id="#+id/imageBtn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/ic_launcher" />
<ImageButton
android:id="#+id/imageBtn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/ic_launcher" />
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Score: 0" />
</LinearLayout>
Never put an weight sum like hundred ,just try using single digits
DisplayMetric dm =new DisplayMetric();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int h= dm.heightPixels;
int w= dm.widthPixels;
h=h/10; // 10 is for example for 10% of display height
w=((w*20)/100); // 20%of display width
LinearLayout.LayoutParams params= new LinearLayout.LayoutParams(w,h);
YourObject.setLayoutParams(params);
//(YourObject) can be everything such as button , image button, textview,...
There are two issues here, one is to fill the size of the screen and the other is supporting the various resolution sizes of mobiles. Even within xxxhdpi, there are variations as new flagship Samsung Mobiles are drifting to 19.5 x 16.
Linear layout along with weight attributes does give a good coverage but beware of the nested tags and performance. It worked out well for most of the scenarios I have handled.
In addition, as pointed out in other answers, different drawables/resources for the standard sizes helps maintaining similar view in all devices.

android -- hide part of linear layout off-screen to the right

I have two linear layouts inside a parent linear layout. all horizontal. I want all of l_child to show and part of r_child to show; while the rest of r_child will be off-screen to the right. How do I accomplish that?
<LinearLayout
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/l_child"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="#+id/r_child"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
Try giving the parent LinearLayout a negative right margin.
android:layout_marginRight="-50dp"
Other solution might be to put the parent LinearLayout inside a ScrollView, and set the widths of children layouts to the desired width. In this case, you'll be able to scroll the right layout back to the screen.
Layout weights are only for distributing layouts to fill a view, so no overflowing can be done this way.
Assigning layout_width with density independent pixels will result in varied success for different size devices.
So I believe you would have more success adding your layouts programmatically based on the screen size.
See this post for getting the screen width:
Get screen dimensions in pixels
Once you have the screen width, you can assign the inner-layout dimensions by pixels (with LinearLayout.LayoutParams), calculated as percentages of the screen width.
If you want the left layout to take up 80% of the screen, use .8*screen_width for the size, and if you want the right layout to then overflow by 20% of the screen, use .4*screen_width for that size.
Do you mean something like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_weight="1"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="70dp"
android:layout_gravity="center_horizontal|top">
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:id="#+id/imageView1"
android:src="#drawable/ic_launcher"
/>
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:id="#+id/imageView2"
android:src="#drawable/ic_launcher"/>
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:id="#+id/imageView3"
android:src="#drawable/ic_launcher"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="70dp"
android:layout_gravity="center_horizontal|top">
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:id="#+id/imageView1"
android:layout_marginRight="200dp"
android:src="#drawable/ic_launcher"/>
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:id="#+id/imageView2"
android:src="#drawable/ic_launcher"/>
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:id="#+id/imageView3"
android:src="#drawable/ic_launcher"/>
</LinearLayout>
</LinearLayout>
You can try using ViewPager, but I don't know if you can get the "bleeding canvas" effect that you're describing using that UI component.
EDIT: Maybe you can try using Gallery widget.
Is this what you're describing?
http://www.bangkokpost.com/media/content/20110915/309542.jpg

Categories

Resources