Saturday 23 May 2015

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

No comments
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: 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="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:Write 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:Write 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: Now Run Your Project:




No comments :

Post a Comment

Follow me Share