Jumat, 09 September 2016

Toast, ImageButton, OnClickListener : Eclipse ADT

 script untuk layout : MyToast/res/layout/activity_my_toast.xml
=============================================================
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MyToast" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <ImageButton
        android:id="@+id/btShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="43dp"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>



java class activity : MyToast/src/MyToast.java
=========================================
package com.example.mytoast;

import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

public class MyToast extends Activity {
    ImageButton btShow;  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_toast);

        myToast();
       
    }
       
    public void myToast(){      
        btShow=(ImageButton)findViewById(R.id.btShow);
        btShow.setOnClickListener(new ImageButton.OnClickListener(){
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub          
                Toast MYtoast=Toast.makeText(getApplicationContext(), "Message",Toast.LENGTH_LONG);      
                MYtoast.setDuration(100);
                MYtoast.setGravity(Gravity.CENTER, 0, 0);
                MYtoast.show();                              
            }
          
        });
      
    }
}
 

==========================================================================