Sunday, March 22, 2020

[ANDROID] Weather App

Leave a Comment
package com.example.cw.weatherapp;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    String mn;
    TextView mainResultTextView;
    TextView cityNameTextView;
    TextView tempTextView;
    TextView pressureTextView;
    String cityName;
    String pressure;
    String temperature;
    EditText editText;

    public class DataDownload extends AsyncTask<String,Void,String>
    {

        @Override        protected String doInBackground(String... urls) {
            URL url;
            HttpURLConnection connection=null;
            String result="";
            try{
                url = new URL(urls[0]);
                connection = (HttpURLConnection) url.openConnection();
                InputStream in = connection.getInputStream();
                InputStreamReader reader = new InputStreamReader(in);

                int data = reader.read();
                while(data!=-1)
                {
                    char ch = (char)data;
                    result += ch;
                    data = reader.read();
                }

                return result;
            }catch (Exception e){
                e.printStackTrace();
                Toast.makeText(MainActivity.this, "Could not get weather", Toast.LENGTH_SHORT).show();
                return null;
            }
        }

        @Override        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            try {
                JSONObject jsonObject = new JSONObject(s);
                String weather = jsonObject.getString("weather");
                JSONArray jsonArray = new JSONArray(weather);
                JSONObject partObject = jsonArray.getJSONObject(0);
                String main = partObject.getString("main");
                String desc = partObject.getString("description");
                mn = main + " : " + desc;

                cityName = jsonObject.getString("name");

                String str = jsonObject.getString("main");
                partObject = new JSONObject(str);
                pressure = "Pressure : "+partObject.getString("pressure");

                temperature = "Temp:"+partObject.getString("temp")+";"+"Temp_MAX:"+partObject.getString("temp_max")+";"+"Temp_MIN:"+partObject.getString("temp_min");

            }catch (Exception e){
                e.printStackTrace();
                Toast.makeText(MainActivity.this, "Could not get weather", Toast.LENGTH_SHORT).show();
            }
        }
    }

    public void buttonClicked(View view)
    {
        DataDownload data = new DataDownload();

        String place = editText.getText().toString();

        if(!place.equals("")) {
            try {
                data.execute("https://openweathermap.org/data/2.5/weather?q=" + place + "&appid=b6907d289e10d714a6e88b30761fae22").get();
                mainResultTextView.setText(mn);
                cityNameTextView.setText(cityName);
                pressureTextView.setText(pressure);
                tempTextView.setText(temperature);

                mainResultTextView.setVisibility(View.VISIBLE);
                cityNameTextView.setVisibility(View.VISIBLE);
                pressureTextView.setVisibility(View.VISIBLE);
                tempTextView.setVisibility(View.VISIBLE);

            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(MainActivity.this, "Could not get weather", Toast.LENGTH_SHORT).show();
            }
        }else{
            Toast.makeText(this, "Input Place Name", Toast.LENGTH_SHORT).show();
        }

    }

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

        mainResultTextView = (TextView)findViewById(R.id.mainResultTextView);
        tempTextView = (TextView)findViewById(R.id.tempTextView);
        pressureTextView = (TextView) findViewById(R.id.pressureTextView);
        cityNameTextView = (TextView) findViewById(R.id.cityNameTextView);

        editText = (EditText)findViewById(R.id.editText);
    }
}


//////////////////////////////////////////
//DESIGN
////////////////////////////////////////

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout 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"    tools:context=".MainActivity">

    <ImageView        android:id="@+id/imageView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:scaleType="centerCrop"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintHorizontal_bias="1.0"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent"        app:layout_constraintVertical_bias="1.0"        app:srcCompat="@drawable/weather" />

    <TextView        android:id="@+id/textView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginEnd="8dp"        android:layout_marginRight="8dp"        android:layout_marginTop="10dp"        android:text="Enter City Name"        android:textColor="?android:attr/colorLongPressedHighlight"        android:textSize="40sp"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintHorizontal_bias="0.5"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent" />

    <EditText        android:id="@+id/editText"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginEnd="8dp"        android:layout_marginRight="8dp"        android:ems="10"        android:hint="eg. London"        android:inputType="textPersonName"        android:textColor="?android:attr/colorLongPressedHighlight"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintHorizontal_bias="0.527"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginEnd="8dp"        android:layout_marginLeft="8dp"        android:layout_marginRight="8dp"        android:layout_marginStart="8dp"        android:layout_marginTop="8dp"        android:onClick="buttonClicked"        android:text="WHAT'S THE WEATHER?"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toBottomOf="@+id/editText" />

    <TextView        android:id="@+id/cityNameTextView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginEnd="8dp"        android:layout_marginLeft="8dp"        android:layout_marginRight="8dp"        android:layout_marginStart="8dp"        android:layout_marginTop="2dp"        android:text="TextView"        android:textColor="@android:color/background_light"        android:textSize="25sp"        android:textStyle="bold"        android:visibility="invisible"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintHorizontal_bias="0.5"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toBottomOf="@+id/button" />

    <TextView        android:id="@+id/mainResultTextView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginEnd="8dp"        android:layout_marginLeft="8dp"        android:layout_marginRight="8dp"        android:layout_marginStart="8dp"        android:layout_marginTop="16dp"        android:text="TextView"        android:textColor="@android:color/background_light"        android:textSize="20sp"        android:textStyle="bold"        android:visibility="invisible"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toBottomOf="@+id/cityNameTextView" />

    <TextView        android:id="@+id/tempTextView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginEnd="8dp"        android:layout_marginLeft="8dp"        android:layout_marginRight="8dp"        android:layout_marginStart="8dp"        android:layout_marginTop="18dp"        android:text="TextView"        android:textColor="@android:color/background_light"        android:textSize="20sp"        android:textStyle="bold"        android:visibility="invisible"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintHorizontal_bias="0.5"        app:layout_constraintStart_toStartOf="@+id/imageView"        app:layout_constraintTop_toBottomOf="@+id/mainResultTextView" />

    <TextView        android:id="@+id/pressureTextView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginEnd="8dp"        android:layout_marginLeft="8dp"        android:layout_marginRight="8dp"        android:layout_marginStart="8dp"        android:layout_marginTop="24dp"        android:text="TextView"        android:textColor="@android:color/background_light"        android:textSize="30sp"        android:textStyle="bold"        android:visibility="invisible"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintHorizontal_bias="0.502"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toBottomOf="@+id/tempTextView" />

</android.support.constraint.ConstraintLayout>
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment