How to set an activity's layout background width to 100% - java

I am using this code to get the device background to be the background for my activity's layout.
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
MyHome = (GridView) findViewById(R.id.gridView1);
MyHome.setBackgroundDrawable(wallpaperDrawable);
However I want to have the width of the background to be as that which is in the device's theme as show in this image here the first image is my activity while the second image is my phone on screen lock. How to I achieve this?

try to setscale to center crop. Maybe this will work you
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/my_image"
android:scaleType="fitCenter" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
</FrameLayout>

You cannot apply scaleType to a background image. Instead, you can change the layout to include an ImageView as the background, and other elements as the foreground.
For example,
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/the_man_in_the_coat_image"
android:scaleType="fitCenter" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- ADD YOUR OTHER ELEMENTS -->
</LinearLayout>
</FrameLayout>

Maybe you should try to setscale to center crop. Maybe this will work you
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/image"
android:scaleType="fitCenter" />
</FrameLayout>
I hope this one help

Related

How to make the GridView center of the device?

<?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:background="#FFFFFF"
android:layout_gravity="center"
android:padding="5dip" >
<GridView
android:id="#+id/homeGridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="200dip"
android:adjustViewBounds="true"
android:layout_gravity="center"
android:gravity="center"
android:horizontalSpacing="0dip"
android:numColumns="2"
android:stretchMode="none"
android:verticalSpacing="#dimen/grid_vertical_space" />
</LinearLayout>
How to make the GridView center of the device?
Use Relative Layout instead of Linear Layout and apply android:layout_centerInParent="true" in your Gird View
Did you tried to set your Gridview's android:layout_width and android:layout_height to wrap_content?

How to make my TextView width half of its parent?

If I want to make my TextView width exactly equal to the width of its parent, I can use
android:layout_width="fill_parent"
What about if I want to make it half the width of the part? Or, in general, set the width relative to the parent's width?
Edit: I am using Relative Layout
My screen looks like this.
The quick and dirty way is like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff0000"
/>
</LinearLayout>
</RelativeLayout>
You have to wrap it in another container, then use only half the weight sum of the parent.
Use android:layout_weight="0.5" it will work for linearLayout
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/feedback"
android:layout_width="wrap_content"
android:layout_weight="0.5"
android:textSize="15pt" />
You may want to set the weightSum of the parent LinearLayout, something like this:-
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:padding="6dp"
android:weightSum="2">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/hello"
android:layout_weight="1" />
</LinearLayout>
First of all don't use "fill_parent" it a deprecated tearm, use "match_parent".
Secondly where do you want to place the half parent TextView in the parent?
As you are using RelativeLayout this action is a little harder, try to do it from code, some thing like this:
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
int myWidth = (int) (parentHeight * 0.5);
super.onMeasure(MeasureSpec.makeMeasureSpec(myWidth, MeasureSpec.EXACTLY),
heightMeasureSpec);
}
reference: How to size an Android view based on its parent's dimensions
If you want to use RelativeLayout, then best approach is to put a dummy view at the center of the screen and put the first view to the right and second to the left hand side of the dummy view like this;
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_contentt"
android:background="#ff0000"
android:layout_toLeftOf="#id/dummyView/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
android:layout_toRightOf="#id/dummyView"/>
<View
android:id="+#id/dummyView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:layout_centerInParent="true"/>
</RelativeLayout>
Wrap your TextView with a Linear Layout, like #r-j and #djhacktorreborn have suggested, and then put the whole thing into a Relative Layout.

Fitting image to background

I want to use an image which is smaller than width of the screen, but has height bigger than the screen. I was thinking to use scroll view, that the whole image could be used, but fitting changes original ratio of the picture. How can I make it to adjust width and increase height by the same ratio? Here is the code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/tutorial"
>
<ScrollView
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/tutorial">
</ImageView>
</ScrollView>
</LinearLayout>
you don't use fill_parent you want use match_parent
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:backround="#drawable/tutorial"/>
use android:scaleType="CENTER_INSIDE" in Your Imageview thats it.
for more details
read more about CENTER_INSIDE here

Android App Layout

I'm developing an android app for 2.2 ver.
My app must hav such structure:
[ Spinner here (fixed height) ]
[ ListView (not-fixed height) ]
[ ImageView (fixed-height) ]
I have to use only portrait orientation.
I use linearLayout for it. How can I calculate listView height to show spinner on the top of the screen, imageview on the bottom, and listview cover all free space, but not push others away from field of view.
It will be cool to make it dinamic for lots of screen resolutions.
Thank you
Use a relative layout to accomplish what you need.
<?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" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:src="#drawable/icon" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/spinner1"
android:layout_above="#+id/imageView1" />
</RelativeLayout>
By doing this listview will adjust its size so that it fits between the spinner and imageview on your screen
You need to use the android:layout_weight attribute on the variable-height item, so it fills the available space.
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Here is another way to do this, using weights. This may be the easiest approach.
You can put in to the weight a percentage of the screen you would like it to take up.
<?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" >
<Spinner
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"/>
<ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="80"></ListView>
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"/>
</LinearLayout>
EDIT:
Now that i think about it, this will "fix" the size of all the widgets. They will be "fixed" at percentages of the screen. This will scale well with other screen sizes, but you did say you wanted ListView to not be fixed.

How can I set the imageview to take up the width of the screen?

I would like an imagview that takes up the width of the screen (or layout) but there seems to be padding around the view that prevents me from doing this.
With the corresponding xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:weightSum="1">
<ImageView android:src="#drawable/icon" android:background="#null"
android:layout_height="106dp" android:id="#+id/imageView1"
android:layout_weight="0.59" android:layout_width="match_parent"></ImageView>
</LinearLayout>
The padding is come from transparent area on android icon.
Just remove unused transparent area or use another image.
Try changing:
android:layout_width="match_parent"
to:
android:layout_width="fill_parent"
make layout width and height as fill_parent
<ImageView android:src="#drawable/icon" android:background="#null"
android:layout_height="fill_parent" android:id="#+id/imageView1"
android:layout_weight="1" android:layout_width="fill_parent"></ImageView>

Categories