Monday, February 15, 2016

Android Image Rotation

Note: Don't forget to create ImageView in your layout xml file with android:id="@+id/imageView1
/*** Rotate image to 45 degrees.*/    public void RotateImage(){
    ImageView image;    Bitmap bMap;    Matrix matrix;
    //Get ImageView from layout xml file    image = (ImageView) findViewById(R.id.imageView1);
    //Decode Image using Bitmap factory.    bMap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    //Create object of new Matrix.    matrix = new Matrix();
    //set image rotation value to 45 degrees in matrix.    matrix.postRotate(45);
    //Create bitmap with new values.    Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0,    bMap.getWidth(), bMap.getHeight(), matrix, true);
    //put rotated image in ImageView.    image.setImageBitmap(bMapRotate);    }
source:http://droid-examplecode.blogspot.pt/2012/01/android-image-rotation_01.html

Sunday, July 12, 2015

Bar charts in Android

The activity_main.xml
<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=".MainActivity">
<LinearLayout     
android:orientation="horizontal"        
android:id="@+id/lcharthart"        
android:layout_width="match_parent"        
android:layout_height="wrap_content"        
android:gravity="bottom"        
android:layout_marginTop="60dp">
<View android:id="@+id/view" 
android:layout_width="35dp" 
android:layout_height="wrap_content" 
android:background="#00FF00">
</View>
</LinearLayout>
</RelativeLayout>

                   The MainActivity.java  is as below 
import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;


public class MainActivity extends Activity  {

     LinearLayout la;
    protected void  onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main);
        la=(LinearLayout) findViewById(R.id.lcharthart);
        int color[]={ 2,2,2,1,1,3,3,3,3};
        int Height[]={100,100,100,200,200,300,300,300,300,200,200,400,400};

        for(int j=0;j<color.length;j++)

        {
            drawChart(1, color[j], Height[j]);
        }
    }

    private void drawChart(int count, int color, int height) {

        System.out.println(count+color+height);

        if(color==1)
        {
            color= Color.BLUE;
        }
        if(color==2)
        {
            color=Color.YELLOW;
        }
        if(color==3)
        {
            color=Color.GREEN;
        }
        if(color==4)
        {
            color=Color.MAGENTA;
        }
        if(color==5)
        {
            color=Color.RED;
        }

        for(int k=1; k<= count; k++)
        {
            View view= new View(this);
            view.setBackgroundColor(color);
            view.setLayoutParams(new LinearLayout.LayoutParams(10, height));
            LinearLayout.LayoutParams parms=(LinearLayout.LayoutParams)view.getLayoutParams();
            parms.setMargins(3,0,0,0);
            view.setLayoutParams(parms);
            la.addView(view);
        }
    }



    @Override    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();

        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}


Friday, July 10, 2015

Android Video Recording Apps (Source You Tube,Mobile Application Tutorials)

The code for  Layout file activity_main file.xml  is as follows 

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

    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Play"        android:id="@+id/playButton"        android:layout_alignParentBottom="true"        android:layout_alignParentLeft="true"        android:layout_alignParentStart="true" />

    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Record"        android:id="@+id/recordButton"        android:layout_alignParentBottom="true"        android:layout_alignRight="@+id/videoView"        android:layout_alignEnd="@+id/videoView" />

    <VideoView        android:layout_width="wrap_content"        android:layout_height="420dp"        android:id="@+id/videoView"        android:layout_alignParentTop="true"        android:layout_alignParentRight="true"        android:layout_alignParentEnd="true" />
</RelativeLayout>


The Code for MainActivity.java is as follows

package com.example.saroj.videorecorder;

import android.app.Activity;
import android.content.Intent;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.VideoView;

import java.net.URI;
import android.net.Uri;


public class MainActivity extends Activity {

    private Button mRecordView, mPlayView;
    private VideoView mVideoView;
    private int ACTIVITY_START_CAMERA_APP=0;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRecordView=(Button) findViewById(R.id.recordButton);
        mPlayView=(Button) findViewById(R.id.playButton);
        mVideoView=(VideoView) findViewById(R.id.videoView);



        mRecordView.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                Intent CallVideoAppInent=new Intent();
                CallVideoAppInent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);


                startActivityForResult(CallVideoAppInent,ACTIVITY_START_CAMERA_APP);

            }
        });

        mPlayView.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                mVideoView.start();

            }
        });
    }

    protected  void onActivityResult(int requestCode,int resultCode,Intent data)
    {
        if(requestCode==ACTIVITY_START_CAMERA_APP && resultCode==RESULT_OK)
        {
             Uri videoUri=data.getData();
             mVideoView.setVideoURI(videoUri);
        }
    }
    @Override    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();

        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}


Tuesday, July 7, 2015

Android Application for Taking Photos

Our activity_main.xml file will be as follows

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

     <TextView         android:id="@+id/textview1"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="Press the button,Capture photo and enjoy"/>

    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Take Photo"        android:layout_below="@+id/textview1"/>
    <ImageView        android:id="@+id/imageview1"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:scaleType="centerCrop"        android:layout_below="@+id/button1"        />
</RelativeLayout>


The MainActivity.java file code is written as below..

import android.app.Activity;
import android.content.Intent;
import  android.graphics.Bitmap;
import  android.provider.MediaStore;
import  android.support.v7.app.ActionBarActivity;
import  android.os.Bundle;
import  android.view.Menu;
import  android.view.MenuItem;
import  android.view.View;
import  android.widget.Button;
import  android.widget.ImageView;


public class MainActivity extends Activity {

    Button btnTakePhoto;
    ImageView imgTakenPhoto;
    private static final int CAM_REQUEST=1313;

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

        btnTakePhoto=(Button) findViewById(R.id.button1);
        imgTakenPhoto=(ImageView) findViewById(R.id.imageview1);

        btnTakePhoto.setOnClickListener(new btnTakePhotoClicker());
    }

    class btnTakePhotoClicker implements Button.OnClickListener
    {
        public void onClick(View v)
        {
            Intent cameraIntent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent,CAM_REQUEST);


        }
    }

    protected void onActivityResult(int requestCode,int resultCode,Intent data)
    {
        super.onActivityResult(requestCode,resultCode,data);

        if(requestCode==CAM_REQUEST)
        {
            Bitmap thumbnail=(Bitmap) data.getExtras().get("data");
            imgTakenPhoto.setImageBitmap(thumbnail);
        }
    }


}

Android apps for voice recorder (Beta)

This is inspired by the profgustin(You Tube) 
 The first file will be AndroidMenifest.xml


<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.saroj.andridrecording" >
    <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
    <uses-permission android:name="android.permission.write_external_storage"></uses-permission>

    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >
        <activity            android:name=".MainActivity"            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>



The Second File is MainActivity.java
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import java.io.File;

public class MainActivity extends Activity {

    //MediaRecorder class is for Recording and Media Player is for Playing the Recorded Media    private MediaRecorder mediaRecorder;
    private MediaPlayer mediaPlayer;
    // The String OUTPUT_FILE will hold the path of the OUTPUT_FILE    private String OUTPUT_FILE;

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

        OUTPUT_FILE= Environment.getExternalStorageDirectory()+"/audiorecorder.3gpp";


    }


    public void buttonTapped(View v)
    {

        switch (v.getId())
        {
            case R.id.StartButton:
                try                {
                    beginRecording();
                }
                catch (Exception ex)
                {
                    ex.printStackTrace();
                }
                break;

            case R.id.FinishButton:
                try                {
                    stopRecording();
                }
                catch (Exception ex)
                {
                    ex.printStackTrace();
                }
                break;

            case R.id.PlayRecording:
                try                {
                    playRecording();
                }
                catch (Exception ex)
                {
                    ex.printStackTrace();
                }
                break;

            case R.id.StopPlayBack:
                try                {
                    stopPlayBack();
                }
                catch (Exception ex)
                {
                    ex.printStackTrace();
                }
                break;


        }


    }

    private void beginRecording()
    {

        ditchMediaRecorder();
        File outFile=new File(OUTPUT_FILE);
        if(outFile.exists())
            outFile.delete();
        mediaRecorder=new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mediaRecorder.setOutputFile(OUTPUT_FILE);

        try        {
            mediaRecorder.prepare();
            mediaRecorder.start();
        }
        catch (Exception ex)
        {

        }


    }

    private void stopRecording()
    {
        if(mediaRecorder!=null) {
            mediaRecorder.stop();
        }
    }


    private void  playRecording()
    {
        ditchMediaPlayer();
        mediaPlayer=new MediaPlayer();
        try {
            mediaPlayer.setDataSource(OUTPUT_FILE);
            mediaPlayer.prepare();
            mediaPlayer.start();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void ditchMediaPlayer() {

        if(mediaPlayer!=null)
        {
            try{
                mediaPlayer.release();

            }
            catch (Exception ex)
            {
                 ex.getMessage();
            }
        }
    }


    private void stopPlayBack()
    {
        if (mediaPlayer!=null)
    {
        mediaPlayer.stop();
    }

    }

    private void ditchMediaRecorder()
    {
        if( mediaRecorder !=null)
        {
            mediaRecorder.release();
        }
    }
}


The Third and final layout file is activity_main.xml,  

<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: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=".MainActivity"    android:orientation="horizontal">

    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical">

        <Button            android:layout_width="match_parent"            android:layout_height="0dp"            android:text="Start Recording"            android:id="@+id/StartButton"            android:layout_weight="1"            android:onClick="buttonTapped" />

        <Button            android:layout_width="match_parent"            android:layout_height="0dp"            android:text="Finish Recording"            android:id="@+id/FinishButton"            android:layout_weight="1"            android:onClick="buttonTapped" />

        <Button            android:layout_width="match_parent"            android:layout_height="0dp"            android:text="Play Recording"            android:id="@+id/PlayRecording"            android:layout_weight="1"            android:onClick="buttonTapped" />

        <Button            android:layout_width="match_parent"            android:layout_height="0dp"            android:text="Stop PlayBack"            android:id="@+id/StopPlayBack"            android:layout_weight="1"            android:onClick="buttonTapped" />
    </LinearLayout>

</LinearLayout>



Saturday, July 4, 2015

Very Basic Android Calculator

Note:This calculator simply performs very basic additon subtraction multiplication and division, The data type is limited only to integer, It perorms well even though there little errors on data validation part.
<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="wrap_content"    android:layout_weight="1"    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=".MainActivity"    android:orientation="vertical">

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"        
android:orientation="vertical">
 <EditText            
android:layout_width="match_parent"            
android:layout_height="wrap_content"            
android:id="@+id/editText1"            
android:layout_weight="1" />        
<EditText            
android:layout_width="match_parent"            
android:layout_height="wrap_content"            
android:id="@+id/editText2"            
android:layout_weight="1" />    
</LinearLayout>    
<LinearLayout        
android:layout_width="match_parent"        
android:layout_height="wrap_content"        
android:orientation="horizontal"        
android:layout_weight="1">
</LinearLayout>
<LinearLayout        
android:layout_width="match_parent"        
android:layout_height="wrap_content"        
android:orientation="horizontal"       
android:layout_weight="1">
<Button
            
android:layout_width="0dp"            
android:layout_weight="1"            
android:layout_height="wrap_content"            
android:text="7"
android:onClick="btnNumOnClick" />
<Button 
android:layout_width="0dp"            
android:layout_weight="1"            
android:layout_height="wrap_content"            
android:text="8"
android:onClick="btnNumOnClick" />
<Button
android:layout_width="0dp"            
android:layout_weight="1"            
android:layout_height="wrap_content"            
android:text="9"
android:onClick="btnNumOnClick" />
        
<Button
android:layout_width="0dp"            
android:layout_weight="1"            
android:layout_height="wrap_content"            
android:text="/"
android:onClick="btnOperators" />    
</LinearLayout>
<LinearLayout        
android:layout_width="match_parent"       
android:layout_height="wrap_content"        
android:orientation="horizontal"        
android:layout_weight="1">
<Button
android:layout_width="0dp"            
android:layout_weight="1"            
android:layout_height="wrap_content"            
android:text="4"
android:onClick="btnNumOnClick" />
<Button
android:layout_width="0dp"            
android:layout_weight="1"            
android:layout_height="wrap_content"            
android:text="5"
android:onClick="btnNumOnClick" />
<Button
android:layout_width="0dp"            
android:layout_weight="1"            
android:layout_height="wrap_content"            
android:text="6"
android:onClick="btnNumOnClick" />
<Button
android:layout_width="0dp"            
android:layout_weight="1"            
android:layout_height="wrap_content"            
android:text="*"
android:onClick="btnOperators" />    
</LinearLayout>
<LinearLayout        
android:layout_width="match_parent"        
android:layout_height="wrap_content"        
android:orientation="horizontal">
<Button
            
android:layout_width="0dp"            
android:layout_weight="1"            
android:layout_height="wrap_content"            
android:text="1"
android:onClick="btnNumOnClick" />
<Button
android:layout_width="0dp"            
android:layout_weight="1"            
android:layout_height="wrap_content"            
android:text="2"
android:onClick="btnNumOnClick" />
<Button
            
android:layout_width="0dp"            
android:layout_weight="1"            
android:layout_height="wrap_content"            
android:text="3"
android:onClick="btnNumOnClick" />
<Button
            
android:layout_width="0dp"            
android:layout_weight="1"            
android:layout_height="wrap_content"            
android:text="-"
android:onClick="btnOperators" />    
</LinearLayout>
<LinearLayout       
 android:layout_width="match_parent"        
android:layout_height="wrap_content"        
android:orientation="horizontal" >
 <Button
 android:layout_width="0dp"            
android:layout_weight="1"            
android:layout_height="wrap_content"            
android:text="0"
android:onClick="btnNumOnClick" />
<Button
android:layout_width="0dp"            
android:layout_weight="1"            
android:layout_height="wrap_content"            
android:text="."            
android:id="@+id/button2" />
<Button
android:layout_width="0dp"            
android:layout_weight="1"            
android:layout_height="wrap_content"            
android:text="="            
android:id="@+id/button3"            
android:onClick="btnEqualClick" />
<Button
android:layout_width="0dp"            
android:layout_weight="1"            
android:layout_height="wrap_content"            
android:text="+"            
android:id="@+id/button4"            
android:onClick="btnOperators" />    
</LinearLayout>
<Button
 android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:text="Back space"        
android:id="@+id/button"        
android:layout_gravity="center_horizontal"        
android:layout_weight="1"        
android:onClick="onBackSpaceClick" />

</LinearLayout>


Here Basic  concepts of string manipulation is used in MainActivity.java, 
The Code is as follows 

 
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import java.util.ArrayList;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

String strTemp1,strTemp2;    
@Override    protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);        
setContentView(R.layout.activity_main);    
}


public void btnNumOnClick(View v)
    {

        Button button=(Button) v;        
strTemp1=button.getText().toString();
         if(strTemp2==null)
         {
          strTemp2=strTemp1+"";         }
        else         {
             strTemp2=strTemp2+strTemp1;         }

        TextView et=(TextView) findViewById(R.id.editText2);        
et.setText(strTemp2);    }


    public void btnOperators(View v)
    {
        strTemp2=null;        
strTemp1=null;        
Button button=(Button) v;        
strTemp1=button.getText().toString();        
TextView view=(TextView) findViewById(R.id.editText2);        
strTemp2=view.getText().toString();        
strTemp2=strTemp2+strTemp1;        
TextView et=(TextView) findViewById(R.id.editText2);        
et.setText(strTemp2);
    }


    public void btnEqualClick(View v)
    {
         if (strTemp2.contains("+"))
        {
            Calculate(strTemp2,"+" );        }
        else if(strTemp2.contains("-"))
         {
           Calculate(strTemp2,"-");         }
        else if(strTemp2.contains("*"))
         {
             Calculate(strTemp2,"*");         }
        else if(strTemp2.contains("/"))
         {
             Calculate(strTemp2,"/" ) ;         }
    }

    public void  Calculate(String strTemp2,String Sign)
    {
        int index1=strTemp2.indexOf(Sign);        
int index2=strTemp2.length();        
String firstValue;        
String secondValue;        
int Result;        
TextView tv=(TextView) findViewById(R.id.editText1);        
firstValue=strTemp2.substring(0,index1);        
secondValue=strTemp2.substring(index1+1,index2);        
int FirstValue= Integer.parseInt(firstValue);        
int SecondValue=Integer.parseInt(secondValue);
        switch (Sign)
        {
            case "+": Result=FirstValue+SecondValue;                
tv.setText(String.valueOf(Result));                     
break;            
case "-":Result=FirstValue-SecondValue;                
tv.setText(String.valueOf(Result));                    
break;            
case "*": Result=FirstValue*SecondValue;                
tv.setText(String.valueOf(Result));                    
 break;            
case  "/": Result=FirstValue/SecondValue;                
tv.setText(String.valueOf(Result));                      
break;        
}

}

public void onBackSpaceClick(View v)
    {

        strTemp2=strTemp2.replace(strTemp2.substring(strTemp2.length()-1),"") ;        
TextView et=(TextView) findViewById(R.id.editText2);       
 et.setText(strTemp2);
    }

}