Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Android

Application Development
Samreen Razzaq
Spinner OR Drop Down List
Spinner allows you to select an item from a
drop down menu
For example. When you are using Gmail application you would get drop
down menu as shown below, you need to select an item from a drop down
menu.
Two ways for creation of
Spinner
1. Create a array in xml and attach to
spinner
2. Declare Spinner in XML , Create Adapter
in java and assign value to spinner
through adapter.
Drop Down menu using
Spinner

<Spinner
android:id="@+id/spiner1"
android:layout_width="match_parent"
android:layout_height="80dp" />
Adapter
An Adapter object acts as a bridge between
an AdapterView and the underlying data for
that view. The Adapter provides access to
the data items. The Adapter is also
responsible for making a View for each item
in the data set.
public class MainActivity extends AppCompatActivity {
@Override
Spinner sp; Spinner in java
String[] cities = {"SELECT", "Lahore", "Islamabad"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp = findViewById(R.id.spiner1);
ArrayAdapter adp = new ArrayAdapter(this,
R.layout.support_simple_spinner_dropdown_item, cities);

adp.setDropDownViewResource(R.layout.support_simple_spinner_dro
pdown_item);
sp.setAdapter(adp);
sp.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View
view, int position, long id) {
Toast.makeText(MainActivity.this, cities[position]),
Toast.LENGTH_SHORT).show();
}

@Override
Spinner in xml using array
resource-Remove adapter
<

resources>
<string name="app_name">My Application</string>

<string-array name = "stars">

<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>

</string-array>
</resources>
<Spinner
android:id="@+id/spiner1"
android:layout_width="match_parent"
android:layout_height="80dp"

android:entries = “@array/stars” />


public class MainActivity extends AppCompatActivity {

@Override
Spinner sp;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp = findViewById(R.id.spiner1);
sp.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View
view, int position, long id) {
Toast.makeText(MainActivity.this,
sp.getSelected().toString(), Toast.LENGTH_SHORT).show();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});
}

}
Intent in Android
Android Intent is the message that is
passed between components such as
activities, content providers, broadcast
receivers, services etc. It is generally used
with startActivity() method to invoke activity,
broadcast receivers etc. The dictionary
meaning of intent is intention or purpose.
Types of Android Intents
1) Explicit Intent
Communication between activates of same
application.
2) Implicit Intent
Transfer data between applications.
Explicit Intent
Android Splash Screen is the first screen
visible to the user when the application's
launched. ... Splash screens are used to
display some animations (typically of the
application logo) and illustrations while some
data for the next screens are fetched.
Write name of new activity
Cut intent filter from xml
Paste in splash activity
package com.hybrid.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class splash extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(splash.this, MainActivity.class);
startActivity(i);
finish();

}
}, 4000);
}
}
package com.hybrid.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class splash extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(splash.this, MainActivity.class);
i.putExtra("NAME", "samreen");
i.putExtra("Age", 30);

startActivity(i);
finish();

}
}, 4000);
}
}
Package com.hybrid.myapplication;

public class MainActivity extends


AppCompatActivity {
String name1 = "";
Integer i;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
Bundle b =
getIntent().getExtras();
if (b != null){
name1 = b.getString("NAME");
i = b.getInt("age");

}
}
}

You might also like