Wednesday 17 August 2016

Button



A button consists of text or an icon (or both text and an icon) that communicates what action occurs when the user touches it.
Depending on whether you want a button with text, an icon, or both, you can create the button in your layout in three ways:
·         With text, using the Button class:
<Button
   
android:layout_width="wrap_content"
   
android:layout_height="wrap_content"
   
android:text="@string/button_text"
    ...
/>
·         With text and an icon, using the Button class with the android:drawableLeft attribute:
<Button
   
android:layout_width="wrap_content"
   
android:layout_height="wrap_content"
   
android:text="@string/button_text"
   
android:drawableLeft="@drawable/button_icon"
    ...
/>


Text
Set the text to textview
Code - android:text="your text here"

Text Size
Specify the size of the size.
Size of text like - 16dp, 20sp, 2em ( 1em= 16dp )
code  - android:textSize="16dp"

Text Alignment
Set Text Left, Right, Centre and more in EditText
code - android:textAlignment="center"

Text  All Caps
Make all text Capital in Textview
must be a boolean ( true/False )
code -  android:textAllCaps="true"

Text Color
Set the color of the text.
Color should be Hexadecimal (#000000 - #ffffff), octal  [Range 0-255] ( rgb(212,212,212,0.5 )
code - android:textColor="#12E9fa"
 
 
borderLessButton
To create a borderless button, apply the borderlessButtonStyle style to the button. For example:
style="?android:attr/borderlessButtonStyle"
 
Background
Set the background color as a image, color.
code - android:background="@drawable/abc"

Text Style
Style (bold , italic, bolditalic) for the text.
Must be one or more (separated by '|') of the following constant values.
Value -
Normal             -           0
Bold                 -           1
Italic                 -           2
code - android:textStyle="bold"

Typeface
Typeface (normal, sans, serif, monospace) for the text.
Must be one of the following constant values.
Value -
Normal             -           0
Sans                -           1
Serif                 -           2
Monospace      -           3
code  - android:typeface="monospace"

Text Appearance
Default appearance of text: color, typeface, size, and style.
code - android:textAppearance="@style/TextAppearance.AppCompat.Large"
 
Some Attribute -
textAppearanceLarge :  style for "large" text. Defaults to primary text color.
Text Color Highlight
Color of the text selection highlight.
code - android:textColorHighlight="#12e7ff"

Text Direction
Set the direction of the text
rtl for Right to Left direction.
code - android:textDirection="rtl"
 
Text Single Line
Constrains the text to a single horizontally scrolling line instead of letting it wrap onto multiple lines,
Must be boolean ( True / False )
code - android:singleLine="true"

Text Shadow color
Place a blurred shadow of text underneath the text, drawn with the specified color.
Must be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb" or hex (#ffaa77)
code - android:shadowColor="#aa6600"

Text ShadowDX
Horizontal offset of the text shadow.
Must be a floating point value, such as "1.2".
code - android:shadowDx="1.2"
 
Text ShadowDY
Vertical offset of the text shadow.
Must be a floating point value, such as "1.2".
code - android:shadowDy="1.2"
 
Text Shadow Radius
Blur radius of the text shadow.
Must be a floating point value, such as "1.2".
code - android:shadowRadius="1.2"
 
 
 
Text IsSelectable
Indicates that the content of a non-editable text can be selected.
Must be a boolean value, either "true" or "false".
code - android:textIsSelectable="true"

Text  ScaleX
Sets the horizontal scaling factor for the text.
Must be a floating point value, such as "1.2".
Code - android:textScaleX="12"


Visibility
show or hide the edittext
Must be 'Visible', 'Invisivble' or 'Gone'.
code  - android:visibility="invisible"

Letter  Spacing
Specing between character .
Must be a floating point value, such as "1.2".
method - android:letterSpacing="2"  range:"1 to depend on you"
 

Line Spacing
Spacing between two line
 
code - android:lineSpacingExtra="16dp"

Hint
Show hint for type of input
Method - android:hint="search_hint"
 
Hint Color
Set color for hint text
code - android:textColorHint="#ff6600"

Line
Makes the TextView be exactly this many lines tall.
Must be an integer value, such as "3".
Code - android:lines="2"
 
Font Family
Font family (named by string) for the text.
Must be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character.
code - android:fontFamily="cursive"
 

Gravity
Specifies how to align the text by the view's x- and/or y-axis when the text is smaller than the view.
Attribute are - top, bottom, left, right, centre, start, end
code - android:gravity="center"

Margin
Specifies extra space on the bottom side of this view. This space is outside this view's bounds. Margin values should be positive.
Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).
Code - android:layout_margin="46dp"
 
XML Attributes
Attribute Name
Related Method
Description
Specifies extra space on the bottom side of this view. 
Specifies extra space on the end side of this view. 
Specifies extra space on the left side of this view. 
Specifies extra space on the right side of this view. 
Specifies extra space on the start side of this view. 
Specifies extra space on the top side of this view. 


Padding
Sets the padding, in pixels, of all four edges. Padding is defined as space between the edges of the view and the view's content. A views size will include it's padding. If a background is provided, the padding will initially be set to that (0 if the drawable does not have padding). Explicitly setting a padding value will override the corresponding padding found in the background.
Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).
code - android:padding="16dp"

 
XML Attributes
Attribute Name
Related Method
Description
Sets the padding, in pixels, of the bottom edge; see padding.
Sets the padding, in pixels, of the end edge; see padding.
Sets the padding, in pixels, of the left edge; see padding.
Sets the padding, in pixels, of the right edge; see padding.
Sets the padding, in pixels, of the start edge; see padding.
Sets the padding, in pixels, of the top edge; see padding.


onClick
Name of the method in this View's context to invoke when the view is clicked. This name must correspond to a public method that takes exactly one parameter of type View. For instance, if you specify android:onClick="sayHello", you must declare a public void sayHello(View v)method of your context (typically, your Activity).
Must be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character.
code - android:onClick="method name"

rotation
rotation of the view, in degrees.
Must be a floating point value, such as "1.2".
code  - android:rotation="12"

Attribute                   -                       Description
android:rotationX           -              rotation of the view around the x axis, in degrees.
android:rotationY           -              rotation of the view around the y axis, in degrees.

Save Enabled
If false, no state will be saved for this view when it is being frozen. The default is true, allowing the view to be saved (however it also must have an ID assigned to it for its state to be saved). Setting this to false only disables the state for this view, not for its children which may still be saved.
Must be a boolean value, either "true" or "false".
code - android:saveEnabled="false"

Sound Effect  Enabled
Boolean that controls whether a view should have sound effects enabled for events such as clicking and touching.
Must be a boolean value, either "true" or "false".
code - android:soundEffectsEnabled="true"
 
 
Tag
Supply a tag for this view containing a String, to be retrieved later with View.getTag() or searched for with View.findViewWithTag(). It is generally preferable to use IDs (through the android:id attribute) instead of tags because they are faster and allow for compile-time type checking.
Must be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character.
code  -  android:tag="tag name here"

ForeGround
Defines the drawable to draw over the content. This can be used as an overlay. The foreground drawable participates in the padding of the content if the gravity is set to fill.
May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb" or image
code - android:foreground="@drawable/abc"

foreGround Gravity
Defines the gravity to apply to the foreground drawable. The gravity defaults to fill.
Must be one or more (separated by '|') of the following constant values.
Constant
Value
Description
top
0x30
Push object to the top of its container, not changing its size.
bottom
0x50
Push object to the bottom of its container, not changing its size.
left
0x03
Push object to the left of its container, not changing its size.
right
0x05
Push object to the right of its container, not changing its size.
center_vertical
0x10
Place object in the vertical center of its container, not changing its size.
fill_vertical
0x70
Grow the vertical size of the object if needed so it completely fills its container.
center_horizontal
0x01
Place object in the horizontal center of its container, not changing its size.
fill_horizontal
0x07
Grow the horizontal size of the object if needed so it completely fills its container.
center
0x11
Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.
fill
0x77
Grow the horizontal and vertical size of the object if needed so it completely fills its container.
clip_vertical
0x80
Additional option that can be set to have the top and/or bottom edges of the child clipped to its container's bounds. The clip will be based on the vertical gravity: a top gravity will clip the bottom edge, a bottom gravity will clip the top edge, and neither will clip both edges.
clip_horizontal
0x08
Additional option that can be set to have the left and/or right edges of the child clipped to its container's bounds. The clip will be based on the horizontal gravity: a left gravity will clip the right edge, a right gravity will clip the left edge, and neither will clip both edges.


foregroundTint
Tint to apply to the foreground. if your foreground is image then overwrite with tint color.
Must be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".
android:foregroundTint="#12e7ff"


 foregroundTintMode
Blending mode used to apply the foreground tint. awsm
Must be one of the following constant values.
Constant
Value
Description
src_over
3
The tint is drawn on top of the drawable. [Sa + (1 - Sa)*Da, Rc = Sc + (1 - Sa)*Dc]
src_in
5
The tint is masked by the alpha channel of the drawable. The drawable’s color channels are thrown out. [Sa * Da, Sc * Da]
src_atop
9
The tint is drawn above the drawable, but with the drawable’s alpha channel masking the result. [Da, Sc * Da + (1 - Sa) * Dc]
multiply
14
Multiplies the color and alpha channels of the drawable with those of the tint. [Sa * Da, Sc * Dc]
screen
15
[Sa + Da - Sa * Da, Sc + Dc - Sc * Dc]
add
16
Combines the tint and drawable color and alpha channels, clamping the result to valid color values. Saturate(S + D)

code -  android:foregroundTintMode="add"



//-------------------------------   Scroll bar start here

ScrollIndicators
Defines which scroll indicators should be displayed when the view can be scrolled. Multiple values may be combined using logical OR, for example "top|bottom".
Must be one or more (separated by '|') of the following constant values.
Constant
Value
Description
none
0x00
No scroll indicators are displayed.
top
0x01
Displays top scroll indicator when view can be scrolled up.
bottom
0x02
Displays bottom scroll indicator when vew can be scrolled down.
left
0x04
Displays left scroll indicator when vew can be scrolled left.
right
0x08
Displays right scroll indicator when vew can be scrolled right.
start
0x10
Displays right scroll indicator when vew can be scrolled in the start direction.
end
0x20
Displays right scroll indicator when vew can be scrolled in the end direction.

code - android:scrollIndicators="bottom"

ScrollX
The initial horizontal scroll offset, in pixels.
Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).
code - android:scrollX="14sp"

ScrollY
The initial vertical scroll offset, in pixels.
Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).
code -  android:scrollY="14sp"

 scrollbarAlwaysDrawHorizontalTrack
Defines whether the horizontal scrollbar track should always be drawn.
Must be a boolean value, either "true" or "false".
android:scrollbarAlwaysDrawHorizontalTrack="true"
 
Below code for vertical

  android:scrollbarAlwaysDrawVerticalTrack="true"

scrollbarDefaultDelayBeforeFade
Defines the delay in milliseconds that a scrollbar waits before fade out.
Must be an integer value, such as "100".
code - android:scrollbarDefaultDelayBeforeFade="100"

scrollbarFadeDuration
Defines the delay in milliseconds that a scrollbar takes to fade out.
Must be an integer value, such as "100".
code - android:scrollbarFadeDuration="100"

scrollbarSize
Sets the width of vertical scrollbars and height of horizontal scrollbars.
Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).
code  - android:scrollbarSize="100dp"

scrollbarStyle
Controls the scrollbar style and position. The scrollbars can be overlaid or inset. When inset, they add to the padding of the view. And the scrollbars can be drawn inside the padding area or on the edge of the view. For example, if a view has a background drawable and you want to draw the scrollbars inside the padding specified by the drawable, you can use insideOverlay or insideInset. If you want them to appear at the edge of the view, ignoring the padding, then you can use outsideOverlay or outsideInset.
Must be one of the following constant values.
Constant
Value
Description
insideOverlay
0x0
Inside the padding and overlaid
insideInset
0x01000000
Inside the padding and inset
outsideOverlay
0x02000000
Edge of the view and overlaid
outsideInset
0x03000000
Edge of the view and inset


scrollbarThumbHorizontal
Defines the horizontal scrollbar thumb drawable.
android:scrollbarThumbHorizontal="@drawable/abc"

scrollbarThumbVertical
Defines the vertical scrollbar thumb drawable.
android:scrollbarThumbVertical="@drawable/abc"
 
 scrollbarTrackHorizontal
Defines the horizontal scrollbar track drawable.
code - android:scrollbarTrackHorizontal="@drawable/abc"
 
 
 
scrollbarTrackVertical
 
Defines the vertical scrollbar track drawable.
code - android:scrollbarTrackVertical="@drawable/abc"
 
 
 
scrollbars
Defines which scrollbars should be displayed on scrolling or not.
Must be one or more (separated by '|') of the following constant values.
Constant
Value
Description
none
0x00000000
No scrollbar is displayed.
horizontal
0x00000100
Displays horizontal scrollbar only.
vertical
0x00000200
Displays vertical scrollbar only.

code - android:scrollbars="horizontal"

--------------------------------------
Java Code

XML PART
 
<Button

    android:id="@+id/button1"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:hint=" button hint text"

    />
 
JAVA PART
public class MainActivity extends AppCompatActivity {



    Button button;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        button =( Button) findViewById(R.id. button1);  // edittext initialization

        

    }

}

getText()
Obtain text from textView and store it.
Runtime
String string = button.getText().toString();

setText()
Set  text to edittext .
Runtime
 
 
button.setText(" Set this value to button ");
 
Directly, parse value to editText
 
or 
 
String string = " Set this value to button ";
button.getText(string);

Store the value to a string first and then set to button.

selectAll
select all text  of view
Place this code inside button.
code - button.selectAll();


setBackGroundColor
Change the color of button.
code -                button.setBackgroundColor(Color.DKGRAY);
                          button.setBackgroundColor(Color.parseColor("#12e7ff"));

append
Add text or string to view or any character sequence.
Code - button.append( string );
 example - button.append("hello");
 
or
String hello="hello diva";
 
button.append(hello);

 append (CharSequence text, int start, int end)
add text in view with specified range ( start and end range )
code - button.append(Char squence, int start,int end);
like - button.append("hellodivakar",3,5);
this code only 'lo' to string

borderLessStyle
One design that can be useful is a "borderless" button. Borderless buttons resemble basic buttons except that they have no borders or background but still change appearance during different states, such as when clicked.
To create a borderless button, apply the borderlessButtonStyle style to the button. For example:
<Button

    android:id="@+id/button_send"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="@string/button_send"

    android:onClick="sendMessage"

    style="?android:attr/borderlessButtonStyle" />
style="@style/Widget.AppCompat.Button"
style="@style/Widget.AppCompat.Button.Colored"
style="@style/Widget.AppCompat.Button.Borderless"
style="@style/Widget.AppCompat.Button.Borderless.Colored"


Custom Background

If you want to truly redefine the appearance of your button, you can specify a custom background. Instead of supplying a simple bitmap or color, however, your background should be a state list resource that changes appearance depending on the button's current state.
Create a new XML file in the res/drawable/ directory (name it something like button_custom.xml). Insert the following XML:
part-I
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   
<item android:drawable="@drawable/button_pressed"
         
android:state_pressed="true" />
   
<item android:drawable="@drawable/button_focused"
         
android:state_focused="true" />
   
<item android:drawable="@drawable/button_default" />
</selector>
part-II
<?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="#FF0000" />

        </shape>

    </item>



    <item android:state_focused="true" >

        <shape>

            <solid android:color="#DDDDDD" />

        </shape>

    </item>



    <item>

        <shape>

            <solid android:color="#994fff" />

            <corners android:topLeftRadius="40dp" />

        </shape>

    </item>



</selector>

This XML file now represents a single drawable resource and when referenced by a Button for its background, the image displayed will change based on these three states.
Then simply apply the drawable XML file as the button background:
<Button
   
android:id="@+id/button_send"
   
android:layout_width="wrap_content"
   
android:layout_height="wrap_content"
   
android:text="@string/button_send"
   
android:onClick="sendMessage"
   
android:background="@drawable/button_custom"  />


onClick Method

Onclick handlers are used to execute a part of code when a button or other part of the user interface is touched (clicked). These onClick handlers are an instance of a class that you need to specify. In short it’s an object with a piece of executable code that get’s triggered by a user interaction.
<Button
   
android:onClick="method_name"
   
/>
and place this code  in java file -
 public void method_name(View v) {
        //Inform the user the button has been clicked
        Toast.makeText(this, "Button1 clicked.", Toast.LENGTH_SHORT).show(); 
    }



onClickListner
You can also declare the click event handler programmatically rather than in an XML layout. This might be necessary if you instantiate the Button at runtime or you need to declare the click behavior in a Fragmentsubclass.
To declare the event handler programmatically, create an View.OnClickListener object and assign it to the button by calling setOnClickListener(View.OnClickListener). For example:
Button button = (Button) findViewById(R.id.button_send);

button.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

        // Do something in response to button click

    }

});





























STYLING
add style-shape
Add shape to editext.
Step 1. create a xml file (radiant_bachground.xml ) to drawable folder and place this code

radial_background.xml
<?xml version="1.0" encoding="UTF-8"?>
    <gradient
            android:type="radial"
            android:startColor="#343030"
            android:endColor="#151515"
            android:gradientRadius="300"
            android:angle="270"
            android:centerY="0.3"/>
</shape>

step -2.  Add this code to your view ( edittext, textview, button or more ) to change background
android:background="@drawable/radial_background"



No comments:

Post a Comment