Create CUSTOM MENU in Blackberry Example

This is a sample example of showing CUSTOM menu in every screen as we like:

=============StartUp.java=================

package com.alishaik.custommenu;

import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;

public class StartUp extends UiApplication
{
public static void main(String[] args)
{
StartUp theApp = new StartUp();
theApp.enterEventDispatcher();
}

public StartUp()
{
pushScreen(new FirstScreen());
}

public static void showAlertMessage(final String message)
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
Dialog.alert(message);
}
});
}
}

==================FirstScreen.java===========

package com.alishaik.custommenu;

public class FirstScreen extends MainScreen implements MenuInterface
{
private ButtonField clickButton;
private CustomMenu firstMenu1,firstMenu2;

public FirstScreen()
{
createGUI();
}

private void createGUI()
{
firstMenu1=new CustomMenu(“First1”, 100, 101, 1);
firstMenu1.setMenuListener(this);
addMenuItem(firstMenu1);

firstMenu2=new CustomMenu(“First2”, 100, 101, 2);
firstMenu2.setMenuListener(this);
addMenuItem(firstMenu2);

add(new LabelField(“First Screen”, Field.FOCUSABLE));
//ButtonField.CONSUME_CLICK if you are not writing this every screen menu shows.
clickButton=new ButtonField(“Clik to go Next”, Field.FOCUSABLE|ButtonField.CONSUME_CLICK);
clickButton.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field, int context)
{
UiApplication.getUiApplication().pushScreen(new SecondScreen());
}
});
add(clickButton);
}

public void onMenuPressed(String menuText, int tag)
{
StartUp.showAlertMessage(“You Selected “+menuText);
if(tag==1)
{
//FirstMenu1 Selected;
}
else
{
//FirstMenu2 Selected;
}
}

protected boolean onSavePrompt()
{
return true;
}
}
==================SecondScreen.java===========

package com.alishaik.custommenu;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;

public final class SecondScreen extends MainScreen implements MenuInterface
{
private ButtonField clickButton;
private CustomMenu secondMenu1,secondMenu2;

public SecondScreen()
{
createGUI();
}

private void createGUI()
{
secondMenu1=new CustomMenu(“Second1”, 100, 101, 1);
secondMenu1.setMenuListener(this);
addMenuItem(secondMenu1);

secondMenu2=new CustomMenu(“Second2”, 100, 101, 2);
secondMenu2.setMenuListener(this);
addMenuItem(secondMenu2);

add(new LabelField(“Second Screen”, Field.FOCUSABLE));
//ButtonField.CONSUME_CLICK if you are not writing this every screen menu shows.
clickButton=new ButtonField(“Back button”, Field.FOCUSABLE|ButtonField.CONSUME_CLICK);
clickButton.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field, int context)
{
UiApplication.getUiApplication().popScreen(SecondScreen.this);
return;
}
});
add(clickButton);
}

public void onMenuPressed(String menuText, int tag)
{
StartUp.showAlertMessage(“You Selected “+menuText);
if(tag==1)
{
//SecondMenu1 Selected;
}
else
{
//SecondMenu2 Selected;
}
}

protected boolean onSavePrompt()
{
return true;
}
}
==================MenuInterface.java=============

package com.alishaik.custommenu;

public interface MenuInterface
{
public void onMenuPressed(String menuText,int tag);
}
=================CustomMenu.java=====================

package com.alishaik.custommenu;

import net.rim.device.api.ui.MenuItem;

public class CustomMenu extends MenuItem
{
private MenuInterface menuInterface;
private String menuText;
private int tag;

public CustomMenu(String text, int ordinal, int priority, int tag)
{
super(text, ordinal, priority);
this.menuText=text;
this.tag=tag;
}

public void run()
{
menuInterface.onMenuPressed(menuText, tag);
}

public void setMenuListener(MenuInterface listener)
{
this.menuInterface=listener;
}
}
===================*******================

Advertisements

Capture The Image, Saved it in SDCard and Show it in to the Screen in Android

For saving the image in SDCard we have to first check that “is SDCard available or not?”

For checking the SDCard availability in android:

public static boolean isSdPresent()
{
      return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}

and The code for the image catpure and show is:

import java.io.ByteArrayOutputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.alishaik.capturedemo.common.FindSDCard;

public class StartUpActivity extends Activity
{
      private Button captureButton;
      private ImageView showImage;
      private Bitmap bitmap;
      @Override
      public void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);

         if(FindSDCard.isSdPresent())
         {
             showImage=(ImageView)findViewById(R.id.captured_img);

              captureButton=(Button)findViewById(R.id.button1);
              captureButton.setText("Open Camera");
              captureButton.setOnClickListener(new Button.OnClickListener()
              {
                  @Override
                  public void onClick(View v)
                  {
                      startCamera();
                  }
             });
         }
        else
        {
            Toast.makeText(this,"Insert SDCard", Toast.LENGTH_LONG).show();
        }
    }

    private void startCamera()
    {
       Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
       intent.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI.getPath());
       startActivityForResult(intent, 1);
   }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode)
        {
           case -1:     Log.i("StartUpActivity", "Photo Captured");
                        bitmap=(Bitmap) data.getExtras().get("data");
                        MediaStore.Images.Media.insertImage(getContentResolver(),bitmap,null,null);
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                        showImage.setImageBitmap(bitmap);
                        break;
          default:      Log.i("StartUpActivity", "Cancelled Camera"); break;
      }
   }

    @Override
    public void onBackPressed()
    {
       super.onBackPressed();
    }

    public static boolean isSdPresent()
    {
        return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
    }
}

and study this link:

http://developer.android.com/training/camera/photobasics.html#Taking Photos Simply

Get The Image From SDCard In Blackberry

For this we have to first check that “is SDCard available or not?”

For checking the SDCARD availability we have to use the following code:

Source code of SDCARD Checking: SDCardTest.java

import java.util.Enumeration;
import javax.microedition.io.file.FileSystemRegistry;
public class SDCardTest
{
 public static boolean isSDCardAvailable()
 {
 boolean sdCardPresent = false;
 String root = null;
 Enumeration e = FileSystemRegistry.listRoots();

 while (e.hasMoreElements())
 {
 root = (String)e.nextElement();
 if(root.equalsIgnoreCase("sdcard/"))
 {
 sdCardPresent = true;
 }
 }
 return sdCardPresent;
 }
}

and the code for Get the Image from SDCARD : LoadingScreen.java

import java.io.IOException;
import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

import net.rim.device.api.io.IOUtilities;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.picker.FilePicker;
import net.rim.device.api.ui.picker.FilePicker.Listener;

import com.hiddenbrains.image.common.SDCardTest;

public class LoadingScreen extends MainScreen implements FieldChangeListener
{
 private VerticalFieldManager ver;
 private ButtonField showImage;
 private BitmapField bitmapField;

 public LoadingScreen()
 {
        ver=new VerticalFieldManager(USE_ALL_WIDTH);
        showImage=new ButtonField("Show Image",Field.FIELD_HCENTER);
        showImage.setChangeListener(this);
        ver.add(showImage);

        bitmapField=new BitmapField(null,Field.FIELD_HCENTER);
        bitmapField.setPadding(10, 0, 10, 0);
       ver.add(bitmapField);

      add(ver);
 }
 public void fieldChanged(Field field, int context)
 {
 if(field==showImage)
 {
 selectImageFromSDCARD();
 }
 }

 private void selectImageFromSDCARD()
 {
 String PATH="";
 if(SDCardTest.isSDCardAvailable())
 PATH = System.getProperty("fileconn.dir.memorycard.photos");//The default stored Images Path;
 else
 PATH = System.getProperty("fileconn.dir.photos");//The default stored Images Path;

 FilePicker filePicker=FilePicker.getInstance();
 filePicker.setPath(PATH);
 filePicker.setListener(new Listener()
 {
 public void selectionDone(String url)
 {
 System.out.println("======================URL: "+url);
 try
 {
 FileConnection file = (FileConnection)Connector.open(url);
 if(file.exists())
 {
 InputStream inputStream = file.openInputStream();
 byte[] data=new byte[inputStream.available()];
 data=IOUtilities.streamToBytes(inputStream);
 Bitmap bitmap=Bitmap.createBitmapFromBytes(data, 0, data.length,1);//Here we get the Image;

 Bitmap scaleBitmap=new Bitmap(400, 300);//Now we are scaling that image;
 bitmap.scaleInto(scaleBitmap, Bitmap.FILTER_LANCZOS);
 bitmapField.setBitmap(scaleBitmap);
 }
 else
 {
 bitmapField.setBitmap(Bitmap.getBitmapResource("icon.png"));
 }
 }
 catch (IOException e)
 {
 bitmapField.setBitmap(Bitmap.getBitmapResource("icon.png"));
 }
 }
 });
 filePicker.show();
 }

 protected boolean onSavePrompt() //It doesn't show the "Save","Discard","Cancel" POPUP;
 {
 return true;
 }

 public boolean onMenu(int instance) //It doesn't show the Menu;
 {
 return true;
 }
}

Ali Shaik

Blackberry, IPhone and Android Application Developer;

BlogSpot: http://alishaik001.blogspot.in

Blackberry Dev Forum: http://supportforums.blackberry.com/t5/user/viewprofilepage/user-id/475839

StackOverFlow: http://stackoverflow.com/users/822327/alishaik786