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% ...
Dark Theme elevation overlay hex 00dp 0% #121212 01dp 5% #1e1e1e 02dp 7% #222222 03dp 8% #242424 04dp 9% #272727 06dp 11% #2c2c2c 08dp 12% #2e2e2e 12dp 14% #333333 16dp 15% #343434 24dp 16% #383838

Custom Fonts

In android, you can define your own custom fonts for the strings in your application. You just need to download the required font, and then place it in assets/fonts folder. Y ou can access it in your java code through Typeface class. - Use  setTypeface()  method TextView textView = (TextView) findViewById(R.id. TextView1 ) ; Typeface custom_font = Typeface. createFromAsset (getAssets() , "fonts/font name.ttf" ) ; textView.setTypeface(custom_font) ;