How to center text in android screen (not center in parent) - android

I want to center a small text in center of screen based on the default "Empty activity" as per code samples in Android Studio 2.1.
The layout xml file i use today is as provided below. I inflate it in the OnCreate method as normal through the setContentView, so the layout as defined in the xml dont fill the whole screen. Thus the layout_centerInParent will center my texts in the parent, but not center in screen. How do i get my text to center on screen, while keeping my action bar?
Illustration of problem
Centering in parent leads to "bad" position. Centering on screen would give "good" postion.
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.sara.internetcheck.MainActivity">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<TextView
android:id="#+id/text_header"
style="#style/TextAppearance.AppCompat.Headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="#string/good_news_title" />
<TextView
android:id="#+id/text_body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/text_header"
android:text="#string/good_news_text" />
</RelativeLayout>
</RelativeLayout>

Use for example CoordinatorLayout. Elements in this layout has no relation between them so can be one on another.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:id="#+id/root_layout"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="?attr/actionBarSize"
android:text="Text not in center">
</TextView>
</RelativeLayout>
<!-- This is centered TextView -->
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="?attr/actionBarSize"
android:text="Text in center"
>
</TextView>
</android.support.design.widget.CoordinatorLayout>
Two important things:
Centered TextView should be children of CoordinatorLayout
To center in CoordinatorLayout use attribute android:layout_gravity="center"
If we want exacly center we need to change margin because toolbar size is our not wanted difference for exactly center, i used android:layout_marginBottom="?attr/actionBarSize" to move TextView up by toolbar height.
Calculating screen and setting position programically is not good proposition, this can cause not wanted problems when for example user rotates screen.

You can get the real dimension of you screen by
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
displayMetrics.widthPixels; //screen width
displayMetrics.heightPixels; //screen height
then
LayoutParams layoutParams = new LayoutParams(int width, int height);
layoutParams.setMargins(int left, int top, int right, int bottom);
viewToCenter.setLayoutParams(layoutParams);

Related

Android layout with view fixed to bottom

I'm trying to create a layout in Andorid. I need a TextView (2 in diagram below) fixed to the bottom of the screen. With content (1 in diagram) comprising of scrollview and editTexts.
The problem I have is when the soft keyboard comes up (3 in diagram), the red box marked 2 also comes up, above the keyboard. I would like the red box to remain offscreen and the entire content to scroll in the space remaining above the soft keyboard.
I've tried putting the red box (2) in the LinearLayout but it never fixes itself to the bottom, there's always a small gap at the bottom of the screen. I've also tried changing android:windowSoftInputMode in the manifest but this isn't the desired affect.
This is what I have:
<?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">
<ScrollView
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:isScrollContainer="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText />
<EditText />
</LinearLayout>
</ScrollView>
<TextView
android:id="#+id/3"
android:layout_alignParentBottom="true"
text="3" />
</RelativeLayout>
Any suggestions would be very much appreciated.
All you need to do is switch your activity's windowSoftInputMode flag to "adjustPan". Check the official documentation for more info.
<activity
...
android:windowSoftInputMode="adjustPan">
</activity>
Try using the below layout see if this is what you are looking for..
<?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">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:isScrollContainer="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<EditText
android:layout_width="80dp"
android:layout_height="40dp"
android:id="#+id/ed1"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:layout_marginBottom="50dp"/>
<EditText
android:layout_width="80dp"
android:layout_height="40dp"
android:id="#+id/ed2"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:layout_marginBottom="50dp"/>
</LinearLayout>
</ScrollView>
<TextView
android:id="#+id/btext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="this is 3rd view"
android:layout_marginBottom="20dp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
TL;DR version, i just removed two lines in your scrollview code:
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
with
android:layout_centerInParent="true"
See if that helps!
And any reason why you are using both android:layout_alignParentTop="true"
android:layout_alignParentBottom="true" on scrollview?
Here's one idea that works.
Adjust the layout so that the red box (2) is inside the LinearLayout
Create a small view between the red box and the last EditText
Give the red box and the LinearLayout an id, so we can manage them from the java class.
Your xml should look like this:
<?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">
<ScrollView
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:isScrollContainer="false">
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText />
<EditText />
<View
android:id="#+id/stretcher"
android:layout_width="match_parent"
android:layout_height="0dp" />
<TextView
android:id="#+id/2"
android:layout_gravity="bottom"
text="2" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
Now in your java onActivityCreated add this code:
final View mainLayout = getView();
final View mainContent = getView().findViewById(R.id.content);
final View stretcherView = getView().findViewById(R.id.stretcher);
//Main layout uses weight some, so we can't hard code the size of the circles.
//We must dynamically re-size
mainLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (android.os.Build.VERSION.SDK_INT >= 16) {
mainLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
mainLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
//Calculate the desired height of the stretcher view to be the remainder between full screen and content.
int stretchHeight = mainLayout.getHeight() - mainContent.getHeight();
//Apply calculated height remainder to stretched view.
//This enables our bottom box to be pushed to the bottom without obstructing the content when the keyboard appears.
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) stretcherView.getLayoutParams();
params.height = stretchHeight;
stretcherView.setLayoutParams(params);
}
});
The view listener measures the height of the full view then subtracts the height of the content. This leaves us with the gap between the content and the bottom of the screen. Using the stretcher view we can now push the red box down to the bottom of the page, just as if it was alignParentBottom.
When the keyboard appears, the red box is inside the scroll.

two views with match_parent in linear_layout in scrollview

Is it possible to have two views that are the same height of the activity screen inside of a linear_layout inside of a scrollview? I tried achieving this like so:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</ScrollView>
The only thing that appears one button with the height of the screen. The other button seems to have disappeared.
I suppose it is paradoxical to have two views to with the attribute match_parent.
However, I was hoping to get two views the same size of the available screen using xml, and having the scrollview make both views accessible. I am aware that it can be done via java, but I want an answer for xml.
In Linear Layout you have to define two things...
1 .android:orientation="vertical"
2 .android:weightSum="2"
weightSum is sum of all element in it's parent layout.It Enable you to define the proportion of width/height a element should take in a layout..
For further details go to weightSum
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/textView1"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2" >
<Button
android:id="#+id/but1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="but 1" />
<Button
android:id="#+id/but2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="but 1" />
</LinearLayout>
</ScrollView>
output
You cannot achieve this in XML only.
As android support multiple screen sizes, At runtime you need to check for each device , the height for each device can be calculated like this
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int height = size.y;
With above code you will get the height of screen and you need to set this height in dp to your view at runtime.
Do this in your activity for both the buttons:
// get view you want to resize
Button but1= (Button) findViewById(R.id.but1);
LinearLayout.LayoutParams listLayoutParams = new LinearLayout.LayoutParams(
height , LinearLayout.LayoutParams.MATCH_PARENT);
but1.setLayoutParams(listLayoutParams);

ScrollView not sizing itself correctly

I'm trying to get a ScrollView to take up as much screen space as it needs until it would start pushing items below (outside) it off the screen, then it needs to stop expanding and become scrolly.
<?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"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0000"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="taking\nup\lots\nof\nlines\nto\nmake\nthe\nscrollview"s\ncontent\ntaller\nthan\nthe\nspace\navailable\nto\nit\nwhich\nshould\nmake\nthe\nscrollview\nstop\nabove\nthe\nbutton\nand\nbecome\nscrollable"
/>
<!--
android:text="just one line"
-->
</ScrollView>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button"
/>
</LinearLayout>
As it is above the ScrollView fills the entire screen height and pushes the button off the bottom of the screen.
If I add android:layout_weight="1" to the ScrollView then it works for this case - the button is at the bottom and the ScrollView stops above it - but when the ScrollView doesn't have much content (replace the text with the one-liner) then the ScrollView doesn't shrink to fit around the content so is far too tall.
I've tried using RelativeLayout with no success - if the Button is android:layout_below the ScrollView then the ScrollView will push it off the bottom of the screen if it has a lot of content.
Here's what I want it to look like: in the first image the ScrollView has a lot of content and so expands to fill the available height but doesn't push the items below it (the button) offscreen, in the second image the ScrollView doesn't have much content so takes up just the height it needs allowing the items below it (the button) to move up the screen:
What you can do, is to correct the height in your code. It's a bit hacky and I would like to see another solution, but off the top of my head I do not know anything better.
What you would need is to add a OnGlobalLayoutListener and calculate within it the minimum of either the ScrollView height or the height of the container surrounding your ScrollView minus the height of your Button.
Then set the size with setLayoutParams() on your ScrollView.
And you have to remove the listener to avoid an endless loop :-)
final View scrollview = findViewById(R.id.scrollview);
final View container = findViewById(R.id.container);
final View button = findViewById(R.id.button);
scrollview.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int height = scrollview.getHeight();
int heightButton = button.getHeight();
int heightContainer = container.getHeight();
int min = Math.min(heightContainer - heightButton, height);
int width = scrollview.getWidth();
Log.v("test", "min: " + min);
scrollview.setLayoutParams(new RelativeLayout.LayoutParams(width, min));
// do not forget to remove the listener
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
scrollview.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
else {
scrollview.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
});
For this to work you have to use wrap_content as the height of the ScrollView in your layout file. And the outer container has to be a RelativeLayout so that the Buttonis rendered and has a non-zero height!
If you use paddings or margins you would have to consider those values in the computation.
Try this:
<?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"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/button"
android:background="#ff0000">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="taking\nup\lots\nof\nlines\nto\nmake\nthe\nscrollview"s\ncontent\ntaller\nthan\nthe\nspace\navailable\nto\nit\nwhich\nshould\nmake\nthe\nscrollview\nstop\nabove\nthe\nbutton\nand\nbecome\nscrollable" />
<TextView
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="taking\nup\lots\nof\nlines\nto\nmake\nthe\nscrollview"s\ncontent\ntaller\nthan\nthe\nspace\navailable\nto\nit\nwhich\nshould\nmake\nthe\nscrollview\nstop\nabove\nthe\nbutton\nand\nbecome\nscrollable" />
<TextView
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="taking\nup\lots\nof\nlines\nto\nmake\nthe\nscrollview"s\ncontent\ntaller\nthan\nthe\nspace\navailable\nto\nit\nwhich\nshould\nmake\nthe\nscrollview\nstop\nabove\nthe\nbutton\nand\nbecome\nscrollable" />
<TextView
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="taking\nup\lots\nof\nlines\nto\nmake\nthe\nscrollview"s\ncontent\ntaller\nthan\nthe\nspace\navailable\nto\nit\nwhich\nshould\nmake\nthe\nscrollview\nstop\nabove\nthe\nbutton\nand\nbecome\nscrollable" />
<TextView
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="taking\nup\lots\nof\nlines\nto\nmake\nthe\nscrollview"s\ncontent\ntaller\nthan\nthe\nspace\navailable\nto\nit\nwhich\nshould\nmake\nthe\nscrollview\nstop\nabove\nthe\nbutton\nand\nbecome\nscrollable" />
<TextView
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="taking\nup\lots\nof\nlines\nto\nmake\nthe\nscrollview"s\ncontent\ntaller\nthan\nthe\nspace\navailable\nto\nit\nwhich\nshould\nmake\nthe\nscrollview\nstop\nabove\nthe\nbutton\nand\nbecome\nscrollable" />
<TextView
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="taking\nup\lots\nof\nlines\nto\nmake\nthe\nscrollview"s\ncontent\ntaller\nthan\nthe\nspace\navailable\nto\nit\nwhich\nshould\nmake\nthe\nscrollview\nstop\nabove\nthe\nbutton\nand\nbecome\nscrollable" />
<TextView
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="taking\nup\lots\nof\nlines\nto\nmake\nthe\nscrollview"s\ncontent\ntaller\nthan\nthe\nspace\navailable\nto\nit\nwhich\nshould\nmake\nthe\nscrollview\nstop\nabove\nthe\nbutton\nand\nbecome\nscrollable" />
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Button" />
</RelativeLayout>
Just try adding fillViewport = "true" on your scroll view

Putting ImageView on the bottom of the screen

I am trying to put the ImageView on the bottom of the layout with following XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#EEEEEE"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:src="#drawable/photo" />
</LinearLayout>
</LinearLayout>
I am following the instructions found here http://sandipchitale.blogspot.co.uk/2010/05/linearlayout-gravity-and-layoutgravity.html
Only difference is that I use ImageView instead of Button as here:
<?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="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/Button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="bottom" >
</Button>
</LinearLayout>
</LinearLayout>
Which puts the button on the expected place:
Unfortunately this isn't the case in my XML on the top of the post. The ImageView isn't placed on the bottom as the Button in reference XML is.
Any ideas why that happens ?
You should change a setting in ImageView itself, to be precise set android:scaleType="fitEnd" on your ImageView.
The other answers seem to be workarounds, including your own answer. This is not a problem with a layout, but with where inside the ImageView the image is loaded when the dimensions of the image and the screen aren't similar, (i.e. the width/height ratio is very different). By default it would load the image in the middle of the ImageView.
Try using RelativeLayout instead of LinearLayout and use parameter android:layout_alignParentBottom = "true".
EDIT: Try giving layout_height and layout_width as wrap_content for your LinearLayout that holds your ImageView.
Or it might be that the image you are using has extra space above and below it.
UPDATE:
The problem is with the size of the image. It is much larger than the screen size. I would suggest you to programmatically read the screen size of the device with below code:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
and set the height and width for imageView programmatically as below:
image_view.getLayoutParams().height = height - 50;
image_view.getLayoutParams().width = width - 50;
Try this inside relative layout
<ImageView
android:id="#+id/imageView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:adjustViewBounds="?attr/isLightTheme"
app:srcCompat="#drawable/client_app_footer" />
tested & working as expected
you did not include android:layout_gravity="bottom" line in your imageview.
I have resized photo size from width 2560 to width 270 and finally ImageView went to bottom.

How to lay image over another image where the middle of the image should align with the bottom of the image underneath

I have a banner that I am displaying on an Android Layout. On this banner, I have two avatars that I would like to display next to each other, and most importantly, I would like to have them displayed where the midway point of these two avatars on the y-axis is aligned with the bottom of the banner that these avatars sit on top of.
How would you do this?
Edit:
In other words, I'm asking how you could use an parameter like android:layout_below, but instead of it aligning the top of the imageview with the botton of the specified layout, to align the center.
Unfortunately, there is no direct layout parameter to align a center point with another edge. If the height of your avatars is fixed, you could add some padding that is half the height so they all line up; i.e.
<?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="wrap_content">
<ImageView
android:id="#+id/banner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="25dp"
android:src="#drawable/banner" />
<ImageView
android:id="#+id/avatar1"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignBottom="#id/banner"
android:src="#drawable/horse" />
<ImageView
android:id="#+id/avatar2"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignBottom="#id/banner"
android:layout_toRightOf="#id/avatar1"
android:src="#drawable/horse" />
</RelativeLayout>
If the heights of those items, however, is dynamic, then you will need to create a custom ViewGroup container so you can measure the avatar heights (in onMeasure()) and apply the padding (or other offset value) at runtime.
Put them in a linear layout, and then give them a width to fill the parent. Then you can use the weight property to disperse the widths equally.
<?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="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/clock" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/clock" />
</LinearLayout>

Categories

Resources