Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 0 additions & 60 deletions app/src/main/java/com/badlogic/weatherapp/CityActivity.java

This file was deleted.

75 changes: 75 additions & 0 deletions app/src/main/java/com/badlogic/weatherapp/CityFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.badlogic.weatherapp;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import static com.badlogic.weatherapp.Constants.CITY;

public class CityFragment extends Fragment {
private Spinner spinner;
private TextView city;
private Button backButton;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.city_layout, container, false);

spinner = view.findViewById(R.id.spinner);
city = view.findViewById(R.id.city);
backButton = view.findViewById(R.id.buttonBack);
clickBackButton();
return view;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initSpinnerCity();
}

private void initSpinnerCity(){
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent,
View itemSelected, int selectedItemPosition, long selectedId) {

String[] choose = getResources().getStringArray(R.array.cities);
Toast toast = Toast.makeText(getContext(),
"You selected: " + choose[selectedItemPosition], Toast.LENGTH_SHORT);
toast.show();

city.setText(choose[selectedItemPosition]);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}

private void clickBackButton(){
backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putString(CITY, city.getText().toString());

Fragment fragment = new WeatherFragment();
fragment.setArguments(bundle);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
});
}
}
44 changes: 5 additions & 39 deletions app/src/main/java/com/badlogic/weatherapp/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,18 @@
public class MainActivity extends AppCompatActivity {

private final String TAG = "Info";
private final String temperatureKey = "temperature";
private TextView temperatureTextView;
private TextView cityTextView;

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

temperatureTextView = findViewById(R.id.textTemperature);
cityTextView = findViewById(R.id.textCountry);

startCityActivity();
getDataFromIntent();
WeatherFragment details = new WeatherFragment();
details.setArguments(getIntent().getExtras());
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, details).commit();
}

@Override
Expand All @@ -40,7 +37,6 @@ protected void onStart() {
@Override
protected void onRestoreInstanceState(Bundle saveInstanceState){
Log("onRestoreInstanceState()");
restoreData(saveInstanceState);
super.onRestoreInstanceState(saveInstanceState);
}

Expand All @@ -59,7 +55,6 @@ protected void onPause() {
@Override
protected void onSaveInstanceState(Bundle saveInstanceState){
Log("onSaveInstanceState()");
saveData(saveInstanceState);
super.onSaveInstanceState(saveInstanceState);
}

Expand All @@ -86,33 +81,4 @@ private void Log(String message){
Log.d(TAG, "onRestart()");
}

private void saveData(Bundle saveInstanceState){
saveInstanceState.putString(temperatureKey, temperatureTextView.getText().toString());
}

private void restoreData(Bundle saveInstanceState){
temperatureTextView.setText(saveInstanceState.getString(temperatureKey));
}

private void startCityActivity(){
cityTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, CityActivity.class);
startActivity(intent);
}
});
}

private void getDataFromIntent(){
Bundle bundle = getIntent().getExtras();
if (bundle == null) {
return;
}

String text = bundle.getString(CITY);
if (!text.isEmpty()){
cityTextView.setText(text);
}
}
}
66 changes: 66 additions & 0 deletions app/src/main/java/com/badlogic/weatherapp/WeatherFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.badlogic.weatherapp;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import static com.badlogic.weatherapp.Constants.CITY;

public class WeatherFragment extends Fragment {
private final String TAG = "Info";
private final String temperatureKey = "temperature";
private TextView temperatureTextView;
private TextView cityTextView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.first_screen_fragment, container, false);

temperatureTextView = view.findViewById(R.id.textTemperature);
cityTextView = view.findViewById(R.id.textCountry);

initCityFragment();
return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Bundle bundle = this.getArguments();

if(bundle != null){
cityTextView.setText(bundle.getString(CITY));
}
}

@Override
public void onSaveInstanceState(Bundle saveInstanceState){
Log("onSaveInstanceState()");
super.onSaveInstanceState(saveInstanceState);
}

private void Log(String message){
Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
Log.d(TAG, "onRestart()");
}

private void initCityFragment(){
cityTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment fragment = new CityFragment();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
});
}
}
73 changes: 4 additions & 69 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">

<ImageView
android:id="@+id/backgroundImage"
android:layout_width="match_parent"
Expand All @@ -17,73 +16,9 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="@drawable/background" />
<LinearLayout
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageWindForce"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/windforce" />
<TextView
android:id="@+id/textWindforce"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="@dimen/upperPanelSize"
android:gravity="center"
android:layout_weight="0.5"
android:text="15m/s" />
<ImageView
android:id="@+id/imageHumidity"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/humidity" />
<TextView
android:id="@+id/textHumidity"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="@dimen/upperPanelSize"
android:gravity="center"
android:layout_weight="0.5"
android:text="90%" />
</LinearLayout>
<TextView
android:id="@+id/textTemperature"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:textSize="@dimen/textTemperatureSize"
android:gravity="center"
android:text="27°C" />
<TextView
android:id="@+id/textCountry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:textSize="@dimen/textCountrySize"
android:gravity="center"
android:text="@string/london_gb" />
<TextView
android:id="@+id/textDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:textSize="@dimen/textDescriptionSize"
android:gravity="center"
android:text="@string/light_snow" />
<ImageView
android:id="@+id/imageDescriptionPicture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
app:srcCompat="@drawable/sun" />
</LinearLayout>
android:layout_height="match_parent" >
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Loading