Temperature Conversion app in Android Studio

Create an application on android studio that allow the users to convert temperature from Celsius to Fahrenheit and Kelvin scale.

 

Let us start actual programming with Android​​ Framework. I assume that you have a little bit working knowledge with Android studio.

Download Android Studio from the link given blow according to your operating system Windows, Mac, Linux, Chrome OS etc.

Link:​​ https://developer.android.com/studio

More downloads are available here.​​ 

Link:​​ https://developer.android.com/studio/archive

 

Change the package name in your program.

make two fragments​​ fragment_a and fragment_b and copy the java and xml code.

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context="com.example.kishan.task629_2.MainActivity">

<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/l1"
android:orientation="horizontal"
android:layout_marginBottom="40dp"
>
<fragment
android:id="@+id/fragment"
android:name="com.example.kishan.task629_2.FragmentA"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/l2"
android:orientation="horizontal"
>

<fragment
android:id="@+id/fragment2"
android:name="com.example.kishan.task629_2.FragmentB"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>


</LinearLayout>

MainActivity.java :

package com.example.kishan.task629_2;

import​​ android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements FragmentA.Communicator{

@Override
protected void​​ onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void comm(String result) {

FragmentManager fm = getSupportFragmentManager();
FragmentB b = (FragmentB)fm.findFragmentById(R.id.fragment2);
b.getData(result);

}
}


fragment_a.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="wrap_content"
android:orientation="vertical"
android:padding="20dp"
android:background="#cfddaa"
tools:context="com.example.kishan.task629_2.FragmentA">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Tempreature Converter"
android:textSize="30dp"
android:textStyle="bold"
android:layout_marginBottom="20dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter tempreature in​​ "
android:textSize="25sp"
android:id="@+id/et1"
android:background="#fff"
android:layout_marginBottom="10dp"
/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:padding="10dp"
android:background="#dfccaa"
>
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="°F"
android:textSize="30sp"
android:id="@+id/b1"
android:background="#6789ff"
android:textStyle="bold"
android:layout_marginRight="30dp"
android:layout_marginLeft="20dp"
android:textColor="#fff"
/>
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="K"
android:background="#6789ff"
android:textSize="30sp"
android:id="@+id/b2"
android:textStyle="bold"
android:textColor="#fff"
android:layout_marginRight="20dp"
/>
</LinearLayout>
</LinearLayout>


</LinearLayout>

FragmentA.java :

package com.example.kishan.task629_2;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

public class FragmentA extends Fragment {

EditText et1;
Button b1,b2;
Communicator c;
public FragmentA() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_a, container, false);
et1 = (EditText) v.findViewById(R.id.et1);
b1 = (Button) v.findViewById(R.id.b1);
b2 = (Button) v.findViewById(R.id.b2);
return v;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
float f = (float) Float.parseFloat(et1.getText().toString());
double temp = (f*1.8)+32.0;
c.comm(temp+" °F");
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int f = (int) Integer.parseInt(et1.getText().toString());
int temp = f+273;
c.comm(temp+" K");
}
});
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
try{
c = (Communicator) context;
}
catch (Exception e){}
}

public interface Communicator{
void comm(String result);
}
}

fragment_b.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="wrap_content"
android:layout_margin="20dp"
tools:context="com.example.kishan.task629_2.FragmentB">


<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tv1"
android:textSize="30sp"
android:textAlignment="center"
/>

</LinearLayout>

FragmentB.java :

package com.example.kishan.task629_2;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


/**
* A simple {@link Fragment} subclass.
*/
public class FragmentB extends Fragment {

TextView tv;
public FragmentB() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this​​ fragment
View v = inflater.inflate(R.layout.fragment_b, container, false);
tv = (TextView) v.findViewById(R.id.tv1);
return v;
}

public void getData(String result){
tv.setText(result);
}
}

Output:

h3UmecAAAAASUVORK5CYII= - Temperature Conversion app in Android Studio

eh+XTOHVJwLD0oCnrOwqEptIECwQAfCuD5QAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAh4f8DwM3uiT9LvwMAAAAASUVORK5CYII= - Temperature Conversion app in Android Studio

AAAAABJRU5ErkJggg== - Temperature Conversion app in Android Studio