Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create border at bottom only?

Tags:

android

button

I am trying create one app and in my app I am trying to create border at bottom in button,but when I run application it shows border at top only,can anybody tell how to achieve this..following is my xml of UI?.........

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
 android:orientation="horizontal"
tools:context="com.example.button_pressed_effect_example.MainActivity" >

 <Button 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="One"
    android:id="@+id/button1"
   android:background="@drawable/custom_btn_black_orange"
   android:drawableBottom="@drawable/liti"

    />
<Button 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="Two"
    android:id="@+id/button2"
   android:background="@drawable/custom_btn_black_orange"
    />

 </LinearLayout>

liti.xml

      <layer-list /android">
         <item   android:left="10dp" android:bottom="10dp">
         <shape android:shape="line">

        <stroke android:width="10dp" android:color="#ffffff" />
        </shape>
        </item>
        </layer-list>

custom_btn_black_orange.xml

 <selector >

  <item android:state_pressed="true" >
     <shape android:shape="rectangle"  >

         <gradient
             android:angle="-90"  
             android:startColor="#5B0B0E"
             android:endColor="#5B0B0E"  />           
     </shape>
 </item>
<item android:state_pressed="false">
     <shape android:shape="rectangle"  >

         <solid  android:color="#731013"/>      
     </shape>
 </item> 
<item >
    <shape android:shape="rectangle"  >
         <gradient
             android:angle="-90"  
             android:startColor="#5B0B0E"
             android:endColor="#5B0B0E" />           
     </shape>
 </item>
 </selector>

custom_btn_black_orangepressed.xml

    <selector>

   <item android:state_pressed="false" >
     <shape android:shape="rectangle"  >

         <gradient
             android:angle="-90"  
             android:startColor="#5B0B0E"
             android:endColor="#5B0B0E"  />           
     </shape>

 </item>
<item android:state_pressed="false">
     <shape android:shape="rectangle"  >

         <solid  android:color="#731013"/>      
     </shape>
 </item> 
<item >
    <shape android:shape="rectangle"  >

         <gradient
             android:angle="-90"  
             android:startColor="#5B0B0E"
             android:endColor="#5B0B0E" />           
     </shape>
 </item>
 </selector>

Myjava.java

    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
               btn.setBackgroundResource(R.drawable.custom_btn_black_orangepressed);
            btn2.setBackgroundResource(R.drawable.custom_btn_black_orange);
        }
    });

    btn2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            btn2.setBackgroundResource(R.drawable.custom_btn_black_orangepressed);
            btn.setBackgroundResource(R.drawable.custom_btn_black_orange);
        }
    });
like image 829
parajs dfsb Avatar asked Dec 25 '22 23:12

parajs dfsb


2 Answers

Your problem is different or you are unable to explain it, however I got it

  1. Crete two files in drawable, bottom_selected and bottom_unselected

Bottom_selected.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="#001EFF" />
        </shape>
    </item>
    <item android:bottom="3dp">
        <shape android:shape="rectangle" >
            <solid android:color="#FFFFFF" />
        </shape>
    </item>

</layer-list>

Bottom_unselected.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="#001EFF" />
        </shape>
    </item>
    <item android:bottom="1dp">
        <shape android:shape="rectangle" >
            <solid android:color="#FFFFFF" />
        </shape>
    </item>

</layer-list>

Now your Buttons

 <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/fragment_container"
        android:layout_marginLeft="54dp"
        android:layout_marginTop="110dp"
        android:background="@drawable/bottom_selected"
        android:text="Button" />

    <Button
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_toRightOf="@+id/button1"
        android:background="@drawable/bottom_unselected"
        android:text="Button" />

    <Button
        android:id="@+id/Button02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/Button01"
        android:layout_alignBottom="@+id/Button01"
        android:layout_toRightOf="@+id/Button01"
        android:background="@drawable/bottom_unselected"
        android:text="Button" />

Final View

enter image description here

like image 80
Murtaza Khursheed Hussain Avatar answered Jan 05 '23 05:01

Murtaza Khursheed Hussain


Code -

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--Minus (-) how much dp you gave in the stroke width from left right top-->
    <item android:left="-10dp" android:right="-10dp" android:top="-10dp">
        <shape
            android:shape="rectangle">
            <stroke
                android:width="10dp"
                android:color="@android:color/holo_red_dark" />
            <!--This is the main background -->
            <solid android:color="#FFDDDDDD" />
        </shape>
    </item>
</layer-list>

Preview -

enter image description here

like image 45
Gk Mohammad Emon Avatar answered Jan 05 '23 06:01

Gk Mohammad Emon