Java tutorial
/* Copyright 2013, 2014 joshua.tee@gmail.com This file is part of wX. wX is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. wX is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with wX. If not, see <http://www.gnu.org/licenses/>. */ package joshuatee.wx; import joshuatee.wx.R; import android.os.Bundle; import android.app.ActionBar.LayoutParams; import android.content.Intent; import android.util.DisplayMetrics; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.Spinner; import android.widget.AdapterView.OnItemSelectedListener; import android.support.v4.app.Fragment; public class ImagesFragment extends Fragment implements OnItemSelectedListener, OnClickListener { // Provides the fragment used by Tab 4 in which access to COD Mosaics is provided // For AK/HI in which COD Radar mosaics are not available Iowa Mesonet mosaic is used. String img_type = ""; String km = "4km"; final String[] cod_label = new String[] { "rad", "vis", "ir", "wv" }; private Spinner spinner1; Intent intent; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_images, container, false); DisplayMetrics dm = new DisplayMetrics(); getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm); ImageView iv_1 = (ImageView) view.findViewById(R.id.iv_1); iv_1.setOnClickListener(this); ImageView iv_2 = (ImageView) view.findViewById(R.id.iv_2); iv_2.setOnClickListener(this); ImageView iv_3 = (ImageView) view.findViewById(R.id.iv_3); iv_3.setOnClickListener(this); ImageView iv_4 = (ImageView) view.findViewById(R.id.iv_4); iv_4.setOnClickListener(this); ImageView iv_5 = (ImageView) view.findViewById(R.id.iv_5); iv_5.setOnClickListener(this); ImageView iv_6 = (ImageView) view.findViewById(R.id.iv_6); iv_6.setOnClickListener(this); ImageView iv_7 = (ImageView) view.findViewById(R.id.iv_7); ImageView iv_8 = (ImageView) view.findViewById(R.id.iv_8); ImageView iv_9 = (ImageView) view.findViewById(R.id.iv_9); ImageView iv_10 = (ImageView) view.findViewById(R.id.iv_10); ImageView iv_11 = (ImageView) view.findViewById(R.id.iv_11); ImageView iv_12 = (ImageView) view.findViewById(R.id.iv_12); ImageView iv_13 = (ImageView) view.findViewById(R.id.iv_13); ImageView iv_14 = (ImageView) view.findViewById(R.id.iv_14); ImageView iv_15 = (ImageView) view.findViewById(R.id.iv_15); ImageView iv_16 = (ImageView) view.findViewById(R.id.iv_16); ImageView iv_17 = (ImageView) view.findViewById(R.id.iv_17); ImageView iv_18 = (ImageView) view.findViewById(R.id.iv_18); iv_7.setOnClickListener(this); iv_8.setOnClickListener(this); iv_9.setOnClickListener(this); iv_10.setOnClickListener(this); iv_11.setOnClickListener(this); iv_12.setOnClickListener(this); iv_13.setOnClickListener(this); iv_14.setOnClickListener(this); iv_15.setOnClickListener(this); iv_16.setOnClickListener(this); iv_17.setOnClickListener(this); iv_18.setOnClickListener(this); LayoutParams params_iv = null; UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.usa_map), iv_1, 3, params_iv); UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.k2), iv_2, 3, params_iv); UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.k1), iv_3, 3, params_iv); UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.northwest_map), iv_4, 3, params_iv); UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.northcentral_map), iv_5, 3, params_iv); UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.northeast_map), iv_6, 3, params_iv); UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.southwest_map), iv_7, 3, params_iv); UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.southcentral_map), iv_8, 3, params_iv); UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.southeast_map), iv_9, 3, params_iv); UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.alaska_map), iv_10, 3, params_iv); UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.hawaii_map), iv_11, 3, params_iv); UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.canada_map), iv_12, 3, params_iv); UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.n_pacific_map), iv_13, 3, params_iv); UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.polar_map), iv_14, 3, params_iv); UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.n_atlantic_map), iv_15, 3, params_iv); UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.s_pacific_map), iv_16, 3, params_iv); UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.hemi_map), iv_17, 3, params_iv); UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.s_atlantic_map), iv_18, 3, params_iv); //iv_1.setImageResource(R.drawable.usa_map); spinner1 = (Spinner) view.findViewById(R.id.spinner1); ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, cod_label); dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner1.setAdapter(dataAdapter); spinner1.setOnItemSelectedListener(this); return view; } /*@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); }*/ public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { img_type = String.valueOf(spinner1.getSelectedItem()); if (img_type.equals("vis")) { km = "1km"; } else { km = "4km"; } } public void onNothingSelected(AdapterView<?> parent) { } @Override public void onClick(View v) { switch (v.getId()) { case R.id.iv_1: intent = new Intent(getActivity(), CODMosaicActivity.class); intent.putExtra(CODMosaicActivity.URL, new String[] { img_type, "usa" }); startActivity(intent); break; case R.id.iv_4: intent = new Intent(getActivity(), CODMosaicActivity.class); intent.putExtra(CODMosaicActivity.URL, new String[] { img_type, "northwest" }); startActivity(intent); break; case R.id.iv_5: intent = new Intent(getActivity(), CODMosaicActivity.class); intent.putExtra(CODMosaicActivity.URL, new String[] { img_type, "northcentral" }); startActivity(intent); break; case R.id.iv_6: intent = new Intent(getActivity(), CODMosaicActivity.class); intent.putExtra(CODMosaicActivity.URL, new String[] { img_type, "northeast" }); startActivity(intent); break; case R.id.iv_7: intent = new Intent(getActivity(), CODMosaicActivity.class); intent.putExtra(CODMosaicActivity.URL, new String[] { img_type, "southwest" }); startActivity(intent); break; case R.id.iv_8: intent = new Intent(getActivity(), CODMosaicActivity.class); intent.putExtra(CODMosaicActivity.URL, new String[] { img_type, "southcentral" }); startActivity(intent); break; case R.id.iv_9: intent = new Intent(getActivity(), CODMosaicActivity.class); intent.putExtra(CODMosaicActivity.URL, new String[] { img_type, "southeast" }); startActivity(intent); break; case R.id.iv_2: intent = new Intent(getActivity(), CODMosaicKMActivity.class); intent.putExtra(CODMosaicKMActivity.URL, new String[] { "2km", img_type, "" }); startActivity(intent); break; case R.id.iv_3: intent = new Intent(getActivity(), CODMosaicKMActivity.class); intent.putExtra(CODMosaicKMActivity.URL, new String[] { "1km", img_type, "" }); startActivity(intent); break; case R.id.iv_12: intent = new Intent(getActivity(), CODMosaicActivity.class); intent.putExtra(CODMosaicActivity.URL, new String[] { img_type, "canada" }); startActivity(intent); break; case R.id.iv_11: intent = new Intent(getActivity(), CODMosaicActivity.class); intent.putExtra(CODMosaicActivity.URL, new String[] { img_type, "hawaii" }); startActivity(intent); break; case R.id.iv_10: intent = new Intent(getActivity(), CODMosaicActivity.class); intent.putExtra(CODMosaicActivity.URL, new String[] { img_type, "alaska" }); startActivity(intent); break; case R.id.iv_13: intent = new Intent(getActivity(), CODMosaicActivity.class); intent.putExtra(CODMosaicActivity.URL, new String[] { img_type, "n_pacific" }); startActivity(intent); break; case R.id.iv_14: intent = new Intent(getActivity(), CODMosaicActivity.class); intent.putExtra(CODMosaicActivity.URL, new String[] { img_type, "polar" }); startActivity(intent); break; case R.id.iv_15: intent = new Intent(getActivity(), CODMosaicActivity.class); intent.putExtra(CODMosaicActivity.URL, new String[] { img_type, "n_atlantic" }); startActivity(intent); break; case R.id.iv_16: intent = new Intent(getActivity(), CODMosaicActivity.class); intent.putExtra(CODMosaicActivity.URL, new String[] { img_type, "s_pacific" }); startActivity(intent); break; case R.id.iv_17: intent = new Intent(getActivity(), CODMosaicActivity.class); intent.putExtra(CODMosaicActivity.URL, new String[] { img_type, "hemi" }); startActivity(intent); break; case R.id.iv_18: intent = new Intent(getActivity(), CODMosaicActivity.class); intent.putExtra(CODMosaicActivity.URL, new String[] { img_type, "s_atlantic" }); startActivity(intent); break; } } }