banner



How To Set Background Color In Android Studio

Here we show how to change background color and epitome in Relative Layout with example in Android Studio.

In this example we create a custom layout in which nosotros display two RelativeLayout's , one is the parent layout and other one  is the child layout in which we display two TextView's. In the groundwork of Parent Relative Layout we set green color and in the background of kid RelativeLayout nosotros set a epitome/drawable. Finally we get the reference of TextView's in our Activity and and then perform setOnClickListener effect on TextView'south. Whenever a user click on any TextView the text value is displayed on screen past using a Toast.

Download Code

Relative Layout Background Color And Image Example

Important Note: We can set color or prototype in the background of RelativeLayout in XML using background aspect or programmatically means in coffee class using setBackgroundColor() for colour and setBackground() method for setting prototype.

Step 1:  Create a new project and name it RelativeLayout'sBackgroundColorAndImage.

Stride 2:  Open up res -> layout ->activity_main.xml (or) main.xml and add following lawmaking:

In this step nosotros open xml file and then create a custom layout in which nosotros display ii RelativeLayout'due south , one is the parent layout and other ane  is the child layout in which we display ii TextView'southward. In the background of Parent Relative Layout we fix green colour and in the background of child RelativeLayout we gear up an epitome/drawable.

<!-- parent RelativeLayout in which we set up dark-green color for the background --> <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:background="@color/greenColor" tools:context=".MainActivity"> <!-- child RelativeLayout in which we set an prototype for the groundwork --> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@drawable/img" android:padding="20dp"> <!-- first TextView --> <TextView android:id="@+id/firstTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="truthful" android:layout_margin="20dp" android:padding="10dp" android:text="Offset Text View" android:textColor="@colour/white" android:textSize="20sp" android:textStyle="bold" /> <!-- 2nd TextView --> <TextView android:id="@+id/secondTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/firstTextView" android:layout_centerHorizontal="true" android:layout_margin="20dp" android:padding="10dp" android:text="2d Text View" android:textColor="@color/white" android:textSize="20sp" android:textStyle="assuming" /> </RelativeLayout>   </RelativeLayout>          

Stride iii:  Open up   src -> package -> MainActivity.java

In this footstep Firstly we get the reference of TextView's  and and so perform setOnClickListener event on TextView's so Whenever a user click on whatever TextView the text value is displayed on screen by using a Toast.

package example.abhiandroid.relativelayoutsbackgroundcolorandimage;  import android.support.v7.app.AppCompatActivity; import android.os.Parcel; import android.view.Card; import android.view.MenuItem; import android.view.View; import android.widget.TextView; import android.widget.Toast;  public class MainActivity extends AppCompatActivity {  TextView firstTextView, secondTextView;  @Override protected void onCreate(Packet savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // get the reference of TextView's firstTextView = (TextView) findViewById(R.id.firstTextView); secondTextView = (TextView) findViewById(R.id.secondTextView); // perform setOnClickListener event on first TextView firstTextView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View five) { // display the text of beginning TextView by using a toast Toast.makeText(getApplicationContext(), firstTextView.getText().toString(), Toast.LENGTH_LONG).show(); } }); // perform setOnClickListener event on first TextView secondTextView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // display the text of second TextView past using a toast Toast.makeText(getApplicationContext(), secondTextView.getText().toString(), Toast.LENGTH_LONG).show(); } }); }  @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the activeness bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; }  @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks hither. The action bar volition // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId();  //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; }  return super.onOptionsItemSelected(item); } }          

Stride 4:  Open up   res -> values-> colors.xml

In this step we ascertain the colors (greenish and white) that used in the groundwork and text colour of TextView's.

<?xml version="1.0" encoding="utf-8"?> <resources>  <!—define colour'due south that are used in our app --> <color name="greenColor">#0f0</color> <color name="white">#fff</colour> </resource>

Output:

Now run the app and yous volition see the beneath output

Relative Layout Background Color And Image Example

How To Set Background Color In Android Studio,

Source: https://abhiandroid.com/ui/relative-layout-background-color-image-example.html

Posted by: mitchelleleer1937.blogspot.com

0 Response to "How To Set Background Color In Android Studio"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel