Center horizontally and/or vertically with 9 patch in Android - java

I can't find a topic talking about my problem which seems to me standard ... And that make me think it is not possible. But I give a try.
I have a RelativeLayout with a 9 patch background (which contain a content area).
Is that possible to center horizontally and/or vertically a TextView inside this RelativeLayout in relation to the content area ?

this layout file will do the task
<?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:background="#drawable/background"
android:gravity="center" >
<TextView
android:id="#+id/txtTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test TextView"
android:textColor="#000000" >
</TextView>
</RelativeLayout>
the 9-patch image i used is

Related

How to make Horizontally scrollable Grid View?

I am trying to make a Grid View and it has a lot of images. So I want to make it Horizontally scrollable, but it is not scrolling. How to make Horizontally scrollable Grid View ? Please help me.
Here is my code.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="horizontal" >
<GridView
android:layout_width="500dp"
android:layout_height="400dp"
android:id="#+id/gridview"
android:columnWidth="300dp"
android:numColumns="3"
android:horizontalSpacing="10dp"
android:scrollbars="horizontal">
</GridView>
</HorizontalScrollView>
</RelativeLayout>
I got the answer, and I am sharing here.
Just go on the following link:-
Link and just add this library into your project. After that just use the xml code at the place of Grid View code.
<com.jess.ui.TwoWayGridView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/grid_viewlevel"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:gravity="center"
app:columnWidth="200dp"
app:rowHeight="200dp"
app:numColumns="15"
app:numRows="2"
app:verticalSpacing="0dp"
app:horizontalSpacing="0dp"
app:stretchMode="columnWidth"
app:scrollDirectionPortrait="horizontal"
app:scrollDirectionLandscape="horizontal"/>
After normally Just apply the following code in your java file.
TwoWayGridView scrollview;
scrollview = (TwoWayGridView) findViewById(R.id.grid_viewlevel);
mAdapter = new LevelAdapter(this, R.layout.levelselect);
scrollview.setAdapter(mAdapter);

Android Widget Wrong Size

I have an Android widget. I would like it to be 4 cells width and 2 cells height. Right now the size of the widget is 4 by 4 (for some reason). And the strange thing is that the content of the widget, i.e. the frame (according to this definition) is of small size.
So, most of the home screen is blank, but I cannot reposition the widget (seems that it takes all 4 by 4 cells on my home screen).
Here is the code of my widget-provider layout file:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="#layout/widget_demo"
android:minHeight="110dp"
android:minWidth="250dp"
android:updatePeriodMillis="1800000"
>
</appwidget-provider>
Here is the widget layout:
<?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="110dp"
android:orientation="vertical"
android:layout_margin="1sp"
android:background="#android:drawable/alert_dark_frame" >
<TextView
android:id="#+id/widgetTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="some_text"
android:textColor="#FFFFFF"
android:textSize="30dp" />
<Button
android:id="#+id/widget_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="some_text"
android:textSize="20dp"/>
</LinearLayout>
What is the problem? Why does it take 4 by 4 instead of 4 by 2?
App Widget layouts should be flexible, resizing to fit their parent container
So, change your LinearLayout
android:layout_width="wrap_content"
android:layout_height="110dp"
to match parent size
android:layout_width="match_parent"
android:layout_height="match_parent"

How do I make images overlap in android using relativelayout

Consider the image above.
The red lines indicate the image file borders, the black lines indicate the actual "useful" pixels of the image, and the white = transparent.
How would I go about getting these images positioned this way?
I'm not really sure this would make a difference in the way the above is done, but I am planning on implementing basic animations to each image (make each image rotate in respect to their respective centers).
This is what I have so far (positioning the 3 elements in the center, overlapping each other):
<?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:layout_centerInParent="true"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription = "#string/img1"
android:src="#drawable/img1"
<ImageView
android:id="#+id/img_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/img2"
android:rotation="45"
android:src="#drawable/img2" />
<ImageView
android:id="#+id/img_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/img3"
android:rotation="200"
android:src="#drawable/img3" />
Thanks!
/e 1 - Thank you for editing in the image
2 - I solved this problem through trial and error, thank you anyways!

Android ImageView scale type centerCrop

I'm creating an activity on Android. I want to show a background image in it. I put a ImageView into my main RelativeLayout.
<RelativeLayout
android:id="#+id/relativeLayoutMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:orientation="vertical" >
<ImageView
android:id="#+id/bgImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:src="#drawable/bg4"
android:scaleType="centerCrop" />
</RelativeLayout>
It makes view like this.
I need to make view like this
How can I do it?
Try set ImageView attribute ScaleType=fitEnd ?
use this code.it's give the out-put as you want.
Code
<?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" >
<RelativeLayout
android:id="#+id/relativeLayoutMain"
android:layout_width="150dp"
android:background="#android:color/background_dark"
android:layout_height="150dp" >
<ImageView
android:id="#+id/bgImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:scaleType="centerCrop"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
</RelativeLayout>
one advice if u give the imageview width fill parent then it's contain whole space according to main container.
Best Luck
Set the android:background="#drawable/bg4" in the RelativeLayout like that:
The whole XML should look like that:
<RelativeLayout
android:id="#+id/relativeLayoutMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="#drawable/bg4
android:orientation="vertical" >
</RelativeLayout>
First, I believe there are a couple of things wrong in the xml:
RelativeLayout does not make use of an orientation, you should remove that
Is this your full xml file? If so then the alignParentLeft in the RelativeLayout doesn't do anything because that is an attribute you use if it's parent is a RelativeLayout.
fill_parent is the deprecated name of match_parent, you should use that.
I also believe that code you have would center the image on the screen putting anything that is to big evenly on both sides and not at all what you said it does.
If you just want a background you can just use android:background="#drawable/bg4" in the RelativeLayout
EDIT: Changing the layoutparams of the ImageView to wrap_content both height and width and removing the scaleType alltogether might make it like you wanted it to be.

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