user7652543
user7652543

Reputation:

Java android Linearlayout set two colors

Hi I try do this I have a LinearLayout and how I can set two colors background inside a layout. I want to that half is a white and half is a black.

I tried this :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <layer-list>
            <item>
                <shape>
                    <corners android:radius="5dip"/>
                    <gradient
                        android:endColor="#fff"
                        android:startColor="#000" />
                </shape>
            </item>
        </layer-list>
    </item>

</selector> 

This is my code from xml ;

<FrameLayout 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:id="@+id/frame"
    android:background="@color/background"
    tools:context="pl.eltegps.smokkomunikator.ui.fragment.TopBarFragment">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/lin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:id="@+id/rel11"
                android:layout_weight="0.5">

                <ImageView
                    android:id="@+id/iv"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginStart="10dp"
                    android:src="@drawable/profil" />

                <TextView
                    android:id="@+id/user"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/iv"
                    android:layout_marginLeft="10dp"
                    android:layout_marginStart="10dp"
                    android:clickable="true"
                    android:text="aaaaaaa"
                    android:textColor="@color/textColor" />

            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/settings"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5">

                <TextView
                    android:id="@+id/currentLocation"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:gravity="left"
                    android:textColor="#fff" />

                <ImageView
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:antialias="true"
                    android:background="@drawable/menu"
                    android:layout_centerVertical="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true"
                    android:layout_marginRight="11dp"
                    android:layout_marginEnd="11dp" />

            </RelativeLayout>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/lin"
            >

            <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <View
                    android:id="@+id/view"
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_gravity="center"
                    android:background="@color/textColor" />

                <LinearLayout
                    android:id="@+id/a"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginBottom="2sp"
                    android:layout_marginTop="2dp"
                    android:background="@drawable/dummy"
                    android:orientation="horizontal"
                    android:padding="5dp">

                    <RelativeLayout
                        android:id="@+id/serverStatusWrapper"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="2dp"
                        android:background="@drawable/shadow_green"
                        android:padding="2dp">

                        <ImageView
                            android:id="@+id/serverStatus"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerInParent="true"
                            android:antialias="true"
                            android:src="@drawable/ic_settings_input_antenna_white_24dp" />

                    </RelativeLayout>

                    <RelativeLayout
                        android:id="@+id/locationStatusWrapper"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@drawable/shadow_red"
                        android:padding="2dp">

                        <ImageView
                            android:id="@+id/locationStatus"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerInParent="true"
                            android:antialias="true"
                            android:src="@drawable/ic_warning_white_24dp" />
                    </RelativeLayout>

                </LinearLayout>

            </FrameLayout>

        </LinearLayout>

    </RelativeLayout>

</FrameLayout>

dummy.xml :

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffffffff" />

    <stroke
        android:width="1dp"
        android:color="#ff000000" />

    <padding
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp" />

    <corners
        android:bottomLeftRadius="7dp"
        android:bottomRightRadius="7dp"
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp" />
</shape>

And I try do this : what is under the view I want to setbackground color black

Upvotes: 1

Views: 1773

Answers (3)

Vishal Senjaliya
Vishal Senjaliya

Reputation: 456

Follow this steps :

Step 1: Create one background.xml file and put that file in drawable folder.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:bottom="20dp">
    <shape android:shape="rectangle" >
        <size android:height="100dp" />
        <solid android:color="#ffffff" />
    </shape>
</item>

<item android:top="300dp">
    <shape android:shape="rectangle" >
        <size android:height="10dp" />
        <solid android:color="#000000" />
    </shape>
</item>

Step 2 : In your Main.xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keypad"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@drawable/background">

Output :

enter image description here

Upvotes: 0

PEHLAJ
PEHLAJ

Reputation: 10126

Try

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="#ffffff" />
        </shape>
    </item>

    <item>
        <shape android:shape="rectangle" >
            <solid android:color="#000000" />
        </shape>
    </item>
</layer-list>

Upvotes: 1

Majid Ali
Majid Ali

Reputation: 495

Each ViewGroup can only have one background color, but you can easily achieve what you want by creating two ViewGroup elements.

Like.

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<FrameLayout
   android:layout_width="0dp"
   android:layout_height="match_parent"
   android:layout_weight="1"

   android:background="#ff0000"/>

<FrameLayout
   android:layout_width="0dp"
   android:layout_height="match_parent"
   android:layout_weight="1"

   android:background="#00ff00"/>

Upvotes: 1

Related Questions