Popular Posts

Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

Jun 16, 2013

Publishing Apps- Get Started

Jun 11, 2013

Get Screen Size and Orientation in Android





Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
int orientation = display.getOrientation();

Feb 4, 2013

Get Value From Date Picker in Android




Import datePicker of your class:
import android.widget.DatePicker;

Declare the variable:
private DatePicker dPicker;
 
Write the code in the button click event:


                      Integer gYear = dPicker.getYear();
                      Integer gMonth = dPicker.getMonth()+1;
                      Integer gDate = dPicker.getDayOfMonth();
                      StringBuilder sb=new StringBuilder();
  
sb.append(gYear .toString()).append("-").append(gMonth.toString()).append("-")
.append(gDate.toString());
                      String dPStr=sb.toString();

Jul 19, 2012

Change Android-emulator AVD Window Size

- Go to run… (dropdown button on the normal run button)
- Run Configurations
- Click on the target tab
- Scroll down until you see the “Additional Emulator Command Line Options”
- You can enter the command “-scale .75″ or "-skin ­­320x480" or some other resolution 
appropriate for you.
- Click apply and then close and restart your emulator

Jun 25, 2012

Exceptions of Android

Exceptions that are supported by Android:

1. InflateException : This exception is thrown When an error conditions are occurred.

2. Surface.OutOfResourceException: This exception is thrown When a surface is not created or resized.

3. SurfaceHolder.BadSurfaceTypeException: This exception is thrown from the lockCanvas() method, when invoked on a Surface whose is SURFACE_TYPE_PUSH_BUFFERS

4. WindowManager.BadTokenException: This exception is thrown at the time of trying to add view an invalid WindowManager.LayoutParamstoken.



Hide Title in android application

You can hide your android application title....

Create MyFirstAndroidApp.Java Class  :   -----
-----------------------------------------------


package my.MyFirstAndroidApp;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;

import android.view.View;
import android.view.View.OnClickListener;

import android.widget.Button;
import android.widget.Toast;

public class MyFirstAndroidAppActivity extends Activity { 
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        // -- hides the title bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);
       
        setContentView(R.layout.main);
    }
}

Features of Android

Android features:

1. Components can be reused and replaced by the application framework.

2. Optimized DVM for mobile devices

3. SQLite enables to store the data in a structured manner.

4. Supports GSM telephone and Bluetooth, WiFi, 3G and EDGE technologies

5. The development is a combination of a device emulator, debugging tools, memory profiling and plug-in for Eclipse IDE.

Jun 20, 2012

Generate 3D Pie Chart using Google Chart Tools

Using Google Chart Tools, we can easily generate 3D pie chart.

Class File:

package com.pie.chart;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class PieChartActivity extends Activity {
    /** Called when the activity is first created. */
   
    final static String urlGoogleChart = "http://chart.apis.google.com/chart";
    final static String urlp3Api = "?cht=p3&chs=400x150&chl=A|B|C|D|E&chd=t:";
     
     EditText inputA, inputB, inputC;
     Button generate;
     ImageView pieChart;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        inputA = (EditText)findViewById(R.id.adata);
        inputB = (EditText)findViewById(R.id.bdata);
        inputC = (EditText)findViewById(R.id.cdata);
        generate = (Button)findViewById(R.id.generate);
        pieChart = (ImageView)findViewById(R.id.pie);
        generate.setOnClickListener(generateOnClickListener);
    }

  Button.OnClickListener generateOnClickListener = new Button.OnClickListener(){

  @Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
   String A = inputA.getText().toString();
   String B = inputB.getText().toString();
   String C = inputC.getText().toString();
   String D = "14";
   String E = "24";
   String urlRqs3DPie = urlGoogleChart + urlp3Api + A + "," + B + "," + C+ "," + D+ "," + E;
   
   Bitmap bm3DPie = loadChart(urlRqs3DPie);
   if(bm3DPie == null){
    Toast.makeText(PieChartActivity.this, "Problem in loading 3D Pie Chart", Toast.LENGTH_LONG).show();
   }else{
    pieChart.setImageBitmap(bm3DPie);
   }
  }};

 
  private Bitmap loadChart(String urlRqs){
      Bitmap bm = null;
      InputStream inputStream = null;
      
      try {
       inputStream = OpenHttpConnection(urlRqs);
       bm = BitmapFactory.decodeStream(inputStream);
       inputStream.close();
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
      
      return bm;
     }
         
     private InputStream OpenHttpConnection(String strURL) throws IOException{
      InputStream is = null;
      URL url = new URL(strURL);
      URLConnection urlConnection = url.openConnection();
           
      try{
       HttpURLConnection httpConn = (HttpURLConnection)urlConnection;
       httpConn.setRequestMethod("GET");
       httpConn.connect();
       
       if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
        is = httpConn.getInputStream();
       }
      }catch (Exception ex){
      }
      
      return is;
     }
   
   
}


Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<LinearLayout
 android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="A "
    />
<EditText
    android:id="@+id/adata"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="number" />
</LinearLayout>
<LinearLayout
 android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="B "
    />
<EditText
    android:id="@+id/bdata"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="number" />
</LinearLayout>
<LinearLayout
 android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="C "
    />
<EditText
    android:id="@+id/cdata"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="number" />
</LinearLayout>
<Button
    android:id="@+id/generate"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Generate 3D Pie Chart"
    />

<ImageView
    android:id="@+id/pie"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

</LinearLayout>


Add   <uses-permission android:name="android.permission.INTERNET" /> to manifest file.


May 6, 2012

Restart App(Android)


Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);

Using Vibrator in android



-Add permission to android Manifest file
 <uses-permission android:name="android.permission.VIBRATE" />

- In java file:
 Vibrator vibrator;
- In Oncreage:
 vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

- Use it:
 vibrator.vibrate(30);

Using Custom Dialog(ANdroid)


Java Code:

private void hintMethod(){

final Dialog dialog = new Dialog(GMTActivity.this);
            dialog.setContentView(R.layout.thinking_ui);
            dialog.setTitle(" Guess My THink!! ");
            dialog.setCancelable(false);
            dialog.setCanceledOnTouchOutside(false);
       
            thinkImageView = (ImageView) dialog.findViewById(R.id.thinkImageView);
            buttonTK = (Button) dialog.findViewById(R.id.buttonTK);
            thinkingTextView = (TextView) dialog.findViewById(R.id.thinkingTextView);
           
            thinkingTextView.setSingleLine(false);
            thinkingTextView.setText("I am thinking between \n"+minNumber+" and "+maxNumber);
       
           
            thinkImageView.setOnClickListener(new OnClickListener() {
               
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    dialog.dismiss();
                }
            });

           
            buttonTK.setOnClickListener(new OnClickListener() {
               
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    dialog.dismiss();
                }
            });
           
            dialog.show();

}

Xml file:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <ImageView
        android:id="@+id/thinkImageView"
        android:layout_width="69dp"
        android:layout_height="58dp"
        android:layout_x="10dp"
        android:layout_y="1dp"
        android:src="@drawable/icon" />

    <TextView
        android:id="@+id/thinkingTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="82dp"
        android:layout_y="18dp"
        android:text="thinking 1 to 12" />

    <Button
        android:id="@+id/buttonTK"
        android:layout_width="96dp"
        android:layout_height="wrap_content"
        android:layout_x="76dp"
        android:layout_y="63dp"
        android:text=" Ok " />

</AbsoluteLayout>

Open website from your apps(Android)


              private final static String WEBSITE = "http://itsjubayer.blogspot.com";


               final Intent visit = new Intent(Intent.ACTION_VIEW);
                visit.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                visit.setData(android.net.Uri.parse(WEBSITE));
               
                // Use a thread so that the menu is responsive when clicked
                new Thread(new Runnable() {
                    public void run() {
                        startActivity(visit);
                    }
                }).start();

                return true;

Apr 29, 2012

Remove white space and delete one character in android

            String k = guessTextView.getText().toString();

            sum  = k.replaceAll(" ", "%20");
            StringBuilder stringBuilder=new StringBuilder(80);
           
            stringBuilder.append(sum);
                      
            sum = stringBuilder.deleteCharAt(stringBuilder.length()-1).toString();
           
            guessTextView.setText(sum);

Apr 27, 2012

Create sub-folder in SD Card using Java code (Android)


First  in AndroidManifest.xml add the permission  "android.permission.WRITE_EXTERNAL_STORAGE"
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

The java code:
 String subDir = "/myFolderName";
      String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
      File myNewFolder = new File(extStorageDirectory + subDir);
      myNewFolder.mkdir();

Apr 24, 2012

Using Alert in Android

AlertDialog.Builder builder = new AlertDialog.Builder(Alert_DialogActivity.this);
                builder.setMessage("Are You Sure you want to exit");
                builder.setCancelable(false);
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                   
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        Alert_DialogActivity.this.finish();
                    }
                 });
                builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                   
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        dialog.cancel();
                    }
                });
               
                builder.setNeutralButton("Jubayer", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        //Toast.makeText(context, text, duration);
                        Log.w("Jub", "Jubayer Clicked");
                    }
                });
               
                AlertDialog alert = builder.create();
                alert.show();

Apr 23, 2012

Send Email in Android


                Intent intentEmail = new Intent(android.content.Intent.ACTION_SEND); 
                 
                String emailList[] = { "user@fakehost.com","user2@fakehost.com" }; 
                String emailCCList[] = { "user3@fakehost.com","user4@fakehost.com"}; 
                String emailBCCList[] = { "user5@fakehost.com" }; 
                 
                intentEmail.putExtra(android.content.Intent.EXTRA_EMAIL, emailList); 
                intentEmail.putExtra(android.content.Intent.EXTRA_CC, emailCCList); 
                intentEmail.putExtra(android.content.Intent.EXTRA_BCC, emailBCCList); 
                 
                intentEmail.putExtra(android.content.Intent.EXTRA_SUBJECT, "My subject"); 
                 
                intentEmail.setType("plain/text"); 
                intentEmail.putExtra(android.content.Intent.EXTRA_TEXT, "My message body."); 
                 
                startActivity(intentEmail);
           

Apr 22, 2012

Android Gods frown upon me!!


You can close your android app with this Code


      android.os.Process.killProcess(android.os.Process.myPid());
                                    System.exit(0);

 

But Android always discourages this. Android handles the memory and close the apps atomically if needed.

Apr 9, 2012

Adding an External Library (.jar) using Eclipse(Android)

You can use a third party JAR in your application by adding it to your Eclipse project as follows:
  1. In the Package Explorer panel, right-click on your project and select Properties.
  2. Select Java Build Path, then the tab Libraries.
  3. Press the Add External JARs... button and select the JAR file.
Alternatively, if you want to include third party JARs with your package, create a new directory for them within your project and select Add Library... instead.
It is not necessary to put external JARs in the assets folder.

Lifetime of the new screen(Android)

An activity can remove itself from the history stack by calling Activity.finish() on itself, or the activity that opened the screen can call Activity.finishActivity() on any screens that it opens to close them.

Print Messages to a Log File(Android)

To write log messages from your application:
  1. Import android.util.Log.
  2. Use Log.v(), Log.d(), Log.i(), Log.w(), or Log.e() to log messages.
    E.g., Log.e(this.toString(), "error: " + err.toString())
  3. Launch DDMS from a terminal by executing ddms in your Android SDK /tools path.
  4. Run your application in the Android emulator.
  5. From the DDMS application, select the emulator (e.g., "emulator-5554") and click Device > Run logcat... to view all the log data.
MORE http://developer.android.com/resources/faq/commontasks.html#newandroidprojectnoeclipse