How to create layout with android's different display size - android

My problem born when i create layout in xml file from graphics tool. RelativeLayout is awful! i don't know which layout to use.. When i put an imageview and a imagebutton, in emulator will displayed in a bad way, one apon the other. what can i do? is there a good tutorial on which i can learn how to resize image and layout in percent of different displays size?
i Try this :
<RelativeLayout android:layout_width="match_parent" android:id="#+id/relativeLayout1" android:layout_height="match_parent">
<LinearLayout android:layout_width="wrap_content" android:id="#+id/linearLayout1" android:orientation="vertical" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true">
<ImageView android:id="#+id/imageView1" android:src="#drawable/testotrova" android:layout_width="243dp" android:layout_height="48dp"></ImageView>
</LinearLayout>
<ImageButton android:layout_width="wrap_content" android:src="#drawable/info" android:layout_height="wrap_content" android:id="#+id/imageButton1" android:layout_alignParentTop="true" android:layout_alignParentRight="true"></ImageButton>
</RelativeLayout>
but when i display it isn't good

Regarding support for multiple screens, have a look here.
You will need to add different layouts for screen size small, normal and large.

They are displayed one upon the other because you are using RelativeLayour and they are both (Image and text) anchored to the upper left:
android:layout_alignParentTop="true" android:layout_alignParentLeft="true"
I strongly suggest you to use LinearLayout if you have only a few items. (http://www.learn-android.com/2010/01/05/android-layout-tutorial/4/)
For your image size, if you want a percent, please use:
android:layout_height="0dip"
android:layout_weight="1"
http://developer.android.com/resources/articles/layout-tricks-efficiency.html

Related

Android image display full height - loaded asynchronously

Hey I begin my adventure with Android and I can't solve one problem.
I am loading pictures from web server using Universal Image Loader. I put them into Relative Layouts which is located in ListView. The effect that I want to achieve, is that pictures are displayed on the entire screen with full width and height with a small box at the bottom.
In the IDE preview, everything looks ok:
But on my phone loaded images look like that. I want them to be displayed with full height. It is necessary to use programming functions to achive it?
Loading images:
imageLoader.displayImage(
wallpapers.get(position).getSrcUrl()//the drawable
, currentWallpaperViewHolder.imageViewWallpaper//the image view
, imageLoaderOptions);//the display options
return itemView;
List view:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:id="#+id/WallpapersListView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Image view:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/wallpaper_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="#drawable/test"
/>
<TextView
android:id="#+id/wallpaper_date_for_use"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:padding="12dip"
android:background="#AA000000"
android:textColor="#ffffffff"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:textStyle="bold"
android:text="Date of use" />
</RelativeLayout>
It's probably because you have a RelativeLayout as your root view. Try wrapping the outside RelativeLayout in a LinearLayout.
You can do this,
Get the device height.
And then set imageView height programatically as the device height.

chess UI in android

I want to create simple UI for chess game in Android.
I want to adjust the screen to all kinds of mobiles.
I make a TableLayout with Table Row , each Row contain 8 Linear Layout that contain the View
for the players , my problem is that i make the width and the height of each linear layout in specific size (40 dp width and height), what is the best teqhniques to make the width and height right , here is the layout in xml file i described one row here ,
<?xml version="1.0"?>
-<TableLayout android:id="#+id/tableLayout" android:layout_height="fill_parent" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
-<RelativeLayout android:layout_height="120dp" android:layout_width="120dp" android:background="#android:color/white">
<TextView android:id="#+id/textView1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="TextView" android:layout_marginTop="24dp" android:layout_marginLeft="33dp" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"/>
</RelativeLayout>
-<TableRow android:id="#+id/tableRow0" android:layout_height="wrap_content" android:layout_width="wrap_content">
<LinearLayout android:id="#+id/n00" android:layout_height="40dp" android:layout_width="40dp" android:background="#android:color/white" android:orientation="horizontal"/>
<LinearLayout android:id="#+id/n01" android:layout_height="40dp" android:layout_width="40dp" android:background="#android:color/black" android:orientation="horizontal"> </LinearLayout>
<LinearLayout android:id="#+id/n02" android:layout_height="40dp" android:layout_width="40dp" android:background="#android:color/white" android:orientation="horizontal"/>
<LinearLayout android:id="#+id/n03" android:layout_height="40dp" android:layout_width="40dp" android:background="#android:color/black" android:orientation="horizontal"> </LinearLayout>
<LinearLayout android:id="#+id/n04" android:layout_height="40dp" android:layout_width="40dp" android:background="#android:color/white" android:orientation="horizontal"/>
<LinearLayout android:id="#+id/n05" android:layout_height="40dp" android:layout_width="40dp" android:background="#android:color/black" android:orientation="horizontal"> </LinearLayout>
<LinearLayout android:id="#+id/n06" android:layout_height="40dp" android:layout_width="40dp" android:background="#android:color/white" android:orientation="horizontal"/>
<LinearLayout android:id="#+id/n07" android:layout_height="40dp" android:layout_width="40dp" android:background="#android:color/black" android:orientation="horizontal"> </LinearLayout>
</TableRow>
If I were developing a chess UI, I would not use frame layouts, but look into using a SurfaceView, and drawing onto a canvas.
Every View has a canvas, which is the what is actually drawn to the screen. If you create your own, you will be able to get much better control and render speeds, and you can just create the checkerboard with a simple look in your Java Code.
Might I suggest a different way to create your chess layout?
Consider a vertical LinearLayout (this will make your rows) containing horizontal linear (your columns). And then in each horizontal LinearLayout, have eight Buttons/ImageButtons. Here's an open source implementation of such a layout.
Now the question is how to correctly space these out (this should be applicable to your orignal layout if you want to keep it). There are two main ways we could do this:
Make the chess board fit on the screen, no scrolling needed.
Make the chess board a minimum width/height, scrolling may be necessary.
To accomplish the former, your outer layout should have match_parent for both width and height. Your inner layouts should match_parent for the width and wrap_content for height. Then your individual cells should have a weight attribute of 1. There are limitations to this approach, like how you won't be able to have any views to the side of the chess board (above and below should have space though).
To accomplish the latter (this is what you were asking for), the width and height attributes of your outer and inner layouts should be wrap_content. I also suggest you embed your structure in a ScrollView in case it expands past the screen.

How to arrange images inside Relative Layout

I am just starting with Android and cannot find a way to arrange the images the way I want.
This is what I am looking for:
This is the code I am currently using, to arrange the images, which unfortunately does not work.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1.5"
android:orientation="horizontal"
android:layout_gravity="center">
<ImageView
android:id="#+id/imageView1"
android:layout_width="240dp"
android:layout_height="240dp"
android:src="#drawable/delicio"/>
<ImageView
android:id="#+id/imageView2"
android:layout_width="240dp"
android:layout_height="240dp"
android:src="#drawable/logoerrado"/>
</RelativeLayout>
How can I arrange the images how I want?
To get image1 in the upper right hand corner, use
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
To get image 2 in the center horizontally, use android:layout_centerHorizontal="true". Then add enough padding via android:paddingTop to get it down as far as you want.
If you want another layout beneath image2, like I see in your drawing, just put android:layout_below="#+id/img2" to it.

android, imageviews inside linear layout don't fit the screen size

I was wondering how to make the four images inside my LinearLayout look as big as possible depending on the screen. When I install the widget in my phone, it always fits only 50% of the screen.
Here comes the xml.
Any hints?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical|clip_horizontal"
android:orientation="horizontal" >
<ImageView
android:id="#+id/hora1_current"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#drawable/neutro_on" />
<ImageView
android:id="#+id/hora2_current"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#drawable/neutro_on" />
<ImageView
android:id="#+id/minuto1_current"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#drawable/neutro_on" />
<ImageView
android:id="#+id/minuto2_current"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#drawable/neutro_on" />
Add android:weightSum="1" to your parent linear layout.
The linear layout should have android:orientation="horizontal"
Now to each of your image views , add android:layout_weight = 0.25.
Hope this works.
hope you read enough about orientation vertical and horizontal .
you should clear here that by filling screen whether you mean altogether all 4 fills the screen such that each takes 25% or every one should take 100% width and added up vertically .
for first case add android:layout_weight=1 for every imageView .
for second case replace android:orientation="horizontal" by android:orientation="vertical" in root LinerLayout
Don't use wrap content, instead use the actual dimensions of the image or fill parent.

Android Image Button Layout

Could someone please explain how android uses image button sizes? I seem to be getting odd behavior with my buttons.
I have the following code as an example. I have two buttons that sit at the bottom of my layout. These buttons share 50% of the total width as they sit side-by-side.
Within Abode PS, the two images (used for these two buttons) are actually 2" x 38" or 495x94 pixels. This size is of course larger than the available space in the layout.
I am using edge effects on my buttons to give them definition. Android is cutting the edges off my buttons in order to center then in the available layout space.
This particular layout that I am working on will only allow vertical orientation, in case that helps.
Thank you.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<ImageButton
android:id="#+id/ImageButton1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_marginLeft="1dip"
android:layout_marginRight="1dip"
android:layout_weight="1"
android:background="#FF000000"
android:src="#drawable/map4" >
</ImageButton>
<ImageButton
android:id="#+id/ImageButton3"
android:layout_width="0dip"
android:layout_marginRight="1dip"
android:layout_weight="1"
android:background="#FF000000"
android:src="#drawable/buy"
android:layout_height="wrap_content"
android:layout_marginLeft="1dip">
</ImageButton>
</LinearLayout>
</RelativeLayout>
Try using an ImageView and android:scaleType:
http://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType
Experiment the values available to see what is the best combination!
Then add a listener to behave like a button...
try use the button and make it with empty text after this set the background .
or :
use the image button and put the source and the background the same image to get button exactly like the image
**you can use the selector to make some beauty for application buttons
Google it it's easy to use ;)

Categories

Resources