Saturday, 2 January 2016

Dailogs and Fragments

01:37

Dialog:

A dialog  is a smaller window that pops up to interact with the user. it can display important messages and can even prompt for some data.
once the interaction with the dialog over,the dialog will disappears.
Fragment:
Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities

lets go for practice...
==========================================================================================================
Alert Dialog  example 1
===================================================== 

(1)write  code  for     MainActivity.java   :
package spinner.test.om.com.myapplication;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity  implements  OnClickListener {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        Button b=(Button)findViewById(R.id.
click_btn);
        b.setOnClickListener(
this);

    }


   
@Override
   
public void onClick(View view) {
        AlertDialog.Builder  alertDialog=
new AlertDialog.Builder(this);
        alertDialog.setTitle(
"Alert Window");
        alertDialog.setIcon(R.drawable.
icon);
        alertDialog.setMessage(
"this is an alert");
        alertDialog.setPositiveButton(
"Ok", new DialogInterface.OnClickListener() {
           
@Override
           
public void onClick(DialogInterface dialogInterface, int i) {
               
return;
            }
        });

        alertDialog.show();
    }
}

(2)write  code for  activity_main.xml file:
<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

    android:layout_height="match_parent" android:fitsSystemWindows="true"

    tools:context=".MainActivity">



    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"

        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"

            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"

            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />



    </android.support.design.widget.AppBarLayout>



    <include layout="@layout/content_main" />



    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"

        android:src="@android:drawable/ic_dialog_email" />



</android.support.design.widget.CoordinatorLayout>

(3)write code content_main.xml   file:
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_main" tools:context=".MainActivity">



    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Click for Alert Dialog"

        android:id="@+id/click_btn"

        android:layout_centerVertical="true"

        android:layout_centerHorizontal="true" />

</RelativeLayout>


 output:

after click on button you will see alert Dialog:

==========================================================================================================
Alert Dialog  example 2
===================================================== 
(1)write   code   for      MainActivity.java :

package spinner.test.om.com.myapplication;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity  implements  OnClickListener {
    TextView 
resp;
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        Button b=(Button)findViewById(R.id.
click_btn);
        b.setOnClickListener(
this);
       
resp=(TextView)findViewById(R.id.textView);


    }


   
@Override
   
public void onClick(View view) {
        AlertDialog.Builder  alertDialog=
new AlertDialog.Builder(this);
        alertDialog.setTitle(
"Alert Window");
        alertDialog.setIcon(R.drawable.
icon);
        alertDialog.setMessage(
"Enter your Name");
        
final EditText username=new EditText(this);
        alertDialog.setView(username);
        alertDialog.setPositiveButton(
"Ok", new DialogInterface.OnClickListener() {
           
@Override
           
public void onClick(DialogInterface dialogInterface, int i) {
                String str=
username.getText().toString();
               
resp.setText("welcome"+str+"!");
               
return;
            }
        });

        alertDialog.setNegativeButton(
"Cancel",new DialogInterface.OnClickListener(){

            
public void onClick(DialogInterface dialogInterface,int i)
            {
               
return;
            }
        });

        alertDialog.show();
    }
}

(2)write code for  activity_main.xml    file:
<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

    android:layout_height="match_parent" android:fitsSystemWindows="true"

    tools:context=".MainActivity">



    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"

        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"

            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"

            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />



    </android.support.design.widget.AppBarLayout>



    <include layout="@layout/content_main" />



    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"

        android:src="@android:drawable/ic_dialog_email" />



</android.support.design.widget.CoordinatorLayout>

(3)write code for content_main.xml   file:
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_main" tools:context=".MainActivity">



    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Click for Alert Dialog"

        android:id="@+id/click_btn"

        android:layout_centerVertical="true"

        android:layout_centerHorizontal="true" />



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceLarge"

        android:text="Large Text"

        android:id="@+id/textView"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="67dp" />

</RelativeLayout>

 output :


after click on  alert it will ask for enter your name:


after click  on ok it will return:
A

Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 comments:

Post a Comment

 

© 2013 ANDROID TRAINING IN MUMBAI BY OM SIR. All rights resevered. Designed by Templateism

Back To Top