{"id":2472,"date":"2017-05-01T07:56:49","date_gmt":"2017-05-01T07:56:49","guid":{"rendered":"http:\/\/webartdevelopers.com\/?p=2472"},"modified":"2017-05-01T10:21:53","modified_gmt":"2017-05-01T10:21:53","slug":"basic-concept-of-spinner-control-and-imageview-control-in-android","status":"publish","type":"post","link":"https:\/\/webartdevelopers.com\/blog\/basic-concept-of-spinner-control-and-imageview-control-in-android\/","title":{"rendered":"Basic Concept of Spinner Control and ImageView Control in Android"},"content":{"rendered":"<p>In this tutorial following is shown:<\/p>\n<ul>\n<li>Basic concepts of Spinner Control of Android<\/li>\n<li>Basic concepts of ImageView Control of Android<\/li>\n<li>How to add new Array from strings.xml file<\/li>\n<li>How to add images in an Android Applicaion is also shown.<\/li>\n<\/ul>\n<p>Points to be remembered:<\/p>\n<ol>\n<li>When you add images in the application first add all the images in the res\/drawable-hdpi folder.<\/li>\n<li>Keep the first character of image name in small caps or image will not be loaded in the application.<\/li>\n<\/ol>\n<p>Code:<\/p>\n<p>activity_main.xml<\/p>\n<p>&lt;RelativeLayout xmlns:android=&#8221;http:\/\/schemas.android.com\/apk\/res\/android&#8221;<br \/>\nxmlns:tools=&#8221;http:\/\/schemas.android.com\/tools&#8221;<br \/>\nandroid:layout_width=&#8221;match_parent&#8221;<br \/>\nandroid:layout_height=&#8221;match_parent&#8221;<br \/>\nandroid:paddingBottom=&#8221;@dimen\/activity_vertical_margin&#8221;<br \/>\nandroid:paddingLeft=&#8221;@dimen\/activity_horizontal_margin&#8221;<br \/>\nandroid:paddingRight=&#8221;@dimen\/activity_horizontal_margin&#8221;<br \/>\nandroid:paddingTop=&#8221;@dimen\/activity_vertical_margin&#8221;<br \/>\ntools:context=&#8221;.MainActivity&#8221; &gt;<\/p>\n<p>&lt;Spinner<br \/>\nandroid:id=&#8221;@+id\/spinner1&#8243;<br \/>\nandroid:layout_width=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_height=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_alignParentTop=&#8221;true&#8221;<br \/>\nandroid:layout_centerHorizontal=&#8221;true&#8221;<br \/>\nandroid:layout_marginTop=&#8221;22dp&#8221;<br \/>\nandroid:entries=&#8221;@array\/image_list&#8221; \/&gt;<\/p>\n<p>&lt;TextView<br \/>\nandroid:id=&#8221;@+id\/textView1&#8243;<br \/>\nandroid:layout_width=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_height=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_alignLeft=&#8221;@+id\/spinner1&#8243;<br \/>\nandroid:layout_below=&#8221;@+id\/spinner1&#8243;<br \/>\nandroid:layout_marginLeft=&#8221;14dp&#8221;<br \/>\nandroid:text=&#8221;TextView&#8221; \/&gt;<\/p>\n<p>&lt;ImageView<br \/>\nandroid:id=&#8221;@+id\/imageView1&#8243;<br \/>\nandroid:layout_width=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_height=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_alignLeft=&#8221;@+id\/spinner1&#8243;<br \/>\nandroid:layout_below=&#8221;@+id\/textView1&#8243;<br \/>\nandroid:layout_marginTop=&#8221;70dp&#8221;<br \/>\nandroid:src=&#8221;@drawable\/desert&#8221; \/&gt;<\/p>\n<p>&lt;\/RelativeLayout&gt;<\/p>\n<p>strings.xml<\/p>\n<p>&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;utf-8&#8243;?&gt;<br \/>\n&lt;resources&gt;<\/p>\n<p>&lt;string name=&#8221;app_name&#8221;&gt;Demo&lt;\/string&gt;<br \/>\n&lt;string name=&#8221;action_settings&#8221;&gt;Settings&lt;\/string&gt;<br \/>\n&lt;string-array name=&#8221;image_list&#8221;&gt;<br \/>\n&lt;item&gt;Desert&lt;\/item&gt;<br \/>\n&lt;item&gt;Koala&lt;\/item&gt;<br \/>\n&lt;item&gt;JellyFish&lt;\/item&gt;<br \/>\n&lt;item&gt;LightHouse&lt;\/item&gt;<br \/>\n&lt;item&gt;Penguins&lt;\/item&gt;<br \/>\n&lt;\/string-array&gt;<\/p>\n<p>&lt;\/resources&gt;<\/p>\n<p>MainActivity.java<\/p>\n<p>package com.example.demo;<\/p>\n<p>import android.os.Bundle;<br \/>\nimport android.app.Activity;<br \/>\nimport android.graphics.Color;<br \/>\nimport android.view.Menu;<br \/>\nimport android.view.View;<br \/>\nimport android.widget.AdapterView;<br \/>\nimport android.widget.ImageView;<br \/>\nimport android.widget.Spinner;<br \/>\nimport android.widget.TextView;<br \/>\npublic class MainActivity extends Activity{<\/p>\n<p>@Override<br \/>\nprotected void onCreate(Bundle savedInstanceState) {<br \/>\nsuper.onCreate(savedInstanceState);<br \/>\nsetContentView(R.layout.activity_main);<br \/>\nfinal Spinner spin=(Spinner)findViewById(R.id.spinner1);<br \/>\nfinal ImageView img=(ImageView)findViewById(R.id.imageView1);<br \/>\nfinal TextView t1=(TextView)findViewById(R.id.textView1);<\/p>\n<p>spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){<\/p>\n<p>@Override<br \/>\npublic void onItemSelected(AdapterView&lt;?&gt; arg0, View arg1,<br \/>\nint arg2, long arg3) {<br \/>\n\/\/ TODO Auto-generated method stub<br \/>\nif(spin.getSelectedItem().equals(&#8220;Desert&#8221;))<br \/>\n{<br \/>\nimg.setImageResource(R.drawable.desert);<br \/>\nt1.setTextColor(Color.GREEN);<br \/>\nt1.setText(&#8220;You have selected Desert Image&#8221;);<br \/>\n}<br \/>\nelse if(spin.getSelectedItem().equals(&#8220;Koala&#8221;))<br \/>\n{<br \/>\nimg.setImageResource(R.drawable.koala);<br \/>\nt1.setTextColor(Color.BLUE);<br \/>\nt1.setText(&#8220;You have selected Koala Image&#8221;);<br \/>\n}<br \/>\nelse if(spin.getSelectedItem().equals(&#8220;JellyFish&#8221;))<br \/>\n{<br \/>\nimg.setImageResource(R.drawable.jellyfish);<br \/>\nt1.setTextColor(Color.RED);<br \/>\nt1.setText(&#8220;You have selected JellyFish Image&#8221;);<br \/>\n}<br \/>\nelse if(spin.getSelectedItem().equals(&#8220;Penguins&#8221;))<br \/>\n{<br \/>\nimg.setImageResource(R.drawable.penguins);<br \/>\nt1.setTextColor(Color.YELLOW);<br \/>\nt1.setText(&#8220;You have selected Penguins Image&#8221;);<br \/>\n}<br \/>\nelse if(spin.getSelectedItem().equals(&#8220;LightHouse&#8221;))<br \/>\n{<br \/>\nimg.setImageResource(R.drawable.lighthouse);<br \/>\nt1.setTextColor(Color.MAGENTA);<br \/>\nt1.setText(&#8220;You have selected LightHouse Image&#8221;);<br \/>\n}<\/p>\n<p>}<\/p>\n<p>@Override<br \/>\npublic void onNothingSelected(AdapterView&lt;?&gt; arg0) {<br \/>\n\/\/ TODO Auto-generated method stub<\/p>\n<p>}});<\/p>\n<p>}<\/p>\n<p>@Override<br \/>\npublic boolean onCreateOptionsMenu(Menu menu) {<br \/>\n\/\/ Inflate the menu; this adds items to the action bar if it is present.<br \/>\ngetMenuInflater().inflate(R.menu.main, menu);<br \/>\nreturn true;<br \/>\n}<\/p>\n<p>}<\/p>\n<p><a href=\"https:\/\/youtu.be\/EEdSSuvLGFY\">https:\/\/youtu.be\/EEdSSuvLGFY<\/a><\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"In this tutorial following is shown: Basic concepts of Spinner Control of Android Basic concepts of ImageView Control of Android How to add new Array from strings.xml file How to add images in an Android Applicaion is also shown. Points to be remembered: When you add images in the application first add all the images in the res\/drawable-hdpi folder. Keep the first character of image name in small caps or image will not be loaded in the application. Code: activity_main.xml &lt;RelativeLayout xmlns:android=&#8221;http:\/\/schemas.android.com\/apk\/res\/android&#8221; xmlns:tools=&#8221;http:\/\/schemas.android.com\/tools&#8221; android:layout_width=&#8221;match_parent&#8221; android:layout_height=&#8221;match_parent&#8221; android:paddingBottom=&#8221;@dimen\/activity_vertical_margin&#8221; android:paddingLeft=&#8221;@dimen\/activity_horizontal_margin&#8221; android:paddingRight=&#8221;@dimen\/activity_horizontal_margin&#8221; android:paddingTop=&#8221;@dimen\/activity_vertical_margin&#8221; tools:context=&#8221;.MainActivity&#8221; &gt; &lt;Spinner android:id=&#8221;@+id\/spinner1&#8243; android:layout_width=&#8221;wrap_content&#8221; android:layout_height=&#8221;wrap_content&#8221; android:layout_alignParentTop=&#8221;true&#8221; android:layout_centerHorizontal=&#8221;true&#8221; android:layout_marginTop=&#8221;22dp&#8221; android:entries=&#8221;@array\/image_list&#8221; \/&gt; &lt;TextView android:id=&#8221;@+id\/textView1&#8243; android:layout_width=&#8221;wrap_content&#8221; android:layout_height=&#8221;wrap_content&#8221; android:layout_alignLeft=&#8221;@+id\/spinner1&#8243; android:layout_below=&#8221;@+id\/spinner1&#8243; android:layout_marginLeft=&#8221;14dp&#8221; android:text=&#8221;TextView&#8221; \/&gt; &lt;ImageView android:id=&#8221;@+id\/imageView1&#8243; android:layout_width=&#8221;wrap_content&#8221; android:layout_height=&#8221;wrap_content&#8221; android:layout_alignLeft=&#8221;@+id\/spinner1&#8243; android:layout_below=&#8221;@+id\/textView1&#8243; android:layout_marginTop=&#8221;70dp&#8221; android:src=&#8221;@drawable\/desert&#8221; \/&gt; &lt;\/RelativeLayout&gt; strings.xml <!-- AddThis Advanced Settings generic via filter on get_the_excerpt -->","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62],"tags":[82],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2472"}],"collection":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/comments?post=2472"}],"version-history":[{"count":2,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2472\/revisions"}],"predecessor-version":[{"id":2474,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2472\/revisions\/2474"}],"wp:attachment":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/media?parent=2472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/categories?post=2472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/tags?post=2472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}