Wednesday, February 5, 2014

Android app development - Temperature Converter

We displayed our "Hello world!" message in our last post. Now, we'll get our hands on some basic coding. We'll be doing our very own converter app that converts temperature from degree Celsius to Fahrenheit and vice-versa.

We'll start off with a New Android Application.


Create some custom icon for the launcher, like the one below.


Use a blank template for the app and click finish.

We will now create some static attributes for the elements that are going to hold our temperature values. We can also add the static attribute for color, to change the background color of the app.
To add an element, open the file res/values/strings.xml and press the add button.


Now select the Color entry as shown.


Edit the attributes for the Color attribute.


Similarly, create three more String attributes with the following values.


The below image shows the created static attributes placed besides the automatically generated xml code.


Now switch to layout editor by double clicking the activity_main.xml and then switching to Graphical Layout. Right click on the "Hello world!" text and select delete option to delete it. Now add a text field by dragging and dropping the text field box into the layout of the application. Also, drag a RadioGroup and one push button from the Form Widgets tab as shown. Delete one of the radio buttons, as we'll require only two of the three buttons.





Next, we'll proceed by assigning the static attributes which we had previously created to these user interfaces. We'll begin by right clicking on the first radio button and selecting the edit text option.


Select the string attribute "celsius" in the Resource Chooser and click OK.


Similarly assign the attributes "fahrenheit" and "calc" to the second radio button and the push button respectively. Set the checked property of the first radio button to true by right clicking on it.


Set the Input type property of the text input field to numberSigned and numberDecimal.


Now, we'll create the main program. Open the MainActivity.java from the src folder. Type the following code and save the program.

package com.example.temperatureconverter;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText) findViewById(R.id.editText1);
}
// This method is called at button click because we assigned the name to the
// "OnClick property" of the button
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
if (text.getText().length() == 0) {
Toast.makeText(this,"Please enter a valid number",
Toast.LENGTH_LONG).show();
return;
}
float inputValue = Float.parseFloat(text.getText().toString());
if (celsiusButton.isChecked()) {
text.setText(String.valueOf(convertFahrenheitToCelsius(inputValue)));
celsiusButton.setChecked(false);
fahrenheitButton.setChecked(true);
} else{
text.setText(String.valueOf(convertCelsiusToFahrenheit(inputValue)));
fahrenheitButton.setChecked(false);
celsiusButton.setChecked(true);
}
break;
}
}
// Converts to celsius
private float convertFahrenheitToCelsius(float fahrenheit) {
return ((fahrenheit - 32) * 5 / 9);
}
// Converts to fahrenheit
private float convertCelsiusToFahrenheit(float celsius) {
return ((celsius * 9) / 5) + 32;
}
}



18 comments:

  1. Hi, probably our entry may be off topic but anyways, I have been surfing around your blog and it looks very professional. It’s obvious you know your topic and you appear fervent about it. I’m developing a fresh blog plus I’m struggling to make it look good, as well as offer the best quality content. I have learned much at your web site and also I anticipate alot more articles and will be coming back soon. Thanks you.

    Android Training in Chennai

    ReplyDelete
  2. I simply want to say I’m very new to blogs and actually loved you’re blog site. Almost certainly I’m going to bookmark your blog post . You absolutely come with great well written articles. Thanks a lot for sharing your blog.
    android app development training in chennai|Android Training institute in chennai with placement | Best Android Training in velachery

    ReplyDelete
  3. Your article on iOS was excellent with the advanced features and the scope of iOS developer in future what you said is absolutely correct. I would like to share with my friends so that they will also get the chance to read the blog.



    Regards:

    Best iOS Training in Chennai |
    iOS Developer Training in Chennai

    ReplyDelete
  4. Nice piece of information on HTML5. With the expansion of smartphones and other portable gadgets, the demand for responsive website design that go comfy on all devices keeps on increasing. This leads to invention and expansion of HTM5 web technology.PHP Training in Chennai|PHP Course in Chennai

    ReplyDelete
  5. Awesome blog, I never read this type of informative blog. https://www.fluper.com/Android-Game-Development

    ReplyDelete
  6. You can find several different sorts of temperature converter, so every one of which contains its own own intent. At the United States, the Fahrenheit scale is most often utilised (for example , to report temperatures from the headlines ). You will find it applied when pharmacies, banks, etc. examine that the day's temperature on electronic indicators in your streets. It is a convenient extension for scientific inquiry also it is used on the planet. Other important temperature scale is Fahrenheit which is used mainly in United States. The method for converting from Celsius to Fahrenheit is more complex.I'm utilizing this specific Temperature converter and earn the most correct conversions immediately, depending on some fever scales. The worthiness in Fahrenheit can be obtained by multi ply Celsius price by 1.8 and then add 32. Water freezing stage are at 32 Fahrenheit. The very Optimal/optimally way to Find the conversion done is to utilize an online fever converter When the temperatures drops approaching 0 degrees, the water molecules will be losing rate, the fascination between these is rising and they crystallize and forming ice. We are all aware the water volume is steadily rising if freezing. This happens as the hydrogen atoms have been exercising a fantastic fascination making the drinking water molecules to be arranged in a particular place, leaving spaces between these, and also this making the volume must be higher compared to the amount of molecules is the exact same.

    ReplyDelete
  7. This is best one article so far I have read, I would like to appreciate you for making it very simple and easy
    Regards,
    PHP Training in Chennai | PHP Course in Chennai

    ReplyDelete
  8. Are you looking for the best web development? Then visit pythonsdevs.org, They are known to have the best web developers.

    python programming Company

    ReplyDelete
  9. Acessing the core of any website or software to customize it and develop a quality product, InnovationM is known for its quality assured handcrafted technological solution, going from software development to website design and Mobile application development. InnovationM is the best Mobile app development and Website design Company in London, UK.

    We are a Software development and web design company providing custom website solutions, law firm web design services in derby, Luton, Birmingham, Sheffield, Derby, and London, UK, and also popularly known for website development in cambridge birmingham, android app design agency derby, software design agency derby, mobile app design agency derby, app developers luton, software design company derby, app development luton, ios app development company derby, mobile app design derby, iphone app development company derby, bespoke app development derby, software app development derby, android app development company in derby, software development company brighton, app agency oxford, mobile app development company derby, iphone app development derby, ios app development derby, development agency brighton, UK.

    And that what we have been known for, InnovationM delivers a quality assure product/ service which is fully dependent on your next virtual need.
    For more: www.innovationm.co.uk

    ReplyDelete
  10. Acessing the core of any website or software to customize it and develop a quality product, InnovationM is known for its quality assured handcrafted technological solution, going from software development to website design and Mobile application development. InnovationM is the best Mobile app development and Website design Company in London, UK.
    Whether it's website development in luton, mobile app agency in manchester or app design company in london, we have everything to offer. Looking forward for iphone app development in birmingham, we got your back! InnovationM UK covers you with app design agency in uk as best website design company in luton, mobile app development services in birmingham, mobile app development company in birmingham, mobile development agency in london.

    As the dgital era is approaching, mobile app development services in birmingham will grow much denser also development of iphone app in derby. Commonly delivering services across software design agency derby, android app design agency derby, mobile app design agency derby, app design bristol, website development in cambridge birmingham, software design company derby, web app development bristol, app development luton, app developers luton, bespoke app development derby, software development company brighton.

    We are also into application development company sheffield, mobile app agency manchester, app design company in london, software development companies glasgow, website development luton, app design agency uk, app development company birmingham, app design companies uk, app design agency in london, app design companies london, mobile development agency london, iphone app development birmingham, app design agency london, web design company derby, mobile app development services birmingham, mobile development agency uk, app design company uk.
    For More: www.innovationm.co.uk

    ReplyDelete
  11. Great Content!! Thank you for sharing this helpful information as it would be very much helpful for the beginner in app development sector. Here is yet another top-notch service provider “X-Byte Enterprise Solutions” who render feasible and credible solutions to global clients that help global brands design and build superior digital products, enabling seamless user experiences across all modern platforms and devices.

    Know more here: Mobile App Development

    ReplyDelete
  12. Great Post!!! Thanks for the data update and waiting for your new updates.

    Would you like to create an App Like Groupon Mobile App ? then we have the best solution for you by The App Ideas .

    ReplyDelete
  13. I just wanted to take a moment to express my appreciation for your Android-related blog. Are you looking for a reliable partner to help you build high-quality Android apps that meet your business needs? Look no further,connect with our experts.

    ReplyDelete
  14. CMOLDS is a leading app development company in Dubai, known for crafting innovative mobile solutions tailored to your business needs. With a dedicated team of experts, we specialize in creating cutting-edge apps that drive success and growth. Partner with us for top-notch app development services and unparalleled expertise in Dubai's competitive market.

    ReplyDelete