{"id":93,"date":"2009-01-31T21:20:11","date_gmt":"2009-02-01T04:20:11","guid":{"rendered":"http:\/\/www.connorgarvey.com\/blog\/?p=93"},"modified":"2010-02-19T13:21:43","modified_gmt":"2010-02-19T20:21:43","slug":"android-button-tutorial","status":"publish","type":"post","link":"https:\/\/connorgarvey.com\/blog\/?p=93","title":{"rendered":"Android button tutorial"},"content":{"rendered":"<p>A lot of people have found this site by searching for an Android button tutorial, so here it is.<\/p>\n<ol>\n<li>This tutorial assumes that you already have an activity and are using an XML layout.<\/li>\n<li>Open the layout XML and add the button element.\u00a0 Assign an ID with the &#8220;@+id&#8221; operator.\u00a0 The + tells Android to generate an ID for this element so that you can reference it in your Java files.\n<ol>\n<li> This is an example.  Your layout and text elements will probably be very different. In this example case, the ID of the button is &#8220;close&#8221;.\n<pre>&lt;Button android:id=\"@+id\/close\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:layout_alignParentBottom=\"true\"\r\n    android:text=\"@string\/title_close\" \/&gt;<\/pre>\n<\/li>\n<\/ol>\n<\/li>\n<li>Open the activity class.\u00a0 Add a class property to hold a reference to the button.<div id=\"wpshdo_1\" class=\"wp-synhighlighter-outer\"><div id=\"wpshdt_1\" class=\"wp-synhighlighter-expanded\"><table border=\"0\" width=\"100%\"><tr><td align=\"left\" width=\"80%\"><a name=\"#codesyntax_1\"><\/a><a id=\"wpshat_1\" class=\"wp-synhighlighter-title\" href=\"#codesyntax_1\"  onClick=\"javascript:wpsh_toggleBlock(1)\" title=\"Click to show\/hide code block\">Code block<\/a><\/td><td align=\"right\"><a href=\"#codesyntax_1\" onClick=\"javascript:wpsh_code(1)\" title=\"Show code only\"><img decoding=\"async\" border=\"0\" style=\"border: 0 none\" src=\"https:\/\/connorgarvey.com\/blog\/wp-content\/plugins\/wp-synhighlight\/themes\/default\/images\/code.png\" \/><\/a>&nbsp;<a href=\"#codesyntax_1\" onClick=\"javascript:wpsh_print(1)\" title=\"Print code\"><img decoding=\"async\" border=\"0\" style=\"border: 0 none\" src=\"https:\/\/connorgarvey.com\/blog\/wp-content\/plugins\/wp-synhighlight\/themes\/default\/images\/printer.png\" \/><\/a>&nbsp;<a href=\"https:\/\/connorgarvey.com\/blog\/wp-content\/plugins\/wp-synhighlight\/About.html\" target=\"_blank\" title=\"Show plugin information\"><img decoding=\"async\" border=\"0\" style=\"border: 0 none\" src=\"https:\/\/connorgarvey.com\/blog\/wp-content\/plugins\/wp-synhighlight\/themes\/default\/images\/info.gif\" \/><\/a>&nbsp;<\/td><\/tr><\/table><\/div><div id=\"wpshdi_1\" class=\"wp-synhighlighter-inner\" style=\"display: block;\"><pre class=\"java\" style=\"font-family:monospace;\"><span class=\"kw1\">private<\/span> <a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Abutton+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky\"><span class=\"kw3\">Button<\/span><\/a> closeButton<span class=\"sy0\">;<\/span><\/pre><\/div><\/div><\/li>\n<li>If you haven&#8217;t already, override the onCreate method.<div id=\"wpshdo_2\" class=\"wp-synhighlighter-outer\"><div id=\"wpshdt_2\" class=\"wp-synhighlighter-expanded\"><table border=\"0\" width=\"100%\"><tr><td align=\"left\" width=\"80%\"><a name=\"#codesyntax_2\"><\/a><a id=\"wpshat_2\" class=\"wp-synhighlighter-title\" href=\"#codesyntax_2\"  onClick=\"javascript:wpsh_toggleBlock(2)\" title=\"Click to show\/hide code block\">Code block<\/a><\/td><td align=\"right\"><a href=\"#codesyntax_2\" onClick=\"javascript:wpsh_code(2)\" title=\"Show code only\"><img decoding=\"async\" border=\"0\" style=\"border: 0 none\" src=\"https:\/\/connorgarvey.com\/blog\/wp-content\/plugins\/wp-synhighlight\/themes\/default\/images\/code.png\" \/><\/a>&nbsp;<a href=\"#codesyntax_2\" onClick=\"javascript:wpsh_print(2)\" title=\"Print code\"><img decoding=\"async\" border=\"0\" style=\"border: 0 none\" src=\"https:\/\/connorgarvey.com\/blog\/wp-content\/plugins\/wp-synhighlight\/themes\/default\/images\/printer.png\" \/><\/a>&nbsp;<a href=\"https:\/\/connorgarvey.com\/blog\/wp-content\/plugins\/wp-synhighlight\/About.html\" target=\"_blank\" title=\"Show plugin information\"><img decoding=\"async\" border=\"0\" style=\"border: 0 none\" src=\"https:\/\/connorgarvey.com\/blog\/wp-content\/plugins\/wp-synhighlight\/themes\/default\/images\/info.gif\" \/><\/a>&nbsp;<\/td><\/tr><\/table><\/div><div id=\"wpshdi_2\" class=\"wp-synhighlighter-inner\" style=\"display: block;\"><pre class=\"java\" style=\"font-family:monospace;\">@Override\n<span class=\"kw1\">protected<\/span> <span class=\"kw4\">void<\/span> onCreate<span class=\"br0\">&#40;<\/span>Bundle savedInstanceState<span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span>\n<span class=\"br0\">&#125;<\/span><\/pre><\/div><\/div><\/p>\n<ol>\n<li>For this example, we don&#8217;t need the saved instance state, so ignore it.<\/li>\n<\/ol>\n<\/li>\n<li>Now, in the onCreate method, attach a listener to the click event for the button.\u00a0 This example will call &#8220;finish()&#8221; on the activity, the Android analog of clicking the close button on a window.<\/li>\n<\/ol>\n<div id=\"wpshdo_3\" class=\"wp-synhighlighter-outer\"><div id=\"wpshdt_3\" class=\"wp-synhighlighter-expanded\"><table border=\"0\" width=\"100%\"><tr><td align=\"left\" width=\"80%\"><a name=\"#codesyntax_3\"><\/a><a id=\"wpshat_3\" class=\"wp-synhighlighter-title\" href=\"#codesyntax_3\"  onClick=\"javascript:wpsh_toggleBlock(3)\" title=\"Click to show\/hide code block\">Code block<\/a><\/td><td align=\"right\"><a href=\"#codesyntax_3\" onClick=\"javascript:wpsh_code(3)\" title=\"Show code only\"><img decoding=\"async\" border=\"0\" style=\"border: 0 none\" src=\"https:\/\/connorgarvey.com\/blog\/wp-content\/plugins\/wp-synhighlight\/themes\/default\/images\/code.png\" \/><\/a>&nbsp;<a href=\"#codesyntax_3\" onClick=\"javascript:wpsh_print(3)\" title=\"Print code\"><img decoding=\"async\" border=\"0\" style=\"border: 0 none\" src=\"https:\/\/connorgarvey.com\/blog\/wp-content\/plugins\/wp-synhighlight\/themes\/default\/images\/printer.png\" \/><\/a>&nbsp;<a href=\"https:\/\/connorgarvey.com\/blog\/wp-content\/plugins\/wp-synhighlight\/About.html\" target=\"_blank\" title=\"Show plugin information\"><img decoding=\"async\" border=\"0\" style=\"border: 0 none\" src=\"https:\/\/connorgarvey.com\/blog\/wp-content\/plugins\/wp-synhighlight\/themes\/default\/images\/info.gif\" \/><\/a>&nbsp;<\/td><\/tr><\/table><\/div><div id=\"wpshdi_3\" class=\"wp-synhighlighter-inner\" style=\"display: block;\"><pre class=\"java\" style=\"font-family:monospace;\"><span class=\"kw1\">protected<\/span> <span class=\"kw4\">void<\/span> onCreate<span class=\"br0\">&#40;<\/span>Bundle savedInstanceState<span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span>\n  <span class=\"kw1\">this<\/span>.<span class=\"me1\">setContentView<\/span><span class=\"br0\">&#40;<\/span>R.<span class=\"me1\">layout<\/span>.<span class=\"me1\">layoutxml<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span>\n  <span class=\"kw1\">this<\/span>.<span class=\"me1\">closeButton<\/span> <span class=\"sy0\">=<\/span> <span class=\"br0\">&#40;<\/span><a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Abutton+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky\"><span class=\"kw3\">Button<\/span><\/a><span class=\"br0\">&#41;<\/span><span class=\"kw1\">this<\/span>.<span class=\"me1\">findViewById<\/span><span class=\"br0\">&#40;<\/span>R.<span class=\"me1\">id<\/span>.<span class=\"me1\">close<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span>\n  <span class=\"kw1\">this<\/span>.<span class=\"me1\">closeButton<\/span>.<span class=\"me1\">setOnClickListener<\/span><span class=\"br0\">&#40;<\/span><span class=\"kw1\">new<\/span> OnClickListener<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span>\n    @Override\n    <span class=\"kw1\">public<\/span> <span class=\"kw4\">void<\/span> onClick<span class=\"br0\">&#40;<\/span><a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Aview+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky\"><span class=\"kw3\">View<\/span><\/a> v<span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span>\n      finish<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">&#125;<\/span>\n  <span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span>\n<span class=\"br0\">&#125;<\/span><\/pre><\/div><\/div>\n<ol>\n<li>Here&#8217;s a short description of what&#8217;s happening.\n<ol>\n<li>First, get the button ID.\u00a0 The ID created earlier in the layout, &#8220;close&#8221;, is compiled by Android and assigned a unique integer ID which is available to the application through the &#8220;R&#8221; class, which I assume is short for &#8220;Resources&#8221;.<\/li>\n<li>Request a reference to the button from the activity by calling &#8220;findViewById&#8221;.\u00a0 The button has to be retrieved from the activity because while an ID is unique in an activity, it is not unique among all activities.<\/li>\n<li>Assign the retrieved button to an instance variable so that if you need it later, you can easily find it without having to query for it again.<\/li>\n<li>Create a class implementing &#8220;OnClickListener&#8221; and set it as the on click listener for the button.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p>As UI elements go, buttons are some of the simplest.\u00a0 Later, I&#8217;ll write about menus and dialogs, which aren&#8217;t so easy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating buttons in Android and assigning code to execute when they&#8217;re clicked is pretty straightforward if you know how to do it.  Insert the button into a layout, get a reference to it from the activity and assign a click listener and you have a working button.  This article describes the process in detail with example code.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[57],"tags":[3,10,4,8,7],"class_list":["post-93","post","type-post","status-publish","format-standard","hentry","category-android","tag-android","tag-button","tag-java","tag-mobile","tag-redpoint"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1ql2n-1v","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/93","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=93"}],"version-history":[{"count":10,"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/93\/revisions"}],"predecessor-version":[{"id":96,"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/93\/revisions\/96"}],"wp:attachment":[{"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=93"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=93"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=93"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}