For the app interface, we need two radiobuttons, one textbox, and one textview. See the picture below.
After creating the CtoFConverter project, please open the activity_main.xml file (in res/layout directory). This file is the source that defines views to be placed on the main activity. Views in Android can be said as components or controls in Java or C#. You need to modify the activity_main.xml file to include the two RadioButtons, one EditText, and one TextView views. So the activity_main.xml file is as shown below.
<?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">
>
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="@+id/ctof_radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ctof_radio"
android:onClick="onRadioButtonClicked"
android:checked="true"
/>
<RadioButton android:id="@+id/ftoc_radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ftoc_radio"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
<EditText
android:id="@+id/txt_input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_ctof"
android:inputType="numberDecimal" />
<TextView
android:id="@+id/txt_result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#992211"
android:textSize="20sp"
/>
</LinearLayout>
The two RadioButtons are placed in the RadioGroup so that only one of them can be selected at a time. The first RadioButton is for Celsius to Fahrenheit conversion. In contrast, the second RadioButton is for Fahrenheit to Celsius conversion.
To define id property for each view, we use android:id property. The @ symbol tells the compiler to read text after the forward slash (/). By using the plus sign (+) before the id, you want to tell the compiler that this id is new and needs to be created. The width and height of a view can be defined by using android:layout_width and android:layout_height respectively. The wrap_content value of these properties specifies that the view wraps its content. The fill_parent value allows the view to be expanded to fit the screen. Besides, wrap_content and fill_parent, you can specify the width and height of the values in dp or sp unit.
To assign text to each RadioButton or other views that support text property, you need to use the android:text property. For the first RadioButton the value of the text property is taken from the string resource called ctof_radio. This string and other strings that are used for this interface construction are defined in strings.xml file (in res/values directory). You need to place @string before the name of the string to indicate that this string is defined in the strings.xml resource file.
This is the content of the strings.xml file used in this app:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">CtoFConverter</string>
<string name="action_settings">Settings</string>
<string name="ctof_radio">Celsius to Fahrenheit</string>
<string name="ftoc_radio">Fahrenheit to Celsius</string>
<string name="hint_ctof">Enter celsius value</string>
<string name="hint_ftoc">Enter fahrenheit value</string>
</resources>
The EditText view is a textbox that allows the user to type a number in Celsius or Fahrenheit. The inputType property specifies the input type of the textbox. The value of the input type can be one of the followings:
-number allows integer number input.
-numberDecimal allows floating-point number input.
-textPassword allows text input. The original text is kept. However, the password symbols are displayed instead of the actual characters.
-textMultiLine allows the text to expand to multiple lines.
-textCapWords displays a capital character at the beginning of each word.
-textCapSentences displays a capital character at the beginning of each sentence.
-date allows a value in date format input.
-time allows a value in time format input.
-datetime allows a combination of date and time input.
The last view that we use is TextView. The TextView view is used to display the result of the conversion. When the user types a value in the EditText view, the calculation is made to produce a result. The result is displayed in the TextView view.
The result can be a value in Celsius or Fahrenheit according to the RadioButtons selection. If the first RadioButton is selected, the user is prompted to enter a value in Celsius so that the conversion is from Celsius to Fahrenheit and the result is in Fahrenheit. Otherwise, the conversion is from Fahrenheit to Celsius so that the result is in Celsius.
All views are placed inside the LinearLayout. You can treat the LinearLayout in Andoid as a layout manager in Java. It is used to layout components in the app interface. The LinearLayout arranges the views in one orientation-vertical or horizontal. You can use its android:orientation property to specify vertical or horizontal orientation.
Receive click event from RadioButton
To perform an action when the RadioButton is clicked, you need to register to the click event to the RadioButton view by using its android:onClick property. The value of this property will be a method to perform the action. In this app, the method to perform the action when the RadioButon is clicked is onRadioButtonClicked. This method is defined in the MainActivity.java file. When the first RadioButton is selected, the hint text of the EditText changes to Enter Celsius value. If the second RadioButton is selected, the the hint text of the EditText changes to Enter Fahrenheit value.MainActivity.java file
package com.example.ctofconverter;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends Activity {
private String strFrom="Celsius";
private String strTo="Fahrenheit";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
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;
}
public void onStart(){
super.onStart();
EditText et=(EditText)findViewById(R.id.txt_input); //refer tot he EditText, txt_input
et.addTextChangedListener(new ChangeListener()); //register the view with the text change listener
et.setHint("Enter celsius value");
}
class ChangeListener implements TextWatcher{
public void beforeTextChanged(CharSequence s, int start, int before, int count){
}
public void onTextChanged(CharSequence s, int start, int before, int count){
convert(strFrom,strTo); //do conversions
}
public void afterTextChanged(Editable ed){
}
}
public void convert(String from, String to){
EditText et=(EditText) findViewById(R.id.txt_input);
TextView tv=(TextView) findViewById(R.id.txt_result);
double cvalue=0.0;
double fvalue=0.0;
if(et.getText().toString().length()>0){
if(from.equals("Celsius")) //convert from Celsius to Fahrenheit
{
cvalue=Double.parseDouble(et.getText().toString());
fvalue=32+(cvalue*180.0/100.0);
tv.setText(fvalue+to);
}
else{ //convert from Fahrenheit to Celsus
fvalue=Double.parseDouble(et.getText().toString());
cvalue=(fvalue-32)*100.0/180.0;
tv.setText(cvalue+to);
}
}
}
public void onRadioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
EditText et=(EditText)findViewById(R.id.txt_input);
switch(view.getId()) {
case R.id.ctof_radio:
if (checked){ //the first RadioButton is selected
strFrom="Celsius";
strTo="Fahrenheit";
et.setHint("Enter celsius value");
}
break;
case R.id.ftoc_radio:
if (checked){ //the second RadioButton is selected
strFrom="Fahrenheit";
strTo="Celsius";
et.setHint("Enter fahrenheit value");
}
break;
}
}
}
Refer a view in code
When you place views in the activity_main.xml file, the id of each view is recorded in the R.java file (in gen directory). When writing code for the main activity or other activities, referring to the views allows you to use the views. You can refer a view by using the findViewById(R.id.id_of_the_veiw) of the Activity class. For example, to refer the EditText, txt_input can be done by this line of code:EditText et=(EditText)findViewById(R.id.txt_input);
Receive text change event
When the user types a value in the EditText, txt_input. This value immediately is calculated to produce a result. Then the result is displayed in the TextView, txt_result. The actions that convert and display the result happen when the user is making change to the value of EditText, txt_input. To perform an action when the text of EditText view is making changed, you need to register this view with the text change listener by using the addTextChangedListener(TextWatcher tw) method. You will need a class to implement the TextWatcher interface. Then create an object of this class and supply it to the addTextChangedListener method. In this app, this class is called ChangeListener. You must implement all methods of the TextWatcher interface. These methods are beforeTextChanged, onTextChanged, and afterTextChanged.Do conversions
To convert a value input in the EditText, txt_input, to produce a result, the convert (String from, String to) is defined in the MainActivity.java file. This method will be placed in the onTextChanged method of ChangeListener class.The conversion from Fahrenheit to Celsius value can be performed by using the formula below:
Celsius=(Fahrenheit-32)*100.0/180.0;
To convert from Fahrenheit to Celsius value, you can use the formula below:
Fahrenheit=32+(Celsius*180.0/100.0);
I’m really impressed with your blog article, such great & useful knowledge you mentioned here.This is awesome for all which I am like in this is blog... Thanks and Regards
ReplyDeleteMobile App Development Company
Android app Development Company
ios app development Company
Mobile App Development Companies
Good article! We will be linking to this great content on our website.
ReplyDeleteKeep up the great writing.
Vmware Training Center in Chennai
CCNA Training Institute in Chennai
Angularjs Training Center in Chennai
Core Java Training in Chennai
RHCSA Training in Chennai
Rhce Training in Chennai
Thanks for your marvelous posting! It is very useful and good.
ReplyDeleteReal Estate Companies in Chennai
Real Estate Broker in Chennai
Real Estate Agent in Chennai
Real Estate Dealers in Chennai
Real Estate Private Equity in India
Real Estate Advisory Companies in India
Real Estate Research in Chennai
Real Estate Tax Advisor in Chennai
Portfolio Management Services in Chennai
Nice it seems to be good post and more technical skills. Thank sharing great post.
ReplyDeleteThis comment has been removed by the author.
ReplyDeletehttp://creatorstechnology.com/android-application.html
DeleteLearn A New Portfolio Management Concept And Mindset That Could Help You Build Your Investment Kingdom.Online Accredited Financial Portfolio management advisor
ReplyDeleteThe information you shared was really its very useful and saving the time to do off page...
ReplyDeleteSEO, SMM, SMO, SEM, Web Designing Training in Chennai
Digital Marketing Course in Chennai
Digital marketing training institute in Chennai
Digital marketing classes in Chennai
Digital marketing training in Chennai
SEO Training Institute in Chennai
SEO Training in Chennai
SEO Classes in Chennai
SEO Course in Chennai
Best SEO Training in Chennai
SMO Training in Chennai
Social Media Marketing Institute in Chennai
SEM Training institute in Chennai
Web Designing Training in Chennai
Web Designing Classes in Chennai
Soft Skills Training in Chennai
i found it so amazing Vidmate for PC
ReplyDeletethanks for sharing it delete a single file from the Trash
ReplyDeleteI'm really impressed about the info you provide in your articles. I must say am highly overwhelmed by your whole story. It’s not easy to get such quality information online nowadays. I look forward to staying here for a long time.
ReplyDeleteI appreciate your hard work. Keep posting new updates with us. This is really a wonderful post. Nice Blog Very interesting and useful information on your website. Thanks for sharing the blog and this great information which is definitely going to help us.
office.com/setup
office.com/setup
office.com/setup
mcafee.com/activate
ReplyDeletetrung tâm tư vấn du học canada vnsava
công ty tư vấn du học canada vnsava
trung tâm tư vấn du học canada vnsava uy tín
công ty tư vấn du học canada vnsava uy tín
trung tâm tư vấn du học canada vnsava tại tphcm
công ty tư vấn du học canada vnsava tại tphcm
điều kiện du học canada vnsava
chi phí du học canada vnsava
#vnsava
@vnsava
Sawai Madhopur to Jaipur airport taxi
ReplyDeleteIt is guaranteed going to pass on that you are looking for key and moderate Whiteboard animation video production services to explain your idea, thing or coalition? You've gone to the ideal spot! Friend down and see how we can help you.
ReplyDeleteDo Follow New Blog Post on the New Current Running Shows:
ReplyDeleteAmerican Idol 2021 who won
American Idol 2021 who won
The Voice 2021 Winner Predictions
The Voice 2021 who won
AGT 2021 who Won the Finale
AGT 2021 Start Date Spoiler Winner
The Voice Australia 2021 Start Date Premiere Schedule
The Voice 2020 Spoiler Winner Name Announced
Great Post!!! thanks for sharing this wonderful post with us.
ReplyDeleteHadoop Training in Chennai
Big Data Training in Chennai
Big Data Training in Bangalore
Big Data Course in Coimbatore
Bigdata Training Online
social media management pricing is the best solution for those who are searching for the top quality social media management company at very affordable charges.
ReplyDelete