Pages

Friday 12 April 2013

Android - Hide android keyboard

Just put the below code and call the function whenever u want to hide the keyboard!
private void hideKeyboard(View currentFocusView) {
 if (currentFocusView instanceof EditText) {
  InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
  imm.hideSoftInputFromWindow(currentFocusView.getWindowToken(), 0);
 }
}

Tuesday 9 April 2013

Android remember fragment to back stack

Use following code to set current fragment to backstack and load new fragment

PdfFragment newFragment = new PdfFragment();

  Bundle args = new Bundle();
  args.putString("name", sub_category.name);
  args.putString("catId", String.valueOf(sub_category.id));
  args.putString("pdf_from", "sub_category");

  newFragment.setArguments(args);

  FragmentTransaction transaction = getSupportFragmentManager()
    .beginTransaction();

  // Replace whatever is in the fragment_container view with this
  // fragment,
  // and add the transaction to the back stack so the user can
  // navigate back
  transaction.setCustomAnimations(R.anim.left_to_right,
    R.anim.right_to_left);
  transaction.replace(R.id.fragment_container, newFragment);

// use following code to set current fragment to back stack and load new fragment
  transaction.addToBackStack(null);

  // Commit the transaction
  transaction.commit();

Monday 8 April 2013

Android How to handle back pressed event?

Following code redirect user to home screen rather then finish activity


@Override
public void onBackPressed() {
   Intent setIntent = new Intent(Intent.ACTION_MAIN);
   setIntent.addCategory(Intent.CATEGORY_HOME);
   setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   startActivity(setIntent);
}

Thursday 4 April 2013

Android how to open pdf in webview

You Need to use google doc viewer service.

please check following code

WebView mWebView = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true);  
String url = "http://docs.google.com/viewer?embedded=true&url="+"http://sitename.com/abc.pdf";
mWebview.loadUrl(url);