Set edittext inside background color in android.
In this tutorial we are creating one extra edittext_background.xml file inside layout folder . This file contain background color and border for edittext. Now we are setting up that particular xml file as background of EditText . So here is the complete step by step tutorial for How to change EditText background color in android.
How to change EditText background color in android.
Code for MainActivity.java file.
package com.changeedittextbackgroundcolor_android_examples.com; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
Code for activity_main.xml layout file.
<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.changeedittextbackgroundcolor_android_examples.com.MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10"
android:hint="Background Change"
android:background="@layout/edittext_background"
android:gravity="center" >
<requestFocus />
</EditText>
</RelativeLayout>
Code for edittext_background.xml file.
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <solid android:color="#4af9ff"/> <stroke android:color="#322fa3" android:width="2dp" /> <corners android:radius="5dp"/> </shape> </item> </selector>
Screenshot: