Monday 12 December 2016

AndroidPhoneCall

Android gives Built-in applications to telephone calls, in a few events we may need to make a telephone call through our application. This should effectively be possible by utilizing verifiable Intent with fitting activities. Likewise, we can utilize PhoneStateListener and Telephony Manager classes, keeping in mind the end goal to screen the adjustments in some communication states on the gadget.

In AndroidMainfest.XML, we should add uses-permission  for Phone Call State as shown below,

<uses-permission android:name="android.permission.CALL_PHONE" />

Activity_main.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:textAlignment="center">
 
<TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:text="Android PHONE Call " 
 android:id="@+id/textView" 
 android:layout_gravity="center_horizontal" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="call" 
 android:layout_marginTop="20dp" 
 android:id="@+id/button" 
 android:layout_gravity="center_horizontal" />

<EditText 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:inputType="phone" 
 android:ems="10" 
 android:layout_marginTop="20dp" 
 android:id="@+id/editText" 
 android:layout_gravity="center_horizontal" />
</LinearLayout> 

MainActivity.java :-

package com.kiranapp;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    Button callbtn;
    EditText ebtn;

@Override 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 callbtn = (Button) findViewById(R.id.button);
 ebtn = (EditText) findViewById(R.id.editText);
 callbtn.setOnClickListener(new View.OnClickListener() {
 
@Override 
 public void onClick(View v) {
 String s1 = ebtn.getText().toString();
 Intent callIntent = new Intent(Intent.ACTION_CALL);
 callIntent.setData(Uri.parse("tel:" + s1));
 if (ActivityCompat.checkSelfPermission(MainActivity.this,
         Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling  // 
 ActivityCompat#requestPermissions 
 // here to request the missing permissions, and then overriding // 
 public void onRequestPermissionsResult(int requestCode, String[] permissions,
 // int[] grantResults) 
 // to handle the case where the user grants the permission. See the documentation 
 // for ActivityCompat#requestPermissions for more details. //
 return;
 }
 startActivity(callIntent);

 }
 });

 }

 }

Output :-

Android phone call
Android PhoneCall figure

Sunday 11 December 2016

Android Wifi

Android WiFi manager can manage WiFi connectivity. And  it can be used as enable WiFi , disable WiFi and also add network or scanning for access points.

In Android manifest.XML we should add the "uses-permission". i.e shown below,

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" /> 

Activity_main.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:textAlignment="center">
 
<TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:text="Android Wifi " 
 android:id="@+id/textView" 
 android:layout_gravity="center_horizontal" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Enable Wifi" 
 android:layout_marginTop="20dp" 
 android:id="@+id/button" 
 android:layout_gravity="center_horizontal" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Disable Wifi" 
 android:layout_marginTop="20dp" 
 android:id="@+id/button1" 
 android:layout_gravity="center_horizontal" />

</LinearLayout>


MainActivity.java :-

package com.kiranapp;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button enablebtn,disablebtn;

@Override 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 enablebtn = (Button) findViewById(R.id.button);
 disablebtn = (Button) findViewById(R.id.button1);
 enablebtn.setOnClickListener(new View.OnClickListener() {
@Override 
 public void onClick(View v) {
 WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
 wifi.setWifiEnabled(true);

 }
 });
 disablebtn.setOnClickListener(new View.OnClickListener() {
@Override 
 public void onClick(View v) {
 WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
 wifi.setWifiEnabled(false);
 }
 });
 }
 }

Output :-

Android wifi disable mode
Android WiFi fig :1







             Android WiFi Disable mode












Android wifi enable mode
Android WiFi fig :2



                Android WiFi Enable mode

SpeechToText

 Android Speech To Text its similar to Android Text To Speech, but here in this code reverse to text to speech and shown example code below.

Activity_main.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:textAlignment="center">
<TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:text="Android SpeechToText " 
 android:id="@+id/textView" 
 android:layout_gravity="center_horizontal" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="speak" 
 android:layout_marginTop="20dp" 
 android:id="@+id/button" 
 android:layout_gravity="center_horizontal" />

<EditText 
 android:layout_width="124dp" 
 android:layout_height="wrap_content" 
 android:id="@+id/editText" 
 android:layout_gravity="center_horizontal" 
 android:hint="input" />

</LinearLayout>

MainActivity.java :-

package com.kiranapp;

import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    Button speak;
    TextView input;
@Override 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 speak=(Button)findViewById(R.id.button);
 input=(TextView)findViewById(R.id.textView);
 speak.setOnClickListener(new View.OnClickListener() {
@Override 
 public void onClick(View v) {
 Intent intent=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,"en_US");
 try {
 startActivityForResult(intent,1);
 }
 catch (Exception e) {
 e.printStackTrace();
 Toast.makeText(MainActivity.this, "Dude Your phone does not 
                            support Speech to Text",Toast.LENGTH_SHORT).show();
 }
 }
 });
 }

@Override 
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 if(requestCode==1){
 if(resultCode==RESULT_OK&&data!=null) {
 ArrayList<String> al=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
 input.setText(al.get(0));
 }
 }
 super.onActivityResult(requestCode, resultCode, data);
 }
 }

Output :-

Android Speech To Text
Android Speech To Text

TextToSpeech

 Android is providing Text To Speech and its speaks many different languages with different names, country or others.

Activiy_main.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:textAlignment="center">


<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="speak" 
 android:id="@+id/button" 
 android:layout_gravity="center_horizontal" />

<EditText
 android:layout_width="124dp" 
 android:layout_height="wrap_content" 
 android:id="@+id/editText" 
 android:layout_gravity="center_horizontal" 
 android:hint="input" />

</LinearLayout>

MainActivity.java :- 

package com.kiranapp;

import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {
Button speakbtn;
EditText input;
TextToSpeech tts;

@Override protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 speakbtn=(Button)findViewById(R.id.button);
 input=(EditText)findViewById(R.id.editText);
 tts=new TextToSpeech(getApplicationContext(), new 
                                  TextToSpeech.OnInitListener() {
@Override 
 public void onInit(int status) {
 Toast.makeText(MainActivity.this, "Text to Speech engine is Intialized",
                                Toast.LENGTH_SHORT).show();
 }
 });
 speakbtn.setOnClickListener(new 
                              View.OnClickListener() {
@Override public void onClick(View v) {
 String name=input.getText().toString();
 String s1="Android";
 tts.speak(s1,RESULT_OK,null);
 tts.setLanguage(Locale.UK);
 }
 });
 }
}

Output :-

Android Text To Speech
Android Text To Speech

Monday 28 November 2016

Android VideoPlayer

The Android mixed media system incorporates bolster for playing assortment of regular media sorts, so you can without much of a stretch coordinate sound, video and pictures into your applications. You can play sound or video from media documents put away in your application's assets (crude assets), from standalone records in the filesystem, or from an information stream landing over a system association, all utilizing MediaPlayer APIs.

And here the code is Android video player shown below.

NOTE :-  Under Resources create one directory i.e raw folder and paste one video it.
  
              res----->raw----> movie.mp4 or movie.wmv.  

activity_main.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">

<VideoView
 android:id="@+id/videoView1" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_alignParentLeft="true" 
 android:layout_centerVertical="true" />
</LinearLayout>

MainActivity.java :-

package com.kiranapp;

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity  {

VideoView videoview;

@Override 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 videoview=(VideoView)findViewById(R.id.videoView1);
 videoview.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+
                                                                 R.raw.movie));
 videoview.setMediaController(new MediaController(this));
 videoview.start();
 }
}
 
 
Output :-

Android VideoPlayer
Android VideoPlayer



Android AudioPlayer

The Android mixed media system incorporates bolster for playing assortment of regular media sorts, so you can without much of a stretch coordinate sound, video and pictures into your applications. You can play sound or video from media documents put away in your application's assets (crude assets), from standalone records in the filesystem, or from an information stream landing over a system association, all utilizing MediaPlayer APIs.

And here i am showing example for Android Audio player.

activity_main.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">

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="PLAY" 
 android:id="@+id/button" 
 android:layout_gravity="center_horizontal" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="PAUSE" 
 android:id="@+id/button2" 
 android:layout_gravity="center_horizontal" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="STOP" 
 android:id="@+id/button3" 
 android:layout_gravity="center_horizontal" />
</LinearLayout>

MainActivity.java :-

package com.kiranapp;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements
                                                View.OnClickListener {
Button playbtn,pausebtn,stopbtn;
MediaPlayer mp;

@Override 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 playbtn=(Button)findViewById(R.id.button);
 pausebtn=(Button)findViewById(R.id.button2);
 stopbtn=(Button)findViewById(R.id.button3);
 playbtn.setOnClickListener(this);
 pausebtn.setOnClickListener(this);
 stopbtn.setOnClickListener(this);
   }

@Override 
 public void onClick(View v) {
 switch (v.getId()){
 case R.id.button:
 mp.start();
 break;
 case R.id.button2:
 mp.pause();
 break;
 case R.id.button3:
 mp.stop();
 mp=MediaPlayer.create(getApplicationContext(),R.raw.dhruva);
    }
}

Output :-

Android Audio
Android Audio Player