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

Wednesday 16 November 2016

SplashScreen

Android application sets aside some opportunity to fire up, particularly the first run through the application is keep running on a gadget (here and there this is alluded to as a chilly begin). The sprinkle screen may show start up advance to the client, or it might show marking data to distinguish and advance the application.

And few steps to know what in splash screen,

1) we have to create one image file (i.e jpg file) resources --> drawable--> splash.jpg(upload or paste).
2) We should create one java class and xml files in the project as shown in code.
3) And Android mainfest.xml we should write the as shown below example,

<activity 
 android:name=".SplashScreen" 
 android:label="@string/app_name" >
<intent-filter>
 <action  
android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity 
 android:name=".MainActivity" 
 android:label="@string/app_name" >
<intent-filter>
<action android:name="com.coderefer.androidsplashscreenexample.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
 
 
code for android splash screen as shown 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">
</LinearLayout>
 
splash.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:background="@drawable/splash">

</LinearLayout>
 
MainActivity.java :-
 
package com.kiranapp;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

@Override 
 public void onCreate(Bundle icicle) {
 super.onCreate(icicle);
 setContentView(R.layout.activity_main);
}
} 
 
SpalshScreen.java :-
 
package com.kiranapp;

/** * Created by Dell on 11/16/2016. */ 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class SplashScreen extends Activity {

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

 Thread timerThread = new Thread(){
 public void run(){
 try{
 sleep(3000);
 }
catch(InterruptedException e){
e.printStackTrace();
}
finally{
Intent intent = new Intent(SplashScreen.this,MainActivity.class);
startActivity(intent);
  }
 }
};
timerThread.start();
}

@Override 
 protected void onPause() {
 super.onPause();
 finish();
 }
} 
 
AndroidMainfest.xml :- 
 

<?xml version="1.0" encoding="utf-8"?> 
<manifest 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 package="com.kiranapp">

<application 
 android:allowBackup="true" 
 android:icon="@mipmap/ic_launcher" 
 android:label="@string/app_name" 
 android:theme="@style/AppTheme" >

<activity 
 android:name=".SplashScreen" 
 android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity 
 android:name=".MainActivity" 
 android:label="@string/app_name" >
<intent-filter>
<action
 android:name="com.coderefer.androidsplashscreenexample.MAINACTIVITY" />

<category 
 android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>

</manifest>
 
Output :-
 
SplashScreen
SplashScreen figure
 
 

Monday 7 November 2016

AndroidFragmentTransaction

Android Fragment exchange (or) "Fragment Transaction" to handle sections. Android Fragment are valuable when we need to bolster different screen measure. To oversee parts we require a FragmentManager that help us to handle Android piece trasaction between sections. With Android part exchange we mean a succession of ventures to include, supplant or evacuate pieces. In the last post we demonstrated to bolster numerous screen size and introduction utilizing sections.

code for FragmentTransactin shown below.

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.fragmenttransaction.MainActivity">

<Button 

 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="fragA" 
 android:id="@+id/button" 
 android:layout_alignParentTop="true" 
 android:layout_centerHorizontal="true" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="fragB" 
 android:id="@+id/button2" 
 android:layout_below="@+id/button" 
 android:layout_centerHorizontal="true" />

<FrameLayout 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:layout_centerHorizontal="true" 
 android:id="@+id/myframe" 
 android:background="#decaca">
</FrameLayout>
</LinearLayout>

fraga.xml :-

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:orientation="vertical"
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:background="#811313">

<TextView 
 android:layout_width="209dp" 
 android:layout_height="54dp" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:text="this fragment A" 
 android:id="@+id/textView" 
 android:layout_gravity="center_horizontal" />
</LinearLayout>


fragb.xml :-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:orientation="vertical"
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:background="#be6f6f">

<TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:text="fragment B" 
 android:id="@+id/textView2" 
 android:layout_gravity="center_horizontal" />
</LinearLayout>

MainActivity.java :-

package com.fragmenttransaction;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
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 b1,b2;
 FragmentManager fragmentManager;
 FragmentTransaction fragmentTransaction,fragmentTransaction1;


@Override 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 fragmentManager=getFragmentManager();
 b1=(Button)findViewById(R.id.button);
 b2=(Button)findViewById(R.id.button2);
 b1.setOnClickListener(this);
 b2.setOnClickListener(this);
 }

 @Override 
 public void onClick(View v) {
 switch (v.getId()) {
 case R.id.button:
 fragmentTransaction=fragmentManager.beginTransaction();
 fragA fragA=new fragA();
 fragmentTransaction.add(R.id.myframe,fragA);
 fragmentTransaction.commit();
 break;
 case R.id.button2:
 fragmentTransaction1=fragmentManager.beginTransaction();
 frabB frabB=new frabB();
 fragmentTransaction1.add(R.id.myframe,frabB);
 fragmentTransaction1.commit();
 break;
 }
 }
}


FragA.java :-

package com.fragmenttransaction;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/** * Created by Dell on 7/11/2016. */
public class fragA extends Fragment {
@Nullable 
@Override 
 public View onCreateView(LayoutInflater inflater, ViewGroup container, 
                                                  Bundle savedInstanceState) {

 return inflater.inflate(R.layout.fraga,container,false);
 }
}

FragB.java :-

package com.fragmenttransaction;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/** * Created by Dell on 7/11/2016. */
 public class frabB extends Fragment {
@Nullable 
@Override 
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                                 Bundle savedInstanceState) {

 return inflater.inflate(R.layout.fragb,container,false);
 }
}


Output :-

AndroidFragmentTransaction
AndroidFragmentTransaction fig:1




















AndroidFragmentTransaction
AndroidFragmentTransaction fig:2




















AndroidFragmentTransaction
AndroidFragmentTransaction fig:3



Thursday 3 November 2016

ExternalStorage

Each Android-perfect gadget bolsters a common "outer stockpiling or External Storage" that you can use to spare records. This can be a removable stockpiling media(Storage media), (for example, a SD card) or an inward (non-removable) capacity. Records spared to the outer stockpiling are world-intelligible and can be changed by the client when they empower USB mass stockpiling to exchange documents(transfer files) on a PC.

Access to external storage  i.e Read or Write files.
In Android manifest.xml we have to include the code.
<manifest>
   - - - - - - - - - - - - - - - - - - - - -  - - - - - - - - - - - -
   - - - - - - - - - - - - - - - - - - - - -  - - - - - - - - - - - -
<uses-permission android : name ="android.permission.WRITE_EXTERNAL_STORAGE/>
<uses-permission android : name ="android.permission.READ_EXTERNAL_STORAGE/>
- - - - - - - - - - - - - - - - - - - - -  - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - -  - - - - - - - - - - - -

</manifest>

In MainActivity.java we have to write code for external storage below shown how it is,

                          //WRITE FILE //
public boolean isExternalStorageWritable() {
    String externalstate = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(externalstate)) {
        return true;
    }
    return false;
}

                         //READ FILE //
public boolean isExternalStorageReadable() {
    String externalstate = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(externalstate) ||
        Environment.MEDIA_MOUNTED_READ_ONLY.equals(externalstate)) {
        return true;
    }
    return false;
}

InternalStorage

Android Internal storage is the capacity of the private information on the gadget memory. Naturally, sparing and stacking documents to the inner stockpiling are private to the application and different applications won't have entry to these records. At the point when the client uninstalls the applications the inward put away documents connected with the application are additionally expelled. Nonetheless, take note of that a few clients root their Android telephones, picking up superuser get to. These clients will have the capacity to peruse and compose whatever records they wish.

For example as shown figure is internal storage in android.

InternalStorage
InternalStorage Figure


code for internal storage 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" 
 android:background="#f3f0f0">

<TextView 
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:text="Welcome to internal storage in android" 
 android:id="@+id/textView" 
 android:layout_marginTop="20dp" 
 android:layout_gravity="center_horizontal" />

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

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Text to write" 
 android:layout_alignTop="@+id/button2" 
 android:id="@+id/button" 
 android:onClick="click text" 
 android:layout_gravity="center_horizontal" />

<Button
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Read from file" 
 android:onClick="click read" 
 android:id="@+id/button2" 
 android:layout_gravity="center_horizontal" />
</LinearLayout>

MainActivity.java :-

package com.kiranapp;

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

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class MainActivity extends AppCompatActivity {
    EditText editText;
   // TextView t1;
  //  Button b1,b2;
    static final int READ_BLOCK_SIZE = 100;
@Override 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
// t1=(TextView)findViewById(R.id.textView);
// b1=(Button)findViewById(R.id.button);
// b2=(Button)findViewById(R.id.button2);
 editText=(EditText)findViewById(R.id.editText);
 }

public void WriteBtn(View v) {
try {
FileOutputStream fileout=openFileOutput("file.txt", MODE_PRIVATE);
OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);
outputWriter.write(editText.getText().toString());
outputWriter.close();
Toast.makeText(getBaseContext(), "saved",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
e.printStackTrace();
}
}
public void ReadBtn(View v) {
try {
FileInputStream fileIn=openFileInput("file.txt");
InputStreamReader InputRead= new InputStreamReader(fileIn);
char[] inputBuffer= new char[READ_BLOCK_SIZE];
String s="";
int charRead;
while ((charRead=InputRead.read(inputBuffer))>0) {
String readstring=String.copyValueOf(inputBuffer,0,charRead);
s +=readstring;
}
InputRead.close();
editText.setText(s);
}
catch (Exception e) {
e.printStackTrace();
 }
}
}

Output :-

InternalStorage
InternalStorage Fig :1

















  


InternalStorage
InternalStorage Fig :2