Place 3 circles in a LinearLayout to occupy equal amount of space - android

I want to have 3 circular buttons horizontally with equal margins. I tried setting programmatically but I get NPE since view was not drawn.
How can I do it in layout file? I tried below but is not circle.
Place 3 buttons in a LinearLayout to occupy equal amount of space
Please help.

Use like this
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</FrameLayout>
</LinearLayout>
Apply style to your buttons

You can try this.
circle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners android:radius="10dip"/>
<stroke android:color="#FF0000" android:width="5dip"/>
<solid android:color="#FF0000"/>
</shape>
Put this inside the drawable folder and set this as background of your buttons (added like in the accepted answer of the link).
Hope this helps

Related

not full height vertical divider android

I already have a vertical divider between my two buttons like this :
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:divider="?android:dividerVertical"
android:showDividers="middle"
>
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/flat_selector"
android:textColor="#android:color/white"
android:text="Connexion"
android:textStyle="bold"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/flat_selector"
android:textColor="#android:color/white"
android:text="Inscription"
android:textStyle="bold"
android:layout_weight="1"/>
</LinearLayout>
but I'd like to have one which doesn't take all the available height, but just half for example, starting at the middle of the layout.
I already tried to create a custom divider :
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<size android:width="0.5dp"/>
<solid android:color="#ffffff"/>
</shape>
and then to play with android:height, but it does nothing good. Any other ideas ?

Drawable as Background for CardView

the CardView ( android.support.v7.cardview ) stays white even though I set a backround drawable via android:backround - The documentation gives me the feeling that it should work. No Idea what I am doing wrong here.
I know this is an old question, but I have a simple solution - just make the first child of your CardView an ImageView and specify the scale type to fitXY. You can get rid of the extra CardView padding by setting cardElevation and cardMaxElevation to 0dp:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
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="200dp"
app:cardCornerRadius="4dp"
app:cardElevation="0dp"
app:cardMaxElevation="0dp">
<ImageView
android:src="#drawable/your_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"/>
<... your layout
.../>
</android.support.v7.widget.CardView>
for drawable or color just use:
cvSellerShopp.setBackgroundResource(R.drawable.ic_action_add_image);
for color use:
cvSellerShopp.setCardBackgroundColor(R.color.colorPrimary);
but this one does not produce the intended results
Make Cardview will host one viewgroup for eg relative layout in this case and then simply set any background to relative layout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/list_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
card_view:cardCornerRadius="4dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:id="#+id/list_container_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
<!--Add cardview contents-->
</RelativeLayout>
</android.support.v7.widget.CardView>
I got this working by adding a linearlayout in the cardview and then setting cardPreventCornerOverlap to false in the cardview.
<android.support.v7.widget.CardView
android:id="#+id/result_cv"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_margin="4dp"
app:cardCornerRadius="16dp"
app:cardElevation="8dp"
app:cardPreventCornerOverlap="false"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient"
android:gravity="center"
>
<!-- your views -->
</LinearLayout>
</android.support.v7.widget.CardView>
The command I used was:
cardView.setBackgroundResource(R.drawable.card_view_bg);
where card_view_bg is a custom XML resource file.
If you have some layouts inside the card view and these layouts are overlapping with the margins of the card view then you might need a separate custom background resource file for the layouts like the one I used for the card view background itself.
Try this
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/list_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
card_view:cardCornerRadius="4dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:id="#+id/list_container_bg"
android:layout_width="match_parent"
android:background="#drawable/yourbackground"
android:layout_height="match_parent">
<!--Add cardview contents-->
</RelativeLayout>
</android.support.v7.widget.CardView>
card_view:cardBackgroundColor="#android:color/holo_green_dark
here you can set any color or make it transparent by giving color transparent
and if u wand some drawable like image or svg put RelativeLayout background
You can use LinearLayout or Relative layout inside CardView and set drawable background it like below :
<android.support.v7.widget.CardView
android:id="#+id/card6"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_marginTop="10dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/card4"
app:cardCornerRadius="10dp"
app:cardElevation="4dp"
app:cardMaxElevation="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/card_bg"
android:padding="10dp">
<ImageView
android:id="#+id/card6Image"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center_horizontal"
android:src="#mipmap/ic_launcher_round" />
<TextView
android:id="#+id/card6Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="5dp"
android:text="Daily Check In"
android:textColor="#ffffff"
android:textSize="15sp" />
<TextView
android:id="#+id/card6Description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="Just Check in Daily and Earn Credits"
android:textColor="#ffffff"
android:textSize="10sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
Below is my Drawable resource file :
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="90"
android:centerColor="#555994"
android:endColor="#b5b6d2"
android:startColor="#555994"
android:type="linear" />
<corners
android:radius="0dp"/>
you can set foreground as follow for the card view to set custom bg
android:foreground="#drawable/bg"
and here is the transparent bg.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#android:color/transparent" />
<stroke
android:width="1dp"
android:color="#d9d9d9" />
<corners android:radius="4dp" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
</shape>
You have 2 possible solutions.
Setting background programmatically:
cardView.setBackgroundResource(R.drawable.background)
Add some child to cardview that will fill cardview's bounds and set background on it:
<android.support.v7.widget.CardView
...>
<ConstraintLayout
android:background="#drawable/background"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.v7.widget.CardView>
This way you can also give images some shape that you can configure by the shape of cardview. Disadvantage is that it is bad for performance.
Try this code:
cardView.setForeground(getResources().getDrawable(R.drawable.your_drawable));
sCard.setBackgroundResource(R.drawable.scard_background);
if you want change background color on cardView just add this code on your cardview XML
app:cardBackgroundColor="#color/whatever_color_you"
You can simply use the below line in your xml file.
app:cardBackgroundColor="#drawable/your_custom_theme_here"

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>

Android List Item: TextView on left, Image on right

I'm creating a custom list item in and android list view. it's very simple, it has a textview on the left and an image on the right. the image should align all the way right, and the text view should take up all the space on the left. problem is, i can get the image to show up, but it pushes left. if i set the textview width to fill_parent, the image disappears. here's my layout:
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="50px" android:layout_gravity="center_vertical">
<!-- item -->
<TextView android:id="#+id/txtItem" android:gravity="center_vertical" android:textSize="20px" android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<!-- disclosure image -->
<ImageView android:layout_gravity="right|center_vertical" android:src="#drawable/common_icon_arrow_disclosure"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>
i've also tried a relative layout, but the same thing happens. what am i doing wrong?
EDIT: for clarification, this is what i'm trying to accomplish:
i want the text field to take up the whole left area, and the image to be on the right, unscaled, vertically centered.
You can also just add the drawable to the textview directly like this.
<TextView
android:id="#+id/txtItem"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawableRight="#drawable/common_icon_arrow_disclosure"
android:gravity="center_vertical"
android:textSize="20px" />
You can use bitmap drawable like this (this also adds rounded corners):
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#android:color/white" />
<stroke
android:width="2dp"
android:color="#adadad"
/>
<corners android:radius="5dp" />
</shape>
</item>
<item android:right="5dp"
android:top="2dp"
android:bottom="2dp">
<bitmap
android:src="#drawable/image"
android:gravity="right"
/>
</item>
</layer-list>
and put this drawable in background property of your TextView:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/right_image"
android:text="some text"
/>
Left-align your text with the parent, and set the gravity of the image to right:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="48dp">
<TextView
android:id="#+id/team_member_name"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:singleLine="true"/>
<ImageView
android:id="#+id/team_member_icon"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="right"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:src="#drawable/circle_green"/>
</RelativeLayout>
Relative layout is what you want. In your case the fill_parent attribute was causing the text view to push the image off of the screen. If you use Relative layout and set the text view to use the layout_toLeftOf it should work.
Here is a quick example that I think will work (I haven't tested it out and it's from memory but the idea is there)...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50px"
android:layout_gravity="center_vertical">
<ImageView android:id="#+id/img"
android:layout_gravity="right|center_vertical"
android:src="#drawable/common_icon_arrow_disclosure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/txtItem"
android:gravity="center_vertical"
android:textSize="20px"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_toLeftOf="#id/img" />
</RelativeLayout>
This worked for me:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:padding="10dp"
android:layout_marginRight="10dp"
android:textSize="16sp"
android:textColor="#color/text_color"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"/>
<ImageView android:id="#+id/imageBtn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:padding="10dp"
android:contentDescription="#string/delete"
android:src="#drawable/delete_minus_png"
android:layout_alignParentRight="true" />
</RelativeLayout>
Line android:layout_marginRight="10dp" did the trick.
Hope it helps!
Looks like there is some layout bug or so. If the TextView has fill_parent flag, the ImageView will be pushed away from the layout, even if the TextView contents are small enough.

How to get rid of the extra gap between a button and other views?

When I created a button view, Android always create some extra space between this button and other views below it.
In this example below, there is a button above the second button. You can see the gap between these two buttons. How can I get rid of this gap? Thanks.
<?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="wrap_content">
<Button android:id="#+id/up"
android:layout_width="fill_parent" android:layout_height="28dip"
android:textSize="9sp" android:gravity="center" android:text="Hide sth"
android:layout_marginBottom="0" android:paddingBottom="0" />
<Button android:id="#+id/down"
android:layout_width="fill_parent" android:layout_height="28dip"
android:textSize="9sp" android:gravity="center" android:text="Show sth" />
</LinearLayout>
Create your own button background nine-patch PNG that eliminates the space. You can find the existing background files in your SDK.
CommonsWare pretty much pointed you in the right direction but here's a sample layout example. Just replace the android:background="COLOR" to a reference of a 9 patch png
<?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="wrap_content">
<Button
android:id="#+id/btn1"
android:text="btn1"
android:padding="0dip"
android:layout_margin="0dip"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:background="#FFF"
/>
<Button
android:id="#+id/btn2"
android:text="btn2"
android:padding="0dip"
android:layout_margin="0dip"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:background="#FF0000"
/>
</LinearLayout>
Hope that helps!
I just figured out the most simple way. We have to simply add background to Buttons and it will automatically reduce spaces.
Notice example
<Button
android:id="#+id/button1"
android:text="Button 1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#000000" />
<Button
android:id="#+id/button2"
android:text="Button 2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#000000" />
Obviously we would have to split Buttons with border line. There are few ways for that.
1. android:layout_marginTop="1dp" to second Button. The small piece of background will separate it.
2. Add style for Button :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#FFFFFF"
android:endColor="#000000"
android:angle="270" />
<corners android:radius="3dp" />
<stroke android:width="1px" android:color="#FFFFFF" />
</shape>
and set it with :
android:background="#drawable/your_button_border"
3. To add View with background of a different color and place it between Buttons.
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#AAA" />
Just pick the most convenient for you way.

Categories

Resources