2011-11-20

Using a third-party jar with Android

This guide will show you how to use a third-party jar library in your Android application. First you want to place the jar file in a specific directory in your Android workspace (I use a directory named "lib"). Next, in Eclipse, select Project from the file menu, then Properties, then Java Build Path. Next click on the Libraries tab, as seen below.


Next, click on the Add JARs... button and you will see a window like the one below. Now navigate to where you copied your jar file earlier and select it.


 Next, click on the Order and Export tab of the Java Build Path window, like in the screenshot below. You want to make sure that your newly added jar file is selected (Mine was not selected yet).



After that, you should be able to import packages and use code from your jar library.

Install Windows 8 using a USB drive


If you're like me, you want to try out that new kid from Redmond, Windows 8. Also, you might be like me and lack an optical (CD/DVD) drive. Many netbooks do not have a disc drive, as well as my MacBook Air not having one. I have two methods below, the first of which I discovered myself and the second was adapted from here. The second method did not work on my MBA, so try it if you can't get the first one to work.

Method #1:


Requirements:

Step 1:
Install the Windows DVD USB tool on a windows machine.

Step 2: 
Run the program and point it to your downloaded Windows 8 ISO and desired USB drive or SD card.

Step 3: 
Make sure your machine is set to boot from a USB drive and reboot to the drive.

That's it! If this method doesn't work for you (it did for me) you can try the more involved method below.


Method #2:
Here's how you can install Windows 8 without a disc drive using a USB drive or SD card and MagicISO.

Requirements:
*USB Flash Drive or SD card (Minimum 4GB for 32 bit and greater than 4 GB for 64-bit Windows 8)
*Windows 8 installation image. You can download Windows 8 developer preview here: http://msdn.microsoft.com/en-us/windows/apps/br229516
*MagicISO

Follow the below steps to create bootable Windows 8 USB drive using which you can install or Windows 8 easily.

Procedure:
1. Plug-in your USB flash drive to USB port and move all the contents from USB drive to a safe location on your system.

2. Open Command Prompt with admin rights. Use any of the below methods to open Command Prompt with admin rights.

*Type cmd in Start menu search box and hit Ctrl+ Shift+ Enter.
Or
*Go to Start menu > All programs > Accessories, right click on Command Prompt and select Run as administrator.

3. You need to know about the USB drive a little bit. Type in the following commands in the command prompt:

First type DISKPART and hit enter to see the below message.

Next type LIST DISK command and note down the Disk number (ex: Disk 1) of your USB flash drive. Mine was Disk 1. You'll be able to tell by the size description.

4. Next type all the below commands one by one. Here I assume that your disk drive no is “Disk 1”. Refer the above step to confirm it.

So below are the commands you need to type and hit enter after each one:

SELECT DISK 1
CLEAN
CREATE PARTITION PRIMARY
SELECT PARTITION 1
ACTIVE
FORMAT FS=NTFS
ASSIGN
EXIT

Don’t close the command prompt as we need to execute one more command at the next step. Just minimize it.

5. Next use MagicISO (download here) to mount your Windows 8 as a virtual disc. Check the drive letter you'll need for the next steps. Mine was F:.

6. Maximize the minimized Command Prompt in the 4th step. Type the following commands now:

F: CD BOOT and hit enter.Where “F” is your DVD drive letter.

CD BOOT

7. Type another command given below to update the USB drive with BOOTMGR compatible code.

BOOTSECT.EXE /NT60 F:

Where “F” is your USB drive letter. Once you enter the above command you will see the below message.

8. Copy your Windows 8 mount contents (find these by right clicking on the Windows 8 mount in your "Computer") to the USB flash drive.

9. Your USB drive is ready to boot and install Windows 8. Only thing you need to change the boot priority at the BIOS to USB from the HDD or CD ROM drive. I won’t explain it as it’s just the matter the changing the boot priority or enabling the USB boot option in the BIOS.

Note: If you are not able to boot after following this guide means you haven’t set the BIOS priority to USB. If you got any problem in following this guide feel free to ask questions by leaving comment.

2011-10-05

Load Seperate Layout for Landscape and Portrait Orientations in Android App


There are many times while creating an app that you want to take advantage of the different widths and heights of a phone in landscape mode and one in portrait mode. An easy way to do this is to load a different layout file when in each orientation.

The files in the default "layout" directory (ProjectName/res/layout) are used in both orientations, unless you add a layout-land directory. Then all you have to do is put your customized landscape layout files in there and they will be loaded when the device switches to landscape mode.

Lock Orientation of Android Activity


Here's how you lock the orientation of an activity in Android:

  1. Open your applications manifest file (usually AndroidManifest.xml)
  2. Add android:screenOrientation="portrait" to your activity's attributes (or landscape if you want to lock it in landscape orientation)
That's it! An example of an activity element in the manifest would look like this:

<activity android:name=".MyActivity"
    android:label="@string/app_name"
    android:screenOrientation="portrait">            
</activity>

2011-10-02

Put a Password on a Zip Archive in Mac OS


Do you have a lot of secret stuff you need to compress? Perhaps you don't want anyone to see your massive collection of kitten pictures? Here's how to create a compressed archive with a password on it in Mac OS.

  1. Open a Terminal window
  2. Change to the directory where your files are that you want zipped (cd /your/secret/files)
  3. Enter this command zip -e secrets.zip secrets/* , where secrets.zip is the name of your to-be-created archive and secrets is the directory where they reside
  4. Enter your desired password in response to the prompts
  5. Done!

2011-09-29

Create a Gradient in Android XML Layout


Android provides an easy way to create a three-color gradient using an xml file. Simply create an xml file in your drawable folder like this:


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient 
        android:startColor="#ff8833" 
        android:endColor="#ff5533"
        android:angle="270"
     />
    <corners android:radius="0dp" />
</shape>


Then reference it by its file name (it would be gradient.xml, here) as the background of the view you want to use to show it. Here it is as the background of a TextView:



<TextView
    android:id="@+id/widget43"
    android:layout_width="fill_parent"
    android:layout_gravity="center_horizontal"
    android:layout_height="wrap_content"
    android:paddingTop="15dp"
    android:paddingBottom="15dp"
    android:textColor="#ffffff"
    android:gravity="center"
    android:background="@drawable/gradient"
    android:textSize="30dp"
    android:text="Super Gradient!"
>
</TextView>



2011-09-28

Saving an Image Captured by Android's Camera



This tutorial will show you how to save an image captured by the Android camera to the SD card with a specific filename.

First, you want to place this where you can access the bitmap created by the camera. onPictureTaken() is generally a good place:

    //...

    public void onPictureTaken(byte[] arg0, Camera arg1) {
        Bitmap bitmapPicture = BitmapFactory.decodeByteArray(arg0,    0, arg0.length);
        //...

Next, you'll want to create a directory/file object:

//...
    File imageDirectory = new File(Environment.getExternalStorageDirectory() + File.separator + "myImageGoesHere");
//...

Then you should make the directory:

//...
    imageDirectory.mkdirs();

//...

Now you can create the file object, with your desired file name:

//...
  File f = new File(Environment.getExternalStorageDirectory()
                        + File.separator + "myImageGoesHere" + File.separator + "myImageFileName");

//...

Next, we write the data from your bitmap to the file:

    try {
        f.createNewFile();
        //write the bytes in file
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(arg0);
    } catch (IOException e) {
e.printStackTrace();
}


And that's it! Your image should be written to the SD card in the specified location.