viernes, 27 de febrero de 2015

Android: Focus lost on EditText inside row in ListView when soft keyboard appears

1) Add android:windowSoftInputMode="adjustPan" attribute into AndroidManifest.xml file on the activity where the ListView is presented.
2) Add android:descendantFocusability="afterDescendants" attribute onto the ListView itself.

viernes, 13 de febrero de 2015

Android: Save screenshot to file, write to an internal file

Save screenshot to file
private void saveScreenshot() {

  View view = (View) topMostView;
  view.setDrawingCacheEnabled(true);
  Bitmap b = view.getDrawingCache();
  String extr = Environment.getExternalStorageDirectory().toString();
  Date date = Calendar.getInstance().getTime();
  DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
  String now = formatter.format(date);
  File myPath = new File(extr, "screenshot_" + now + ".jpg");
  FileOutputStream fos = null;
  try {
   fos = new FileOutputStream(myPath);
   b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
   fos.flush();
   fos.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
Write to internal file (no permission are required)
String filename = "myfile";
String string = "Hello world!";
FileOutputStream outputStream;

try {
  outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
  outputStream.write(string.getBytes());
  outputStream.close();
} catch (Exception e) {
  e.printStackTrace();
}

martes, 3 de febrero de 2015

Android: Simple animation

Animation
private void animateOpen() {

        Animation loadAnimation = AnimationUtils.loadAnimation(getContext(),
                R.anim.slide_in_from_top);
        loadAnimation.setFillAfter(true);
        loadAnimation.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
                setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                opened = !opened;
            }
        });

        startAnimation(loadAnimation);
    }
res/anim/slide_in_from_top
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="500"
        android:fromYDelta="-100%"
        android:interpolator="@android:anim/decelerate_interpolator"
        android:toYDelta="0"
        android:zAdjustment="top" />

</set>