Sunday, 23 October 2016

Menus in android

Menus are a typical UI part in numerous sorts of utilizations. To give a well known and steady client encounter, you ought to utilize the Menu APIs to present client activities and different choices in your exercises.
There are three types of Menus in android.

Menus in Android


1) Options menu :-

The Options menu (or) choices menu is the essential gathering of menu things for a movement. It's the place you ought to place activities that globally affect the application, for example, "Look", "Form email," and "Settings."

    Example :-
                   @Override
                       public boolean onCreateOptionsMenu(Menu menu) {
                       MenuInflater inflater=getMenuInflater();
                       inflater.inflate(R.menu.kiran_menu,menu);
                       return true;
                       }

2) Context menu :-

    A Context menu is a skimming menu that shows up when the client plays out a long-tap on a     
    component. It gives activities that influence the chose substance or setting outline.

   Example :-
                     @override
                        public boolean onContextItemSelected(MenuItem item)
                        {
                        AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
                        Switch (item.getItemId())
                       {
                        case R.id.contacts:
                        contacts(info.id);
                        return true;
                        case R.id.settings:
                        settings(info.id);
                        return true;
                        default:
                        return super.onContextItemSelected(item);
                        }

 Contextual action mode :-

  The Contextual action mode shows activity things that influence the chose content in a bar at the 
 highest point of the screen and permits the client to choose various things.

3) Popup menu :-

    A popup menu shows a rundown of things in a vertical rundown that is secured to the view that conjured the menu. It's useful for giving a flood of activities that identify with particular substance or to give choices to a second part of a summon. Activities in a popup menu ought not specifically influence the relating substance—that is the thing that relevant activities are for. Or maybe, the popup menu is for stretched out activities that identify with areas of substance in your action.

Example :-
               PopupMenu ppm = new PopupMenu(this,view);
               getMenuInflater().inflate(R.menu.main,ppm.getMenu());
                ppm.show();

Saturday, 22 October 2016

Fragment code

  In Fragments there are two types are adding for Fragment .

 1) Static Fragment :-

Syntax : -
  xml :-              
      <?xml version="1.0" encoding="utf-8"?>
      <fragment  
       android:id="@+id/fragment1" 
       android:name="com.kiranapp.dynamicfragment" 
       android:layout_width="0px" 
       android:layout_height="match_parent" 
       android:layout_weight="1" /> 
 
  java :-
               Fragment frag=getFragmentManager().findFragmentByID(R.id.static_fragment);

 2) Dynamic Fragment :-

Syntax :-
 xml : -       
     <FrameLayout 
      android:layout_width="match_parent"     
      android:layout_height="wrap_content" 
      android:layout_marginTop="20dp" 
      android:id="@+id/myframe" 
      android:layout_gravity="center_horizontal" 
      android:background="#8a99c3">
    </FrameLayout>

 java : -         
     fragmentManager=getFragmentManager();
     dynamicfragment dynamifrag=new dynamicfragment();
     fragmentTransaction=fragmentManager.beginTransaction();
     fragmentTransaction.add(R.id.myframe,dynamifrag);
     fragmentTransaction.commit();    
 
    DynamicFragment :-

               Below code is example for  dynamicfragment . And i have taken another one xml    
               and java.

 
 activity_main.xml :-

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout 
  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" 
  android:orientation="vertical" 
  tools:context="com.kiranapp.MainActivity" 
  android:background="#e6cbcb">
 <TextView 
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content" 
  android:textAppearance="?android:attr/textAppearanceLarge"  
  android:text="This is Activity" 
  android:id="@+id/textView" 
  android:layout_marginTop="10dp" 
  android:layout_gravity="center_horizontal" />
 <FrameLayout 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:layout_marginTop="20dp"  
  android:id="@+id/myframe" 
  android:layout_gravity="center_horizontal" 
  android:background="#8a99c3">
 </FrameLayout>
</LinearLayout>

dynamicfragment.xml :- 

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:orientation="vertical" 
  android:background="#7b1818">

<TextView  
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:text="Dynamic Fragments" 
 android:id="@+id/textView" 
 android:layout_marginTop="10dp" 
 android:layout_gravity="center_horizontal" 
 android:background="#e9e5e5" />
<Button  
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="frag b" 
 android:layout_marginTop="20dp" 
 android:id="@+id/button" 
 android:layout_gravity="center_horizontal" />
</LinearLayout>

MainActivity.java :-

package com.kiranapp;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;

@Override protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 fragmentManager=getFragmentManager();
 dynamicfragment dynamifrag=new dynamicfragment();
 fragmentTransaction=fragmentManager.beginTransaction();
 fragmentTransaction.add(R.id.myframe,dynamifrag);
 fragmentTransaction.commit();
 }
}

dynamicfragment.java :-

package com.kiranapp;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

/** * Created by dell on 8/9/2016. */
public class dynamicfragment extends Fragment {
    Button b1;
@Nullable
@Override 
 public View onCreateView(LayoutInflater inflater, ViewGroup container, 
                                                      Bundle savedInstanceState) {
 return inflater.inflate(R.layout.dyanamicfragment,container,false);
 }
@Override 
 public void onActivityCreated(Bundle savedInstanceState) {
 super.onActivityCreated(savedInstanceState);
 b1=(Button)getActivity().findViewById(R.id.button);
 b1.setOnClickListener(new View.OnClickListener() {
@Override 
 public void onClick(View v) {
 Toast.makeText(getActivity(), "Button is Working", Toast.LENGTH_SHORT).show();
 }
 });
}
}


Output :-

Fragment code
DynamicFragment fig :1





















Fragment code
DynamicFragment fig :2








Friday, 21 October 2016

Fragments

 Fragment :-
Android piece lifecycle is influenced by movement lifecycle on the grounds that sections are incorporated into action. Every part has its own life cycle strategies that is influenced by action life cycle since pieces are installed in action. The FragmentManager class is mindful to make cooperation between piece objects.
How to Creating a Fragment :-
To make a part, you should make a subclass of Fragment (or a current subclass of it). The Fragment class has code that looks a great deal like an Activity. It contains callback strategies like a movement, for example, onCreate(), onStart(), onPause(), and onStop(). Indeed, in case you're changing over a current Android application to utilize pieces, you may just move code from your action's callback strategies into the separate callback techniques for your part.
 How Fragment are in single screen or multiple screen as shown figure below :

Android Fragment fig :1



















Thursday, 20 October 2016

Explicit Intent

Unequivocal purposes indicate the segment to begin by name (the completely qualified class name). You'll normally utilize an unequivocal plan to begin a part in your own particular application, since you know the class name of the movement or administration you need to begin. For instance, begin another action because of a client activity or begin an administration to download a document out of sight.

Syntax : -  By using Intent class
                  Intent in=new Intent(Currentclass.this,TargetActivity.class);
                  startActivity(in);

activity_main.xml :-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
 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" 
 android:orientation="vertical" 
 tools:context="com.kiranapp.MainActivity"     
 android:background="#868226">
<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="call to second activity" 
 android:id="@+id/button" 
 android:layout_marginTop="20dp" 
 android:layout_gravity="center_horizontal" 
 android:background="#ca4b4b" />

</LinearLayout>

MainActivity.java :-

package com.kiranapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Menu;
import android.view.MEnuItem;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

 Button entersecond;

@Override protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 entersecond=(Button)findViewById(R.id.button);
 entersecond.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
 Intent in=new Intent(MainActivity.this,secondactivity.class);
 startActivity(in);
 }
 });
 }
}
 
Note :-  

In this code i taken one activty and second activity only.

 For secondactivity.class we have to create one java class i.e under java give right click and create the class name i.e "secondactivity.java".

 Similarly in layout i.e second_activity.xml also should create the layout.
      Shown below figure 2 i.e you enter second activity (Text View).

If we want more activities we can create " java class and xml "


Output :-

Explicit Intent
Explicit Intent fig :1





















Explicit Intent
Explicit Intent fig :2













Tuesday, 18 October 2016

Implicit Intent

Understood Intent doesn't specify the segment. In such case, plan gives data of accessible parts gave by the framework that will be summoned.  It does not require multiple screens.

Syntax : -  By using Intent class
                  Intent in=new  
                  Intent(Intent.Action_view,Url.parse("http://androiddevelopersstudio.blogspot.in"));     
                  startActivity(in);

activity_main.xml :-

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout 
  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" 
  android:orientation="vertical"  
  tools:context="com.kiranapp.MainActivity">
<Button  
 android:layout_width="wrap_content"  
 android:layout_height="wrap_content" 
 android:text="android developers studio" 
 android:layout_marginTop="29dp" 
 android:id="@+id/button" 
 android:layout_gravity="center_horizontal" />
<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content"  
 android:text="Facebook" 
 android:id="@+id/button2" 
 android:layout_marginTop="20dp" 
 android:layout_gravity="center_horizontal" />
</LinearLayout>

MainActivity.java :-

package com.kiranapp;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
 Button androidbtn,fbbtn;
@Override protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 androidbtn=(Button)findViewById(R.id.button);
 fbbtn=(Button)findViewById(R.id.button2);
 androidbtn.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
//Implicit Intent Using Intent Class 
Intent in=new Intent(Intent.ACTION_VIEW,Uri.parse("http://androiddevelopersstudio.blogspot.in"));
  startActivity(in);
 }
 });
fbbtn.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.facebook.com"));
startActivity(intent);
}
});
}
}

Output :-

Implicit Intent
Implicit Intent fig :1

Implicit Intent
Implicit Intent fig:2




















Implicit Intent fig :3

Android Intents

An Intent is an informing object you can use to ask for an activity from another application segment. In spite of the fact that goals encourage correspondence between parts in a few routes, there are three major utilize cases are begin a movement, begin an administration, convey a communicate.

Intents looks like :

Intent

 There are two types of android intents :

1) Implicit Intent :-

Understood Intent doesn't specify the segment. In such case, plan gives data of accessible parts gave by the framework that will be summoned.  It does not require multiple screens.

Syntax : - By using Intent class
                Intent in=new  
                Intent(Intent.Action_view,Url.parse("http://androiddevelopersstudio.blogspot.in"));      
                startActivity(in);

2) Explicit Intent :- 

Unequivocal purposes indicate the segment to begin by name (the completely qualified class name). You'll normally utilize an unequivocal plan to begin a part in your own particular application, since you know the class name of the movement or administration you need to begin. For instance, begin another action because of a client activity or begin an administration to download a document out of sight.

Syntax: -  By using Intent class
               Intent in=new Intent(Currentclass.this,TargetActivity.class);
               startActivity(in);
Explicit Intent


Monday, 17 October 2016

Android Activity LifeCycle

Activity means an action speaks to a solitary screen with a UI(User Interface). And its supports of java code .
There are seven different android activity lifecycle are described below .

1) onCreate() :-

Called when the movement is initially made. This is the place you ought to do the greater part of your    ordinary static set up: make sees, tie information to records, and so on.

2) onRestart() : -

Called after your action has been halted, preceding it being begun once more.

3) onStart() : -

Called when the movement is getting to be obvious to the client. Taken after by onResume() if the action goes to the frontal area, or onStop() on the off chance that it gets to be covered up.

4) onResume() : -

Called when the action will begin interfacing with the client. Now your movement is at the highest point of the action stack, with client input going to it.

5) onPause () : -

Called as a major aspect of the movement lifecycle when an action is going away from plain sight, yet has not (yet) been slaughtered.

6) onStop() : -

Called when you are no more extended unmistakable to the client. You will next get either onRestart(), onDestroy(), or nothing, contingent upon later client movement.

7) onDestroy() : -

The last call you get before your action is decimated. This can happen either on the grounds that the movement is completing (somebody called complete() on it, or in light of the fact that the framework is incidentally wrecking this occasion of the action to spare space. You can recognize these two situations with the isFinishing() strategy.

        Diagram or flow chart of Android Activity LifeCycle shown below .

Android Activity LifeCycle
Android Activity LifeCycle Figure

Code for Activity LifeCycle :-

Here in the code we are using "TOAST" (or) we can use "Log.d" content display in the Logcat. If we  want to try Log.d and replace in place of Toast. For 

       Log.id("Activity lifecycle", onCreate()");

package com.kiranapp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import android.view.Menu;

public class MainActivity extends AppCompatActivity {

@Override protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 Toast.makeText(MainActivity.this,"onCreate() Method",Toast.LENGTH_SHORT);
 }

@Override protected void onStart() {
 super.onStart();
 Toast.makeText(MainActivity.this,"onStart() Method",Toast.LENGTH_SHORT);
 }
 
@Override protected void onResume() {
 super.onResume();
 Toast.makeText(MainActivity.this,"onResume() Method",Toast.LENGTH_SHORT);
 }

@Override protected void onPause() {
 super.onPause();
 Toast.makeText(MainActivity.this,"onPause() Method",Toast.LENGTH_SHORT);
 }

@Override protected void onStop() {
 super.onStop();
 Toast.makeText(MainActivity.this,"onStop() Method",Toast.LENGTH_SHORT);
 }

@Override protected void onRestart() {
 super.onRestart();
 Toast.makeText(MainActivity.this,"onRestart() Method",Toast.LENGTH_SHORT);
 }
 
@Override protected void onDestroy() {
 super.onDestroy();
 Toast.makeText(MainActivity.this,"onDestroy() Method",Toast.LENGTH_SHORT);
 }
}