I'm using dialog fragment and everything is fine until I'm trying to remove title bar. There is a code:
Dialog XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#f0f0f0">
<GridView
android:id="#+id/gv_registration_avatars"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_margin="#dimen/avatar_layout_margin"
android:columnWidth="100dp"
android:drawSelectorOnTop="true"
android:gravity="center"
android:numColumns="2"
android:stretchMode="columnWidth"
android:focusable="true"
android:clickable="true"
/>
<Button
android:id="#+id/dismiss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/avatar_layout_margin"
android:layout_marginRight="#dimen/avatar_layout_margin"
android:layout_below="#id/gv_registration_avatars"
android:text="Ok"
android:layout_gravity="right"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar_avatars"
android:layout_centerInParent="true"
android:visibility="gone"/>
DialogFragment class is :
public class AvatarPickDialogFragment extends DialogFragment {
private GridView mGridView;
private ProgressBar mProgressBar;
private GridViewAvatarAdapter mGridAdapter;
private ArrayList<String> mGridData;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.avatar_pick_dialog_fragment, container, false);
mGridView = (GridView) rootView.findViewById(R.id.gv_registration_avatars);
mProgressBar = (ProgressBar) rootView.findViewById(R.id.progressBar_avatars);
mGridData = new ArrayList<>();
mGridAdapter = new GridViewAvatarAdapter(getActivity(), R.layout.grid_item_avatar_layout, mGridData);
mGridView.setAdapter(mGridAdapter);
mProgressBar.setVisibility(View.VISIBLE);
String item;
for (int i = 0; i < 10; i++) {
item = new String();
item= new String("string");
mGridData.add(item);
}
mProgressBar.setVisibility(View.GONE);
mGridAdapter.setGridData(mGridData);
Button dismiss = (Button) rootView.findViewById(R.id.dismiss);
dismiss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dismiss();
}
});
return rootView;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// The only reason you might override this method when using onCreateView() is
// to modify any dialog characteristics. For example, the dialog includes a
// title by default, but your custom layout might not need it. So here you can
// remove the dialog title, but you must call the superclass to get the Dialog.
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
}
When I am using dialog.requestWindowFeature(Window.FEATURE_NO_TITLE) title bar disappear, but my dialog window width changes to very short and I don't know why, because with title bar it is fine. Any suggestions?
You can try setting fragment width in fragment's onStart() method
#Override
public void onStart()
{
super.onStart();
if (getDialog() == null)
return;
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}
Related
I need to have access for click on my behind fragment ( such as click on a button ) when DialogFragment is shown.
But when I click outside of DialogFragment, this will dismiss.
Used code to show DialogFragment:
FragmentManager fragmentManager = getFragmentManager();
MyDialogMapFragment dialogFragment = new MyDialogMapFragment();
dialogFragment.show( fragmentManager, "" );
MyDialogMapFragment Class:
public class MyDialogMapFragment extends DialogFragment {
private Activity mActivity;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.dialog_map_info, container, false);
return rootView;
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new Dialog(getActivity(), R.style.DialogMap);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_map_info);
dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.BOTTOM;
dialog.getWindow().setAttributes(lp);
return dialog;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof Activity) {
mActivity = (Activity) context;
}
}
}
dialog_map_info.xml Layout:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="90dp"
android:paddingLeft="20dp"
android:paddingRight="20dp">
<RelativeLayout
android:id="#+id/mainFrame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:clickable="true"
android:focusable="true"
android:background="#drawable/background_map_info">
<ProgressBar
android:id="#+id/progress_loader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_centerInParent="true"
style="?android:attr/progressBarStyle" />
<TextView
android:id="#+id/itemTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Test" />
</RelativeLayout>
</FrameLayout>
Below image will describe what is my layout:
If you need to detect a click the while a dialog is being shown it would be easier to set an onClickListener to a view on your dialog.
When you the onClickListener is called then you can either perform a click on the button button.perfomClick() or even better call the function that the button onClickListener calls.
How to I use my xml as a layout for my dialog? This class is used to show a dialog but the problem is i want to set my own layout.
public static void showRateDialog(final Context mContext, final SharedPreferences.Editor editor) {
final Dialog dialog = new Dialog(mContext);
dialog.setTitle("Rate " + APP_TITLE);
LinearLayout ll = new LinearLayout(mContext);
ll.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(mContext);
tv.setText("If you enjoy using " + APP_TITLE + ", please take a moment to rate it. Thanks for your support!");
tv.setWidth(240);
tv.setPadding(4, 0, 4, 10);
ll.addView(tv);
Button b1 = new Button(mContext);
b1.setText("Rate " + APP_TITLE);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + APP_PNAME)));
dialog.dismiss();
}
});
ll.addView(b1);
Button b2 = new Button(mContext);
b2.setText("Remind me later");
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
ll.addView(b2);
Button b3 = new Button(mContext);
b3.setText("No, thanks");
b3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (editor != null) {
editor.putBoolean("dontshowagain", true);
editor.commit();
}
dialog.dismiss();
}
});
ll.addView(b3);
dialog.setContentView(ll);
dialog.show();
}
I want it to have 3 buttons and a picture. Is it possible to do so?
You can set custom layout to your dialog like below:
Create a custom layout file:
custom.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF" />
<TextView
android:id="#+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"/>
</RelativeLayout>
Then in your activity:
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text1);
text.setText("Text view 1");
TextView text = (TextView) dialog.findViewById(R.id.text2);
text.setText("Text view 2");
dialog.show();
DialogFragment is now the canonical way to display overlays; using Dialog directly is considered bad practice
Usage
Custom View
<!-- fragment_edit_name.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/edit_name"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:orientation="vertical" >
<TextView
android:id="#+id/lbl_your_name" android:text="Your name"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<EditText
android:id="#+id/txt_your_name"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:inputType="text"
android:imeOptions="actionDone" />
</LinearLayout>
and DialogFragment will be
import android.support.v4.app.DialogFragment;
// ...
public class EditNameDialogFragment extends DialogFragment {
private EditText mEditText;
public EditNameDialogFragment() {
// Empty constructor is required for DialogFragment
// Make sure not to add arguments to the constructor
// Use `newInstance` instead as shown below
}
public static EditNameDialogFragment newInstance(String title) {
EditNameDialogFragment frag = new EditNameDialogFragment();
Bundle args = new Bundle();
args.putString("title", title);
frag.setArguments(args);
return frag;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_edit_name, container);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Get field from view
mEditText = (EditText) view.findViewById(R.id.txt_your_name);
// Fetch arguments from bundle and set title
String title = getArguments().getString("title", "Enter Name");
getDialog().setTitle(title);
// Show soft keyboard automatically and request focus to field
mEditText.requestFocus();
getDialog().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}
}
and showing the dialog in an Activity:
public class DialogDemoActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showEditDialog();
}
private void showEditDialog() {
FragmentManager fm = getSupportFragmentManager();
EditNameDialogFragment editNameDialogFragment = EditNameDialogFragment.newInstance("Some Title");
editNameDialogFragment.show(fm, "fragment_edit_name");
}
}
Custom alert dialog
* using create custom alert dialog box*/
private void Multiple_spinner_alert(int position) {
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.multiple_spinner_recycler_layout);
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.CENTER;
dialog.getWindow().setAttributes(lp);
Window window = dialog.getWindow();
final RecyclerView recycler_spinner = (RecyclerView) dialog.findViewById(R.id.recycler_spinner);
LinearLayoutManager layoutManager = new LinearLayoutManager(context);
recycler_spinner.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, true));
recycler_spinner.setLayoutManager(layoutManager);
mAdapter = new MultipleSpinnerRecyclerAdapter(context, getSetList,listPK_ProviderTypeOptions_ID);
recycler_spinner.setAdapter(mAdapter);
final TextView alert_tv_ok=(TextView)dialog.findViewById(R.id.alert_tv_ok);
onChangeSelectedReceivers();
alert_tv_ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) { {
// do your code
dialog.dismiss();
}
});
dialog.show();
}
I am using custom dialog inflate layout like this
set dialog height and width according to your requirements
it helps you
In your layout resource file make sure the android:layout_width is
android:layout_height = "wrap_content"
and not
android:layout_height = "match_parent"
Red text came out a little small. Here is what it says:
Need to get the background color of the dialog so I can set the background of the "more info" textview to that same color. Need to do this programmatically because color of the dialog is different depending on API level.
Java code:
public class CityDetailDialog extends DialogFragment {
private SharedPreferences sharedPreferences;
private City mCity;
private LinearLayout mCityContainerLayout;
private ImageView mImageFlag;
private TextView mTextCity;
private ImageView mImageCity;
private ImageButton mButtonGoogleMaps;
private ImageButton mButtonWikipedia;
private TextView mMoreInfoTextView;
public CityDetailDialog() {
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
sharedPreferences = getActivity().getSharedPreferences("settings", Context.MODE_PRIVATE);
return inflater.inflate(R.layout.dialog_city_detail, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mCity = (City) getArguments().getSerializable("city");
mCityContainerLayout = (LinearLayout) view.findViewById(R.id.container_city_dialog);
mImageFlag = (ImageView) view.findViewById(R.id.flag_image);
mTextCity = (TextView) view.findViewById(R.id.city_name);
mImageCity = (ImageView) view.findViewById(R.id.city_image);
mButtonGoogleMaps = (ImageButton) view.findViewById(R.id.button_google_maps);
mButtonWikipedia = (ImageButton) view.findViewById(R.id.button_wikipedia);
mMoreInfoTextView = (TextView) view.findViewById(R.id.more_info_label);
mImageFlag.setImageResource(ResourceByNameRetriever.getDrawableResourceByName(mCity.
getCountryName(), getActivity()));
mTextCity.setText(ResourceByNameRetriever.getStringResourceByName(mCity.getCityName(),
getActivity()));
Picasso.with(getActivity()).load(mCity.getImageUrl()).into(mImageCity);
mButtonGoogleMaps.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent mapsIntent = new Intent(Intent.ACTION_VIEW);
mapsIntent.setData(Uri.parse("geo:" + mCity.getCoordinates()));
Intent chooser = Intent.createChooser(mapsIntent, getString(R.string.launch_maps));
startActivity(chooser);
}
});
mButtonWikipedia.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent wikiIntent = new Intent(Intent.ACTION_VIEW);
wikiIntent.setData(Uri.parse(mCity.getWikiUrl()));
Intent chooser = Intent.createChooser(wikiIntent, getString(R.string.launch_browser));
startActivity(chooser);
}
});
}
}
Based on the code in your xml file on github following changes need to be made to achieve the desired effect :
<LinearLayout
android:id="#+id/container_buttons"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal"
android:layout_below="#+id/relativeLayout">
.
.
.
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/relativeLayout">
<View
android:id="#+id/horizontal_divider1"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_centerInParent="true"
android:background="#color/gray"
android:layout_toLeftOf="#+id/more_info_label"
android:layout_toStartOf="#+id/more_info_label"/>
<TextView
android:id="#+id/more_info_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:padding="5dp"
android:text="#string/more_info"
android:textSize="18dp" />
<View
android:id="#+id/horizontal_divider2"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_centerInParent="true"
android:background="#color/gray"
android:layout_toRightOf="#+id/more_info_label"
android:layout_toEndOf="#+id/more_info_label"/>
</RelativeLayout>
I use the following codes to create a PopupWindow with dim background by clicking a menu item in Toolbar:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.btn_1:
Log.i(TAG, "Clicked Button 1");
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup_window, null);
final PopupWindow popupWindow = new PopupWindow(popupView, Toolbar.LayoutParams.MATCH_PARENT, Toolbar.LayoutParams.MATCH_PARENT);
Button btnDismiss = (Button)popupView.findViewById(R.id.btn_dismiss);
btnDismiss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
However, I want to the dim background to exclude the action bars / toolbars. How can I achieve it?
The layout of the PopupView is as follow:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/bac_dim_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#C0000000"
android:visibility="visible" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="1dp"
android:background="#android:color/darker_gray">
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="20dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="It's a PopupWindow" />
<Button
android:id="#+id/btn_dismiss"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Dismiss" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
if you want popup window to display with dim background except ToolBar/ActionBar
here i tried setting position of popup window. fetch statusbar & navigation bar height & set Y position to the popup
int actionBarHeight = 0;
View popupView = LayoutInflater.from(getActivity()).inflate(R.layout.popup_, null);
final PopupWindow popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
actionBarHeight = getNavigationBarHeight(getActivity(), Configuration.ORIENTATION_PORTRAIT);
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
actionBarHeight = actionBarHeight + getResources().getDimensionPixelSize(resourceId);
}
Button btnDismiss = (Button) popupView.findViewById(R.id.btn_dismiss);
btnDismiss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
popupWindow.showAsDropDown(popupView, 0, actionBarHeight);
Another way to display popup as you said in comment.Below mentioned you can try
Take one hidden control(TextView) in layout which will be aligned to top. Take the location of the TextView & assigned Y position to popup window
public class MyPopupTest extends Fragment{
public static final String TAG = "MyPopupTest";
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.home_screen_slider, container, false);
}
#Override
public void onViewCreated(final View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.btnSelect).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showPopup(view);
}
});
}
public void showPopup(View view) {
View popupView = LayoutInflater.from(getActivity()).inflate(R.layout.popup_, null);
final PopupWindow popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
Button btnDismiss = (Button) popupView.findViewById(R.id.btn_dismiss);
btnDismiss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
int[] locationOfHiddenTextView = new int[2];
view.findViewById(R.id.hiddenTopTextView).getLocationOnScreen(locationOfHiddenTextView);
//Give Position as a start point for popup
popupWindow.showAsDropDown(popupView, 0, locationOfHiddenTextView[1]);
}
}
XML File of the fragment which is goint to display
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relative_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffc699">
<TextView
android:id="#+id/hiddenTopTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:visibility="gone" />
<Button
android:id="#+id/btnSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Popup" />
</RelativeLayout>
I have a solution, but I only test with DialogFragment, not sure it works with dialog or not. I will post it anyway:
1. create your own class which extends DialogFragment.
2. create xml file with your designed dim color:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#80000000" // your designed dim color
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/goto_dialog_title"
android:textColor="#android:color/white"
android:textSize="#dimen/km_25sp"/>
3. Override funcions in your DialogFragment:
private class MyDialog extends DialogFragment{
//override your onCreateView to show your xml here
//override onCreate to show dialog without border and without dim background
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_FRAME, 0);
}
// override to show dim background and avoid dimming actionbar
#Override
public void onResume() {
super.onResume();
int statusBarAndActionBarHeight = 200; //u should calculate yourself
Window window = getDialog().getWindow();
// set size to your dialog --> fullscreen size to show dim bg all screen except action bar and status bar
DisplayMetrics displaymetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
window.setLayout(displaymetrics.widthPixels, displaymetrics.heightPixels - statusBarAndActionBarHeight);
//margin top to avoid dim background above actionbar and status bar
WindowManager.LayoutParams p = window.getAttributes();
p.y = statusBarAndActionBarHeight;
getDialog().getWindow().setGravity(Gravity.TOP);
getDialog().getWindow().setAttributes(p);
}
}
I have an activity file which uses a ViewPager to show different pages - each which are fragments with a video. I want some of the pages to have buttons which when I press turn into a pop up to display some information - is this possible?
I've tried this code below:
Here is the code in my fragment java:
public class MassageandBeauty extends Fragment implements View.OnClickListener {
Button btnPopup;
showPopup showPopup;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.first_fragment, container, false);
btnPopup = (Button)v.findViewById(R.id.cavern);
btnPopup.setOnClickListener(this);
return v;
}
//#Override
public void onViewCreated(View v) {
btnPopup = (Button)v.findViewById(R.id.cavern);
btnPopup.setOnClickListener(this);
}
//#Override
#Override
public void onClick(View parent) {
new showPopup(getActivity().getApplicationContext()).cavern(parent);
}
Here is the code in my popup:
public class showPopup extends ActionBarActivity {
Context ctx;
public showPopup(Context ctx){
this.ctx = ctx;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popup_layout);
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/Raleway-Black.ttf");
TextView myTextView = (TextView)findViewById(R.id.textViewtop);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myTextView.setTypeface(myTypeface);
}
public void cavern(View parent) {
final PopupWindow popup = new PopupWindow(ctx);
ViewGroup.LayoutParams para = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
TextView tvMessage = new TextView(ctx);
Button btnDismiss = new Button(ctx);
tvMessage.setLayoutParams(para);
btnDismiss.findViewById(R.id.close);
btnDismiss.setLayoutParams(para);
btnDismiss.setWidth(20);
btnDismiss.setHeight(10);
btnDismiss.setText("x");
popup.setContentView(btnDismiss);
popup.setWidth(1200);
popup.setHeight(800);
popup.showAtLocation(parent, Gravity.CENTER_HORIZONTAL, 10, 10);
popup.update();
btnDismiss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
popup.dismiss();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_popup_layout, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(new CalligraphyContextWrapper(newBase));
}
}
Here is the xml of my PopUp
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/showpopup"
android:layout_height="fill_parent"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:background="#8000"
android:orientation="vertical"
tools:context="morzineappy.morzineappy.showPopup">
<TextView
android:id="#+id/textViewtop"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:textColor="#FFF"
android:textSize="30sp"
android:text="The Cavern" />
<Button
android:id="#+id/close"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="1200dp"
android:layout_gravity="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x" />
<ImageView
android:id="#+id/poster"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/cavern"
android:layout_marginTop="20dp"
android:layout_marginLeft="800dp"
android:layout_marginRight="20dp"
android:layout_gravity="right" />
<TextView
android:id="#+id/textViewtop"
android:layout_width="800dp"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_marginTop="60dp"
android:layout_marginLeft="20dp"
style="#style/BusinessFont"
android:text="Text"
/>
For some reason it won't display the text in the popup- I want to add the layout into the popup but I'm not sure how- any ideas?!
First create your popup or message dialog with extending DialogFragment, then on button click:
MyDialogFragment dialog = new MyDialogFragment(....);
dialog.show(getActivity().getSupportFragmentManager(), "name of fragment here");
Your question is not very clear, but I'll assume that you're trying to use something like an AlertDialog. Typically when creating these, you need a Context object as an argument. In an Activity, you can usually create an AlertDialog like so:
AlertDialog d = new AlertDialog(this);
But I'm guessing that your issue is that since you are in a fragment, the this object is not a Context. So for you, it would be more like so:
AlertDialog d = new AlertDialog(getActivity());