Using image as background Header Bar and footer shows white paddings - android-layout

I want to style the action bar and footer using the background image of header and footer respectively. When it comes to the execution by modifying the layout, it seems there are white spaces which is not pleasant for the appearance. All of my images uses png files with transparent background. Are there any methods for modifying the transparency of the header and footer background ? The below os my relative layout structure :
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent" >
<!-- Header aligned to top -->
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#drawable/heading"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Fixed Header"
android:textColor="#000"
android:textSize="20sp" />
</RelativeLayout>
<!-- Footer aligned to bottom -->
<RelativeLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/footer"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Fixed Footer"
android:textColor="#000"
android:textSize="20sp" />
</RelativeLayout>
<!-- Scrollable Item below header and above footer -->
<ScrollView
android:id="#+id/scrollableContents"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/footer"
android:layout_below="#id/header"
android:background="#ff5" >
<!-- Inflate the contents of the ScrollView dynamicaly -->
</ScrollView>
</RelativeLayout>

The white part of your header & footer comes from activity background (because listview is below header and above footer) which could be changed by using styles :
Define custom color in res/values/colors.xml
<color name="custom_theme_color">#b0b0ff</color>
Create a style in res/values/styles.xml
<style name="CustomTheme" parent="android:Theme.Light">
<item name="android:windowBackground">#color/custom_theme_color</item>
<item name="android:colorBackground">#color/custom_theme_color</item>
</style>
(windowBackground can be a drawable reference too)
Set this style to your activity in Manifest.xml :
<activity
android:name="myActivity"
android:theme="#style/CustomTheme">
For more details see Android Styles and Themes

Related

Bottom Sheet Dialog - Transparent background header with close button

I am trying to make a transparent background header for close button. The background header is working as expected, however the background layout can see the visible and button is also not clear.
Am trying to build like this below picture
https://ibb.co/CVgrzjq
My code is as follows:
Activity Code
final BottomSheetDialog dialog = new BottomSheetDialog(this, R.style.BottomSheetMainStyle);
`dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
`dialog.show();
Layout Code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:gravity="center|top"
android:orientation="vertical">
<ImageView
android:id="#+id/img_close"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:background="#android:color/transparent"
android:clickable="true"
android:forceDarkAllowed="false"
android:paddingBottom="#dimen/dimen_8"
android:src="#drawable/bottom_close"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_below="#id/img_close"
android:background="#drawable/rounded_dialog"
app:background="#color/bkgcolor"
android:orientation="vertical">
<!-- All your required widgets here-->
</RelativeLayout>
</RelativeLayout>
Activity code
final BottomSheetDialog dialog = new BottomSheetDialog(this, R.style.trans_bg_dialog);
`dialog.show();
Add this in your style.xml file
<style name="trans_bg_dialog" parent="#style/Theme.AppCompat.Dialog">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
</style>

Material Design Toolbar CustomView RelativeLayout

RelativeLayout doesn't seem to play nice in the v7 support library. I have three elements, One is aligned to the bottom and the other two stack above it. Rather than appear nicely stacked at the bottom, they are rendered off screen, this is unexpected.
I've included an example XML file which is for the custom view added to the toolbar.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/name"
android:layout_above="#+id/divider"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textColor="#color/textLighter"
android:text="#string/example_sequence_name"
android:singleLine="true" />
<View
android:id="#+id/divider"
android:layout_width="wrap_content"
android:background="#color/textLighter"
android:layout_above="#+id/description"
android:layout_height="81dp"/>
<EditText
android:id="#+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_description"
android:textAppearance="#style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle"
android:textColor="#color/textLighter"
android:layout_alignParentBottom="true"
android:singleLine="true" />
Results in this view.

TextView expands layout but webview does not

I've got a layout with a textview with some buttons at the bottom. The textview is set to expand to fill the space. This is fine. However, when I change the textview to a webview, the webview does not expand to fill the space. What's wrong?
This is working fine:
<?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="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
<!-- height set to fill because background is black and we want
the light background -->
<TextView
style="#style/subtopicView"
android:id="#+id/subtopic_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</ScrollView>
<include layout="#layout/include_html_view_footer" />
</LinearLayout>
This is not, but the only change is the textview to a webview and all other values remain the same.
<?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="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
<!-- height set to fill because background is black and we want
the light background -->
<WebView
style="#style/subtopicView"
android:id="#+id/subtopic_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</ScrollView>
<include layout="#layout/include_html_view_footer" />
</LinearLayout>
the include is simply this:
<?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="wrap_content"
android:background="#color/tintedColour"
android:orientation="horizontal"
android:paddingBottom="#dimen/spacer"
android:paddingTop="#dimen/spacer" >
<Button
android:id="#+id/PrevPage"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/previous" />
<Button
android:id="#+id/NextPage"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/next" />
<Button
android:id="#+id/Copy"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/copyclipboard" />
</LinearLayout>
Thanks for any help.
It's generally a bad idea to nest scrollable elements like you have done by including a WebView inside a ScrollView. Touch events will be unpredictable, and it would probably be a better idea to drop the ScrollView altogether.
That said, the height of your WebView is set to fill_parent. However, the ScrollView is simultaneously trying to minimize the size of its children. As Romain Guy writes in a blog post on this topic:
In attempt to achieve this effect, I have seen several Android developers try to set the height of the view inside the scroll view to fill_parent. Doing so does not work and leads to the following result:
To understand this result, you must remember that android:layout_height=”fill_parent” means “set the height to the height of the parent.” This is obviously not what you want when using a ScrollView. After all, the ScrollView would become useless if its content was always as tall as itself.
By setting android:fillViewport="true", the scroll view’s child expands to the height of the ScrollView. (When the child is taller than the ScrollView, the attribute has no effect.)

How do I load two fragments vertically in the same layout?

I am making an app for my university's SafeWalk program and I am trying to make an activity that will display a map fragment with a button bar below it with emergency call buttons. The problem I am running in to is that when I load the following layout file, only the map fragment is displayed, and not the buttons along the bottom.
Note: I am also using a navigation drawer in this layout so I have included that code in case it is conflicting.
I have tried changing the layout_width and layout_height values of the two parent FrameLayouts
and I only can obtain one of two results, a full page of map, or a full page of my buttons (stretched to the top). I can attach the layout for the CallButtonFragment if necessary, but it is a horizontal linear layout with the button bar style containing two buttons.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/content_frame" >
<fragment
android:id="#+id/titles"
android:name="edu.purdue.SafeWalk.CallButtonFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</RelativeLayout>
<!-- The navigation drawer -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
Following the suggestion of Android-er-naut below, I have this result:
(It is an improvement, but the buttons need to be a bit larger and fit the width of the screen.)
How about this -
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<fragment
android:id="#+id/titles"
android:name="edu.purdue.SafeWalk.CallButtonFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>

no background when ListView doesn't fill screen on different phone

I have an app that I am putting together and I have a background on it. There is a header on top and a ListView in the middle. When I have a list in the middle that isn't at least as big as the screen it just shows gray not the background of the parent layout. The weird thing is that this doesn't happen on my older phone, the background fills the whole screen even if the list doesn't. Here is the xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- This is the xml for HomeActivity, more information in HomeActivity.java -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/common_aboutLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg">
<!-- This is the header on the top of the home screen -->
<LinearLayout android:id="#+id/dynamicBanner"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#drawable/header"
android:layout_width="fill_parent">
<!-- This is the temp pop-up on start -->
<TextView android:text="#+id/TextView01"
android:id="#+id/titleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:textColor="#FFFFFF"
android:visibility="invisible"
android:textStyle="bold"
android:typeface="sans"
android:ellipsize="end"
android:textSize="18sp"
android:gravity="center_vertical|center_horizontal">
</TextView>
</LinearLayout>
<!-- This is the layout of the list of ringtones. -->
<ListView android:id="#+id/homeListView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
</ListView>
<LinearLayout android:id="#+id/adLayout"
android:layout_width="fill_parent"
android:layout_height="0dp">
<com.admob.android.ads.AdView android:id="#+id/ad"
admob:textColor="#FFFFFF"
admob:keywords="Android application"
android:layout_width="fill_parent"
android:layout_gravity="bottom|center"
admob:backgroundColor="#666666"
admob:refreshInterval="30"
android:layout_height="0dp" />
</LinearLayout>
</LinearLayout>
The TextView is just a temp thing when the window first opens.
Just looking for some help, Thanks.
put All views in RelativeLayout.then put this admob layout and add a tag to plcae this to bottom of layout
like
android:layout_alignParentBottom="true"
<LinearLayout android:id="#+id/adLayout"
android:layout_width="fill_parent"
android:layout_height="0dp">
<com.admob.android.ads.AdView android:id="#+id/ad"
admob:textColor="#FFFFFF"
admob:keywords="Android application"
android:layout_width="fill_parent"
android:layout_gravity="bottom|center"
admob:backgroundColor="#666666"
admob:refreshInterval="30"
android:layout_height="0dp" />
</LinearLayout>
and then Header part inside the relative layout
android:layout_alignParentTop="true"
and the put the ListView and
android:layout_above="#+id/adLayout"
android:layout_below=HEADERVIEW

Resources