I am very new to android. I am making a custom toast but by app is crashing just after the toast appears in the screen. This is my mainactivity.java -
public class MainActivity extends Activity {
int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void dosome(View V) {
Log.d("message","a message");
if(V.getId() == R.id.launchmap) {
Toast toast = new Toast(this);
toast.setGravity(Gravity.BOTTOM, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
LayoutInflater inflater = getLayoutInflater();
View appear = inflater.inflate(R.layout.toast_layout,(ViewGroup)findViewById(R.id.root),true);
toast.setView(appear);
toast.show();
}
}
}
And these are my xml files.
toast_layout.xml -
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/images" />
activity_main.xml -
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/root"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/launchmarket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="dosome"
android:text="launchmarket" />
<Button
android:id="@+id/launchmap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="dosome"
android:text="launchmap" />
</LinearLayout>
However, in the inflater.inflate, if i pass the 3rd parameter as false, everything works. Why so?
Thing is toast is not supposed to be child of some layout in your view, its an independent layout. This is the layout of your toast (toast_layout.xml in your case).
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/images" />
You need to wrap this with a layout lets say a LinearLayout and give id toast_layout_root for that particular linearLayout. Like so
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/toast_layout_root">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/images" />
</LinearLayout>
Then do this while making your Toast
View appear = inflater.inflate(R.layout.toast_layout,(ViewGroup)findViewById(R.id.toast_layout_root),true);
What you are doing currently is trying attach toast to the linearlayout that is in your activity. Toasts have to be independent.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.