{"id":2028,"date":"2016-07-03T06:08:11","date_gmt":"2016-07-03T06:08:11","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?p=2028"},"modified":"2019-06-12T12:33:07","modified_gmt":"2019-06-12T12:33:07","slug":"relative-layout-background-color-image-example","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/ui\/relative-layout-background-color-image-example.html","title":{"rendered":"Relative Layout Background Color And Image Example In Android Studio"},"content":{"rendered":"<p>Here we show how to change background color and image in Relative Layout with example in Android Studio.<\/p>\n<p>In this example we create a custom layout in which we display two RelativeLayout\u2019s , one is the parent layout and other one \u00a0is the child layout in which we display two TextView\u2019s. In the background of Parent Relative Layout we set green color and in the background of child RelativeLayout we set a image\/drawable. Finally we get the reference of TextView\u2019s in our Activity and then perform setOnClickListener event on TextView\u2019s. Whenever a user click on any TextView the text value is displayed on screen by using a Toast.<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/RelativeLayoutsBackgroundColorAndImage\" target=\"_blank\" rel=\"nofollow\">Download Code<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2086\" src=\"\/ui\/wp-content\/uploads\/2016\/06\/Relative-Layout-Background-Color-And-Image-Example.jpg\" alt=\"Relative Layout Background Color And Image Example\" width=\"262\" height=\"406\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/06\/Relative-Layout-Background-Color-And-Image-Example.jpg 262w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/06\/Relative-Layout-Background-Color-And-Image-Example-194x300.jpg 194w\" sizes=\"auto, (max-width: 262px) 100vw, 262px\" \/><\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important\u00a0Note:<\/strong><\/span> We can set color or image in the background of RelativeLayout in XML\u00a0using background attribute or programmatically means in java class using setBackgroundColor() for color and setBackground() method for setting image.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span>\u00a0Create a new project and name it RelativeLayout&#8217;sBackgroundColorAndImage.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span>\u00a0Open res -&gt; layout -&gt;activity_main.xml (or) main.xml and add following code:<\/p>\n<p>In this step we open xml file and then create a custom layout in which we display two RelativeLayout\u2019s , one is the parent layout and other one \u00a0is the child layout in which we display two TextView\u2019s. In the background of Parent Relative Layout we set green color and in the background of child RelativeLayout we set an image\/drawable.<\/p>\n<pre>&lt;!-- parent RelativeLayout in which we set green color for the background --&gt;\r\n&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\nxmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"match_parent\"\r\nandroid:background=\"@color\/greenColor\"\r\ntools:context=\".MainActivity\"&gt;\r\n&lt;!-- child RelativeLayout in which we set an image for the background --&gt;\r\n&lt;RelativeLayout\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_centerInParent=\"true\"\r\nandroid:background=\"@drawable\/img\"\r\nandroid:padding=\"20dp\"&gt;\r\n&lt;!-- first TextView --&gt;\r\n&lt;TextView\r\nandroid:id=\"@+id\/firstTextView\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_centerHorizontal=\"true\"\r\nandroid:layout_margin=\"20dp\"\r\nandroid:padding=\"10dp\"\r\nandroid:text=\"First Text View\"\r\nandroid:textColor=\"@color\/white\"\r\nandroid:textSize=\"20sp\"\r\nandroid:textStyle=\"bold\" \/&gt;\r\n&lt;!-- second TextView --&gt;\r\n&lt;TextView\r\nandroid:id=\"@+id\/secondTextView\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_below=\"@+id\/firstTextView\"\r\nandroid:layout_centerHorizontal=\"true\"\r\nandroid:layout_margin=\"20dp\"\r\nandroid:padding=\"10dp\"\r\nandroid:text=\"Second Text View\"\r\nandroid:textColor=\"@color\/white\"\r\nandroid:textSize=\"20sp\"\r\nandroid:textStyle=\"bold\" \/&gt;\r\n&lt;\/RelativeLayout&gt;\r\n\r\n\r\n&lt;\/RelativeLayout&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span>\u00a0Open\u00a0 \u00a0src -&gt; package -&gt; MainActivity.java<\/p>\n<p>In this step Firstly we get the reference of TextView\u2019s\u00a0 and then perform setOnClickListener event on TextView\u2019s so Whenever a user click on any TextView the text value is displayed on screen by using a Toast.<\/p>\n<pre>package example.abhiandroid.relativelayoutsbackgroundcolorandimage;\r\n\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.view.Menu;\r\nimport android.view.MenuItem;\r\nimport android.view.View;\r\nimport android.widget.TextView;\r\nimport android.widget.Toast;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\nTextView firstTextView, secondTextView;\r\n\r\n@Override\r\nprotected void onCreate(Bundle savedInstanceState) {\r\nsuper.onCreate(savedInstanceState);\r\nsetContentView(R.layout.activity_main);\r\n\/\/ get the reference of TextView's\r\nfirstTextView = (TextView) findViewById(R.id.firstTextView);\r\nsecondTextView = (TextView) findViewById(R.id.secondTextView);\r\n\/\/ perform setOnClickListener event on first TextView\r\nfirstTextView.setOnClickListener(new View.OnClickListener() {\r\n@Override\r\npublic void onClick(View v) {\r\n\/\/ display the text of first TextView by using a toast\r\nToast.makeText(getApplicationContext(), firstTextView.getText().toString(), Toast.LENGTH_LONG).show();\r\n}\r\n});\r\n\/\/ perform setOnClickListener event on first TextView\r\nsecondTextView.setOnClickListener(new View.OnClickListener() {\r\n@Override\r\npublic void onClick(View v) {\r\n\/\/ display the text of second TextView by using a toast\r\nToast.makeText(getApplicationContext(), secondTextView.getText().toString(), Toast.LENGTH_LONG).show();\r\n}\r\n});\r\n}\r\n\r\n@Override\r\npublic boolean onCreateOptionsMenu(Menu menu) {\r\n\/\/ Inflate the menu; this adds items to the action bar if it is present.\r\ngetMenuInflater().inflate(R.menu.menu_main, menu);\r\nreturn true;\r\n}\r\n\r\n@Override\r\npublic boolean onOptionsItemSelected(MenuItem item) {\r\n\/\/ Handle action bar item clicks here. The action bar will\r\n\/\/ automatically handle clicks on the Home\/Up button, so long\r\n\/\/ as you specify a parent activity in AndroidManifest.xml.\r\nint id = item.getItemId();\r\n\r\n\/\/noinspection SimplifiableIfStatement\r\nif (id == R.id.action_settings) {\r\nreturn true;\r\n}\r\n\r\nreturn super.onOptionsItemSelected(item);\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 4:<\/strong><\/span>\u00a0Open\u00a0 \u00a0res -&gt; values-&gt; colors.xml<\/p>\n<p>In this step we define the colors (green and white) that used in the background and text color of TextView\u2019s.<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;resources&gt;\r\n\r\n&lt;!\u2014define color\u2019s that are used in our app --&gt;\r\n&lt;color name=\"greenColor\"&gt;#0f0&lt;\/color&gt;\r\n&lt;color name=\"white\"&gt;#fff&lt;\/color&gt;\r\n&lt;\/resources&gt;<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>Now run the app and you will see the below output<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-2086\" src=\"\/ui\/wp-content\/uploads\/2016\/06\/Relative-Layout-Background-Color-And-Image-Example-194x300.jpg\" alt=\"Relative Layout Background Color And Image Example\" width=\"194\" height=\"300\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/06\/Relative-Layout-Background-Color-And-Image-Example-194x300.jpg 194w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/06\/Relative-Layout-Background-Color-And-Image-Example.jpg 262w\" sizes=\"auto, (max-width: 194px) 100vw, 194px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here we show how to change background color and image in Relative Layout with example in Android Studio. In this example we create a custom layout in which we display two RelativeLayout\u2019s , one is the parent layout and other one \u00a0is the child layout in which we display two TextView\u2019s. In the background of &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/relative-layout-background-color-image-example.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Relative Layout Background Color And Image Example In Android Studio<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":2086,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1,67],"tags":[],"class_list":["post-2028","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-archieve","category-layout"],"acf":[],"psp_head":"<title>Relative Layout's Background Color And Image Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Here we show how to change background color and image in Relative Layout with example in Android Studio.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/relative-layout-background-color-image-example.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/2028","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/comments?post=2028"}],"version-history":[{"count":2,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/2028\/revisions"}],"predecessor-version":[{"id":2819,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/2028\/revisions\/2819"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media\/2086"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=2028"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/categories?post=2028"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/tags?post=2028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}