Skip to main content

Get Memory Details

  • activity_main.xml
<LinearLayout 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:orientation="vertical"
    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=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/GET_MEMORY_DETAIL"
        android:textColor="@color/Black"
        android:textSize="20sp" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="@color/Gray_divider" />

    <TextView
        android:id="@+id/txtAvailableInternal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:textColor="@color/Gray_Text" />

    <TextView
        android:id="@+id/txtTotalInternal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/Red" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="20dp"
        android:background="@color/Gray_divider" />

    <TextView
        android:id="@+id/txtExternalAvailable"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/Green" />

    <TextView
        android:id="@+id/txtAvailableExternal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/Blue" />

    <TextView
        android:id="@+id/txtTotalExternal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/Orange_divider" />

</LinearLayout>


  • values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="Green">#006400</color>
    <color name="Blue">#0e72c8</color>
    <color name="Red">#CC0000</color>
    <color name="Black">#000000</color>
    <color name="Gray_divider">#ccc</color>
    <color name="list_divider">#F2F2F2</color>
    <color name="Label_bg">#ececec</color>
    <color name="Orange_divider">#FE9A2E</color>
    <color name="Gray_Text">#808080</color>
    <color name="Gray_Header">#EBEBEB</color>

</resources>


  • values/string.xml
  <string name="GET_MEMORY_DETAIL">GET MEMORY DETAIL</string>


  • MainActivity.java
public class MainActivity extends Activity {

private TextView txtAvailableInternal, txtTotalInternal,
txtExternalAvailable, txtAvailableExternal, txtTotalExternal;

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

txtAvailableInternal = (TextView) findViewById(R.id.txtAvailableInternal);
txtTotalInternal = (TextView) findViewById(R.id.txtTotalInternal);
txtExternalAvailable = (TextView) findViewById(R.id.txtExternalAvailable);
txtAvailableExternal = (TextView) findViewById(R.id.txtAvailableExternal);
txtTotalExternal = (TextView) findViewById(R.id.txtTotalExternal);

txtAvailableInternal.setText("Available Internal MemorySize : "
+ getAvailableInternalMemorySize());

txtTotalInternal.setText("Total Internal MemorySize : "
+ getTotalInternalMemorySize());

txtExternalAvailable.setText("External Memory Available ? "
+ String.valueOf(externalMemoryAvailable()));

txtAvailableExternal.setText("Available External MemorySize : "
+ getAvailableExternalMemorySize());

txtTotalExternal.setText("Total External MemorySize : "
+ getTotalExternalMemorySize());

}

public static boolean externalMemoryAvailable() {
return android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
}

public static String getAvailableInternalMemorySize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return formatSize(availableBlocks * blockSize);
}

public static String getTotalInternalMemorySize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return formatSize(totalBlocks * blockSize);
}

public static String getAvailableExternalMemorySize() {
if (externalMemoryAvailable()) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return formatSize(availableBlocks * blockSize);
} else {
return "ERROR";
}
}

public static String getTotalExternalMemorySize() {
if (externalMemoryAvailable()) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return formatSize(totalBlocks * blockSize);
} else {
return "ERROR";
}
}

public static String formatSize(long size) {
String suffix = null;

if (size >= 1024) {
suffix = "KB";
size /= 1024;
if (size >= 1024) {
suffix = "MB";
size /= 1024;
}
}

StringBuilder resultBuffer = new StringBuilder(Long.toString(size));

int commaOffset = resultBuffer.length() - 3;
while (commaOffset > 0) {
resultBuffer.insert(commaOffset, ',');
commaOffset -= 3;
}

if (suffix != null)
resultBuffer.append(suffix);
return resultBuffer.toString();
}

@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;
}

}

Comments

Popular posts from this blog

Transparent Color Code

Transparent Color Code : Color hexadecimal notation is like the following:  #AARRGGBB A: alpha R: red G: green B: blue ____________________________________________ E.g. for 50% white you'd use             #80FFFFFF.              # ( Transparent  hex value ) (Any Color code) ____________________________________________ Normal opaque black hex- "#000000" Fully transparent - "#00000000" Fully opaque - "#FF000000" 50% transparent - "#80000000" ____________________________________________ Percentages to hex values.  100% — FF 95% — F2 90% — E6 85% — D9 80% — CC 75% — BF 70% — B3 65% — A6 60% — 99 55% — 8C 50% — 80 45% — 73 40% — 66 35% — 59 30% — 4D 25% — 40 20% — 33 15% — 26 10% — 1A 5% — 0D 0% — 00 ____________________________________________ All hex value from 100% to 0% alpha: 100% — FF 99% — FC 98% — FA 97% — F7 96% — F5 95% — F2 94% — F0 93% ...

Android Loading Control

Android Loading Control res/anim/ rotate_anim.xml <?xml version="1.0" encoding="utf-8"?> <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"     android:drawable="@drawable/img_loading"     android:pivotX="50%"     android:pivotY="50%" /> res/drawable/loading_anim.xml <?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android"     android:oneshot="false" >     <item         android:drawable="@drawable/img1"         android:duration="200"/>     <item         android:drawable="@drawable/img2"         android:duration="200"/>     <item         android:drawable="@drawable/img3"         android:duration="200"/> </animat...

onClick open browser with given URL

onClick  open browser with given  URL Output : // on click its open in browser with url = http://www.google.com Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(" http://www.google.com ")); intent.setComponent(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity")); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); getApplicationContext().startActivity(intent);