Menus

Wednesday, 29 June 2016

Running State Changing Example


Different States of an Android Activity




Methords


 @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, "onStart");
    }


    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume");
    }


    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG, "onPause");
    }


    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, "onStop");
    }


    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i(TAG, "onRestart");
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy");
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.i(TAG, "onSaveInstanceState");
    }


    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.i(TAG, "onRestoreInstanceState");
    }



If you have abstract methods to implement, press Alt+Enter over class name to open a wizard. Otherwise, for overriding a function, just start typing the name of your method (without any access attribute or type), anywhere in your class scope. Then, Android Studio should suggest you overridable methods.


-------------------------------------------------------------------------------------------------------------------------

Method Overriding in Java

If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in java.
In other words, If subclass provides the specific implementation of the method that has been provided by one of its parent class, it is known as method overriding.

Usage of Java Method Overriding

  • Method overriding is used to provide specific implementation of a method that is already provided by its super class.
  • Method overriding is used for runtime polymorphism

Rules for Java Method Overriding

  1. method must have same name as in the parent class
  2. method must have same parameter as in the parent class.
  3. must be IS-A relationship (inheritance).
-------------------------------------------------------------------------------------------------------------------------



Example of adding different activity in my android application


package com.example.root.myfirstandroidapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.util.Log;


public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MyActivity";

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG,"onCreate");
    }

    @Override    protected void onStart() {
        super.onStart();
        Log.i(TAG, "onStart");
    }


    @Override    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume");
    }


    @Override    protected void onPause() {
        super.onPause();
        Log.i(TAG, "onPause");
    }


    @Override    protected void onStop() {
        super.onStop();
        Log.i(TAG, "onStop");
    }


    @Override    protected void onRestart() {
        super.onRestart();
        Log.i(TAG, "onRestart");
    }


    @Override    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy");
    }


    @Override    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.i(TAG, "onSaveInstanceState");
    }


    @Override    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.i(TAG, "onRestoreInstanceState");
    }


    public void onButtonClick(View V){

        EditText e1 = (EditText)findViewById(R.id.editText);
        EditText e2 = (EditText)findViewById(R.id.editText2);
        EditText e3 = (EditText)findViewById(R.id.editText3);

        int num1 = Integer.parseInt(e1.getText().toString());
        int num2 = Integer.parseInt(e2.getText().toString());

        int sum = num1+num2;

       
        e3.setText(Integer.toString(sum));

       
    }

}


Log details of above code run


06-29 11:12:48.872 2996-2996/com.example.root.myfirstandroidapplication I/MyActivity: onCreate
06-29 11:12:48.872 2996-2996/com.example.root.myfirstandroidapplication I/MyActivity: onStart
06-29 11:12:48.872 2996-2996/com.example.root.myfirstandroidapplication I/MyActivity: onResume
06-29 11:28:18.959 2996-2996/com.example.root.myfirstandroidapplication I/MyActivity: onPause
06-29 11:28:19.037 2996-2996/com.example.root.myfirstandroidapplication I/MyActivity: onStop
06-29 11:28:19.037 2996-2996/com.example.root.myfirstandroidapplication I/MyActivity: onDestroy






Friday, 24 June 2016

Running a Simple App


Setting up your projects in Android Studio



In this section first explain to setting up your projects on Android Studio.

The first step in the application development process is to create a new project within the Android Studio environment.

First open android studio and goto menu File -> New -> NewProject


New Project 

Once this windows appears, Android studio is ready for a new project to be crated. Fill the all field in new project window.




Target Adroid Studio

This window help to select the form factors your app will run on.

The minimum SDK version determines the lowest level of Android that your app will run on.






Add an Activity to Mobile


The next step is to define the type of initial activity that is to be created for the application.
A range of different activity types is available when developing Android applications. 
simply select the option to create a Blank Activity.






Customize the Activity






Finally, click on Finish to initiate the project creation process.


User Interface Designer tool


This window is used to create user interface and add or edit code. 




Application test to run for click to run button




Running a simple APP


Android Application Add two numbers Calculator adding two numbers in android 

public void onButtonClick(View v){

        EditText e1 = (EditText)findViewById(R.id.editText);
        EditText e2 = (EditText)findViewById(R.id.editText2);
        TextView t1 = (TextView)findViewById(R.id.textView4);


        int num1  = Integer.parseInt(e1.getText().toString());
        int num2  = Integer.parseInt(e2.getText().toString());

        int sum = num1 + num2;

        t1.setText(Integer.toString(sum));

        
    }

}

















Thursday, 23 June 2016

What is Android


What is Android

Android is an open source operating system, created by Google specifically for use on mobile devices (cell phones and tablets)

Linux based (2.6 kernel)

Can be programmed in C/C++ but most app development is done in Java (Java access to C Libraries via JNI (Java Native Interface))

Supports Bluetooth, Wi-Fi, and 3G and 4G networking

Android has a large number of applications available for download in Google play store. Around 1 million apps are published and over 50 billion downloads.

 According to survey conducted in April-May 2013 found that Android is the most popular platform for developers .It is used by almost 71% of the mobile developer’s population.

Google Proudly announced that 1 billion activated devices now use the Android OS worldwide.

Android was founded by  Andy Rubin(co-founder of Danger),

Rich Minner(co-founder of wild-fire communications)and

Chris White(headed design and interface development  at WebTV).

Android Application Life Cycle

The Android application life cycle is unique in that the system controls much of the life cycle of the application. All Android applications, or Activities, are run within their own process. All of the running  processes are watched by Android and, depending on how the activity is running (this is, a foreground activity, background activity, and so forth),Android may choose to end the activity to reclaim needed resources.
An activity represents a single screen with a user interface just like window or frame of Java.Android activity is the subclass of ContextThemeWrapper class.
If you have worked with C, C++ or Java programming language then you must have seen that your program starts from main() function. Very similar way, Android system initiates its program with in an Activity starting with a call ononCreate() callback method. There is a sequence of callback methods that start up an activity and a sequence of callback methods that tear down an activity as shown in the below Activity life cycle diagram:





Some of the specific methods and description

onCreate()      This is the first callback and called when the activity is first created.

onStart()         This callback is called when the activity becomes visible to the user.

onResume()     This is called when the user starts interacting with the application.

onPause()        The paused activity does not receive user input and cannot execute any code and                                   called when the current activity is being paused and the previous activity is being                                 resumed.

onStop()          This callback is called when the activity is no longer visible.

onDestroy()     This callback is called before the activity is destroyed by the system.

onRestart()       This callback is called when the activity restarts after stopping it.





























Wednesday, 22 June 2016

Develop Android Application

Android software development


Android software development is the process by which new applications are created for the Android operating system. Applications are usually developed in Java programming language using the Android software development kit (SDK), but other development environments are also available.


Integrated development environment (IDE) 

Eclipse using the Android Development Tools (ADT) Plugin.

NetBeans IDE also supports Android development via a plugin.

Android Studio is the official integrated development environment (IDE) for Android platform development.


Java download

http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html


Installing Android Studio


Android Studio provides the fastest tools for building apps on every type of Android device.
World-class code editing, debugging, performance tooling, a flexible build system, and an instant build/deploy system all allow you to focus on building unique and high quality apps.

https://developer.android.com/studio/index.html












Monday, 25 April 2016

Yii2 Grid View Operations


INDEX
Adding Classes to Rows in the GridView
Grid view action column change
Yii2 Gridview merge two columns
Yii2 Gridview change column background and text color



Gridview In Yiiframework 2.0


In yiiframework 2.0, The gridview widget is used to display the data like image, text, etc in a grid. It is having a lot features like sorting, pagination, filtering, styling etc.


Adding Classes to Rows in the GridView

Yii2 Gridview row by row css expression



Dynamically change GridView Row Background Color based on condition 


<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
     
       //Check conditions and add to row color in gridview.
        'rowOptions' => function ($model){
          if($model->status == 0){
            return ['class' => 'danger'];
          }else{
            return ['class' => 'success'];
          }
        },
             
        'columns' => [          
            [
                'header' =>'Sl.No',
                'class' => 'yii\grid\SerialColumn',            
                'options' => ['width' => '50']
            ],          
            'eid',          
            [
                'attribute' => 'expence_date',
       'format' =>  ['date', 'php:d-m-Y'],
       'options' => ['width' => '150'],              
   ],
            'pay_to',
            'description',          
            [                              
                            'attribute'=>'head',
                            'content' => function($data){
                                        return Groupitems::getGroupitems($data->head);
                                        }
                            ],
            'amount',
            [        
                'class' => 'yii\grid\ActionColumn',
                'contentOptions' => ['style' => 'width:60px;'],
                'header'=>'Actions',
            ],
        ],
    ]); ?>



Output of above script







Grid view action column change


How to change view, update and delete url on action column in yii2


[
          'class' => 'yii\grid\ActionColumn',
          'header' => 'Actions',
          'headerOptions' => ['style' => 'color:#337ab7'],
          'template' => '{view}{update}{delete}',
          'buttons' => [
            'view' => function ($url, $model) {
                return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, [
                            'title' => Yii::t('app', 'lead-view'),
                ]);
            },

            'update' => function ($url, $model) {
                return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, [
                            'title' => Yii::t('app', 'lead-update'),
                ]);
            },
            'delete' => function ($url, $model) {
                return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, [
                            'title' => Yii::t('app', 'lead-delete'),
                ]);
            }

          ],
          'urlCreator' => function ($action, $model, $key, $index) {
            if ($action === 'view') {
                $url ='index.php?r=client-login/lead-view&id='.$model->id;
                return $url;
            }

            if ($action === 'update') {
                $url ='index.php?r=client-login/lead-update&id='.$model->id;
                return $url;
            }
            if ($action === 'delete') {
                $url ='index.php?r=client-login/lead-delete&id='.$model->id;
                return $url;
            }

          }
          ],

Example 1

I want to gridview action column only show edit and view button and change to url.




Gridview code here


    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [

        ....
        .....                                  

        [
          'class' => 'yii\grid\ActionColumn',
          'header' => 'Actions',
          'headerOptions' => ['style' => 'color:#337ab7'],
          'template' => '{view}{update}',
          'buttons' => [
            'view' => function ($url, $model) {
                return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, [
                            'title' => Yii::t('app', 'view-student'),
                ]);
            },

            'update' => function ($url, $model) {
                return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, [
                            'title' => Yii::t('app', 'update-register number'),
                ]);
            },
            

          ],

          'urlCreator' => function ($action, $model, $key, $index) {
            if ($action === 'view') {                
                $url = 'index.php?r=master/students/view&ccid='.$model->ccid.'&admission_number='.$model->admission_number;
                return $url;
            }

            if ($action === 'update') {
                $url = 'index.php?r=master/students/addregisternochange&ccid='.$model->ccid.'&admission_number='.$model->admission_number;
                return $url;
            }
            
          }
          ],                            
                                        

    ], 
                                 
                                 
    ]); ?>




Example 1

This example gridview to show only view and edit without change url.


            [          
                'class' => 'yii\grid\ActionColumn',
                'contentOptions' => ['style' => 'width:50px;'],
                'header'=>'Actions',
                'template' => '{view} {update}',
            ], 



Yii2 Gridview merge two columns


//add columns in gridview

            [
                'label'=>'Rem/TotFee',
                'value' => function($model) { return $model->remitted_fee.'/'.$model->total_fee ;}
                 
            ],



Yii2 Gridview change column background and text color

Text colur

[
                'attribute'=>'student_name',
                'contentOptions' => function ($model, $key, $index, $column) {
                    if($model->employee_teachersid)            
                                return ['style' => 'color:green'];
                    else
                                return ['style' => 'color:black'];
                }
            ],

Background color

        [
                'label'=>'Rem/TotFee',
                'value'=> function($data)
                  {  
                      return  $data->remitted_fee.'/'.$data->total_fee;              
                  },
                          
                'contentOptions' => function ($model, $key, $index, $column) {
                                             
                        if($model->remitted_fee < $model->total_fee){
                           return ['style' => 'background-color:red'];     
                        }else{
                            return ['style' => 'background-color:lime'];
                        }
                        
                },          
                'format' => 'raw'              
            ], 


Gridview column text with badge

[
        'attribute'=>'color',
        'value'=>function ($model, $key, $index, $widget) {
            return "<span class='badge' style='background-color: {$model->color}'> </span>  <code>" . 
                $model->color . '</code>';
        },
        'filterType'=>GridView::FILTER_COLOR,
        'vAlign'=>'middle',
        'format'=>'raw',
        'width'=>'150px',
        'noWrap'=>true
    ],


Grid view column color with value


'value'=>function ($model, $key, $index, $widget) 
                  {  
                      
                            $d =  'Text here';
                            $color="red";
                            return "<td  style='background-color: {$color}'> {$d}</td> " ;
                          
                  },





Related Links

Yii2 gridview widget filter

YII2 Grid View Advanced Options

Yii2 grid with Editable column

Importing data from Excel Sheet