Showing posts from September, 2012

How to restart android Activty?

Here is how to restart an activity in Android.

public void reload() {

    Intent intent = getIntent();
    overridePendingTransition(0, 0);

    overridePendingTransition(0, 0);

But Since API level 11, you can use recreate() method. The recreate() method acts just like a configuration change, so your onSaveInstanceState() and onRestoreInstanceState() methods are also called, if applicable.

Android: Dynamically add View at the runtime

Here is the coding to add a view such a as a button, layout etc. at the runtime.

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup parent = (ViewGroup)findViewById(;
inflater.inflate(R.layout.the_child_view, parent);

How to hide Android Soft Keyboard

If you want to hide the Android Soft Keyboard, you can use following methods to do that.

Method 1

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
Method 2


Method 3

Add following code into the AndroidManifest.xml file, inside the Activity tag.


Method 4

Try below code inside onCreate() method in your activity class.

EditText edtView=(EditText)findViewById(;



***This method works as a means of getting around the "can't hide the soft keyboard" bug in 2.0 and 2.1 as described in

Method 5

If you work with TabHost and you want to hide the keyboard on onTabChanged() method, try the follow…

Java : Get size and orientation of android device

// First, you need to get the Display from the WindowManager Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); // Now you can call all display-related methodsint width = display.getWidth(); int height = display.getHeight(); int orientation = display.getOrientation();

How to get icons of Installed Applications in Android

I wanted to get icons of running activities And here I done it.

List<PackageInfo> packs = getPackageManager().getInstalledPackages(0); Drawable[] icons=new Drawable[packs.size]; for(int i=0;i<packs.size();i++) {     PackageInfo p = packs.get(i);     icons[i]= p.applicationInfo.loadIcon(getPackageManager());  } And you may need to convert the return values to Bitmap.

Android: How to add static header to the top of a ListActivity

I wanted to add static header to my ListView and here how I done it.

In my ListActivity Class.

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ListView listView = getListView(); LayoutInflater inflater = getLayoutInflater(); ViewGroup header = (ViewGroup)inflater.inflate(R.layout.header, lv, false); listView.addHeaderView(header, null, false); String[] values = getResources().getStringArray(R.array.reg_dates); ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, values); setListAdapter(adapter); } And here is my header.xml file.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+…