Skip to content

Instantly share code, notes, and snippets.

@codingwithsara
Last active July 17, 2019 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codingwithsara/c539cb92d4de77ac46b4c555dd2fcb49 to your computer and use it in GitHub Desktop.
Save codingwithsara/c539cb92d4de77ac46b4c555dd2fcb49 to your computer and use it in GitHub Desktop.
Custom Toast
package jp.codeforfun.customtoast;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showToast(View view) {
LayoutInflater layoutInflater = getLayoutInflater();
View customToastView = layoutInflater.inflate(R.layout.toast_layout, null);
// TextViewのテキストを変更する
((TextView)customToastView.findViewById(R.id.msg)).setText("Hello World!");
Toast toast = Toast.makeText(customToastView.getContext(), "", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setView(customToastView);
toast.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment