Sunday, May 31, 2015

SDcard information

          Hello friends, in this android example i show you how to get storage information of SDcard, And check status of SDcard like its mounted or available in devise or not.here is simple code is to get Total size of SDcard and Remaining space.Lets go for coding.In this we get size of storage in GB,MB,KB and Byte.


CREATE NEW ANDROID PROJECT

Step 1: Write code into activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/state"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15dip"
        android:textStyle="bold"
        android:typeface="normal" >
    </TextView>

    <TextView
        android:id="@+id/info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dip" >
    </TextView>

</LinearLayout>


Step 2:Write code into MainActivity.java


package dev.Androidapplink.sdcardinfoapp;

import java.text.NumberFormat;

import adm.emprovantiontechsol.sdcardinfoapp.R;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.widget.TextView;

public class MainActivity extends Activity 
{
//the statistics of the SD card
private StatFs stats;
//the state of the external storage
private String externalStorageState;

//the total size of the SD card
private double totalSize;
//the available free space
private double freeSpace;

//a String to store the SD card information
private String outputInfo;
//a TextView to output the SD card state
private TextView tv_state;
//a TextView to output the SD card information
private TextView tv_info;

//set the number format output
private NumberFormat numberFormat;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //initialize the Text Views with the data at the main.xml file
        tv_state = (TextView)findViewById(R.id.state);
        tv_info = (TextView)findViewById(R.id.info);
        
        //get external storage (SD card) state
        externalStorageState = Environment.getExternalStorageState();
        
        //checks if the SD card is available in device 
        if(externalStorageState.equals(Environment.MEDIA_MOUNTED)
        || externalStorageState.equals(Environment.MEDIA_UNMOUNTED)
        || externalStorageState.equals(Environment.MEDIA_MOUNTED_READ_ONLY))
        {
        //obtain the stats from the root of the SD card.
        stats = new StatFs(Environment.getExternalStorageDirectory().getPath());
       
        //Add 'Total Size' to the output string:
        outputInfo = "\nTotal Size:\n";
       
        //total usable size
        totalSize = stats.getBlockCount() * stats.getBlockSize();
       
        //initialize the NumberFormat object
        numberFormat = NumberFormat.getInstance();
        //disable grouping
        numberFormat.setGroupingUsed(false);
        //display numbers with two decimal places
        numberFormat.setMaximumFractionDigits(2); 
       
        //SD card's total size in gigabytes, megabytes, kilobytes and bytes
        outputInfo += "Size in gigabytes: " + numberFormat.format((totalSize / (double)1073741824)) + " GB \n"
        + "Size in megabytes: " + numberFormat.format((totalSize / (double)1048576)) + " MB \n" 
        + "Size in kilobytes: " + numberFormat.format((totalSize / (double)1024)) + " KB \n" 
        + "Size in bytes: " +  numberFormat.format(totalSize) + " B \n"; 
       
        //Add 'Remaining Space' to the output string:
        outputInfo += "\nRemaining Space:\n";
       
        //available free space
        freeSpace = stats.getAvailableBlocks() * stats.getBlockSize();
       
        //SD card's available free space in gigabytes, megabytes, kilobytes and bytes
        outputInfo += "Size in gigabytes: " + numberFormat.format((freeSpace / (double)1073741824)) + " GB \n"
        + "Size in megabytes: " + numberFormat.format((freeSpace / (double)1048576)) + " MB \n" 
        + "Size in kilobytes: " + numberFormat.format((freeSpace / (double)1024)) + " KB \n"
        + "Size in bytes: " + numberFormat.format(freeSpace) + " B \n"; 
       
        //output the SD card state
        tv_state.setTextColor(Color.GREEN);
        tv_state.setText("SD card found! SD card is " + externalStorageState +".");
       
        //output the SD card info
        tv_info.setText(outputInfo);
        }
        else //external storage was not found
        {
        //output the SD card state
        tv_state.setTextColor(Color.RED);
        tv_state.setText("SD card not found! SD card state is \"" + externalStorageState + "\".");
        }
    }
}

NOTE:Give permission in AndroidManifest.xml

 <uses-permission android:name="android.permission.STORAGE" />


Step 3:Run project to see Output:




Start New Activity on button click Example

In this tutorial, we show you how to interact with activity by implement On button click activity. when a button is clicked, go one screen to another screen.Open one XML file to another XML file on button click.Most applications have multiple activities to represent different screens, for e.g. one activity to display a list of the application status and another activity is to display the application functions,settings etc...

CREATE NEW ANDROID PROJECT

In this example we need Two XML file and Two java file.

XML:

1) activity_main.xml
2) activity_main2.xml


JAVA:

1) MainActivity.java
2) MainActivity2.java


Step 1: Write code into activity_main.xml


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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="First screen (activity_main.xml)"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Click me" />

</LinearLayout>

Step 2: Write code into activity_main2.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Second screen (activity_main2.xml)"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

Step 3: Write code into MainActivity.java

package com.example.activityapp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

Button button;

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

public void addListenerOnButton() {

final Context context = this;

button = (Button) findViewById(R.id.button1);

button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

Intent intent = new Intent(context, MainActivity2.class);
                startActivity(intent);   

}

});

}

}


Step 4: Write code into MainActivity2.java

package com.example.activityapp;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;

public class MainActivity2 extends Activity {

Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}

}


Step 5: write code into AndroidManifest.xml

Register second activity in manifest file.

        <activity
            android:name=".MainActivity2"
            android:label="@string/app_name" >
        </activity>

IT's look like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.activityapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.activityapp.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity2"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>

Step 6: Run your project and see Output:



Saturday, May 23, 2015

Set color In XML

In this chapter we learn how to set color using HEX code, its simple way. to set color of textview,text color,searchview,background color,layout color etc. here we take example to set color.

CREATE NEW PROJECT

Step 1: Write code into activity_main.xml

Set color by write direct HEX code:e.g "#ffffff" or RGB "#fff".

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#ffffff"
    tools:context=".MainActivity" >

Set layout color:

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="76dp"
        android:background="#aa0000" >

    </LinearLayout>

Set Button text color

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/linearLayout1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="85dp"
        android:text="Button"
        android:textColor="#0000ff" />

Set text view color:

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp"
        android:background="#00f033"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Output:

Time converter for convert time into hour,minute or in a second and rate per hour or minute

In this example we will learn convert time into hour,minute or in a second. and also convert rate per hour or minute.

Step 1:copy code into activity_main.xml


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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >


    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Time converter" />

        <Spinner

            android:id="@+id/Spinner01"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:spinnerMode="dialog"
            android:textColor="#A72CFC" />

        <EditText

            android:id="@+id/etValue"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="@string/enter_value"
            android:inputType="number"
            android:textColor="#fff" >

            <requestFocus />

        </EditText>

        <TextView

            android:id="@+id/tvAns"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="answer" />

        <LinearLayout

            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <Button

                android:id="@+id/bth1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="hour" />

            <Button

                android:id="@+id/btm1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="minute" />

            <Button

                android:id="@+id/bts1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="second" />
        </LinearLayout>

        <TextView

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Rate per hour or minute" />

        <Spinner

            android:id="@+id/Spinner02"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:spinnerMode="dialog" />


        <EditText

            android:id="@+id/EditText02"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="value"
            android:inputType="number"
            android:textColor="#fff" />

        <EditText

            android:id="@+id/EtRs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/enter_rupi"
            android:inputType="number"
            android:textColor="#fff" />

        <TextView

            android:id="@+id/tvAns2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="answer" />

        <LinearLayout

            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <Button

                android:id="@+id/bth2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="hour" />

            <Button

                android:id="@+id/btm2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="minute" />
        </LinearLayout>


Step 2:copy code into spineritem.xml its custom spiner, you can create your own style.e.g change background color or font color etc.


<TextView

    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="17sp"
    android:padding="8dp"
    android:gravity="center"
    android:textStyle="normal"
    android:textColor="#020202">
 
</TextView>

Step 3:copy code into MainActivity.java


package com.converter.Timeconverter;


import java.util.ArrayList;

import java.util.List;

import  com.converter.Timeconverter.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

protected int mPos;
protected String mSelection;

Spinner sp1, sp2;

EditText edtval1, edttxt2, edttxtrs;
TextView tvans1, tvans2;
Button bth1, btm1, bts1, bth2, btm2, btnhlp;
String sp12, sp22;

@Override

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
// WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.activity_main);

sp1 = (Spinner) findViewById(R.id.Spinner01);
sp2 = (Spinner) findViewById(R.id.Spinner02);
edtval1 = (EditText) findViewById(R.id.etValue);
edttxt2 = (EditText) findViewById(R.id.EditText02);
edttxtrs = (EditText) findViewById(R.id.EtRs);
tvans1 = (TextView) findViewById(R.id.tvAns);
tvans2 = (TextView) findViewById(R.id.tvAns2);
bth1 = (Button) findViewById(R.id.bth1);
btm1 = (Button) findViewById(R.id.btm1);
bts1 = (Button) findViewById(R.id.bts1);
bth2 = (Button) findViewById(R.id.bth2);
btm2 = (Button) findViewById(R.id.btm2);
btnhlp = (Button) findViewById(R.id.bthelp1);

List<String> list = new ArrayList<String>();

list.add("Select Category");
list.add("Hour");
list.add("Minute");
list.add("Second");

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,

R.layout.spineritem, list);
sp1.setAdapter(dataAdapter);

sp1.setOnItemSelectedListener(new OnItemSelectedListener() {


@Override

public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// TODO Auto-generated method stub
sp12 = parent.getItemAtPosition(pos).toString();
}

@Override

public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}

});
bth1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

String s = edtval1.getText().toString();


if (s.length() == 0) {

Toast.makeText(getApplicationContext(), "Empty Text Field",
Toast.LENGTH_LONG).show();

}


else if (sp12.equals("Select Category")) {

Toast.makeText(getApplicationContext(),
"Select Any Category ", Toast.LENGTH_LONG).show();
}

else if (sp12.equals("Hour")) {

int m = Integer.parseInt(edtval1.getText().toString());
tvans1.setText("" + m);
}

else if (sp12.equals("Minute")) {

int m = Integer.parseInt(edtval1.getText().toString());
float n = m;
n = (float) (n / 60.0);
tvans1.setText("" + n);
}

else if (sp12.equals("Second")) {

int m = Integer.parseInt(edtval1.getText().toString());
float k;
float n = m;
n = (float) (n / 60.0);
k = (float) (n / 60.0);
tvans1.setText("" + k);
}
}
});

btm1.setOnClickListener(new OnClickListener() {


@Override

public void onClick(View arg0) {
// TODO Auto-generated method stub

String s = edtval1.getText().toString();


if (s.length() == 0) {

Toast.makeText(getApplicationContext(), "Empty Text Field",
Toast.LENGTH_LONG).show();

} else if (sp12.equals("Select Category")) {

Toast.makeText(getApplicationContext(),
"Select Any Category ", Toast.LENGTH_LONG).show();
} else if (sp12.equals("Minute")) {
int m = Integer.parseInt(edtval1.getText().toString());
int n = m;
float k = n;

tvans1.setText("" + m);


}


else if (sp12.equals("Second")) {

// n = n / 60.0;

int m = Integer.parseInt(edtval1.getText().toString());

int n = m;
float k = n;

k = (float) (n / 60.0);

tvans1.setText("" + k);
}

else if (sp12.equals("Hour")) {


int m = Integer.parseInt(edtval1.getText().toString());

int n = m;
float k = n;

n = n * 60;

tvans1.setText("" + n);
}
}
});

bts1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

String s = edtval1.getText().toString();


if (s.length() == 0) {

Toast.makeText(getApplicationContext(), "Empty Text Field",
Toast.LENGTH_LONG).show();

} else if (sp12.equals("Select Category")) {

Toast.makeText(getApplicationContext(),
"Select Any Category ", Toast.LENGTH_LONG).show();
}

else if (sp12.equals("Hour")) {

int m = Integer.parseInt(edtval1.getText().toString());
int n = m;
n = n * 60 * 60;
tvans1.setText("" + n);
}

else if (sp12.equals("Minute")) {

int m = Integer.parseInt(edtval1.getText().toString());
int n = m;
n = n * 60;
tvans1.setText("" + n);

}


else if (sp12.equals("Second")) {

int m = Integer.parseInt(edtval1.getText().toString());
int n = m;
tvans1.setText("" + n);

}

}
});

List<String> list1 = new ArrayList<String>();


list1.add("Select Category");

list1.add("Hour");
list1.add("Minute");

ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this,

R.layout.spineritem, list1);
sp2.setAdapter(dataAdapter1);

sp2.setOnItemSelectedListener(new OnItemSelectedListener() {


@Override

public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// TODO Auto-generated method stub

sp22 = parent.getItemAtPosition(pos).toString();


edttxt2.setText("");

edttxtrs.setText("");
edttxt2.setFocusable(true);
// tvans2.setTextColor();
tvans2.setText("Answer");

}


@Override

public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}

});

btm2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

String edtxr = edttxtrs.getText().toString();

String edt2 = edttxt2.getText().toString();

if (edtxr.length() == 0 || edt2.length() == 0) {


Toast.makeText(getApplicationContext(), "Empty Text Field",

Toast.LENGTH_LONG).show();
}

else if (sp22.equals("Select Category")) {

Toast.makeText(getApplicationContext(),
"Select Any Category ", Toast.LENGTH_LONG).show();

} else if (sp22.equals("Minute")) {

float m = Float.parseFloat(edttxt2.getText().toString());
float n = Integer.parseInt(edttxtrs.getText().toString());
float k, s;
s = (float) (n / m);
// s = s*60;
tvans2.setText("" + s);

} else if (sp22.equals("Hour")) {

float m = Float.parseFloat(edttxt2.getText().toString());
float n = Integer.parseInt(edttxtrs.getText().toString());
float k, s;
k = n / (m * 60);
tvans2.setText("" + k);
}

}

});
bth2.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {
// TODO Auto-generated method stub

String edtxr = edttxtrs.getText().toString();

String edt2 = edttxt2.getText().toString();

if (edtxr.length() == 0 || edt2.length() == 0) {


Toast.makeText(getApplicationContext(), "Empty Text Field",

Toast.LENGTH_LONG).show();
}

else if (sp22.equals("Select Category")) {


Toast.makeText(getApplicationContext(),

"Select Any Category ", Toast.LENGTH_LONG).show();
} else if (sp22.equals("Hour")) {
float m = Float.parseFloat(edttxt2.getText().toString());
float n = Integer.parseInt(edttxtrs.getText().toString());
float s, k;
k = n / m;
tvans2.setText("" + k);

} else if (sp22.equals("Minute")) {

float m = Float.parseFloat(edttxt2.getText().toString());
float n = Integer.parseInt(edttxtrs.getText().toString());
float s, k;
s = ((n * 60) / m);
tvans2.setText("" + s);

}

}
});
btnhlp.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {
// TODO Auto-generated method stub

Intent i = new Intent(getApplicationContext(), helpAc.class);

startActivity(i);

}

});

}


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

}


NOTE: No need special permission in AndroidManifest.xml


Step 5:Run Your time converter.