Monday, October 28, 2013

View, SurfaceView

1. What is View in Android?
Ans: A widgets like buttons and text fields. A activity uses views and fragmaents to create the user interface. View Hierarchy: Hierarchy Viewer allows you to debug and optimize your user interface. It provides a visual representation of the layout's View hierarchy (the Layout View) and a magnified inspector of the display (the Pixel Perfect View).

2. What is Surface View?
Ans: Provides a dedicated drawing surface embedded inside of a view hierarchy. You can control the format of this surface and, if you like, its size; the SurfaceView takes care of placing the surface at the correct location on the screen

3. How you Use Surface view in Android and Show me working code?
Ans: with surfaceCreated(surfaceholder) and surfaceDestroyed(surfaceholder) methods.

4. Is it possible to add any view in Android Surface View ?
Ans. yes

5. If adding view on surface view then Show one example?

Ans. Enclose your surfaceView with a FrameLayout in your xml Layout. Then add your buttons to the same FrameLayout. Make sure they are placed below the surface view so they get drawn on top of it.

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

android:layout_height="wrap_content"
android:layout_width="wrap_content">



android:layout_height="wrap_content"
android:layout_width="wrap_content">









6. What is media Player in android, why it’s needed?
Ans: MediaPlayer class can be used to control playback of audio/video files and streams.
Displays a video file. The VideoView class can load images from various sources (such as resources or content providers), takes care of computing its measurement from the video so that it can be used in any layout manager, and provides various display options such as scaling and tinting.

7. Can Media player UI resize? if so, show me a working code?
Ans: yes

8. What are the layouts available to create a UI-layout in .xml file?
Ans: Relative Layout, Linear Layout.Table Layout and Frame Layout.

9. Is there difference between Media player from Layout .xml file and Media player from android java code?
yes

Tuesday, October 22, 2013

How to store Logs in a txt file using the android

Here with I attached simple Logger class definition, you can use at it is. To store the log information in to Log.txt file in SDCARD, use at it is.


package com.clientname.projectname;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.FileHandler;
import android.os.Environment;

import android.util.Log;/**
•@author Rakesh.Jha
•Date - 07/10/2013
•Definition - Logger file use to keep Log info to external SD with the simple method
*/

public class Logger{

public static FileHandler logger = null;
private static String filename = "ProjectName_Log";
static boolean isExternalStorageAvailable = false;
static boolean isExternalStorageWriteable = false;
static String state = Environment.getExternalStorageState();

public static void addRecordToLog(String message){
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
isExternalStorageAvailable = isExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
isExternalStorageAvailable = true;
isExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but all we need

// to know is we can neither read nor write
isExternalStorageAvailable = isExternalStorageWriteable = false;
}

File dir = new File("/sdcard/Files/Project_Name");
if (Environment.MEDIA_MOUNTED.equals(state)) {
if(!dir.exists())
{

Log.d("Dir created ", "Dir created ");
dir.mkdirs();
}

File logFile = new File("/sdcard/Files/Project_Name/"+filename+".txt");
if (!logFile.exists())
{
try
{
Log.d("File created ", "File created ");
logFile.createNewFile();

}
catch (IOException e)
{

// TODO Auto-generated catch block

e.printStackTrace();

}

}
try
{
//BufferedWriter for performance, true to set append to file flag
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
buf.write(message + "\r\n");
//buf.append(message);
buf.newLine();
buf.flush();
buf.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();

}
}
}
}


Now once you created this file, where ever you want to store a log info into log.txt file use below code. -

package com.clientname.projectname;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;/**

•@author Rakesh.Jha

•Date - 03/10/2013
•Definition - //ToDO
*/


public class MainActivity extends Activity {
private static final String TAG = null;
Logger logger;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("Testing :","log");
// no need to do this line, use below line
logger.addRecordToLog("Testing : log " );
logger.addRecordToLog("TAG MediaPlayer audio session ID: " );
MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.test);

//test is audio file, u have to keep in raw folder
logger.addRecordToLog( "MediaPlayer audio session ID: " + mediaPlayer.getAudioSessionId());
logger.addRecordToLog( "Media Player started " + "Started !");
mediaPlayer.start();
// no need to call prepare(); create() does that for you
}
}
private void prepareMediaServer() {


}
}

Singleton in Android

Singleton is a part of Gang of Four design pattern and it is categorized under creational design patterns.

-> Static member : This contains the instance of the singleton class.
-> Private constructor : This will prevent anybody else to instantiate the Singleton class.
-> Static public method : This provides the global point of access to the Singleton object and returns the instance to the client calling class.

1.create private instance
2.create private constructor
3.use getInstance() of Singleton class


public class Logger{
private static Logger objLogger;
private Logger(){
//ToDo here
}

public static Logger getInstance() {
if (objLogger == null) {
objLogger = new Logger();
}
return objLogger;
}
}