Android Flash Light Example?

Today am going to write the on Android Flash Light, This Flash light is working only for Camera flash is available in mobile device.
We can declare this user permission in manifest file.

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />

http://developer.android.com/reference/android/hardware/Camera.Parameters.html

MainActivity.java

  1. public class FlashlightActivity extends Activity {
    private Camera camera;
    private ToggleButton button;
    private final Context context = this; 
    @Override
    protected void onStop() {
    super.onStop();
    if (camera != null) {
    camera.release();
    }
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.flashlight);
    button = (ToggleButton) findViewById(R.id.togglebutton);

    final PackageManager pm = context.getPackageManager();
    if (!isCameraSupported(pm)) {
    AlertDialog alertDialog = new AlertDialog.Builder(context).create();
    alertDialog.setTitle("No Camera");
    alertDialog.setMessage("The present device's doesn't support camera.");
    alertDialog.setButton(RESULT_OK, "OK", new DialogInterface.OnClickListener() {
    public void onClick(final DialogInterface dialog, final int which) {
    Log.e("err", "The device's doesn't support camera.");
    }
    });
    alertDialog.show();
    }
    camera = Camera.open();
    }

    public void onToggleClicked(View view) {
    PackageManager pm = context.getPackageManager();
    final Parameters p = camera.getParameters();
    if (isFlashSupported(pm)) {
    boolean on = ((ToggleButton) view).isChecked();
    if (on) {
    Log.i("info", "torch is turn on!");
    p.setFlashMode(Parameters.FLASH_MODE_TORCH);
    camera.setParameters(p);
    camera.startPreview();
    } else {
    Log.i("info", "torch is turn off!");
    p.setFlashMode(Parameters.FLASH_MODE_OFF);
    camera.setParameters(p);
    camera.stopPreview();
    }
    } else {
    button.setChecked(false);
    AlertDialog alertDialog = new AlertDialog.Builder(context).create();
    alertDialog.setTitle("No Camera Flash");
    alertDialog.setMessage("The device's camera doesn't support flash.");
    alertDialog.setButton(RESULT_OK, "OK", new DialogInterface.OnClickListener() {
    public void onClick(final DialogInterface dialog, final int which) {
    Log.e("err", "The device's camera doesn't support flash.");
    }
    });
    alertDialog.show();
    }
    }  
    private boolean isFlashSupported(PackageManager packageManager) {
    // if device support camera flash?
    if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
    return true;
    }
    return false;
    }

    private boolean isCameraSupported(PackageManager packageManager) {
    // if device support camera?
    if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
    return true;
    }
    return false;
    }
    } 




flashlight.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:id="@+id/relativeLayout1"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent" >

  6.     <ToggleButton
  7.         android:id="@+id/togglebutton"
  8.         android:layout_width="wrap_content"
  9.         android:layout_height="wrap_content"
  10.         android:layout_marginTop="20dp"
  11.         android:gravity="center"
  12.         android:onClick="onToggleClicked"
  13.         android:textOff="Turn Flashlight on"
  14.         android:textOn="Turn Flashlight off" />

  15. </RelativeLayout>
  16.  



AndroidManifest.xml

  1.  <uses-permission android:name="android.permission.CAMERA" />
  2.  <uses-feature android:name="android.hardware.camera" />



Simple Adapter Example?

Let’s Code!

This Example is Also used to click any specific row that data display in the  Second Activity.
We only need this files to run this sample code successfully.
activity_main.xml -- our main layout. Our trigger to show the ListView


  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     tools:context=".MainActivity" >

  6.     <ListView
  7.         android:id="@+id/listView"
  8.         android:layout_width="match_parent"
  9.         android:layout_height="wrap_content" >
  10.     </ListView>

  11. </RelativeLayout>
  12.  
MainActivity.java
  1. public class MainActivity extends Activity {
  2. ListView listview;

  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);

  6. String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
  7. "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2" };
  8.    int[] images = new int[] {R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4,
  9. R.drawable.image5, R.drawable.ic_launcher };
  10. listview = (ListView) findViewById(R.id.listView);

  11. // use your own layout
  12. MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, R.layout.rowlayout, values, images);
  13. listview.setAdapter(adapter);
  14. listview.setOnItemClickListener(new OnItemClickListener() {
  15. @Override
  16. public void onItemClick(AdapterView<?> arg0, View view, int id, long position) {
  17. Object  ob = (Object) view.getTag();  
  18. if(ob != null)  {
  19. TotalFavouriteInfo history_title = (TotalFavouriteInfo) view.getTag();  
  20. String title = history_title.getPage_name();
  21. Bitmap image = history_title.getPage_date();
  22.   Toast.makeText(MainActivity.this, "position ==> " +position + title + image , Toast.LENGTH_SHORT).show();
  23. Intent intent = new Intent(MainActivity.this, SecondActivity.class);
  24. intent.putExtra("position", title);
  25. intent.putExtra("mage_bit", image);
  26. startActivity(intent);
  27.   }
  28. }
  29. });
  30. }
  31. } 
MySimpleArrayAdapter.java
  1. package com.example.adapterexample;

  2.  
  3. import android.content.Context;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.ArrayAdapter;
  8. import android.widget.ImageView;
  9. import android.widget.TextView;

  10. public class MySimpleArrayAdapter extends ArrayAdapter<String> {
  11.   private final Context context;
  12.   private final String[] values;
  13.   private final int[] images;

  14.   public MySimpleArrayAdapter(Context context,int _names, String[] values, int[] images) {
  15.     super(context, R.layout.rowlayout, values);
  16.     this.context = context;
  17.     this.values = values;
  18.     this.images = images;
  19.   }

  20.   @Override
  21.   public View getView(int position, View convertView, ViewGroup parent) {  
  22.  if(convertView == null) {
  23.  LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  24.  convertView = inflater.inflate(R.layout.rowlayout, parent, false);    
  25.  }
  26.  
  27.  TextView textView = (TextView) convertView.findViewById(R.id.textView1);
  28.  ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
  29.  
  30.  textView.setText(values[position]);  
  31.  
  32.  String string = values[position];  
  33.  if(string.equals("WindowsMobile")) {
  34.  imageView.setImageResource(R.drawable.image2);
  35.  } else if (string.equals("Windows7")){
  36.  imageView.setImageResource(R.drawable.image3);
  37.  } else if (string.equals("Ubuntu")){
  38.  imageView.setImageResource(R.drawable.image1);
  39.  } else if (string.equals("WebOS")){
  40.  imageView.setImageResource(R.drawable.image4);
  41.  } else if (string.equals("Linux")){
  42.  imageView.setImageResource(R.drawable.image5);
  43.  } else {
  44.  imageView.setImageResource(R.drawable.ic_launcher);
  45.  } 
  46.  
  47.  TotalFavouriteInfo total_acc = new TotalFavouriteInfo();
  48.  total_acc.setPage_name(string);
  49.  
  50.  convertView.setTag(total_acc);   
  51.  return convertView;
  52.   }
  53.  
rowlayout.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="horizontal" >

  6.     <LinearLayout
  7.         android:layout_width="match_parent"
  8.         android:layout_height="50dp"
  9.         android:orientation="horizontal" >

  10.         <ImageView
  11.             android:id="@+id/imageView1"
  12.             android:layout_marginLeft="5dp"
  13.             android:layout_width="50dp"
  14.             android:layout_height="50dp"
  15.             android:src="@drawable/ic_launcher" />

  16.         <TextView
  17.             android:id="@+id/textView1"
  18.             android:layout_marginLeft="10dp"
  19.             android:layout_width="wrap_content"
  20.             android:layout_height="wrap_content"
  21.             android:layout_gravity="center"
  22.             android:text="TextView" />
  23.     </LinearLayout>

  24. </LinearLayout>
TotalFavouriteInfo.java
  1. public class TotalFavouriteInfo {
  2. private String page_name;
  3. private Bitmap page_date;

  4. public Bitmap getPage_date() {
  5. return page_date;
  6. }
  7. public void setPage_date(Bitmap bmap) {
  8. this.page_date = bmap;
  9. }
  10. public String getPage_name() {
  11. return page_name;
  12. }
  13. public void setPage_name(String acc_info) {
  14. this.page_name = acc_info;
  15. }
  16. }
  17.  
SecondActivity.java
  1. public class SecondActivity extends Activity {

  2. protected void onCreate(Bundle saveInstanteState) {
  3. super.onCreate(saveInstanteState);
  4. setContentView(R.layout.second);

  5. TextView button = (TextView) findViewById(R.id.textView1);
  6. ImageView image = (ImageView) findViewById(R.id.imageView1);
  7. Intent intent = getIntent();
  8. Bitmap bitmap = (Bitmap) intent.getParcelableExtra("mage_bit");
  9. String title = getIntent().getExtras().getString("position");
  10.   Toast.makeText(SecondActivity.this, title, Toast.LENGTH_SHORT).show();
  11. button.setText(title);
  12.   image.setImageBitmap(bitmap);
  13. }
  14. }
second.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical" >

  6.     <TextView
  7.         android:id="@+id/textView1"
  8.         android:layout_width="wrap_content"
  9.         android:layout_height="wrap_content"
  10.         android:text="TextView" />

  11.     <ImageView
  12.         android:id="@+id/imageView1"
  13.         android:layout_width="wrap_content"
  14.         android:layout_height="wrap_content"
  15.         android:src="@drawable/ic_launcher" />

  16. </LinearLayout>

Android TelephonyManager Details?

The android.telephony.TelephonyManager (TELEPHONYMANAGER) class provides information about the telephony services such as subscriber id, sim serial number, phone network type etc. Moreover, you can determine the phone state etc.

Let's see the simple example of TelephonyManager that prints information of the telephony services As for new Eclipse up date like up date(Add new library project to Main Application, Mainxml and as well as fragementxml file).

activity_main.xml


  1.  <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.telephonemanager.MainActivity"
        tools:ignore="MergeRootFrame" />

fragment_main.xml

  1.   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.telephonemanager.MainActivity$PlaceholderFragment" >

        <TextView
            android:id="@+id/textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />

    </RelativeLayout>
MainActivity.java

  1.   public class MainActivity extends ActionBarActivity {
    private static TelephonyManager  tm;
    private static String IMEINumber, subscriberID,SIMSerialNumber,networkCountryISO,
    SIMCountryISO,softwareVersion,voiceMailNumber;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Get the instance of TelephonyManager  
    tm=(TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);  
            
    //Calling the methods of TelephonyManager the returns the information  
    IMEINumber=tm.getDeviceId();  
            subscriberID=tm.getDeviceId();  
            SIMSerialNumber=tm.getSimSerialNumber();  
            networkCountryISO=tm.getNetworkCountryIso();  
            SIMCountryISO=tm.getSimCountryIso();  
            softwareVersion=tm.getDeviceSoftwareVersion();  
            voiceMailNumber=tm.getVoiceMailNumber();  
            
            if (savedInstanceState == null) {
             getSupportFragmentManager().beginTransaction()
             .add(R.id.container, new PlaceholderFragment()).commit();
            }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
    return true;
    }
    return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
    private TextView textView1;
    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    textView1=(TextView) rootView.findViewById(R.id.textview);  
    //textView1.setText("new data of this");
    //Get the phone type  
            String strphoneType="";             
            int phoneType=tm.getPhoneType();  
      
            switch (phoneType)   {  
            case (TelephonyManager.PHONE_TYPE_CDMA):  
             strphoneType="CDMA";  
            break;  
            case (TelephonyManager.PHONE_TYPE_GSM):   
             strphoneType="GSM";                
            break;  
            case (TelephonyManager.PHONE_TYPE_NONE):  
             strphoneType="NONE";                
            break;  
            }    
          //getting information if phone is in roaming  
            boolean isRoaming=tm.isNetworkRoaming();  
              
            String info="Phone Details:\n";  
            info+="\n IMEI Number:"+IMEINumber;  
            info+="\n SubscriberID:"+subscriberID;  
            info+="\n Sim Serial Number:"+SIMSerialNumber;  
            info+="\n Network Country ISO:"+networkCountryISO;  
            info+="\n SIM Country ISO:"+SIMCountryISO;  
            info+="\n Software Version:"+softwareVersion;  
            info+="\n Voice Mail Number:"+voiceMailNumber;  
            info+="\n Phone Network Type:"+strphoneType;  
            info+="\n In Roaming? :"+isRoaming;  
            //displaying the information in the textView 
            textView1.setText(info); 
            
            return rootView;
    }
    }
    }
 AndroidManifest.xml

You need to give READ_PHONE_STATE permission in the AndroidManifest.xml file.

Output





Android Display Google Map in Android Device?

GETTING KEY FROM GOOGLE CONSOLE

Before we create Application Generate API key in  Google API Console .

1. Now open Google API Console
2. Select Services on left side and turn on Google Maps Android API v2

3. Now select API Access on left side and on the right side click on Create new Android key…
4.  I have given like below
BE:03:E1:44:39:7B:E8:17:02:9F:7F:B7:98:82:EA:DF:84:D0:FB:6A;com.example.name
And note down the API key which required later in our project.

After that we can write code like this.

MAINACTIVITY.JAVA


  1. public class MainActivity extends FragmentActivity {
    // Google Map
    private GoogleMap googleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try {
    // Loading map
    initializeMap();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    /**
     * function to load map. If map is not created it will create it for you
     * */
    private void initializeMap() {
    if (googleMap == null) {
    googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    // check if map is created successfully or not
    if (googleMap == null) {
    Toast.makeText(getApplicationContext(),"Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
    }
    }
    }

    @Override
    protected void onResume() {
    super.onResume();
    initializeMap();
    }
    }
XML.JAVA

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

        <fragment
            android:id="@+id/map"
            class="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </RelativeLayout>

Finally we add Manifest.xml file

  1. <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.googlemaps.nag"
        android:versionCode="1"
        android:versionName="1.0" >

        <permission
            android:name="com.googlemaps.nag.permission.MAPS_RECEIVE"
            android:protectionLevel="signature" />

        <uses-permission android:name="com.googlemaps.nag.permission.MAPS_RECEIVE" />

        <uses-sdk
            android:minSdkVersion="12"
            android:targetSdkVersion="18" />

        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

        <!-- Required to show current location -->
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

        <uses-feature
            android:glEsVersion="0x00020000"
            android:required="true" />

        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.googlemaps.nag.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>

            <meta-data

                android:name="com.google.android.maps.v2.API_KEY"
                android:value="YOUR API KEY" />
            <meta-data
                android:name="com.google.android.gms.version"
                android:value="4323000" />  // depending on the version value 
        </application>

    </manifest>


OUTPUT





Select DateRange UsingRangePicker.

  /* * This Method is for select range from picker. * */ private fun selectDateRangeUsingRangePicker () { pageNumber = 1 val displ...