1)main xml
2 ) create another tost.xml file
java file
package com.example.viraj.blog_5_custom_tost;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.id_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//===============================================================================================
////layout inflator ka instance create karange
// LayoutInflater l1 = getLayoutInflater();
//Getting the View object as defined in the tost.xml file jo create ki he
//View layout = l1.inflate(R.layout.tost,
// (ViewGroup) findViewById(R.id.tostxml));
//tost name ka xml page he aur uss me tostxml layout ki id he
//==============================================================================================
View layout=getLayoutInflater().inflate(R.layout.tost, (ViewGroup)findViewById(R.id.tostxml) );
Toast toast=new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0,0);
toast.setView(layout);
toast.show();
}
});
}
}
xml second toast.xml
2 ) create another tost.xml file
java file
package com.example.viraj.blog_5_custom_tost;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.id_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//===============================================================================================
////layout inflator ka instance create karange
// LayoutInflater l1 = getLayoutInflater();
//Getting the View object as defined in the tost.xml file jo create ki he
//View layout = l1.inflate(R.layout.tost,
// (ViewGroup) findViewById(R.id.tostxml));
//tost name ka xml page he aur uss me tostxml layout ki id he
//==============================================================================================
View layout=getLayoutInflater().inflate(R.layout.tost, (ViewGroup)findViewById(R.id.tostxml) );
Toast toast=new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0,0);
toast.setView(layout);
toast.show();
}
});
}
}
xml second toast.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tostxml">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@drawable/maxresdefault" />
</LinearLayout>
main 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="com.example.viraj.blog_5_custom_tost.MainActivity">
<Button
android:id="@+id/id_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="35dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>
0 comments:
Post a Comment