activity_main.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="26sp"
android:hint="x value"
android:inputType="numberSigned"/>
<EditText
android:id="@+id/editText2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="26sp"
android:hint="y value"
android:inputType="numberSigned"/>
<Button
android:id="@+id/btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="toast meesage"
android:textSize="20sp"
android:onClick="onBtnClicked"/>
</LinearLayout>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "toast shape change"
android:onClick="onBtn2Clicked"
android:layout_gravity="center"
android:textSize="20sp"/>
toast_boarder.xml
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:padding="10dp"
android:background="@drawable/toast_background"/>
toast_background.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="5dp"
android:color="#00f"/>
<solid
android:color="#0ff"/>
<padding
android:left="20dp"
android:top="20dp"
android:right="20dp"
android:bottom="20dp"/>
<corners
android:radius="20dp"/>
</shape>
MainActivity.java
public class MainActivity extends AppCompatActivity {
EditText editText1;
EditText editText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = findViewById(R.id.editText1);
editText2 = findViewById(R.id.editText2);
}
public void onBtnClicked(View v){
try {
Toast toast = Toast.makeText(context: this, text: "hahaha", Toast.LENGTH_LONG);
int xValue = Integer.parseInt(editText1.getText().toString());
int yValue = Integer.parseInt(editText2.getText().toString());
//입력된 좌표값을 가져온다
toast.setGravity(gravity: Gravity.TOP| Gravity.TOP, xValue, yValue);
toast.show();
}catch(NumberFormatException e){
e.printStackTrace();
}
}
//toast message 모양 바꾸기
public void onBtn2Clicked(View v){
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_boarder, (ViewGroup)findViewById(R.id.toast_layout));
TextView textView = layout.findViewById(R.id.textView);
Toast toast = new Toast(context: this);
textView.setText("style changed toast");
toast.setGravity(Gravity.CENTER, xOffset: 0, yOffset: -50);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();
}
}
'android studio' 카테고리의 다른 글
카드뷰(CardView) (0) | 2019.08.12 |
---|---|
버튼 style변경(onDraw(), invalidate()) (0) | 2019.08.12 |
inflation (0) | 2019.08.11 |
GridView (0) | 2019.08.11 |
Spinner (0) | 2019.08.10 |