Monday, July 9, 2012

Get Screen Brightness

In general the screen brightness varies in between 0 to 255. The below snippet helps to find the screen brightness.

public static int getScreenBrightness(Context context)
{
          try
          {
              return Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
          }
          catch (SettingNotFoundException e)
          {
              Log.e("getScreenBrightness()", ""+e.getMessage());
              return -1;
          }
}

We have to pass context of the activity from where we are calling this method.

This method will return screen brightness as a result.

Getting WI-FI strength in percentage

The below snippet helps to find the WI-FI strength in the percentage.

public static int getWifiStrength(Context
{
          try
          {
              WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
              int rssi = wifiManager.getConnectionInfo().getRssi();
              int level = WifiManager.calculateSignalLevel(rssi, 10);
              int percentage = (int) ((level/10.0)*100);
              return percentage;
          }
          catch (Exception e)
          {
              return 0;
          }
}


We have to pass context of the activity from where we are calling this method.

It will return WI-FI strength in percentage. In case if any exception found then it will return 0;

Get Application version based on package name

The below snippet helps to find the application version

public static String getVersionName(Context context, String strPkgName)
{
         try
         {
            return context.getPackageManager().getPackageInfo(strPkgName, 0).versionName;
         }
         catch (NameNotFoundException e)
         {
            Log.e("getVersionName()", ""+e.getMessage());
            return "";
         }
}

Here we have to pass context of the application and package name of the application that you want to find the version.

If the application exists(Installed) in the mobile then it will return the application version as string. otherwise it will return empty string.

Tuesday, April 17, 2012

Way to find device screen category (small, normal, large, xlarge)

The below snippet helps to find the screen catogory

public void getScreenSize()
      {
          switch (getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK)
          {
              case Configuration.SCREENLAYOUT_SIZE_SMALL:             
                  Toast.makeText(Home.this, "SMALL SCREEN", Toast.LENGTH_LONG).show();
                  break;
           
            case Configuration.SCREENLAYOUT_SIZE_LARGE:
                Toast.makeText(Home.this, "LARGE SCREEN", Toast.LENGTH_LONG).show();
                break;
           
            case Configuration.SCREENLAYOUT_SIZE_XLARGE:
                Toast.makeText(Home.this, "XLARGE SCREEN", Toast.LENGTH_LONG).show();
            break;
           
            case Configuration.SCREENLAYOUT_SIZE_NORMAL:
                Toast.makeText(Home.this, "NORMAL SCREEN", Toast.LENGTH_LONG).show();
            break;

         }

Tuesday, April 10, 2012

Get Available Internal Memory

The below snippet helps to find the available internal memory. It will return the result in MB.

/**
       * Calculates the available internal memory
       * @return internal memory available in float
       */
      private float getAvailableInternalMemory()
      {
              float availMB;
              File path ;
              StatFs stat;       
              long blockSize;           
              long availableBlocks;
              try
              {
                  path = Environment.getDataDirectory();
                  stat = new StatFs(path.getPath());
                  blockSize = stat.getBlockSize();
                  availableBlocks = stat.getAvailableBlocks();
                  availMB = (availableBlocks*blockSize)/(1024*1024);               
              }
              catch (Exception e)
              {
                  availMB = -1;
              }         
             
              return availMB;
      }

Monday, March 26, 2012

Overriding Home Key Android

After a deep search i came to know that there is a possibility to lock(Disable) home key in the android like back key. It was pretty easy. Add the below snippet to the activity in which you want to disable home key click...


@Override
public void onAttachedToWindow()
{
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}

Thursday, February 23, 2012

Expense Manager

Cool App ... Check it out from Android Market


The Expense Manager helps you to organize your expenses in a proper way. This app reduces the overhead of maintaining our expenses in a diary and tracking at the end of the month. Using this app we can track the expenses based on different options like By Category, Date, Month, year and Payment Mode in a simple way.



Available in Android Market








Monday, October 3, 2011

PopUpWindow in Android

Android supports Popup Window like in IPhone. The difference between pop up window and dialog is we have to set the position (like center, top, bottom...) and also x and y positions. The below snippets explains the way.
 Home.java:
package com.popup.activities;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;

public class Home extends Activity
{
    private Button btnShowPopUp;
    private PopupWindow mpopup;
       
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        btnShowPopUp = (Button)findViewById(R.id.btnShowPopUp);
       
        btnShowPopUp.setOnClickListener(new OnClickListener()
        {           
            @Override
            public void onClick(View arg0)
            {
                View popUpView = getLayoutInflater().inflate(R.layout.popup, null); // inflating popup layout
                mpopup = new PopupWindow(popUpView, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, true); //Creation of popup
                mpopup.setAnimationStyle(android.R.style.Animation_Dialog);  
                mpopup.showAtLocation(popUpView, Gravity.BOTTOM, 0, 0);    // Displaying popup
               
                Button btnOk = (Button)popUpView.findViewById(R.id.btnOk);
                btnOk.setOnClickListener(new OnClickListener()
                {                   
                    @Override
                    public void onClick(View v)
                    {
                        mpopup.dismiss();  //dismissing the popup
                    }
                });
               
                Button btnCancel = (Button)popUpView.findViewById(R.id.btnCancel);
                btnCancel.setOnClickListener(new OnClickListener()
                {                   
                    @Override
                    public void onClick(View v)
                    {
                        mpopup.dismiss(); dismissing the popup
                    }
                });
            }
        }); 
    }
}
main.xml:
<?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">
  
    <Button
        android:layout_width="wrap_content"
        android:id="@+id/btnShowPopUp"
        android:text="ShowPopUp"
        android:layout_height="wrap_content">
    </Button>
  
</LinearLayout>
popup.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:orientation="vertical"
  android:background="#90FFFFFF"
  android:layout_height="wrap_content">
 
  <TextView
      android:layout_width="fill_parent"
      android:text="PopUp Window"
      android:textColor="#000000"
      android:gravity="center"
      android:textStyle="bold"
      android:layout_height="wrap_content">
  </TextView>
 
  <Button
      android:layout_width="fill_parent"
      android:id="@+id/btnOk"
      android:text="Ok"
      android:layout_height="wrap_content">
  </Button>
 
  <Button
      android:layout_width="fill_parent"
      android:id="@+id/btnCancel"
      android:text="Cancel"
      android:layout_height="wrap_content">
  </Button>
 
</LinearLayout>

Sunday, September 18, 2011

Showing Buttons on either side of a 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:background="#FFFFFF"
    android:layout_height="fill_parent">
   
    <!-- Header -->
    <RelativeLayout
        android:layout_width="fill_parent"
        android:background="@drawable/top_header"
        android:paddingLeft="5dip"
        android:paddingRight="5dip"
        android:layout_height="50dip">
       
        <Button
            android:layout_width="wrap_content"
            android:background="@drawable/back_btn"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_height="wrap_content">
        </Button>
       
        <Button
            android:layout_width="wrap_content"
            android:background="@drawable/cancle_btn"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_height="wrap_content">
        </Button>
       
    </RelativeLayout>
   
    <!-- Body -->
     
</LinearLayout>

Friday, September 16, 2011

Three Level Expandable list

The below snippet helps to implement three level list in android.
First Group
--Sub Group
   ---   Child1
   ---   Child 2
   ---   Child 3
in this pattern.....
main.xml:
<?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">
 
    <ExpandableListView
    android:layout_width="fill_parent"
    android:id="@+id/ParentLevel"
    android:groupIndicator="@null"
    android:layout_height="fill_parent">
    </ExpandableListView>

</LinearLayout>

Home.java
package com.threeelevellist.activities;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class Home extends Activity 
{
    ExpandableListView explvlist;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        explvlist = (ExpandableListView)findViewById(R.id.ParentLevel);
        explvlist.setAdapter(new ParentLevel());
        
    }
    
    public class ParentLevel extends BaseExpandableListAdapter
    {

@Override
public Object getChild(int arg0, int arg1) 
{
return arg1;
}

@Override
public long getChildId(int groupPosition, int childPosition) 
{
return childPosition;
}

@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) 
{
CustExpListview SecondLevelexplv = new CustExpListview(Home.this);
SecondLevelexplv.setAdapter(new SecondLevelAdapter());
SecondLevelexplv.setGroupIndicator(null);
return SecondLevelexplv;
}

@Override
public int getChildrenCount(int groupPosition) 
{
return 3;
}

@Override
public Object getGroup(int groupPosition) 
{
return groupPosition;
}

@Override
public int getGroupCount() 
{
return 5;
}

@Override
public long getGroupId(int groupPosition) 
{
return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) 
{
TextView tv = new TextView(Home.this);
tv.setText("->FirstLevel");
tv.setBackgroundColor(Color.BLUE);
tv.setPadding(10, 7, 7, 7);
return tv;
}

@Override
public boolean hasStableIds() 
{
return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) 
{
return true;
}    
    }
    
    public class CustExpListview extends ExpandableListView
    {
int intGroupPosition, intChildPosition, intGroupid;
public CustExpListview(Context context) 
{
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
{
widthMeasureSpec = MeasureSpec.makeMeasureSpec(960, MeasureSpec.AT_MOST);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(600, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
    }
    
    public class SecondLevelAdapter extends BaseExpandableListAdapter
    {

@Override
public Object getChild(int groupPosition, int childPosition) 
{
return childPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) 
{
return childPosition;
}

@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) 
{
TextView tv = new TextView(Home.this);
tv.setText("child");
tv.setPadding(15, 5, 5, 5);
tv.setBackgroundColor(Color.YELLOW);
tv.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return tv;
}

@Override
public int getChildrenCount(int groupPosition) 
{
return 5;
}

@Override
public Object getGroup(int groupPosition) 
{
return groupPosition;
}

@Override
public int getGroupCount() 
{
return 1;
}

@Override
public long getGroupId(int groupPosition) 
{
return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) 
{
TextView tv = new TextView(Home.this);
tv.setText("-->Second Level");
tv.setPadding(12, 7, 7, 7);
tv.setBackgroundColor(Color.RED);
return tv;
}

@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
   
    }
}