why there is some space in this layout? - android

I use this code for making this layout.
<?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:layout_margin="20dp"
android:background="#android:color/transparent">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_centerInParent="true"
android:background="#drawable/rounded_white_bg">
<EditText
android:id="#+id/login"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#drawable/dialog_edittex"
android:layout_margin="10dp"/>
<EditText
android:id="#+id/password"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_margin="10dp"
android:background="#drawable/dialog_edittex"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#android:color/black"
android:text="AAAAAAAAAAAA"
android:layout_gravity="center"
android:textSize="20sp"
/>
<ImageView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:src="#drawable/dialog_button_bg"/>
</LinearLayout>
</RelativeLayout>
rouded_whiite.bg.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff" />
<corners
android:radius="20dp"/>
</shape>
But when I do this I have see this result. As you can see there is some margin below of red button. I didn't set any margin or something but there is some space there. What should I do here for getting exact result as in fist image.

Remove the margin from your RelativeLayout and make your LinearLayout gravity android:layout_centerHorizontal="true"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_centerHorizontal="true"
android:background="#drawable/rounded_white_bg">

Use :
android:layout_centerHorizontal="true"
instead of
android:layout_centerInParent="true"
and remove margin from bottom...

<LinearLayout
android:layout_height="wrap_content"
it is because of this. remove the imageview from the linear layout and set in the relative layout and give property as alignparent bottom and for the linear layout give alignparent top and above the image view property,

Try This
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:background="#android:color/transparent">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="#drawable/rounded_white_bg">
<EditText
android:id="#+id/login"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#drawable/dialog_edittex"
android:layout_margin="10dp"/>
<EditText
android:id="#+id/password"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_margin="10dp"
android:background="#drawable/dialog_edittex"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#android:color/black"
android:text="AAAAAAAAAAAA"
android:layout_gravity="center"
android:textSize="20sp"
/>
<ImageView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:src="#drawable/dialog_button_bg"/>
</LinearLayout>
</RelativeLayout>

If your using drawable for imageview as .png or .jpg use the following drawable for your image may be it would help. save this as backgroung_bg.xml or anything else as you wish
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<shape >
<corners android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="0dp"
android:topRightRadius="0dp"/>
<solid android:color="#CF4647"/>
<padding android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp"/>
</shape>
</item>
when you apply this you would get following warning just ignore and run it. if you need gradient type of color ask me
The graphics preview in the layout editor may not be accurate:
Different corner sizes are not supported in Path.addRoundRect. (Ignore for this session)

Related

How To get Ripple effect like play store on tab click with changing color of toolbar and notification bar?

I am new in android.i am trying to achive ripple effect on tab click with color chages of toolbar and notification bar like play store.how to do that?Please Help
If you just want to get ripple effect on devices of Android 5.0,new drawable ripple_bg_drawable:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#d10c1d" <!--ripple color-->
tools:targetApi="LOLLIPOP">
<item>
<shape android:shape="rectangle">
<solid android:color="#8cc476" />
</shape>
</item>
</ripple>
then add android:background="#drawable/ripple_bg_drawable" to your widget
or like this:
android:background="?attr/selectableItemBackgroundBorderless"
you should add this code to your layout file!,but this way you can't customize the ripple color!
but if you want to be compatible with devices below Android 5.0,the only way is you do it yourself by customizing!
Here is a open source project RippleView
There is a library that helps you achieve that:
https://github.com/armcha/PlayTabLayout
<io.armcha.playtablayout.core.PlayTabLayout
android:id="#+id/playTabLayout"
android:layout_width="match_parent"
android:layout_height="some_dp" />
Create res/drawable/ripple_effect.xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#af0c0e"
tools:targetApi="lollipop">
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#af0c0e" />
</shape>
</item>
</ripple>
Then create res/layout/ripple_animation.xml
<?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">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:elevation="6dp">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:gravity="center"
android:padding="10dp"
android:text="Ripple Animation/Effect in Android" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#fff"
android:elevation="6dp">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#drawable/ripple_effect"
android:clickable="true"
android:gravity="center"
android:padding="10dp"
android:text="Ripple Animation/Effect in Android" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#fff"
android:elevation="6dp">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:gravity="center"
android:padding="10dp"
android:text="Ripple Animation/Effect in TextView" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#fff"
android:elevation="6dp">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#drawable/ripple_effect"
android:clickable="true"
android:gravity="center"
android:padding="10dp"
android:text="Ripple Animation/Effect in TextView" />
</FrameLayout>
</LinearLayout>
Try this example. For set ripple effect use android:background="#drawable/ripple_effect".
set below properties to the view on which you need ripple effect.
android:background="?attr/selectableItemBackground"
android:clickable="true"

Border Outside of Layout

I need to set border to linearLayout without modifying it's size, I try setting in the background of linearLayout some drawable with border and padding but this one is set inside of the linearLayout and modifies size, image and textView. Do you have a way of setting border outside of view or superpose views?
My Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/itemCarouselContainer"
android:layout_width="#dimen/carousel_item_width"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="#dimen/carousel_item_margin_right"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/imageCarousel"
android:layout_width="match_parent"
android:layout_height="#dimen/carousel_image_poster_height"
/>
<TextView
android:id="#+id/live"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="#dimen/carouse_item_live_margin_left"
android:layout_marginTop="#dimen/carousel_item_live_margin_top"
android:padding="#dimen/carousel_item_live_padding"
android:textColor="#color/carousel_live_text"
android:textSize="#dimen/carousel_item_live_text_size"
android:textStyle="bold" />
</RelativeLayout>
<TextView
android:id="#+id/titleCarousel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#color/carousel_content_title_background"
android:gravity="center"
android:padding="7dp"
android:singleLine="true"
android:textColor="#color/carousel_content_title_text"
android:textSize="#dimen/carousel_title_size" />
</LinearLayout>
Create this as a drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#color/miniBlack"
android:dashGap="10px"
android:dashWidth="10px"/>
</shape>
Set it as background of your layout giving it a 2dp padding so the stroke won't override the content
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/itemCarouselContainer"
android:layout_width="#dimen/carousel_item_width"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="#dimen/carousel_item_margin_right"
android:orientation="vertical"
android:background="#drawable/myfile"
android:paddings="2dp"
>...

How to separate the footer of a layout with a line in android

I am building a page with a Linearlayout and numerous elements within it. At the bottom of the screen, I want to add a line, separating the "footer". How can this be achieved? I was thinking for something as a TextView, and the line to be the text added, but am sure there is a better way to be done. Thanks!
just add a view with specify background into the xml layout like below:
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#android:color/darker_gray"/>
use this xml for your purpose ...... It just example ....
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5">
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Footer" />
</LinearLayout>
</LinearLayout>
Note:- 2nd Linear layout is For main content .... 3rd Linear layout for footer .....
output of above xml ..
To Seperate Footer from the Container you can add line as a Seperator. But I can give better approach you have to add Shadow to the LinearLayout it will make sense that your Main Container and Footer will be seperate.
My Approach.
footer.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#drawable/linearlayout_upper_shadow"
android:layout_alignParentBottom="true">
<LinearLayout
android:id="#+id/below_linear_layout"
android:layout_width="0.0dp"
android:layout_height="match_parent"
android:layout_weight="50.0"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="0.0dp"
android:layout_marginLeft="#dimen/normal_margin15"
android:layout_marginTop="#dimen/normal_margin5"
android:layout_weight="0.50"
android:text="$1,605"
android:textSize="#dimen/textsize20"
android:textColor="#color/black1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="0.0dp"
android:layout_marginLeft="#dimen/normal_margin15"
android:layout_weight="0.50"
android:text="View price detail"
android:textColor="#color/colorPrimary"
android:textSize="#dimen/textsize13"
android:layout_marginTop="-5dp" />
</LinearLayout>
<Button
android:id="#+id/continue_button"
android:layout_width="0.0dip"
android:layout_height="match_parent"
android:layout_margin="#dimen/normal_margin8"
android:layout_weight="50.0"
android:background="#color/colorPrimary"
android:gravity="center"
android:text="#string/continue_string"
android:textColor="#color/white1"
android:textSize="#dimen/textsize13" />
</LinearLayout>
for separator.
separator.xml
<?xml version="1.0" encoding="utf-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#color/linear_layout_shadow"/>
/>
</shape>
</item>
<item
android:left="0dp"
android:right="0dp"
android:top="1dp"
android:bottom="0dp">
<shape android:shape="rectangle">
<solid android:color="#android:color/white"/>
<corners android:radius="2dp" />
</shape>
</item>
</layer-list>
OutPut :

Styling Linear Layout with multi edit text

I am trying to make a LinearLayout with several EditText for inputting fields like this:
I checked several answers here to make a custom drawable for the EditText but how can I make it for the whole linear layout?
You can't do this with a single LinearLayout.
You need minimum 2 LinearLayouts with horizontal orientation, and one LinearLayout with vertial orientation, that contains those two.
And you can set the borders for your EditTexts with shape Drawables.
See examples here: How to draw rounded rectangle in Android UI?
Try below code ,You can also inclue textview inside it,
<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.55"
android:background="#drawable/selector"/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.15"
android:background="#drawable/selector"/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.30"
android:background="#drawable/selector"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:background="#drawable/selector"/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:background="#drawable/selector"/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.60"
android:background="#drawable/selector" />
</LinearLayout>
</LinearLayout>
where selector drawable is xml file inside xml folder
selector.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#android:color/white" />
<stroke
android:width="1dp"
android:color="#android:color/black"
/>
<padding
android:left="5dp"
android:right="5dp"
android:top="5dp"
android:bottom="5dp" />
</shape>

How to draw two horizontal lines for the layout(top and bottom) in android

I need to draw two horizontal lines to the layout.One line want to be at the top and the second one should want to be at the bottom.
How to achieve this?
Thanks for you precious time..
I think, it will help you.
<?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:background="#android:color/white" >
<View
android:layout_width="match_parent"
android:layout_alignParentTop="true"
android:layout_height="2dp"
android:background="#android:color/black" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_alignParentBottom="true"
android:background="#android:color/black" />
</RelativeLayout
use this code
<View
android:id="#+id/line_top"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#FF0000"
android:layout_alignParentTop="true"/>
<View
android:id="#+id/line_bottom"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#FF0000"
android:layout_alignParentBottom="true"/>
Method1
Use a relative layout. You can define other ui elements also. Have View with a specified height and place it at the top and the bottom.
<?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:orientation="vertical"
tools:context=".MainActivity" >
<View
android:layout_width="fill_parent"
android:layout_height="20dp" // specify a number in dp to increase or decrease height
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#FF2824" change do your desired color
android:orientation="vertical"
/>
<View
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#FF2824"//change do your desired color
android:orientation="vertical"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout1"
android:layout_marginLeft="102dp"
android:layout_marginTop="106dp"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button1"
android:layout_marginTop="66dp"
android:text="Button" />
</RelativeLayout>
Resulting snap shot.
Method 2
Here you are not using any new views. Just add a custom background to the existing layout. This will occupy less memory than the above coz you are not creating any new views.
<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:orientation="vertical"
android:background="#drawable/bkg" //add custom background
tools:context=".MainActivity" >
create a drawable folder under resources and define bkg.xml under it.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FF0000" />
</shape>
</item>
<item android:top="20dp" android:bottom="20dp" >
<shape android:shape="rectangle">
<solid android:color="#ffffff" />
</shape>
</item>
</layer-list>

Categories

Resources