Thursday, 22 August 2019

Android Programming Image Button and Image View

Android Programming

 Image View and Image Button 

 D:\Treesdetailsusingimageviewandimagebutton\app\src\main\res\layout\activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:background="#FFF"    >

    //android:background="#FFFF00"

    <LinearLayout         
android:layout_width="match_parent"         
android:layout_height="match_parent" 
 android:id="@+id/llayout1" 
 android:orientation="vertical">

    <LinearLayout         
android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="horizontal">
 
 <ImageView 
 android:id="@+id/imgvw1"                 
android:layout_width="200dp" 
 android:layout_height="100dp" 
 android:scaleType="fitCenter"                 
android:tooltipText="Trees " 
 android:src="@drawable/tree"/>
 
 <Space 
 android:layout_width="10sp" 
 android:layout_height="wrap_content" />
 
 <TextView 
 android:id="@+id/tt1" 
 android:layout_width="wrap_content"             
android:layout_height="wrap_content" 
 android:text="Name"             
android:textAlignment="center" 
 android:textColor="@android:color/holo_red_dark"             
android:textSize="25sp"             
android:textStyle="italic" />
 
 <Space 
 android:layout_width="10sp" 
 android:layout_height="wrap_content" />
    </LinearLayout>
 
 <LinearLayout 
 android:layout_width="match_parent" 
 android:layout_height="match_parent"         
android:orientation="horizontal">

    <ImageButton                 
android:id="@+id/imgButton1"                 
android:layout_width="200dp"                 
android:layout_height="100dp" 
 android:scaleType="fitCenter" 
 android:src="@drawable/bananatree"/>
 
 <Space 
 android:layout_width="10sp" 
 android:layout_height="wrap_content" />
 
 <TextView 
 android:id="@+id/t1" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content"
 android:textAlignment="center" 
 android:textColor="@android:color/holo_red_dark" 
 android:textSize="25sp" 
 android:textStyle="italic" />
 
 <Space 
 android:layout_width="10sp" 
 android:layout_height="wrap_content" />
 
 </LinearLayout>
    <LinearLayout 
 android:layout_width="match_parent" 
 android:layout_height="match_parent"         
android:orientation="horizontal">
 
 <ImageButton                 
android:id="@+id/imgButton2" 
 android:layout_width="200dp" 
 android:layout_height="100dp" 
 android:scaleType="fitCenter" 
 android:src="@drawable/pappayatree"/>
 
 <Space                 
android:layout_width="10sp" 
 android:layout_height="wrap_content" />
 
 <TextView             
android:id="@+id/t2"             
android:layout_width="wrap_content"             
android:layout_height="wrap_content"
android:textAlignment="center" 
 android:textColor="@android:color/holo_red_dark" 
 android:textSize="25sp"             
android:textStyle="italic" />
 
 <Space             
android:layout_width="10sp" 
 android:layout_height="wrap_content" />
 
 </LinearLayout>
 <LinearLayout         
android:layout_width="match_parent" 
 android:layout_height="match_parent"         
android:orientation="horizontal">
 
 <ImageButton 
 android:id="@+id/imgButton3" 
 android:layout_width="200dp"                 
android:layout_height="100dp"                 
android:scaleType="fitCenter"                 
android:src="@drawable/managotree"/>
 
 <Space 
 android:layout_width="10sp" 
 android:layout_height="wrap_content" />
 
 <TextView             
android:id="@+id/t3"             
android:layout_width="wrap_content" 
 android:layout_height="wrap_content"
 android:textAlignment="center" 
 android:textColor="@android:color/holo_red_dark" 
 android:textSize="25sp" 
 android:textStyle="italic" />
 <Space             
android:layout_width="10sp" 
 android:layout_height="wrap_content" />
 </LinearLayout>
 <LinearLayout 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="horizontal">
 
 <ImageButton 
 android:id="@+id/imgButton4"             
android:layout_width="200dp" 
 android:layout_height="100dp" 
 android:scaleType="fitCenter" 
 android:src="@drawable/close"/>
 
 <Space 
 android:layout_width="10sp"             
android:layout_height="wrap_content" />
 
 <TextView 
 android:id="@+id/t4" 
 android:layout_width="wrap_content"         
android:layout_height="wrap_content"         
android:text="Wind Up !"         
android:textAlignment="center" 
 android:textColor="@android:color/holo_red_dark" 
 android:textSize="25sp" 
 android:textStyle="italic" />
    </LinearLayout>
    </LinearLayout>
</ScrollView>
 
_____________________________________________________________________________
 
D:\Treesdetailsusingimageviewandimagebutton\app\src\main\res\layout\activity_main.xml
 
package com.ldc.treesdetailsusingimageviewandimagebutton;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.graphics.Color;


public class MainActivity extends Activity {
    EditText edittext1, edittext2;
    Button buttonSum,buttonSub,buttonMul;
    ImageButton imgb1,imgb2,imgb3,imgb4;
    TextView t1,t2,t3,t4;



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

        imgb1=(ImageButton) findViewById(R.id.imgButton1);
        imgb2=(ImageButton) findViewById(R.id.imgButton2);
        imgb3=(ImageButton) findViewById(R.id.imgButton3);
        imgb4=(ImageButton) findViewById(R.id.imgButton4);
        final LinearLayout layout = (LinearLayout) findViewById(R.id.llayout1);

        t1 = (TextView) findViewById(R.id.t1);
        t2 = (TextView) findViewById(R.id.t2);
        t3 = (TextView) findViewById(R.id.t3);
       // t4 = (TextView) findViewById(R.id.t4);        imgb1.setBackgroundColor(Color.RED);
        imgb2.setBackgroundColor(Color.RED);
        imgb3.setBackgroundColor(Color.RED);
        imgb4.setBackgroundColor(Color.RED);

        imgb1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                t1.setText("BANANA TREE");
                layout.setBackgroundColor(Color.YELLOW);

            }});
        imgb2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                t2.setText("PAPPAYA TREE");
                layout.setBackgroundColor(Color.GREEN);

            }});
        imgb3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                t3.setText("MANGO TREE");
                layout.setBackgroundColor(Color.CYAN);

            }});


        imgb4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();

            }
        });

    }
}
______________________________________________________________ 

No comments:

Post a Comment