How to Use image as border in an android xml - android-layout

I have a popup to show in my xml. It should have a border. Following is the code:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="20dp" >
<RelativeLayout
android:id="#+id/rl_startpopup"
android:layout_width="340dp"
android:layout_height="320dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/popup_outline"
android:orientation="vertical" />
</RelativeLayout>
The popup_outline is a shape
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="270"
android:endColor="#192423"
android:startColor="#4a7669"
android:type="linear" />
<corners android:radius="8dp" />
<stroke
android:width="2dp"
android:color="#e4f4d3" />
</shape>
here is the image of the popup:-
This "popup_outline" is the white border shown. I want to use a repeatable pattern image instead of one color as the border. I have to do this in xml. Please help

You can use a FrameLayout to show the border and inside FrameLayout use ImageView or RelativeLayout to show other content.
Example:-
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/IMAGE_OF_YOUR_CHOICE_WITH_repeatable pattern"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
></RelativeLayout>
</FrameLayout>

You can use any layout to use border and inside the layout you free to use any view or layout e.g TextView, ImageView
Example:-
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/popupbox"
android:padding="50dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Garage is empty" />
</RelativeLayout>

Related

How can I create a custom InfoWindow layout/background for Google Maps on with a 'stalk'

I have managed to define and use a custom InfoWindow in my Google Maps app on Android, but I can't figure out how to create a similar custom layout to that shown on the right in these examples on developer.google.
I have created a simple rounded box with a red outline as a layout and am using this as the background drawable resouce for the layout:
round_bg_box_white_with_red_border.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="5dp" />
<solid android:color="#fff" />
<stroke
android:width="2dp"
android:color="#f00" />
</shape>
custom_info_window.xml
<?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="wrap_content"
android:background="#drawable/round_bg_box_white_with_red_border"
android:orientation="horizontal"
android:padding="5dp">
<TextView
android:id="#+id/fixed_prefix"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:maxLines="1"
android:textColor="#f00"
android:textSize="14sp"
android:text="prefix:"
android:textStyle="bold" />
<TextView
android:id="#+id/marker_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:textColor="#000"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
Thsi all works with my code, but I'd like to add the down pointing triangle extension to the background/layout so that the InfoWindow sits a little above the marker and points to it - as in the standard Google InfoWindows.
I want the box itself to be scaled to it the content (hence using a rectangle shape), but the 'stalk' that points down to NOT scale. ie the same behaviour as the standard InfoWindows.
One might consider it as a work-around, but here is my approach to the following design outcome:
The custom window consists of the content, the outer layout with a drawable as background and an adapted material down arrow icon with 0dp margin.
Your background can easily be updated with yours, despite the fact that a border would not show up. That would need some customization if necessary. The vector code of the triangle might be integrated in another drawable combining the two or smth.
After setup of your infoWindow overwrite
#Override
public View getInfoWindow(#NonNull Marker marker) {
View infoWindow = getLayoutInflater().inflate(R.layout.info_contents, requireView().findViewById(R.id.map), false);
TextView title = infoWindow.findViewById(R.id.location_marker_title);
title.setText(marker.getTitle());
TextView snippet = infoWindow.findViewById(R.id.location_marker_snippet);
snippet.setText(marker.getSnippet());
return infoWindow;
}
My layout looks like this (important the 0dp margin bottom, layout may be simplified):
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:cropToPadding="true">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="0dp"
android:background="#drawable/bg_tooltip"
android:gravity="center_horizontal|center_vertical"
android:maxWidth="250dp"
android:maxHeight="200dp"
android:minWidth="200dp"
android:orientation="horizontal"
android:padding="#dimen/activity_vertical_margin"
app:layout_constraintBottom_toTopOf="#+id/imageView5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/location_marker_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="230dp"
android:text=""
android:textColor="#color/textDark"
android:textFontWeight="600"
android:textSize="20sp" />
<TextView
android:id="#+id/location_marker_snippet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="230dp"
android:text=""
android:textColor="#color/textDark"
android:textFontWeight="400"
android:textSize="14sp"
/>
</LinearLayout>
</LinearLayout>
<ImageView
android:id="#+id/imageView5"
android:layout_width="80dp"
android:layout_height="20dp"
android:layout_margin="0dp"
android:cropToPadding="true"
android:adjustViewBounds="true"
android:padding="0dp"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="#drawable/ic_baseline_arrow_drop_down_24"
app:tint="#color/backgroundAltDark"
tools:ignore="ContentDescription" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
My background is a simple shape with round corners:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_horizontal">
<item>
<shape>
<solid android:color="#color/backgroundAltDark" />
<corners android:radius="#dimen/radius_small" />
</shape>
</item>
</layer-list>
And the arrow looks after customized like this:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="30"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="#android:color/white"
android:pathData="M7,10l5,5 5,-5z"/>
</vector>

How to position ViewPager above RecyclerView?

This xml layout has been populated by ViewPager and RecyclerView via an activity class, I want to position ViewPager on top and RecyclerView at the bottom, I tired to use android:layout_below="#+id/awesomepager" in the LinearLayout of RecyclerView but it doesn't work (it stays on top behind the toolbar[Deep blue color in the picture]), I also used android:layout_alignBottom="#+id/awesomepager" but that puts it inside the ViewPager at the bottom.
I basically want ViewPager and RecyclerView to be siblings , like this picture , black area to be ViewPager at the top and the green area to be RecyclerView at the bottom.
what is correct way to do this?
viewpager_with_toolbar_overlay.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ProgressBar
android:id="#+id/progressbar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v4.view.ViewPager
android:id="#+id/awesomepager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#068006"
android:layout_below="#+id/awesomepager"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/episodesLIST"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:scrollbars="horizontal" />
</LinearLayout>
</RelativeLayout>
<include layout="#layout/toolbar_layout" />
</FrameLayout>
Update:
If I define the hight of ViewPager the problem will be resolved , but when screen size changes lower part of the screen will be blank
I searched a lot and finally came up with a solution, first I changed RelativeLayout to LinearLayout and added android:orientation="vertical" to it, then added android:layout_weight="1" to ViewPager.
Final version
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ProgressBar
android:id="#+id/progressbar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.v4.view.ViewPager
android:id="#+id/awesomepager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#068006"
android:layout_below="#+id/awesomepager"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/episodesLIST"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:scrollbars="horizontal" />
</LinearLayout>
</LinearLayout>
<include layout="#layout/toolbar_layout" />
</FrameLayout>

BottomSheet move to top of screen when changing visibility of its component

I have a bottom sheet with a NestedScrollView inside (see below). When I press on a FAB button, I want to make some parts in this NestedScrollView invisible. But when I change some linearlayouts visibilities to GONE, bottomsheet fly aways from the top.
My XML code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/messageOptionBottomSheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/back_miscellaneous"
android:gravity="center"
android:orientation="vertical"
app:behavior_peekHeight="0dp"
app:layout_behavior="#string/bottom_sheet_behavior">
<View
android:id="#+id/divider2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/textSecondary" />
<LinearLayout
android:id="#+id/layoutDeleteMessage"
android:layout_width="match_parent"
android:layout_height="#dimen/_35sdp"
android:layout_margin="#dimen/_10sdp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:animateLayoutChanges="true"
tools:ignore="UseCompoundDrawables">
<ImageView
android:layout_width="#dimen/_22sdp"
android:layout_height="#dimen/_22sdp"
android:contentDescription="#string/app_name"
android:src="#drawable/ic_remove" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/_10sdp"
android:fontFamily="#font/ubuntu_medium"
android:includeFontPadding="false"
android:text="#string/delete_message"
android:textColor="#color/textSecondary"
android:textSize="#dimen/_12ssp" />
<TextView
android:id="#+id/textConfirmDelete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/_10sdp"
android:fontFamily="#font/ubuntu_medium"
android:gravity="end"
android:includeFontPadding="false"
android:text="#string/confirm"
android:textColor="#color/red"
android:textSize="#dimen/_12ssp"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
Your bottom sheet is moving top because you are using
android:animateLayoutChanges="true"
remove this line from your layoutDeleteMessage (Linear layout)
Hope this solve your problem

Textview width is not the fit the width of the parent

I couldn't make equal width of Textview and linearlayout. Although I set width match_parent in Textview it doesn't fit with the parent width.
I saw these posts :
Android textView width not increasing
Android TextView does not expand to match parent
TextView width match drawableTop width
How to adjust text font size to fit textview
How to adjust text font size to fit textview
But my problem isn't resolved
<?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="wrap_content"
android:gravity="right|center_vertical"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:orientation="horizontal">
<ImageView
android:id="#+id/thumbnail"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:src="#drawable/defaultpicpic" />
<TextView
android:id="#+id/title"
style="#style/textViewItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp" />
</LinearLayout>
I resolved my problem by wrapping the views in a LinearLayout and followed by a small View beneath.
It is not clear why this worked.
<?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:background="?android:attr/selectableItemBackground"
android:gravity="right|center_vertical"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="right|center_vertical"
android:orientation="horizontal">
<ImageView
android:id="#+id/thumbnail"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:src="#drawable/thumb" />
<TextView
android:id="#+id/title"
style="#style/textViewItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2px" />
</LinearLayout>

Rounded list item with different background color on right side

This is what I try to achieve:
The amount of text will determine how large portion of the list item that should have the blueish background color.
I try to achieve this with a selector but then I end up with a blueish square where the uttermost parts are grey. Like this:
Any ideas how this can be done in a simple way? I.e. preferrably using XML only.
This is my approach (yields second picture):
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="true"
android:variablePadding="false">
<item android:drawable="#drawable/btn_round_grey"/>
</selector>
and:
<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="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:background="#android:color/transparent"
android:clickable="true"
android:descendantFocusability="blocksDescendants"
tools:background="#drawable/btn_round_grey"
>
<RelativeLayout
android:id="#+id/grey"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<FrameLayout
android:id="#+id/text_container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignBottom="#id/grey"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="false"
android:layout_alignParentTop="true"
android:padding="#dimen/list_item_padding"
>
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="#dimen/list_item_padding"
android:background="#android:color/transparent"
android:text="TEXT"
/>
</FrameLayout>
</RelativeLayout>
You can achieve the desired output by the following code
<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="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:background="#drawable/roundcorner_selector"
android:clickable="true"
android:descendantFocusability="blocksDescendants" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/half_roundcorner_selector"
android:padding="10dp"
android:text="TEdasdadaXT" />
</RelativeLayout>
roundcorner_selector:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="10dp" />
<solid android:color="#android:color/darker_gray" />
</shape>
half_roundcorner_selector:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners
android:bottomRightRadius="10dp"
android:topRightRadius="10dp" />
<solid android:color="#android:color/holo_blue_light" />
</shape>
You can modify this as per your requirements.
In your code you need to set the custom drawable for the TextView. Because by setting the color as background it will take as square.

Resources