Category Archives: android

Android Full Screen Video Playing

1.VideoApp.java

package com.javapages4all.videoapp;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.VideoView;

public class VideoApp extends Activity
{
VideoView mVideoView;
MediaController mc;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.videoapp);

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//if you want.

//File video = new File(“your file path”);
mVideoView = (VideoView) findViewById(R.id.myvideoview);
mc = new MediaController(this);
mVideoView.setMediaController(mc);
mc.setAnchorView(mVideoView);
mVideoView.setMediaController(mc);
//mVideoView.setVideoPath(video.getAbsolutePath());
mVideoView.setVideoURI(Uri.parse(“http://www.pocketjourney.com/downloads/pj/video/famous.3gp”));
mVideoView.requestFocus();
mVideoView.start();
}
}

2.videoapp.xml

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” >
<VideoView android:id=”@+id/myvideoview”
android:layout_width=”fill_parent”
android:layout_alignParentRight=”true”
android:layout_alignParentLeft=”true”
android:layout_alignParentTop=”true”
android:layout_alignParentBottom=”true”
android:layout_height=”fill_parent”>
</VideoView>
</RelativeLayout>

 

3.AndroidManifest.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
package=”com.javapages4all.videoapp”
android:versionCode=”1″
android:versionName=”1.0″ >

<uses-sdk
android:minSdkVersion=”8″
android:targetSdkVersion=”18″ />

<uses-permission android:name=”android.permission.INTERNET” />

<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”com.javapages4all.videoapp.VideoApp”
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>

 

Getting IMEI Number

1.Android Device:

——————–

Dial *#06# to know your IMEI Number.

2.Direct Process:

——————-

Remove your battery and look for IMEI,you will find it.

3.Android Program:

———————-

You’ll need the

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

permission in AndroidManifest.xml file to do this.

final TelephonyManager tm =(TelephonyManager)getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);

String deviceid = tm.getDeviceId();

File Encryption and Decryption in Java And Android

package raja.JavaPages4all;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;

public class AES
{
private static final String key = “1234567891234567”;
private static final String method = “AES”;
/**
* @param args
*/
public static void main(String[] args)
{
try
{
long stime=System.currentTimeMillis();
File inFile = new File(“D:/sample.wmv”);
File outFile = new File(“D:/sample_encrypted.wmv”);
File outFile_dec = new File(“D:/sample_decrypted.wmv”);
encrypt(new FileInputStream(inFile), new FileOutputStream(outFile));
long etime=System.currentTimeMillis();
System.out.println((etime-stime));
decrypt(new FileInputStream(outFile), new FileOutputStream(outFile_dec));
System.out.println(System.currentTimeMillis()-etime);
} catch (Exception e)
{
e.printStackTrace();
} catch (Throwable e) {
e.printStackTrace();
}
}

public static SecretKeySpec keyGen(String s) throws NoSuchAlgorithmException
{
KeyGenerator kgen = KeyGenerator.getInstance(method);
kgen.init(128); // 192 and 256 bits may not be available
byte[] raw = s.getBytes();
SecretKeySpec result = new SecretKeySpec(raw, method);
//System.out.println(“result “+new String(result.getEncoded()));
// Instantiate the cipher
return result;
}

public static void encrypt(InputStream is, OutputStream os)
{
encryptOrDecrypt(Cipher.ENCRYPT_MODE, is, os);
}

public static void decrypt(InputStream is, OutputStream os)
{
encryptOrDecrypt(Cipher.DECRYPT_MODE, is, os);
}

public static void encryptOrDecrypt(int mode, InputStream is, OutputStream os)
{
try
{
Cipher cipher = Cipher.getInstance(method); // DES/ECB/PKCS5Padding for SunJCE
if (mode == Cipher.ENCRYPT_MODE)
{
cipher.init(Cipher.ENCRYPT_MODE, keyGen(key));
CipherInputStream cis = new CipherInputStream(is, cipher);
doCopy(cis, os);
} else if (mode == Cipher.DECRYPT_MODE)
{
cipher.init(Cipher.DECRYPT_MODE, keyGen(key));
CipherOutputStream cos = new CipherOutputStream(os, cipher);
doCopy(is, cos);
}
} catch (Exception e)
{
e.printStackTrace();
} catch (Throwable e)
{
e.printStackTrace();
}
}

public static void doCopy(InputStream is, OutputStream os) throws IOException
{
/* byte[] bytes = new byte[1024];
int numBytes;
while ((numBytes = is.read(bytes)) != -1) {
os.write(bytes, 0, numBytes);
}
os.flush();
os.close();
is.close();*/

byte[] buffer = new byte[8192]; // Or larger (but use powers of 2)
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1)
{
os.write(buffer, 0, bytesRead);
}
os.flush();
os.close();
is.close();
}

}

Batch insert to SQLite database on Android

try
{
  db.beginTransaction();
  for each record in the list 
 {
   do_some_processing();
   if (line represent a valid entry) 
   {
    db.insert(SOME_TABLE, null, SOME_VALUE);
  }
  some_other_processing();
 }
 db.setTransactionSuccessful();
} catch (SQLException e) {
} finally {
 db.endTranscation();
}

private void insertOneHundredRecords() {
          for (int i = 0; i<100; i++) {
                     ContentValues values = new ContentValues();
                     values.put("FirstNumber", i);
                     values.put("SecondNumber", i);
                     values.put("Result", i*i);
                     sampleDB.insert(SAMPLE_TABLE_NAME,null,values);
           }
}

private void bulkInsertOneHundredRecords() {
          String sql = "INSERT INTO "+ SAMPLE_TABLE_NAME +" VALUES (?,?,?);";
          SQLiteStatement statement = sampleDB.compileStatement(sql);
          sampleDB.beginTransaction();
          for (int i = 0; i<100; i++) {
                    statement.clearBindings();
                    statement.bindLong(1, i);
                    statement.bindLong(2, i);
                    statement.bindLong(3, i*i);
                    statement.execute();
           }
           sampleDB.setTransactionSuccessful();	
           sampleDB.endTransaction();
}

Ruppe symbol showing in Android

package com.raja.customtoast;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;

public class Ruppe extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Typeface typeface = Typeface.createFromAsset(getAssets(), “Rupee.ttf”);
TextView textview = (TextView) findViewById(R.id.textView1);
textview.setTypeface(typeface);
textview.setText(“`”);
}
}

2) activity_main.xml:

<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android&#8221;
    android:layout_width=“match_parent”
    android:layout_height=“match_parent”
    android:background=“#5F04B4”
    android:orientation=“vertical” >
    <TextView
        android:id=“@+id/textView1”
        android:layout_width=“match_parent”
        android:layout_height=“wrap_content”
        android:layout_marginTop=“10dp”
        android:gravity=“center”
        android:text=“Rupee”
        android:textColor=“#FFFFFF”
        android:textSize=“40dp”
        android:textStyle=“bold” />
</LinearLayout>
Paste in asserts

Underling Text View in Android

There are three ways of underling the text in Text View.

  1. SpannableString

  2. setPaintFlags(); of TextView

  3. Html.fromHtml();

Let me explain you all approaches :

1st Approach

For underling the text in TextView you have to use SpannableString

String udata="Underlined Text";
SpannableString content = new SpannableString(udata);
content.setSpan(new UnderlineSpan(), 0, udata.length(), 0);
mTextView.setText(content);

2nd Approach

You can make use of setPaintFlags method of TextView to underline the text of TextView.

For eg.

mTextView.setPaintFlags(mTextView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
mTextView.setText("This text will be underlined");

You can refer constants of Paint class if you want to strike thru the text.

3rd Approach

Make use of Html.fromHtml(htmlString);

String htmlString="<u>This text will be underlined</u>";
mTextView.setText(Html.fromHtml(htmlString));

Show or Hide soft keyboard on opening a dialog or activity in android

For showing the soft keyboard on entering into the activity or to the dialog we can use this coding

Add the following code just before alert.show()

InputMethodManager input = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
input.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);

for hiding keyboard
InputMethodManager input = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
input.hideSoftInputFromWindow(singleedittext.getWindowToken(),0);
%d bloggers like this: