Wednesday 8 April 2015

Gradient_ button _effect_ in_ Android_using_xml

(1)MainActivity.java  save this file under  src   folder:

package com.example.itexamnotes;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;



public class MainActivity extends Activity {


    private Button button;



    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        addListenerOnButton();



    }



    public void addListenerOnButton() {



        //Select a specific button to bundle it with the action you want

        button = (Button) findViewById(R.id.button1);



        button.setOnClickListener(new OnClickListener() {



            @Override

            public void onClick(View view) {



              Intent openBrowser =  new Intent(Intent.ACTION_VIEW, Uri.parse("http://test.itexamtime.com"));

              startActivity(openBrowser);

            }



        });



    }



}


(2)write code for activity_main.xml  save under folders path "res/layout" :


<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.itexamnotes.MainActivity" >

   
         
       
       
       

<Button android:text="BSCIT NOTES" android:id="@+id/button1" android:background="@drawable/btn_red" style="@style/ButtonText">

</Button>
       
       


</RelativeLayout>

(3)write  code for  btn_red.xml given below  & create a new folder "drawable" inside res folder:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
        <solid
        android:color="#ef4444" />
            <stroke
                android:width="1dp"
                android:color="#992f2f" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#ef4444"
                android:endColor="#992f2f"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#992f2f" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>


(4)write code for  String.xml file which is under folders path "res/value":

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">ITEXAMTIME</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>


<style name="ButtonText">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textColor">#ffffff</item>
    <item name="android:gravity">center</item>
    <item name="android:layout_margin">3dp</item>
    <item name="android:textSize">30dp</item>
    <item name="android:textStyle">bold</item>
    <item name="android:shadowColor">#000000</item>
    <item name="android:shadowDx">1</item>
    <item name="android:shadowDy">1</item>
    <item name="android:shadowRadius">2</item>
</style>

</resources>



Output:


No comments:

Post a Comment