I'm placing a textview over a line as textview have light color background the line behind the textview is visible, I want to hide that portion of line thats behind textview, how can i do that
Thanks in advance!
Like this
Try this
<?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:layout_marginTop="20dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_centerInParent="true"
android:background="#color/colorAccent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#color/colorPrimary"
android:gravity="center"
android:text="Nilesh" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:gravity="center"
android:orientation="horizontal">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_centerInParent="true"
android:layout_weight="1"
android:background="#color/colorAccent" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:background="#FFFFFF"
android:text="NILu" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_centerInParent="true"
android:layout_weight="1"
android:background="#color/colorAccent" />
</LinearLayout>
</LinearLayout>
OUTPUT
You can do it in multiple ways using a RelativeLayout.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<View
android:layout_width="match_parent"
android:layout_centerVertical="true"
android:background="#000000"
android:layout_height="2dp"/>
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:background="#FFFFFF"
android:paddingLeft="10dp"
android:paddingRight="10sp"
android:layout_centerInParent="true"
android:text="Some Demo text"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</RelativeLayout>
Other way can be as .
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:layout_width="match_parent"
android:layout_centerVertical="true"
android:background="#000000"
android:layout_toLeftOf="#+id/txt"
android:layout_height="2dp"/>
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10sp"
android:layout_centerInParent="true"
android:text="Some Demo text"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<View
android:layout_width="match_parent"
android:layout_centerVertical="true"
android:background="#000000"
android:layout_toRightOf="#+id/txt"
android:layout_height="2dp"/>
</RelativeLayout>
I think there are two options:
You use three views side-by-side to have line - text - line
You use two views in a FrameLayout: The first one will be the view for the line, and the second one the TextView, which will be drawn above the line. Make sure that the TextView has a non-transparent background and its layout_width is wrap_content. Depending on your desired result you might want to add some horizontal padding to the TextView, so that the line doesn't directly touch the text.
You can either try it like following code
shape_line.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line"
>
<solid android:color="#FFFFFF">
</solid>
<stroke
android:width="1dp"
android:color="#000000" />
<size android:height="2dp" />
</shape>
Add this to your drawable folder.
and in your layout add following layout
<?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="120dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/shape_bg_line"
tools:ignore="MissingConstraints">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#FFFFFF"
android:padding="5dp"
android:text="Hello" />
</RelativeLayout>
Here is the output :
Related
why my last row is hidden in listview?
Hello, why my last row is hidden in listview?
Hello, why my last row is hidden in listview?
I have changed the xml code 1000 times already and still can't fix it.
SwipeRefreshLayout work good.
enter image description here
fragment_list.xml
`
<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp">
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#android:color/transparent"
android:listSelector="#android:color/transparent"
android:divider="#null"
android:dividerHeight="0dp">
</ListView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>
<com.google.android.material.textview.MaterialTextView
android:id="#+id/empty"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="#string/empty_list_text" />
</RelativeLayout>
`
list_row.xml
`
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.material.card.MaterialCardView
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:rippleColor="#android:color/transparent"
card_view:cardElevation="0dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:strokeWidth="0dp"
app:cardElevation="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.material.imageview.ShapeableImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="194dp"
app:srcCompat="#drawable/image_list"
app:shapeAppearanceOverlay="#style/ShapeAppearanceOverlay.App.SelectedCornerRadius"
android:scaleType="centerCrop" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="16dp">
<com.google.android.material.textview.MaterialTextView
android:id="#+id/city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?attr/textAppearanceHeadline6" />
<com.google.android.material.textview.MaterialTextView
android:id="#+id/region"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textAppearance="?attr/textAppearanceBody2" />
<com.google.android.material.textview.MaterialTextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="2"
android:layout_marginTop="12dp"
android:textAppearance="?attr/textAppearanceBody2" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:layout_gravity="center"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
android:id="#+id/btn_check_region"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:text="#string/check_list" />
<com.google.android.material.button.MaterialButton
android:id="#+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/check_delete" />
</LinearLayout>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</RelativeLayout>
`
I have no idea how to fix it -.-
The ListView's height is MATCH_PARENT which constrains it to the height of its parent ViewGroup. You should always define the height of scrollable views like ListView as WRAP_CONTENT so that it can freely expand according to the number of children.
Also it might be a good idea for the parent LinearLayout to be anchored to the top of the RelativeLayout:
android:layout_alignParentTop="true".
Leaving views floating freely in RelativeLayouts may have surprising consequences.
I am using RecyclerView to display quote list Fragment. I am trying to use Ripple effect in list item but its not working. My RecyclerView is like below
<android.support.v7.widget.RecyclerView
android:id="#+id/listQuoteView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/BackColor"
android:layout_above="#+id/startAppBanner"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
And List Item XML is like below
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="4dp"
app:cardCornerRadius="6dp"
app:cardElevation="4dp">
<LinearLayout
android:id="#+id/quoteActionView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/TextColor"
android:clickable="true"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageAuthorView"
android:layout_width="#dimen/imageAuthorView"
android:layout_height="#dimen/imageAuthorView"
android:visibility="visible" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/textQuote"
android:ellipsize="end"
android:layout_width="match_parent"
android:layout_height="0dp"
android:paddingRight="#dimen/list_text_margin"
android:paddingLeft="#dimen/list_text_margin"
android:gravity="center_vertical"
android:layout_weight="2"
android:maxLines="2"
android:layout_margin="2dp"
android:lineSpacingExtra="#dimen/linespace"
android:textColor="?attr/MainTextColor"
android:text="Hello there two line text for show here which I am typing for a test"
android:scrollHorizontally="true"
android:textSize="#dimen/textDetailQuote"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="?attr/dark_color"
android:orientation="horizontal">
<ImageView
android:layout_width="#dimen/likeIcon"
android:layout_height="#dimen/likeIcon"
android:layout_weight="1"
android:tint="?attr/List_Text"
android:src="#drawable/time_new_ic" />
<TextView
android:id="#+id/lblTimeCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="00:00"
android:textStyle="bold"
android:textColor="?attr/List_Text"
android:textSize="#dimen/lblTime" />
<ImageView
android:layout_width="#dimen/likeIcon"
android:layout_height="#dimen/likeIcon"
android:layout_weight="1"
android:tint="?attr/List_Text"
android:src="#drawable/fav_new_ic" />
<TextView
android:id="#+id/lblLikeCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="200"
android:textStyle="bold"
android:textColor="?attr/List_Text"
android:textSize="#dimen/lblTime" />
<ImageView
android:layout_width="#dimen/likeIcon"
android:layout_height="#dimen/likeIcon"
android:layout_weight="1"
android:tint="?attr/List_Text"
android:src="#drawable/share_new_ic" />
<TextView
android:id="#+id/lblShareCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textStyle="bold"
android:text="100"
android:textColor="?attr/List_Text"
android:textSize="#dimen/lblTime" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
I have tried many solutions listed in stackoverflow but not worked any. If I remove CardView from List Item.... Ripple Effect shows itself behind LinearLayout but I want to use CardView also. Let me know if someone can help me to solve this issue. Thanks.
Note : I have solved it using this solution. Thanks
Set the ripple as a foreground drawable on the CardView:
android:foreground="?attr/selectableItemBackground"
This will allow the ripple to show up above the content inside the CardView, which is usually what you want for a CardView
Create ripple drawable and set it as foreground of your layout
ripple.xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#color/your_color"
tools:targetApi="lollipop">
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#color/your_color" />
</shape>
</item>
</ripple>
Change your code like this:
<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="wrap_content">
<android.support.v7.widget.CardView
android:clickable="true"
android:foreground="#drawable/ripple"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="4dp"
app:cardCornerRadius="6dp"
app:cardElevation="4dp">
Use android:background="?android:attr/selectableItemBackground"
I created linearlayout with two buttons in center (buttons 1 and 2), but I can't manage it to create the view 3 on top, which I need to make it fill whole width and adjust it's height by it (keep proportions). How do I create third view?
Image:
Code:
<?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="#drawable/bg"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content" >
<ImageButton
android:background="#drawable/btn_one"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1.0" />
<ImageButton
android:background="#drawable/btn_second"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1.0" />
</LinearLayout>
I would recommend you use relative layouts, they are the closest thing in android to an "absolute layout" replace the button tags with Image button tags
<?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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</RelativeLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="215dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button3"
android:layout_below="#+id/button2"
android:layout_alignStart="#+id/button2" />
</RelativeLayout>
try this code set height and width what size of image button you need and simple look what you need.
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:orientation="vertical"
android:gravity="center">
<ImageButton
android:layout_width="200dp"
android:layout_height="50dp"/>
<ImageButton
android:layout_width="200dp"
android:layout_height="50dp" />
</LinearLayout>
I'm trying to space out my image buttons in my table row (so it doesn't touch each other. It doesn't seem to space out. Can anyone tell me how to? Thanks for any responses.
Heres a screenshot:
My activity_main xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff000000"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/adViewFunny">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/iLikeTurtlesButton"
android:clickable="true"
android:src="#drawable/bluesound"
android:background="#drawable/transparent"
android:onClick="PlayILikeTurtles"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_column="0" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton12"
android:clickable="true"
android:src="#drawable/bluesound"
android:background="#drawable/transparent"
android:onClick="PlayILikeTurtles"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_column="14" />
</TableRow>
</TableLayout>
</ScrollView>
<com.google.android.gms.ads.AdView android:id="#+id/adViewFunny"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adUnitId="XXXX"
ads:adSize="BANNER"/>
To center the children of the TableRow, add android:gravity="center" to the TableRow.
To add some space between the children you can use a divider like this:
Create a drawable drawable/horizontal_spacer.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- define how much spacing you want -->
<size android:width="10dp"/>
<solid android:color="#android:color/transparent"/>
</shape>
Then use android:divider and android:showDividers on your TableRow:
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:divider="#drawable/horizontal_spacer"
android:showDividers="middle">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/iLikeTurtlesButton"
... />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton12"
... />
</TableRow>
I've got a few linear layouts stacked on top of each other, the centre one has an imageview. I'm trying to get a textview on top of the imageview in the centre however I'm having trouble. I've tried with relative layout however it messes up all my other layouts. Any simple solutions to this?
EDIT ----
<?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:background="#000000"
android:orientation="horizontal"
android:paddingLeft="50dp"
android:paddingRight="50dp" >
<ImageButton
android:id="#+id/btnBck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#null"
android:src="#drawable/ic_launcher" />
<ImageButton
android:id="#+id/btnHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#null"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="6"
android:orientation="vertical" >
<ImageView
android:id="#+id/picPreview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:contentDescription="#string/imageDesc"
android:src="#null" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="horizontal"
android:paddingLeft="50dp"
android:paddingRight="50dp" >
<ImageButton
android:id="#+id/btnAccept"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#null"
android:src="#drawable/ic_launcher" />
<ImageButton
android:id="#+id/btnDecline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#null"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
I'm trying to get the text view on top of the id/picPreview image view
LinearLayout is designed to place layouts/views in a row (linearly).
One solution would be something like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="6"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/picPreview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:contentDescription="#string/imageDesc"
android:layout_centerInParent="true"
android:src="#null" />
<TextView
android:id="#+id/picPreviewLbl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:text="Overlay" />
</RelativeLayout>
</LinearLayout>
Why not just use a TextView in place of the centered ImageView and then set the background of the text be the solid color that you are currently using? Eg.
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:text="Sample Text" ... />