{"id":1986,"date":"2016-06-13T05:55:53","date_gmt":"2016-06-13T05:55:53","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?p=1986"},"modified":"2019-06-12T12:36:42","modified_gmt":"2019-06-12T12:36:42","slug":"dynamic-relativelayout-params-programmatically","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/ui\/dynamic-relativelayout-params-programmatically.html","title":{"rendered":"Dynamic RelativeLayout Params Programmatically Example In Android Studio"},"content":{"rendered":"<p>Below is the example of Dynamic RelativeLayout aka programmatically. In this example we created dynamic view\u2019s (ImageView and Button) programmatically in Java class. For creating views programmatically firstly we define the Relative Layout in our xml file and then add the code in java class to create ImageView and a Button one after another. Finally we perform onClick event listener on Button so whenever a user click on Button a message \u201c Button Clicked\u201d is displayed on screen by using a Toast.<\/p>\n<p>The <a href=\"\/ui\/relative-layout\">Relative Layout<\/a> is very flexible layout used in android for custom layout designing. It gives us the flexibility to position our component\/view based on the relative or sibling component\u2019s position. Relative layout is the most used layout after the\u00a0<a href=\"\/ui\/linear-layout\">Linear Layout<\/a>\u00a0in Android.<\/p>\n<p>Below you can download code, see final output and step by step explanation:<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/DynamicRelativeLayout\" target=\"_blank\" rel=\"nofollow\">Download Code<\/a><a class=\"help\" title=\"Learn How To Download Code And Import In Android Studio\" href=\"\/androidstudio\/download-code-abhiandroid\" target=\"_blank\" rel=\"nofollow\"> ?<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2025\" src=\"\/ui\/wp-content\/uploads\/2016\/05\/Dynamic-RelativeLayout-Params-Programmatically-Example-In-Android.jpg\" alt=\"Dynamic RelativeLayout Params Programmatically Example In Android\" width=\"274\" height=\"408\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/05\/Dynamic-RelativeLayout-Params-Programmatically-Example-In-Android.jpg 274w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/05\/Dynamic-RelativeLayout-Params-Programmatically-Example-In-Android-201x300.jpg 201w\" sizes=\"auto, (max-width: 274px) 100vw, 274px\" \/><\/p>\n<p><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span>\u00a0Create a new project and name it DynamicRelativeLayout.<\/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 add RelativeLayout as main layout in which we add views programmatically means in java class.<\/p>\n<pre>&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:paddingBottom=\"@dimen\/activity_vertical_margin\"\r\nandroid:paddingLeft=\"@dimen\/activity_horizontal_margin\"\r\nandroid:paddingRight=\"@dimen\/activity_horizontal_margin\"\r\nandroid:paddingTop=\"@dimen\/activity_vertical_margin\"\r\ntools:context=\".MainActivity\"\r\nandroid:id=\"@+id\/simpleRelativeLayout\"&gt;\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 RelativeLayout. After that we add the code to create <a href=\"\/ui\/imageview\">ImageView<\/a> and a <a href=\"\/ui\/button\">Button<\/a> one after another. We also set the different properties of ImageView and Button. Finally we perform onClick event listener on Button so whenever a user click on Button a message \u201c Button Clicked\u201d is displayed on screen by using a Toast.<\/p>\n<pre>package example.abhiandroid.dynamicrelativelayout;\r\n\r\nimport android.graphics.Color;\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.Button;\r\nimport android.widget.ImageView;\r\nimport android.widget.RelativeLayout;\r\nimport android.widget.Toast;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n    RelativeLayout simpleRelativeLayout;\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        \/\/ get the reference of RelativeLayout\r\n        simpleRelativeLayout = (RelativeLayout) findViewById(R.id.simpleRelativeLayout);\r\n        \/\/ set the layout params for ImageView\r\n        RelativeLayout.LayoutParams imageViewParam = new RelativeLayout.LayoutParams(\r\n                RelativeLayout.LayoutParams.WRAP_CONTENT,\r\n                RelativeLayout.LayoutParams.WRAP_CONTENT);\r\n        \/\/ create a new ImageView\r\n        ImageView simpleImageView = new ImageView(this);\r\n        simpleImageView.setId(1);  \/\/ set ImageView's id\r\n        simpleImageView.setLayoutParams(imageViewParam); \/\/ set defined layout params to ImageView\r\n        simpleImageView.setImageResource(R.drawable.abhi_android);    \/\/ set resource in ImageView\r\n        simpleImageView.setBackgroundColor(Color.BLACK); \/\/ set black color in the background of ImageView\r\n        imageViewParam.addRule(RelativeLayout.CENTER_HORIZONTAL); \/\/ align ImageView in the center\r\n        simpleRelativeLayout.addView(simpleImageView); \/\/ add ImageView in RelativeLayout\r\n\r\n        \/\/ set the layout params for Button\r\n        RelativeLayout.LayoutParams buttonParam = new RelativeLayout.LayoutParams(\r\n                RelativeLayout.LayoutParams.MATCH_PARENT,\r\n                RelativeLayout.LayoutParams.WRAP_CONTENT);\r\n        Button myButton = new Button(this);  \/\/ create a new Button\r\n        myButton.setText(\"Dynamic Button\"); \/\/ set Text in the Button\r\n        myButton.setLayoutParams(buttonParam); \/\/ set defined layout params to Button\r\n        myButton.setTextColor(Color.WHITE); \/\/ set white color for the text of Button\r\n        myButton.setBackgroundColor(Color.parseColor(\"#95C03C\")); \/\/ set Button's background color\r\n        buttonParam.addRule(RelativeLayout.BELOW, 1); \/\/ set Button to the below of ImageView\r\n        simpleRelativeLayout.addView(myButton); \/\/ add Button in RelativeLayout\r\n        \/\/ perform setOnClickListener event\r\n        myButton.setOnClickListener(new View.OnClickListener() {\r\n            @Override\r\n            public void onClick(View v) {\r\n                \/\/ display a toast on Button click\r\n                Toast.makeText(getApplicationContext(), \"Button Clicked\", Toast.LENGTH_LONG).show();\r\n            }\r\n        });\r\n    }\r\n\r\n    @Override\r\n    public boolean onCreateOptionsMenu(Menu menu) {\r\n        \/\/ Inflate the menu; this adds items to the action bar if it is present.\r\n        getMenuInflater().inflate(R.menu.menu_main, menu);\r\n        return true;\r\n    }\r\n\r\n    @Override\r\n    public 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\n        int id = item.getItemId();\r\n\r\n        \/\/noinspection SimplifiableIfStatement\r\n        if (id == R.id.action_settings) {\r\n            return true;\r\n        }\r\n\r\n        return super.onOptionsItemSelected(item);\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>Now run the App and you will see one image and a Button which is created dynamically i.e. programmatically in JAVA.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-2025\" src=\"\/ui\/wp-content\/uploads\/2016\/05\/Dynamic-RelativeLayout-Params-Programmatically-Example-In-Android-201x300.jpg\" alt=\"Dynamic RelativeLayout Params Programmatically Example In Android\" width=\"201\" height=\"300\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/05\/Dynamic-RelativeLayout-Params-Programmatically-Example-In-Android-201x300.jpg 201w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/05\/Dynamic-RelativeLayout-Params-Programmatically-Example-In-Android.jpg 274w\" sizes=\"auto, (max-width: 201px) 100vw, 201px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Below is the example of Dynamic RelativeLayout aka programmatically. In this example we created dynamic view\u2019s (ImageView and Button) programmatically in Java class. For creating views programmatically firstly we define the Relative Layout in our xml file and then add the code in java class to create ImageView and a Button one after another. Finally &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/dynamic-relativelayout-params-programmatically.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Dynamic RelativeLayout Params Programmatically Example In Android Studio<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":2025,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1986","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-archieve"],"acf":[],"psp_head":"<title>Dynamic RelativeLayout Params Programmatically Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Below is the example of Dynamic RelativeLayout aka programmatically. In this example we create dynamic view\u2019s(ImageView and Button) means create view\u2019s programmatically in Java class.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/dynamic-relativelayout-params-programmatically.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/1986","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=1986"}],"version-history":[{"count":2,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/1986\/revisions"}],"predecessor-version":[{"id":2820,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/1986\/revisions\/2820"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media\/2025"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=1986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/categories?post=1986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/tags?post=1986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}