Android UI-re-paint EditText and implement gradient of Button, ui-edittext

Source: Internet
Author: User

Android UI-re-paint EditText and implement gradient of Button, ui-edittext

In this article, we implement a common re-drawing of EditText and adding gradient to buttons or windows.

Because EditText is inherited from TextView, you can re-paint EditText. When re-painting, you only need to inherit EditText and override its onDraw () method.

When adding gradient to a button or window, you need to use the GradientDrawable method to set the gradient direction and gradient color, put the gradient color in an array, and then access it. Use the setBackgroundDrawable () method to display it on the interface.

Run this example:

The specific implementation code is as follows:

MainActivity

Package com. example. testxml; import android. annotation. suppressLint; import android. app. activity; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. drawable. gradientDrawable; import android. graphics. drawable. gradientDrawable. orientation; import android. OS. bundle; import android. widget. button; import android. widget. editText; public class MainActivity extends Activity {private Button btn; private EditText edtxt; // private DrawEdit drawedtxt; @ SuppressLint ("WrongCall") @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); btn = (Button) this. findViewById (R. id. btncenter); // gradient GradientDrawable gradientdrawable = new GradientDrawable (Orientation. LEFT_RIGHT, new int [] {Color. RED, Color. BLACK, Color. YELLOW}); // set the gradient background color of the current window // getWindow (). setBackgroundDrawable (gradientdrawable); btn. setBackgroundDrawable (gradientdrawable );}}

The DrawEdit code for redrawing EditText is as follows:

1 package com. example. testxml; 2 3 import android. content. context; 4 import android. graphics. canvas; 5 import android. graphics. color; 6 import android. graphics. paint; 7 import android. util. attributeSet; 8 import android. widget. editText; 9 10 public class DrawEdit extends EditText {11 12 // implementation of the DrawEdit constructor 13 public DrawEdit (Context context) {14 super (context ); 15} 16 17 public DrawEdit (Context context, AttributeSet attrs) {18 super (context, attrs); 19} 20 21 protected void onDraw (Canvas canvas) {22 super. onDraw (canvas); 23 Paint paint = new Paint (); 24 paint. setTextSize (18); 25 paint. setColor (Color. GREEN); 26 // draw the text 27 canvas. drawText ("Draw text", 2, getHeight ()/2 + 5, paint); 28 29} 30 31 32}

Xml layout file:

1 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 2 xmlns: tools = "http://schemas.android.com/tools" 3 android: id = "@ + id/LinearLayout1" 4 android: layout_width = "match_parent" 5 android: layout_height = "match_parent" 6 android: orientation = "vertical" 7 tools: context = ". mainActivity "> 8 9 <Button10 android: id =" @ + id/btncenter "11 android: layout_width =" fill_parent "12 android: layout_hei Ght = "wrap_content" 13 android: layout_marginTop = "20dp" 14 android: text = "@ string/btncenter"/> 15 16 <! -- You need to customize the EditText when re-painting it, so that you do not need to call it in MainActivity, and he will call it himself --> 17 18 <com. example. testxml. drawEdit19 android: layout_width = "match_parent" 20 android: layout_height = "wrap_content" 21 android: paddingLeft = "100dp" 22/> 23 24 25 </LinearLayout>

The above implements the re-painting of EditText and the gradient process for button settings.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.