Advertisement
mmayoub

School, ImageView example 28.01.2018

Jan 28th, 2018
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.12 KB | None | 0 0
  1. ImageView Example
  2. ==================
  3.  
  4.  
  5. activity_main.xml
  6. ------------------
  7. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  8.     android:layout_width="match_parent"
  9.     android:layout_height="match_parent"
  10.     android:orientation="vertical">
  11.  
  12.     <EditText
  13.         android:id="@+id/etTemp"
  14.         android:layout_width="match_parent"
  15.         android:layout_height="wrap_content"
  16.         android:gravity="center"
  17.         android:hint="tempreture ..."
  18.         android:inputType="numberSigned"
  19.         android:textSize="22sp" />
  20.  
  21.     <Button
  22.         android:id="@+id/btnShow"
  23.         android:layout_width="match_parent"
  24.         android:layout_height="wrap_content"
  25.         android:text="Show"
  26.         android:textSize="22sp" />
  27.  
  28.     <ImageView
  29.         android:id="@+id/imgImage"
  30.         android:layout_width="match_parent"
  31.         android:layout_height="match_parent" />
  32. </LinearLayout>
  33.  
  34.  
  35. MainActivity.java
  36. ------------------
  37. package com.example.mohamadpc.bmiproject;
  38.  
  39. import android.os.Bundle;
  40. import android.support.v7.app.AppCompatActivity;
  41. import android.view.View;
  42. import android.widget.Button;
  43. import android.widget.EditText;
  44. import android.widget.ImageView;
  45.  
  46. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  47.     EditText etTemp;
  48.     Button btnShow;
  49.     ImageView imgImage;
  50.  
  51.     @Override
  52.     protected void onCreate(Bundle savedInstanceState) {
  53.         super.onCreate(savedInstanceState);
  54.         setContentView(R.layout.activity_main);
  55.  
  56.         etTemp = findViewById(R.id.etTemp);
  57.         btnShow = findViewById(R.id.btnShow);
  58.         imgImage = findViewById(R.id.imgImage);
  59.  
  60.         btnShow.setOnClickListener(this);
  61.  
  62.     }
  63.  
  64.     @Override
  65.     public void onClick(View view) {
  66.         double temp;
  67.         temp = Double.parseDouble(etTemp.getText().toString());
  68.  
  69.         if (temp < 10) {
  70.             imgImage.setImageResource(R.drawable.cold);
  71.         } else if (temp < 20) {
  72.             imgImage.setImageResource(R.drawable.clouds);
  73.         } else {
  74.             imgImage.setImageResource(R.drawable.sun);
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement