Create Custom Pop-up on Specific view with using specific layout and also on specific time

First Create Layout for where you can display pop-up dialogue and also specify view with id.

  mainlayout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"              
                     android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bhadresh.PopUpDialog">
 <View
 android:id="@+id/popo_up"
 android:layout_width="fill_parent"
 android:layout_height="40dp"
 android:background="@color/button_material_dark"></View>
</RelativeLayout>

After that Create Layout for pop-up dialogue which you want to display.

             poupdialoge.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/got_it_bg"
android:layout_gravity="center_horizontal"
android:id="@+id/layPopup"
android:layout_marginLeft="100dp"
android:orientation="horizontal">
</LinearLayout>

After Create both layout implement this code in main class file

public class PopUpDialog extends Activity {
private PopupWindow outComePopupForTulTip;
private PopupWindow outComePopup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlayout);
final View anyView = findViewById(R.id.popo_up); //object or view of mainlayout.xml where you can display your dialogue
anyView.postDelayed(new Runnable() {
@Override
public void run() {
callFirstToolTip(anyView);
}
}, 5000);
}
public void callFirstToolTip(View view) {
View layout = LayoutInflater.from(PopUpDialog.this).inflate(R.layout.poupdialoge, null);
                LinearLayout layPopup = (LinearLayout) layout.findViewById(R.id.layCopyDelete);
outComePopup = new PopupWindow(layout);
outComePopup.setFocusable(true);
layPopup.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
outComePopup.setWidth(layPopup.getMeasuredWidth());
outComePopup.setHeight(layPopup.getMeasuredHeight());
outComePopup.setBackgroundDrawable(getResources().getDrawable(android.R.color.transparent));
outComePopup.setOutsideTouchable(true);

outComePopup.showAsDropDown(view);
}
}


No comments: