Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ImageView Example
- ==================
- activity_main.xml
- ------------------
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <EditText
- android:id="@+id/etTemp"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:hint="tempreture ..."
- android:inputType="numberSigned"
- android:textSize="22sp" />
- <Button
- android:id="@+id/btnShow"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Show"
- android:textSize="22sp" />
- <ImageView
- android:id="@+id/imgImage"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- </LinearLayout>
- MainActivity.java
- ------------------
- package com.example.mohamadpc.bmiproject;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ImageView;
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- EditText etTemp;
- Button btnShow;
- ImageView imgImage;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- etTemp = findViewById(R.id.etTemp);
- btnShow = findViewById(R.id.btnShow);
- imgImage = findViewById(R.id.imgImage);
- btnShow.setOnClickListener(this);
- }
- @Override
- public void onClick(View view) {
- double temp;
- temp = Double.parseDouble(etTemp.getText().toString());
- if (temp < 10) {
- imgImage.setImageResource(R.drawable.cold);
- } else if (temp < 20) {
- imgImage.setImageResource(R.drawable.clouds);
- } else {
- imgImage.setImageResource(R.drawable.sun);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement