" Jai Saraswati Maa"
Hi ... Dear All ..Today I am uploading a superb example of Customized Spinner which you never find on google.
In this example when you click on any Row of spinner it will send to you on next Activity with full description about your clicked item. Best part of this is we have only one new activity for Items. Please check.
I am using Android Studio 1.5.1
Minimun SDK API 19
Target SDK API 23
My student partner for this example is " Sahib Singh"
Please Like us & put your valuable suggestions in comment box.
A. Layouts (a)
(b)
(c)
B. xml files
a. activity_main.xml
Hi ... Dear All ..Today I am uploading a superb example of Customized Spinner which you never find on google.
In this example when you click on any Row of spinner it will send to you on next Activity with full description about your clicked item. Best part of this is we have only one new activity for Items. Please check.
I am using Android Studio 1.5.1
Minimun SDK API 19
Target SDK API 23
My student partner for this example is " Sahib Singh"
Please Like us & put your valuable suggestions in comment box.
A. Layouts (a)
(b)
(c)
B. xml files
a. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="sahib.customisedspinnerwithimageclick.MainActivity"> <Spinner android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:layout_marginTop="33dp"
android:spinnerMode="dialog"
android:prompt="@string/mypromot"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" /> </RelativeLayout>
b. row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="70dp"
android:layout_height="70dp"
android:id="@+id/srspinner"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/srlt"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/srspinner"
android:layout_toEndOf="@+id/srspinner" /> <TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/srmt"
android:layout_below="@+id/srlt"
android:layout_toRightOf="@+id/srspinner"
android:layout_toEndOf="@+id/srspinner" /> </RelativeLayout>
c. next_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/srlspinner"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" /> <TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/srllt"
android:layout_alignBottom="@+id/srlspinner"
android:layout_centerHorizontal="true"
android:layout_marginBottom="25dp" /> <TextView android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/srlmt"
android:layout_below="@+id/srlspinner"
android:layout_alignParentLeft="true"
android:layout_marginBottom="40dp"
android:layout_alignParentStart="true" /> <Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back to Spinner"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="back"/>
</RelativeLayout>
C. Resource File string.xml
<resources>
<string name="app_name">Spinner</string>
<string name="mypromot">Make a choice.</string>
<string-array name="title"> <item>Select</item> <item>Android</item>
<item>Java</item> <item>Oracle</item>
<item>Facebook</item> </string-array> <string-array name="desc_short"> <item> </item>
<item>Android is a mobile operating system currently developed by Google</item>
<item>Java is a set of computer software and specifications
developed by Sun Microsystems</item> <item>The Oracle Corporation is an American global computer
technology corporation, headquartered in Redwood City, California.</item> <item>Facebook is a corporation and online social networking
service headquartered in Menlo Park, California, in the United States</item>
</string-array> <string-array name="desc_long"> <item> </item>
<item>Android is a mobile operating system (OS) currently developed
by Google, based on the Linux kernel and designed primarily for touchscreen
mobile devices such as smartphones and tablets. Android\'s user interface is
mainly based on direct manipulation, using touch gestures that loosely correspond
to real-world actions, such as swiping, tapping and pinching, to manipulate on-screen
objects, along with a virtual keyboard for text input. In addition to touchscreen
devices, Google has further developed Android TV for televisions, Android Auto for cars,
and Android Wear for wrist watches, each with a specialized user interface.
Variants of Android are also used on notebooks, game consoles, digital cameras,
and other electronics.</item> <item>Java is a set of computer software and specifications developed by
Sun Microsystems, later acquired by Oracle Corporation, that provides a
system for developing application software and deploying it in a cross-platform
computing environment. Java is used in a wide variety of computing platforms
from embedded devices and mobile phones to enterprise servers and supercomputers.
While less common, Java applets run in secure, sandboxed environments to provide
many features of native applications and can be embedded in HTML pages.</item> <item>The Oracle Corporation is an American global computer technology corporation,
headquartered in Redwood City, California. The company primarily specializes in
developing and marketing database software and technology, cloud engineered systems
and enterprise software products – particularly its own brands of database management
systems. In 2011 Oracle was the second-largest software maker by revenue, after Microsoft. The company also develops and builds tools for database development and systems of
middle-tier software, enterprise resource planning (ERP) software, customer relationship
management (CRM) software and supply chain management (SCM) software.</item> <item>Facebook is a corporation and online social networking service
headquartered in Menlo Park, California, in the United States. Its website
was launched on February 4, 2004, by Mark Zuckerberg with his Harvard College roommates
and fellow students Eduardo Saverin, Andrew McCollum, Dustin Moskovitz and Chris Hughes.
The founders had initially limited the website\'s membership to Harvard students,
but later expanded it to colleges in the Boston area, the Ivy League, and Stanford
University. It gradually added support for students at various other universities and
later to high-school students.</item> </string-array>
</resources>
D. Java files
a. MainActivity.java
package sahib.customisedspinnerwithimageclick; import android.content.Intent; import android.content.res.Resources; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.view.View; import android.widget.AdapterView; import android.widget.Spinner; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements
AdapterView.OnItemSelectedListener { Toolbar tb; Spinner spinner; String longtext[],desc[],title[]; int imgs[]; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spinner=(Spinner) findViewById(R.id.spinner); Resources res=getResources(); longtext=res.getStringArray(R.array.desc_long); desc=res.getStringArray(R.array.desc_short); title=res.getStringArray(R.array.title); Images b=new Images(); imgs=b.img; MyAdapter adp=new MyAdapter(this,title,desc,imgs); spinner.setAdapter(adp); spinner.setOnItemSelectedListener(this); } public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { TextView t1=(TextView)findViewById(R.id.srlt); if(position==0) {} else { Intent ob = new Intent(this, NextActivity.class); ob.putExtra("title", t1.getText().toString()); ob.putExtra("position", position); ob.putExtra("desc_l",longtext[position] ); startActivity(ob); } } @Override public void onNothingSelected(AdapterView<?> parent) { }
}
b. MyAdapter.java
package sahib.customisedspinnerwithimageclick; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; public class MyAdapter extends ArrayAdapter { String title[],des[]; int img[]; Context context; public MyAdapter(Context cont, String titl[], String desc[], int im[]) { super(cont, R.layout.row, R.id.srlt, titl); context = cont; title=titl; img=im; des=desc; } @Override
public View getDropDownView(int position, View convertView, ViewGroup parent) { return getMyView(position, convertView, parent); } @Override
public View getView(int position, View convertView, ViewGroup parent) { return getMyView(position, convertView, parent); } public View getMyView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater=(LayoutInflater) context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE); View obj=inflater.inflate(R.layout.row,parent,false); ImageView iv=(ImageView) obj.findViewById(R.id.srspinner); TextView t1=(TextView) obj.findViewById(R.id.srlt); TextView t2=(TextView) obj.findViewById(R.id.srmt); iv.setImageResource(img[position]); t1.setText(title[position]); t2.setText(des[position]); return obj; }
}
c. NextActivity.java
package sahib.customisedspinnerwithimageclick; import android.content.Intent; import android.media.Image; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.ImageView; import android.widget.TextView; public class NextActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.next_activity); Images ob=new Images(); int img[]=ob.img; ImageView imageView=(ImageView) findViewById(R.id.srlspinner); TextView t1=(TextView) findViewById(R.id.srllt); TextView t2=(TextView) findViewById(R.id.srlmt); Intent iob=getIntent(); t1.setText(iob.getStringExtra("title")); t2.setText(iob.getStringExtra("desc_l")); int position=iob.getIntExtra("position",0); imageView.setImageResource(img[position]); } public void back(View view) { startActivity(new Intent(this, MainActivity.class)); }
}
d. Image resource please add five images in your mipmap folder or either
drawable folder carefully we added five images of different densities
in mipmap and retrieve in Java file
Images.java
package sahib.customisedspinnerwithimageclick;
public class Images { int img[]; public Images() { img=new int[]{R.mipmap.ic_launcher,R.mipmap.android,R.mipmap.java,
R.mipmap.oracle,R.mipmap.fb}; } }
Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.
ReplyDeletedigital marketing training in tambaram
digital marketing training in annanagar
digital marketing training in marathahalli
digital marketing training in rajajinagar
Digital Marketing online training
full stack developer training in pune
I am really very happy to read this blog!!
ReplyDeleteandroid training in chennai
android online training in chennai
android training in bangalore
android training in hyderabad
android Training in coimbatore
android training
android online training