** Toast 모양 변경하기 **

 

0. 프로젝트명 : ToastModify

1. Toast컬러 변경 및 layout을 이용한 Toast변경

 

2. 소스

    1. MainActivity.xml              : layout 조회 및 Toast보여주기

    2. drawable/toast.xml        : Toast모양 변경

    3. layout/activity_main.xml : main activity

    4. layout/activity_toast.xml : Toast모양을 변경 할 Layout. Activity. View

 

3. 화면 :

    - Toast의 좌표

       .  메소드 : Toast.setGravity(int gravity, int xOffset, int yOffset)

       .  gravity : Toast의 기본 위치 지정 (TOP, LEFT, CENTER..)

       .  xOffset, yOffset : gravity 기본 위치를 기준으로 x, y 좌표임

       .  예)  toast.setGravity(Gravity.TOP | Gravity.LEFT150, 300);

 

    - [단순 토스트표시] 버튼 클릭 시 Toast

        

 

   - [LAYOUT토스트 표시]버튼  클릭 시 Toast

       . activity_toast.xml layout을 이용해서 생성

       . drawable/toast.xml 파일에 shape, strok, solid, padding, corner, gradient   tag등을 통해서 Toast 모양 변경

       


 

 

  1. MainActivity.xml

 

package com.example.farmer.toastmodify;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button button;
    Button button2;

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

        button = (Button) findViewById(R.id.button);
        button2 = (Button) findViewById(R.id.button2);

        button.setOnClickListener(toastOnclickListener);
        button2.setOnClickListener(layoutOnclickListener);
    }

    Button.OnClickListener toastOnclickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Toast toast = Toast(getApplicationContext());
            //toast.setText(sResult);
            //toast.setDuration(Toast.LENGTH_LONG);

            String notes = "토스트 내용";

            Toast toast = Toast.makeText(getApplicationContext(), notes, Toast.LENGTH_SHORT);

            //Toast 위치 지정.
            //x,y 좌표는  TOP, LEFT 의 위치에서 시작되는 좌표임
            toast.setGravity(Gravity.TOP | Gravity.LEFT150, 300);

            //Toast는 View를 상속받지 않아서 직접View 객체에 대한 속성을 넣을 수 없다.
            //대신 getView()메소드를 통해서 View객체를 받아서 View속성정의 하면 Toast속성에 적용 된다.
            View viewToast = toast.getView();

            //Toast BackgroundColor 변경
            //color 표기시 xml 에서는 "#ffffff"로 하지만
            //java 코드에서는 아래와 같이 rgb로 변환하여 사용해줘야 한다.
            viewToast.setBackgroundColor(Color.rgb(255, 0, 255));

            //Toast Font Color변경
            TextView tvToast = (TextView) viewToast.findViewById(android.R.id.message);
            tvToast.setTextColor(Color.RED);

            toast.show();

            //Toast.makeText(getApplicationContext(), notes, Toast.LENGTH_LONG).show();
        }
    };

    Button.OnClickListener layoutOnclickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //토스트 모양을 정의하고 있는 레이아웃을 인플레이션 한다.
            //기존 토스트 모양을 작성한 toastborder.xml로 인플레이션 한다.
            LayoutInflater inflater = getLayoutInflater();
            View layout = inflater.inflate(R.layout.activity_toast,
                    (ViewGroup)findViewById(R.id.toast_layout_root));

            //레이아웃의 텍스트뷰에 보여줄 문자열을 설정한다.
            TextView text = (TextView)layout.findViewById(R.id.text);
            text.setText("Layout을 이용한 토스트");

            //토스트 객체 만들기
            Toast toast = new Toast(MainActivity.this);

            //토스트가 출력될 위치를 지정
            //x,y 좌표는  CENTER 의 위치에서 시작되는 좌표임
            toast.setGravity(Gravity.CENTER, 0, -200);

            toast.setDuration(Toast.LENGTH_SHORT);

            //토스트에 뷰를 설정
            toast.setView(layout);

            //토스트 부여주기
            toast.show();
        }
    };
}

 

 

 

  2. drawable/toast.xml

 

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <!--
      1. stroke    : 테두리 선
      2. solid      : Toast 내부 색 설정
      3. padding : 여백
      4. corners : 모서리 설정
      5. gradient : 경사도. 변화도
    -->
    <stroke
        android:width="4dp"
        android:color="#ff0008"
        android:dashWidth="5dp"
        android:dashGap="2dp"/>

    <solid
        android:color="#ff883300"/>

    <padding
        android:left="20dp"
        android:top="20dp"
        android:right="20dp"
        android:bottom="20dp"/>

    <corners
        android:radius="100dp"/>

    <gradient
        android:startColor="#ffff0000"
        android:endColor="#80ff00ff"
        android:angle="270"/>

</shape>

 

 

 

 

  3. layout/activity_main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="com.example.farmer.toastmodify.MainActivity">

    <Button
        android:id="@+id/button"
        android:text="단순 토스트 표시"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button2"
        android:text="Layout 토스트 표시"
        android:layout_width="wrap_content"
        android:layout_toRightOf="@id/button"
        android:layout_height="wrap_content" />

</RelativeLayout>

 

 

 

 

  4. layout/activity_toast.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:orientation="horizontal"
    android:background="#00ffff"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:background="@drawable/toast"/>

</LinearLayout>

 

 

 

Posted by 농부지기
,