Showing posts with label Learn Android. Show all posts
Showing posts with label Learn Android. Show all posts

Sunday, 11 December 2016

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

Thursday, 27 October 2016

Services

A Service is an application part that can perform long-running operations out of sight and does not give a UI.
 Another application segment can begin an administration and it will keep on running out of sight regardless of the possibility that the client changes to another application.
Also, a segment can tie to an administration to cooperate with it and even perform interprocess correspondence (IPC).
For instance, an administration may handle arrange exchanges, play music, perform document I/O, or collaborate with a substance supplier, all from the foundation.
It is subclass of ContextWrapperclass.

Example :-  Music player and Alarm.

Services of lifecycle,There are  types of service.

1) Start Service :-

 We can begin an administration from a movement or other application part by passing an Intent (determining the administration to begin) to startService().
The Android framework calls the administration's onStartCommand() technique and passes it the Intent. (You ought to never call onStartCommand() straightforwardly.)

     Syntax :-
                    Using Intent Class
                    Intent in=new Intent(Currentclass.this,TargetClass);
                    startService(in);

2) Stop Service :-

A began benefit must deal with its own particular lifecycle. That is, the framework does not stop or decimate the administration unless it must recuperate framework memory and the administration keeps on pursuing onStartCommand() returns.
 In this way, the administration must stop itself by calling stopSelf() or another part can stop it by calling stopService().

     Syntax :-
                     Using Intent Class
                     Intent in=new Intent(Currentclass.this,TargetClass);
                     stopService(in);

3) Bound Service :-

A bound administration is one that permits application parts to tie to it by calling bindService() keeping in mind the end goal to make a long-standing association (and by and large does not permit segments to begin it by calling startService()).
You ought to make a bound administration when you need to interface with the administration from exercises and different parts in your application or to uncover some of your application's usefulness to different applications, through interprocess correspondence (IPC).

Code For Service in Android :-

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="Start Service" 
 android:id="@+id/button" 
 android:layout_marginTop="20dp" 
 android:layout_gravity="center_horizontal" />
 
<Button  
 android:layout_width="wrap_content"  
 android:layout_height="wrap_content"  
 android:text="Stop Service"  
 android:id="@+id/button"  
 android:layout_marginTop="20dp"  
 android:layout_gravity="center_horizontal" /> 
 
</LinearLayout>

MainActvity.java :-

package com.kiranapp;

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

public class MainActivity extends AppCompatActivity
 {
    Button startserbtn,stopserbtn;

@Override
    protected void onCreate(Bundle savedInstanceState)
   {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startserbtn=(Button)findViewById(R.id.button_start);
    stopserbtn=(Button)findViewById(R.id.button_stop);
    startserbtn.setOnClickListener(new View.OnClickListener()
   {

@Override
    public void onClick(View v)
   {
    Intent in=new Intent(MainActivity.this,MyServices.class);
    startService(in);
    }
    });
   stopserbtn.setOnClickListener(new View.OnClickListener()
  {

@Override
    public void onClick(View v)
   {
    Intent in=new Intent(MainActivity.this,MyServices.class);
    stopService(in);
    }
    });
    }
}

Output :-

                     Run the app on emulator or on mobile phone  

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













Thursday, 6 October 2016

ToggleButton

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" 
 android:paddingLeft="@dimen/activity_horizontal_margin" 
 android:paddingRight="@dimen/activity_horizontal_margin"     
 android:paddingTop="@dimen/activity_vertical_margin" 
 android:paddingBottom="@dimen/activity_vertical_margin" 
 tools:context="com.kiranapp.MainActivity">

 <ToggleButton 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="ToggleButton1" 
  android:id="@+id/toggleButton" 
  android:layout_alignParentTop="true" 
  android:layout_alignParentLeft="true" 
  android:layout_marginLeft="60dp" 
  android:layout_marginTop="18dp" 
  android:textOff="Off" 
  android:textOn="On"         
  android:checked="false" />

 <ToggleButton 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="ToggleButton2" 
  android:layout_alignBaseline="@+id/toggleButton" 
  android:layout_alignBottom="@+id/toggleButton" 
  android:id="@+id/toggleButton2" 
  android:layout_marginLeft="44dp" 
  android:layout_toRightOf="@+id/toggleButton" 
  android:textOff="Off" 
  android:textOn="On"/>

<Button 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="Submit" 
  android:id="@+id/button" 
  android:layout_marginTop="85dp" 
  android:layout_below="@+id/toggleButton" 
  android:layout_centerHorizontal="true" />
</RelativeLayout>
 

MainActivity.java :-

package com.kiranapp;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends Activity {
  private ToggleButton toggleButton, toggleButton2;
  private Button Submit
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

   addListenerOnButtonClick();
 }
 public void addListenerOnButtonClick(){

 toggleButton=(ToggleButton)findViewById(R.id.toggleButton);
 toggleButton2=(ToggleButton)findViewById(R.id.toggleButton2);
 Submit=(Button)findViewById(R.id.button);
 Submit.setOnClickListener(new OnClickListener(){

@Override 
 public void onClick(View view) {
 StringBuilder result = new StringBuilder(); 
 result.append("ToggleButton1 : ").append(toggleButton.getText());
 result.append("\nToggleButton2 : ").append(toggleButton2.getText());

Toast.makeText(getApplicationContext(), result.toString(),Toast.LENGTH_LONG).show();
}

});
}
@Override 
 public boolean onCreateOptionsMenu(Menu menu) {

 getMenuInflater().inflate(R.menu.activity_main, menu);
 return true;
}

}
 
Output :-
 
ToggleButton
ToggleButton figure