Sunday, 21 May 2017

Android ProgressBar

Android ProgressBar

We can display the android progress bar dialog box to dislay the status of work being done e.g. downloading file, analyzing status of work etc.

Here, I am going to display the progress dialog for dummy file download operation.

Here I am using android.ap.ProgressDialog class to show the progress bar, Android ProgressDialog is the subclass of AlertDialog class.

The ProgressDialog class provides method to work on progress bar like setProgress(), setMessage(), setProgressStyle(), show() etc. The progress range of Progress Dialog Is 0 to 10000.

How to define ProgressDialog

ProgressDialog progressBar=new ProgressDialog(this);

progressBar.setCancelable(true); //you can cancel it by pressing back button

progressBar.setMessage(“File downloading…”);

progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

progressBar.setProgress(0); //initially progress is 0

progressBar.setMax(100); //set the maximum value 100

progressBar.show();//dislay the progress bar


Androidissimples: Android TimePicker

Androidissimples: Android TimePicker

Android TimePicker

Android TimePicker

Android TimePicker widget is used to select date.It allows you to select time by hour and minute. You cannot select time by seconds.

The android.widget.TimePicker is the subclass f FrameLayout class.

Android TimePicker Example

Activity_main.xml

File:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="com.mahesh.androidissimples.timepickerdemo.MainActivity">

   <TimePicker
       android:id="@+id/timePicker"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentTop="true"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="50dp"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/timePicker"
        android:layout_alignParentTop="true"
        android:layout_marginTop="10dp"
        android:text="Current Time:"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/timePicker"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="37dp"
        android:text="Change Time"
        android:layout_marginTop="20dp"/>

</RelativeLayout>

Activity Class

File:MainActivity.class

package com.mahesh.androidissimples.timepickerdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

public class MainActivity extends AppCompatActivity {
    TextView textView;
    TimePicker timePicker;
    Button changetime;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        textView= (TextView) findViewById(R.id.textView);
        timePicker= (TimePicker) findViewById(R.id.timePicker);
       
        changetime= (Button) findViewById(R.id.button);
        textView.setText(getCurrentTime());
        changetime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView.setText(getCurrentTime());
            }
        });
    }

    private String getCurrentTime() {
        String currentTime="Current Time:"+timePicker.getCurrentHour()+":"+timePicker.getCurrentMinute();
        return currentTime;
    }
}

Output:




Android SeekBar

Andriod SeekBar

Android SeekBar is a kind of ProgressBar With draggable thumb. The end user can drag the thum left and right to move the progress of song, file download etc.

The SeekBar.OnSeekBarChangeListener interface provides mthods to perform even handling for seek bar.

Android SeekBar and RatingBar classes are the sub classes of AbsSeekBar.

Android SeekBar Example

activity_main.xml

File:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="com.mahesh.androidissimples.seekbardemo.MainActivity">

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="39dp"/>

</RelativeLayout>

Activity class
File:MainActivity.class
package com.mahesh.androidissimples.seekbardemo;

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

public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
    SeekBar seekBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        seekBar= (SeekBar) findViewById(R.id.seekBar1);
        seekBar.setOnSeekBarChangeListener(this);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        Toast.makeText(this, "seekbar progress:"+progress, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        Toast.makeText(this,"seekbar touch started!",Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        Toast.makeText(this, "seekbar touch stopped!", Toast.LENGTH_SHORT).show();

    }
}


Output:

Android WebView

Android WebView

Android WebView is used to display web page in android. The web page can be loaded from same application or URL. It is used to display online content in android activity.

Android Web View uses webkit engine to display web page.

The android.webkit.WebView is the subclass of AbsoluteLayout class.
The loadUrl() and loadData() method of Android WebView class are used to load and display web page.

Android WebView Example

activity_main.xml

File:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 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"
    tools:context="com.mahesh.androidissimples.webviewdemo.MainActivity">

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">

    </WebView>

</RelativeLayout>

Activity class

File:MainActivity.java

package com.mahesh.androidissimples.webviewdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView mywebview= (WebView) findViewById(R.id.webView1);

        mywebview.loadUrl("http://www.google.com");
    }

}

Androidissimples: Android ToggleButton

Androidissimples: Android ToggleButton

Androidissimples: Android ToggleButton

Androidissimples: Android ToggleButton

Androidissimples: Activity and its Lifecycle

Androidissimples: Activity and its Lifecycle

Activity and its Lifecycle

                                              Activity and its Lifecycle

What is an Activity?

Which present something to you with which you can interact there call Activity.An application component that provides a screen

What they do.

You can draw different type of UI on its windows.Every app has 1 main activity and other activities.An ap can start any Activity belonging to certain conditions.When a new Activity starts,the previous Activity is stopped and added to stack knows as BackStack.

What is a callback method?

Android OS calls certain method on your Activity class to notify whether your app is running currently or not.
Just like JVM calling public static void main

General guidelines

1.       Don’t do heavy processing or network consuming operations when user is currently away from your app.
2.       App should not crash when another app is started.
3.       Don’t lose the user’s progress or session data.

Activity Lifecycle Methods

When user launch your apps for the first time now the user can launch your app from the app section or he can launch through widget or whatever so what happen when the user click your app icon. These three methods are called onCreate (), onStart (), onResume ().What happens when the user actually try to pause your app. So in this case onPause (), onStop () method are called. What happens when the user click the back button. So in this case these methods are called onRestart (), onStart (), onResume () in a quick.

What is Logcat?

Logcat is used for debugging purposes. We will use the logcat in activity to check the Activity Lifecycle Method. Logcat Print different messages using android.util.Log class.

Log.d (String tag, String message)
Log.d (“LIFECYCLE”, ”onCreate was called”);

There are other method in Logcat
1.     For information we can use Log.i(String tag, String message)
2.     For Error we can use Log.e(String tag, String message)
3.     For warning we can use Log.w(String tag, String message)
4.     For Verbose we can use Log.v(String tag, String message)


Activity Lifecycle Diagram


Example:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("Android is simples","onCreate was called");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("Android is simples","onResume was called");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("Android is simples","onStart was called");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("Android is simples","onPause was called");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("Android is simples","onStop was called");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("Android is simples","onRestart was called");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("Android is simples","onDestroy was called");
    }
}

Android Button

Android Button

Android Button represent a push-button. The android.widget.Button is subclass of TextView class and CompoundButton is the subclass of Button class.

There are different types of buttons in android such as RadioButton, ToggleButton, CompoundButton etc.

Here,we are goig to create two textfield and one button for sum of two numbers. If user clicks button, sum of two input values is displayed on the Toast.

Now write the code to generate the UI components

File:activity_main

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/number1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:hint="Enter First number"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="20dp"/>

    <EditText
        android:id="@+id/number2"
        android:layout_below="@+id/number1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:hint="Enter Second number"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="20dp"/>

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add"
        android:layout_marginEnd="20dp"
        android:layout_marginStart="20dp"
        android:onClick="addition"
        android:layout_marginTop="15dp"
        android:layout_below="@+id/number2"/>

</RelativeLayout>

Activity class

Now write the code to display the sum of two numbers.

File:MainActivity.java

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

public class MainActivity extends AppCompatActivity {
    Button b1;
    EditText et1,et2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et1= (EditText) findViewById(R.id.number1);
        et2= (EditText) findViewById(R.id.number2);
        b1= (Button) findViewById(R.id.button);

    }



    public void addition(View view) {
        String n1=et1.getText().toString();
        String n2=et2.getText().toString();
        int a=Integer.parseInt(n1);
        int b=Integer.parseInt(n2);
        int sum=a+b;
        Toast.makeText(this, String.valueOf(sum), Toast.LENGTH_SHORT).show();
    }
}

Output:

Toast Class

Toast Class

Toast class is used to show notification for a particular interval of time. After sometime it disappears. It doesn’t block the user interaction.

Constants of Toast Class

There are only 2 constants of Toast class which are given below.
Constant
Description
Public static final int LENGTH_LONG
displays view for the long duration of time.
Public static final int LENGTH_SHORT
Displays view for the short duration of time.

Methods of Toast class

The widely used method of Toast class are given below.

Method
Description
Public static Toast makeText(Context context, CharSequence text, int duration)
Makes the toast containing text and duration.
Public void show().
Displays
Public void setMargin(float horizontalMargin,float verticalMargin)
Changes the horizontal and vertical margin difference.
Public void setGravitiy(Gravity.LEFT,0,0)
Change the gravity of the Toast

Android Toast Example

Toast.makeText(getApplicationContext(),”Hello Android is simples”,Toast.LENGTH_SHORT).show().

Another Code:

Toast toast=Toast.makeText(getAplicationContext(),”Hello Android is Simles”,Toast.LENGTH.SHORT);
Tast.setMargin(50,50);
Toast.show();

Here, getAplicationContext() method return the instance of Context.