Horizontally and vertically center a ProgressBar in Relative Layout - java

I am trying to achieve the following progress dialog (without the text)
My current XML code is as follows:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background"
>
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:visibility="gone"
/>
I am programatically hiding and showing the progress bar when required (and hiding/showing other screen elements), all of which are under the Relative Layout in the XML code above.
However, whenever the Progress Bar shows, it appears centered at the top of the screen instead of being centered horizontally AND vertically.

Setting the ScrollView's attribute "fillViewport" to true should do it.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background"
android:orientation="vertical"
android:fillViewport="true"
>
also make sure its child's height is set to "fill_parent" and the ProgressBar is properly centered
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"

Related

set background png on Linearlayout without affecting layout size

Background image doesn't fill Linearlayout and changes that size
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#mipmap/dashboard_background"
android:orientation="vertical"
android:padding="10dp">
desirable look is this
How can I reach that?
You should use a frame layout with an imageview and a linearlayout inside
<FrameLayout
android:id="#+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/background"/>
<LinearLayout
---Your Layout---
</LinearLayout>
Background always stretches to its View size. So you can't really control how it will look like. Instead, put an ImageView as a bottom most (meaning that it will lay under everything else) element in your layout, and use your background image as a source (src) for this ImageView, and so you will be able to control the look of the background with scaleType (probable you would want to use centerCrop or fitCenter). More about scaleType here.
In the end your layout should look something like this:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:src="#mipmap/dashboard_background" />
<FrameLayout
android:id="#+id/content_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Put your view elements here -->
</FrameLayout>
</FrameLayout>

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 image and button inside a scrollview

I am using following configuration to properly fit image inside a scrollview.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical" >
<ImageView
android:id="#+id/textNimbo"
android:layout_width="match_parent"
android:layout_height="2492dp"
android:layout_alignParentRight="true"
android:src="#drawable/inicio"
android:adjustViewBounds="true" />
</ScrollView>
However I need to set also a button inside scrollView and below the ImageView. I tried relative and linear layouts inside scrollView but no succes, button is not visible or image doesn't fit to scrollView. Any recommended configuration? Thank you.
A ScrollView can only have one direct child element. So to add a Button and an ImageView, you'll need something like this:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical" >
<LinearLayout
android:id="#+id/layout"
android:layout_width="fill_parent"
android"layout_height="fill_parent">
<ImageView
android:id="#+id/textNimbo"
android:layout_width="match_parent"
android:layout_height="2492dp"
android:layout_alignParentRight="true"
android:src="#drawable/inicio"
android:adjustViewBounds="true" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
You could also use ImageButton I
instead of a ImageView with a transparent Button on it

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>

problem in showing listview in horizontal layout?

I am trying to show a list view with calendar view in a linear layout. when i am using vertical layout then list view appears but in horizontal layout the same listview disaappears. I solved the problem using relative layout, but can i do this using linear layout.here is my xml...
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.exina.android.calendar.CalendarView
android:layout_height="wrap_content"
android:id="#+id/calendar" android:layout_width="wrap_content">
</com.exina.android.calendar.CalendarView>
<ListView android:layout_height="fill_parent" android:id="#+id/android:list"
android:layout_weight="1" android:scrollingCache="false"
android:layout_width="fill_parent" android:drawSelectorOnTop="false"
android:dividerHeight="4.0sp"></ListView>
</LinearLayout>
</ScrollView>
You need to set the fillviewport flag in the scrollview :
android:fillViewPort="true"
Check this link to know what fillviewport flag does: ScrollView

Categories