package com.example.sampletoast1;

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

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {
@BindView(R.id.editText)
EditText editText;
@BindView(R.id.editText2)
EditText editText2;
@BindView(R.id.button)
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);

}
@OnClick({R.id.button})
public void onClick(View v){
try{
Toast toastView=Toast.makeText(this,"위치가 바뀐 토스트 메시지입니다.",Toast.LENGTH_LONG);
int xOffset=Integer.parseInt(editText.getText().toString());
int yOffset=Integer.parseInt(editText2.getText().toString());

toastView.setGravity(Gravity.TOP|Gravity.TOP,xOffset,yOffset);
toastView.show();
}catch (NumberFormatException e){
e.printStackTrace();
}
}
@OnClick({R.id.button2})
public void onClick2(View v){
LayoutInflater inflater=getLayoutInflater();

View layout=inflater.inflate(
R.layout.toastborder,(ViewGroup)findViewById(R.id.toast_rayout_root));
TextView text=layout.findViewById(R.id.text);
Toast toast=new Toast(this);
text.setText("모양 바꾼 텍스트");
toast.setGravity(Gravity.CENTER,0,-100);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}

@OnClick({R.id.button3})
public void onClick3(View v){
Snackbar.make(v,"스낵바입니다",Snackbar.LENGTH_LONG).show();
}

}

Activity

<?xml version="1.0" encoding="UTF-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<stroke
android:width="4dp"
android:color="#ffffff00"
/>

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

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

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

</shape>

toast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_rayout_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="10dp"
>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:textSize="32sp"
android:background="@drawable/toast"/>

</LinearLayout>

toastborder.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:hint="X 위치"
android:inputType="numberSigned"
/>
<EditText
android:id="@+id/editText2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:hint="Y 위치"
android:inputType="numberSigned"/>
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="띄우기"
android:textSize="20sp"
/>
</LinearLayout>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="모양 바꿔 띄우기"

/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="스낵바 사용"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>

activity_main.xml







+ Recent posts