Setting a graphic to the bottom center of the phone - java

Hello can any one help me, I am trying to get a graphic to be at the bottom of the screen and centered using LinearLayout. I would rather use the Bitmap method, because this is for a game. Thanks

Not sure what you mean by bitmap method but this will do what you ask:
<?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"
android:gravity="center_horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"
android:layout_gravity="bottom"
/>
</LinearLayout>

Related

Android: Adding Frames to images

I have two images. One is a simple image and the second is a fancy frame. I want to set the frame on the image. I searched through some blogs, but unable to find an answer.
Could anyone tell me the idea or any concept about how to do that?
You can you FrameLayout to use 2 Images 1 over other like below to achieve like Frame -
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/image1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:layout_gravity="center"
android:background="#drawable/image2"/>
</FrameLayout>

How to stretch the image till top in android

I am trying to stretch the image till the top in layout. But I couldn't achieve it. Any help is appreciated.
I want to move this image at the top. so there is no space between the top and image. Right now there is space on top.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#fafaf8"
tools:context=".MainActivity"
>
<ImageView
android:id="#+id/titlePic"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#drawable/nebcaplivebanner"
>
</ImageView>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="1dp"
android:divider="#C0C0C0"
/>
</LinearLayout>
try to use -
<ImageView
android:id="#+id/titlePic"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:src="#drawable/nebcaplivebanner"
android:scaleType="fitXY"/>
That's what fitXY is supposed to do. You can try with centerInside if you don't want to crop your image, or centerCrop if you want to fill all the space (and cropping your image).
you can achieve this using scaleType fitxy
<ImageView
android:id="#+id/titlePic"
android:layout_width="fill_parent"
android:scaleType="fitXY"
android:layout_height="40dp"
android:src="#drawable/nebcaplivebanner" />
for detail ScaleType property:
https://abhiandroid.com/ui/scaletype-imageview-example.html
https://thoughtbot.com/blog/android-imageview-scaletype-a-visual-guide

Set background with gravity without resizing the container View in Android

How can I add a background to a view, without resizing its size, only showing the part of the image that fits the previous space?
Example:
I have the following initial layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#8900"
android:orientation="vertical"
tools:context="com.tappx.myapplication.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="#0f0"
android:gravity="center"
android:text="HELLO" />
<TextView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="#f0f"
android:gravity="center"
android:text="HELLO" />
</LinearLayout>
</LinearLayout>
And I want to apply the following image as gradient to the white background:
To achieve this (please note the gradient is filling the horizontal and has bottom gravity):
TARGET:
My problem is that setting the gradient as android:background expands the container view size to this (losing the red bottom part)
Also tried making a xml bitmap with no results:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap
android:gravity="bottom"
android:src="#drawable/gradient" />
</item>
</layer-list>
How can I achieve it?
The image you are using is very long, and your container is set to wrap content and so it is expanding to accommodate the image.
Since you are already using fixed heights on the inner elements, you can simply set a fixed height for the container (200dp) and it'll work

Making the gridView center in Android

<?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:orientation="horizontal"
android:background="#FFFFFF"
android:layout_gravity="center_horizontal"
android:padding="5dip" >
<GridView
android:id="#+id/homeGridView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnWidth="160dip"
android:adjustViewBounds="true"
android:layout_gravity="center_horizontal"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:numColumns="2"
android:stretchMode="none"/>
</RelativeLayout>
I have been breaking my head on how to make the gridView center horizonal and vertical. I have also attached my screen-shot for the same.
Below is my image code.
<?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:orientation="horizontal"
android:layout_gravity="center_horizontal"
android:padding="5dip" >
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_centerHorizontal="true">
</ImageView>
</RelativeLayout>
Well if you don't want to use TableLayout (as you says in your precedent post), dealing with GridView is pretty complicated (i've lost a lot of times on this).
The only idea i have his to force your GridView to have an exact width, that is the sum of your 2 views: 160dip * 2.
android:layout_width="320dip"
Feel free to add 10dip or more for padding.
N.B.: i'm not sure if this is the best way and if this is proper for every device. Try it and let me know ;) .
Use these 2 tags inside ur gridview see if it works
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"

Overwriting paddingLeft in a LinearLayout on Android

i am currently making one android application, i moded title bar, so all content for title bar is holded in window_title.xml where i have LinearLayout set: paddingLeft="5dip" . And now i need to load one imageView on the right side of this titleBar how i am able to some kind of overwrite this paddingLeft i cant delete it as it required for text and image on the left but i need it on right too...
Here is current xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:gravity="center_vertical"
android:paddingLeft="5dip"
android:background="#222222">
<ImageView
android:id="#+id/header"
android:src="#drawable/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="TextTexast"
android:textSize="10pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="5dip"
android:textColor="#F7F7F7"
/>
</LinearLayout>
If I understand your question correctly, you're looking to apply some padding to an ImageView that you display to the right of the titlebar TextView. You can use android:paddingRight in your root LinearLayout like this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:gravity="center_vertical"
android:paddingLeft="5dip"
android:paddingRight="5dip
android:background="#222222">
Alternatively, you can specify the android:paddingRight attribute in the ImageView that you want to align to the right.
To move the grey figure to the right just set android:layout_gravity for that to "right".

Categories

Resources