Android Basics

Android Studio

Hello visitors,
Today we are going to start the basic about android studio, i.e. how to install and how to set up all settings.
Android Studio is the software platform where user can create their own applications by using this software. Following are the features of android:
Beautiful UI , Connectivity , Storage , Media Support , Messaging , Web Browser , Multi-touch , Multitasking , Re-sizable widgets , Multi-language , GCM , FCM , Android Beam etc.
Activity Life Cycle :

Android_ActivityLifeCycle
In the above activity life cycle, activity starts with the onCreate() method, i.e. this is the main method where the activity can present at the initial level.
Android Life cycle has mainly Following methods :
  1. onCreate :  It called , when activity is firstly created.
  2. onStart    :  It called , when activity is visible to the user.
  3. onResume: It called , when activity will start the communication with user.
  4. onPause : It called , when activity is not visible to the user.
  5. onStop : It called , when activity is no longer visible to the user.
  6. onRestart: It called , when your activity is stopped and prior to start.
  7. onDestroy: It called , before the activity is destroyed.  

USB Debugging

Should be enabled on phone to use developer features
In the main apps screen select Settings ->Applications -> Development -> USB debugging (it needs to be checked)

Following are the example of Custom Spinner Widget in Android

In this topic we will learn how to create a custom spinner in android. Spinners is one of the widgets in Android which allows the user to pick one item from a list of items.
Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a drop-down menu with all other available values, from which the user can select a new one.

Example :-  Displaying the list of subjects on selecting an item it will display that item in a Toast.

1) Declare the spinner Globally  i.e  outside the onCreate method

// Declare the variable outside onCreate
Spinner spin;  
    
2) Within the onCreate method : 
Typecasting spinner
Creating Data Pump
Declaring an ArrayAdapter

// Typecasting the variable here
spin = (Spinner) findViewById(R.id.spn1);

// Array of Months acting as a data pump
String[] objects = { "Math", "C", "CPP", "OS", "Data Structure",
".NET", "VB" };

// Declaring an Adapter and initializing it to the data pump
ArrayAdapter adapter = new ArrayAdapter(
getApplicationContext(),android.R.layout.simple_list_item_1 ,objects);

// Setting Adapter to the Spinner
spin.setAdapter(adapter);

// Setting OnItemClickListener to the Spinner
spin.setOnItemSelectedListener(this);

3) Outside the onCreate method : The callback methods that are necessary to be implemented with OnItemSelectedListener.
There are two callback methods :

onItemSelected
onNothingSelected

// Defining the Callback methods here

public void onItemSelected(AdapterView parent, View view, int pos,long id) 
{
Toast.makeText(getApplicationContext(),
spin.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG)
.show();
}

// Defining the Callback methods here
@Override
public void onNothingSelected(AdapterView arg0) {
// TODO Auto-generated method stub

}

Android Menu:


Menu is an important user interface component which provides some action options for a particular activity. In this post I’ll show the basics for setting up a menu.
The activity will contain 2 menu options: Add and Help. When clicking an option menu, a Toast notification will be displayed.
 Fill in the content of options.xml file with following code:
1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
   <item
      android:id="@+id/add"
      android:icon="@android:drawable/ic_menu_add"
      android:title="Add"/>

   <item
      android:id="@+id/help"
      android:icon="@android:drawable/ic_menu_help"
      android:title="Help"/>
</menu>
menu – defines the menu
item  – defines a menu item

Open the main Activity class and type following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class MenuTestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
}

// Inflate the menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
   MenuInflater inflater = getMenuInflater();
   inflater.inflate(R.menu.options, menu);
   return true;
}

// Handle click events
@Override
public boolean onOptionsItemSelected(MenuItem item) {
   switch (item.getItemId()) {
   case R.id.add:
      Toast.makeText(this, "Add", Toast.LENGTH_SHORT).show();
      return true;
   case R.id.help:
      Toast.makeText(this, "Help", Toast.LENGTH_SHORT).show();
      return true;

   default:
      return super.onOptionsItemSelected(item);
}
}
}
– The onCreateOptionsMenu() method inflates your menu defined in the XML resource.
– The onOptionsItemSelected() method handle click events. When the user selects an item from the options menu, the system calls the onOptionsItemSelected() method. This method passes the MenuItemselected. The MenuItem can identify the item by calling the getItemId() which returns the unique ID for the menu item defined by the android:id attribute in the menu resource. Depending of the returned ID, we display the appropriate Toast notification.



Share this

Related Posts

Previous
Next Post »