Java tutorial
package de.handler.mobile.android.weatherapp.app.ui; /** * WeatherFragment */ import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.Typeface; import android.support.v4.app.Fragment; import android.view.View; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.TextView; import org.androidannotations.annotations.AfterViews; import org.androidannotations.annotations.App; import org.androidannotations.annotations.EFragment; import org.androidannotations.annotations.ViewById; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import de.handler.mobile.android.weatherapp.app.R; import de.handler.mobile.android.weatherapp.app.WeatherApp; import de.handler.mobile.android.weatherapp.app.datasource.helper.Helper; import de.handler.mobile.android.weatherapp.app.datasource.icons.IconHandler; import de.handler.mobile.android.weatherapp.app.datasource.rest.owm.OWMResult; import de.handler.mobile.android.weatherapp.app.datasource.rest.owm.WeatherDataDaily; import de.handler.mobile.android.weatherapp.app.datasource.rest.owm.result.OWMTemperature; /** * The weather fragment. */ @EFragment(R.layout.fragment_main) public class WeatherFragment extends Fragment implements View.OnClickListener { static final String WEATHER_DATA = "weather_data"; static final String POSITION = "position"; private int mPosition = 0; @ViewById(R.id.fragment_background) ImageView fragmentBackground; @ViewById(R.id.fragment_location) TextView location; @ViewById(R.id.fragment_copyright) TextView copyright; @ViewById(R.id.fragment_cc_image) ImageView ccImageView; @ViewById(R.id.fragment_textviewDay) TextView day; @ViewById(R.id.fragment_textviewDate) TextView date; @ViewById(R.id.fragment_icon) TextView icon; @ViewById(R.id.fragment_textviewTemp) TextView temperature; @ViewById(R.id.fragment_textviewCond) TextView condition; @ViewById(R.id.humidity_icon) ImageView humidityIcon; @ViewById(R.id.fragment_textviewHumidity) TextView humidity; @ViewById(R.id.fragment_location_button) ImageButton locationButton; @App WeatherApp app; @Override public void onAttach(Activity activity) { super.onAttach(activity); setRetainInstance(true); } @AfterViews void afterView() { locationButton.setOnClickListener(this); ccImageView.setOnClickListener(this); ArrayList<Bitmap> bitmaps = app.getImages(); List<String> authors = app.getAuthors(); // Get data from arguments bundle WeatherDataDaily weatherDataDaily = getArguments().getParcelable(WEATHER_DATA); mPosition = getArguments().getInt(POSITION); // prepare data OWMTemperature owmTemperature = weatherDataDaily.getList()[mPosition].getTemp(); OWMResult[] weatherList = weatherDataDaily.getList(); //Handle data //Background Image Bitmap background = null; if (bitmaps != null && !(bitmaps.size() < mPosition)) { background = bitmaps.get(mPosition); } if (background != null) { fragmentBackground.setImageBitmap(background); } else { fragmentBackground.setImageResource(R.drawable.splash_small); } //Owner of picture if (authors != null && authors.size() >= mPosition) { copyright.setText(authors.get(mPosition) + " on "); } // Location if (!weatherDataDaily.getCity().getName().equals("")) { location.setText(weatherDataDaily.getCity().getName()); // Days SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE"); day.setText(String.valueOf(simpleDateFormat.format(weatherList[mPosition].getDate() * 1000))); // Dates simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy"); date.setText(String.valueOf(simpleDateFormat.format(weatherList[mPosition].getDate() * 1000))); // Create custom typeface Typeface myTypeface = Typeface.createFromAsset(getActivity().getAssets(), "fonts/WeatherIcons.ttf"); String iconString = IconHandler.getIcon(weatherList[mPosition].getWeather()[0].getIcon()); icon.setText(iconString); icon.setTypeface(myTypeface); temperature.setText( String.valueOf((int) owmTemperature.getMin()) + " / " + (int) owmTemperature.getMax() + " C"); condition.setText(weatherList[mPosition].getWeather()[0].getDescription()); humidity.setText(String.valueOf((int) weatherList[mPosition].getHumidity()) + " %"); } else { location.setText(getString(R.string.location_unknown)); day.setVisibility(View.INVISIBLE); date.setVisibility(View.INVISIBLE); icon.setVisibility(View.INVISIBLE); temperature.setVisibility(View.INVISIBLE); condition.setVisibility(View.INVISIBLE); humidityIcon.setVisibility(View.INVISIBLE); humidity.setVisibility(View.INVISIBLE); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.fragment_location_button: Intent intent = new Intent(getActivity(), LocationActivity_.class); intent.putExtra(Helper.POSITION, mPosition); startActivityForResult(intent, Helper.LOCATION_REQUEST_CODE); break; case R.id.fragment_cc_image: Intent webIntent = new Intent(getActivity(), WebActivity_.class); webIntent.putExtra(WebActivity.URI, "https://creativecommons.org/licenses/"); startActivity(webIntent); break; } } /** * Receives data from activities started with startActivityForResult() * */ @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == Helper.LOCATION_REQUEST_CODE) { if (resultCode == Activity.RESULT_OK) { Intent intent = new Intent(getActivity(), SplashScreenActivity_.class); startActivity(intent); // Finish MainActivity as you should not be able to navigate back getActivity().finish(); } } } }