{"id":5558,"date":"2014-04-01T02:48:00","date_gmt":"2014-04-01T02:48:00","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/01\/android-how-to-change-layout-on-button-click-collection-of-common-programming-errors\/"},"modified":"2014-04-01T02:48:00","modified_gmt":"2014-04-01T02:48:00","slug":"android-how-to-change-layout-on-button-click-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/01\/android-how-to-change-layout-on-button-click-collection-of-common-programming-errors\/","title":{"rendered":"android: how to change layout on button click?-Collection of common programming errors"},"content":{"rendered":"<p>I have to following code for selecting layout on button click.<\/p>\n<pre><code>View.OnClickListener handler = new View.OnClickListener(){\n    public void onClick(View v) {\n\n        switch (v.getId()) {\n\n            case R.id.DownloadView: \n                \/\/ doStuff\n                setContentView(R.layout.main);\n                break;\n            case R.id.AppView: \n                \/\/ doStuff\n                setContentView(R.layout.app);\n                break;\n        }\n    }\n};\n\nfindViewById(R.id.DownloadView).setOnClickListener(handler);\nfindViewById(R.id.AppView).setOnClickListener(handler);\n<\/code><\/pre>\n<p>When I click the &#8220;AppView&#8221; button, the layout changes, but when I click the &#8220;DownloadView &#8220;button, nothing happens.<\/p>\n<p>This link says that I have to start a new activity.<\/p>\n<p>But I don&#8217;t know how to use the code there of intent to start new activity, will a new file be added? I&#8217;m new to java and android dev, many thanks in advanced for any help! \ud83d\ude42<\/p>\n<p><strong>EDIT: I have my code on the new activity:<\/strong><\/p>\n<pre><code>package com.example.engagiasync;\n\nimport android.app.Activity;\nimport android.content.Intent;\nimport android.os.Bundle;\nimport android.view.View;\nimport android.view.View.OnClickListener;\nimport android.widget.TextView;\n\npublic class AppView extends Activity implements OnClickListener{\n\n\n    @Override\n    public void onCreate(Bundle savedInstanceState){\n\n        setContentView(R.layout.app);\n\n        TextView tv = (TextView) this.findViewById(R.id.thetext);\n        tv.setText(\"App View yo!?\\n\");\n    }\n\n    @Override\n    public void onClick(View v) {\n        \/\/ TODO Auto-generated method stub\n\n    }\n}\n<\/code><\/pre>\n<p><strong>but it does not work, it force closes, the logcat says:<\/strong><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/XTAhV.jpg\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have to following code for selecting layout on button click. View.OnClickListener handler = new View.OnClickListener(){ public void onClick(View v) { switch (v.getId()) { case R.id.DownloadView: \/\/ doStuff setContentView(R.layout.main); break; case R.id.AppView: \/\/ doStuff setContentView(R.layout.app); break; } } }; findViewById(R.id.DownloadView).setOnClickListener(handler); findViewById(R.id.AppView).setOnClickListener(handler); When I click the &#8220;AppView&#8221; button, the layout changes, but when I click the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5558","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5558","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5558"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5558\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5558"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5558"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}