I want my dialog to not fit the whole screen like the image shown. How should i make it so it is not as long (height).
public class SidemenuInfoDialogFragment extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.sidemenu_info_main, container, true);
return layout;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new Dialog(new ContextThemeWrapper(getActivity(), R.style.Theme_Window_NoMinWith));
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
}
The View:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#+id/sidemenu_info_main_relative">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/text1"
android:text="TEST TEXT "/>
</RelativeLayout>
Let me know if there is something else needed to be able to help me get on the right path of fixing this.
Give your textview inside your dialog view height and width as "wrap_content".Like:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#+id/sidemenu_info_main_relative">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text1"
android:text="TEST TEXT "/>
</RelativeLayout>
Try changing you xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#+id/sidemenu_info_main_relative">
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:id="#+id/text1"
android:text="TEST TEXT "/>
</RelativeLayout>
If that does not work with you here is another approach that I usually do :
public class PopUp {
public void poupUP(final Activity activity) {
final Dialog dialog = new Dialog(activity, R.style.dialogwithoutTitle);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
//here add the layout
dialog.setContentView(R.layout.pop_up);
// The comments are to change the position of the alert dialogu
// WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();
//wmlp.gravity = Gravity.TOP | Gravity.END;
//wmlp.x = 100; //x position
//wmlp.y = 100; //y position
dialog.show();
}
}
Then wherever you want it to show do this :
PopUp p = new PopUp();
p.popUP();
RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.sidemenu_info_main, container, false);
Try this:
alert.getWindow().setLayout(dialogWidth, LayoutParams.WRAP_CONTENT);
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(alert.getWindow().getAttributes());
layoutParams.x = xPosition;
layoutParams.y = yPosition;
alert.getWindow().setAttributes(layoutParams);
Related
I am working on an Android Application , Here I have created a custom dialog box , The dialog box was appearing on complete height of the device , but I wanted to leave margins from top and bottom , so I set margin using below code
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(alertDialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = Utility.convertDPtoPixel(400, screen);
alertDialog.show();
alertDialog.getWindow().setAttributes(lp);
Above code worked perfectly, but the dialog appears with black surroundings.
Here is the screen shot:
Now I want to remove these black surroundings from the dialog box.
Here is the code of dialog box:
public void showDialog() {
AlertDialog.Builder builder;
final AlertDialog alertDialog;
//final Dialog alertDialog;
Context mContext;
mContext = screen;
LayoutInflater inflater = (LayoutInflater) LoginActivity.screen.getSystemService(LoginActivity.screen.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.countrylist, null);
LinearLayout rellayout = (LinearLayout) layout.findViewById(R.id.rellayout);
RelativeLayout closeAcessCodeDialog = (RelativeLayout)layout.findViewById(R.id.closeAcessCodeDialog) ;
ListView listview = (ListView) layout.findViewById(R.id.listview);
listview.setAdapter(new CustomAdapter1(LoginActivity.screen, countryList));
builder = new AlertDialog.Builder(LoginActivity.screen);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(alertDialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = Utility.convertDPtoPixel(400, screen);
alertDialog.show();
alertDialog.getWindow().setAttributes(lp);
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id) {
countryName = idCountry.get(pos);
System.out.println("Country code is: " + countryName);
alertDialog.cancel();
text.setText(countryList.get(pos));
}
});
closeAcessCodeDialog.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
alertDialog.dismiss();
}
});
}
countrylist.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#ffe264"
app:cardCornerRadius="10dp"
>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rellayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_above="#+id/listview"
android:gravity="center_vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select your Country"
android:textColor="#000"
android:textSize="18sp"
android:textStyle="bold"
android:layout_centerInParent="true"
/>
<RelativeLayout
android:id="#+id/closeAcessCodeDialog"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:gravity="center">
<ImageButton
android:id="#+id/closeDialog"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/close_country_dialog" />
</RelativeLayout>
</RelativeLayout>
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#android:color/transparent"
android:divider="#e5e5e5"
android:background="#drawable/list_bacground"
android:dividerHeight="1px" />
</LinearLayout>
I visited many similar links over stackoverflow , but no one was helpfull .
Hope this will help you a lot
I think its default theme color which is Black so you have to check the import of your AlertDialog it should be import android.support.v7.app.AlertDialog; and one more thing you should use custom color for Background like below
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Try this.
Dialog alertDialog = new Dialog(this);
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.setContentView(R.layout.yourlayout);
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alertDialog.show();
So I am trying to add a button programatically to a DialogFragment but the button is never shown.
I have the following layout:
<?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"
android:background="#99000000"
android:padding="10dp">
<RelativeLayout
android:id="#+id/notificationLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/logo_background_shape"
android:padding="2dp"
android:layout_centerVertical="true"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/input_layout"
android:paddingBottom="20dp">
<TextView
android:id="#+id/start_date_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Some text"
android:paddingLeft="10dp"
android:textColor="#color/my_blue"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/clickables_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/input_layout"
android:paddingBottom="10dp">
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
Here is the way I create my dialog's view:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
/**
* Setup the dialog and its styles
*/
final Dialog dialog = new Dialog(getActivity(), android.R.style.Theme_Black_NoTitleBar_Fullscreen);
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
final View view = getActivity().getLayoutInflater().inflate(R.layout.notification_fragment_layout, null);
final RelativeLayout relLayout = (RelativeLayout)getActivity().findViewById(R.id.clickables_layout);
RelativeLayout.LayoutParams paramsButton1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
paramsButton1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
Button button1 = (Button) mDynamicNotification.getClickableList().get(0).getElementView(getActivity(), paramsButton1);
relLayout.addView(button1);
/**
* Code block handling blurring of the background
*/
final Drawable d = new ColorDrawable(Color.TRANSPARENT);
dialog.getWindow().setBackgroundDrawable(d);
dialog.getWindow().setContentView(view);
//allow the dialog to be canceled when touching outside of the dialog
dialog.setCanceledOnTouchOutside(false);
final Activity activity = getActivity();
final View content = activity.findViewById(android.R.id.content).getRootView();
if (content.getWidth() > 0) {
Bitmap image = BlurrBuilder.blur(content);
dialog.getWindow().setBackgroundDrawable(new BitmapDrawable(activity.getResources(), image));
} else
content.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Bitmap image = BlurrBuilder.blur(content);
dialog.getWindow().setBackgroundDrawable(new BitmapDrawable(activity.getResources(), image));
}
});
return dialog;
}
Problem:
The problem is that the button that I am adding is never shown in the layout once launched. It almost seems like the layout gets created before I add the button so the button is never shown...
The problem is that the button that I am adding is never shown in the
layout once launched
Because forget to add relLayout in Dialog layout which passing in dialog.getWindow().setContentView.
Use view.addView method to add Button in view :
relLayout.addView(button1);
view.addView(relLayout);
I created a popup window to show a disclaimer in my app but I can't figure out why i cannot set text to TextView. It is an HTML string so I did this:
private void showPopup(final Activity context) {
final PopupWindow pwindo;
Button btnClosePopup;
try {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup_layout, (ViewGroup) findViewById(R.id.popup_element));
pwindo = new PopupWindow(layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
TextView txt = (TextView)findViewById(R.id.txtView);
txt.setText(Html.fromHtml(getString(R.string.tos_text)));
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
popup_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#4d4d4d"
android:orientation="vertical"
android:padding="10sp" >
<TextView
android:id="#+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5sp"
android:textColor="#ffffff" />
<Button
android:id="#+id/btn_close_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Close" />
</LinearLayout>
strings.xml
<string name="tos_text"><![CDATA[
<p>This is a html-formatted string with <b>bold</b> and <i>italic</i> text</p>
<p>This is another paragraph of the same string.</p>
]]>
</string>
It works only if I add android:text="#string/tos_text" in the XML file, but i see the raw html codes. What could be the problem?
Try next:
private void showPopup() {
//if you call this method correctly then you do not need to wrap
// this method by try-catch block which affects performance
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup_layout, (ViewGroup) findViewById(R.id.popup_element), false);
final PopupWindow pwindo = new PopupWindow(layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
//get txt view from "layout" which will be added into popup window
//before it you tried to find view in activity container
TextView txt = (TextView) layout.findViewById(R.id.txtView);
txt.setText(Html.fromHtml(getString(R.string.tos_text)));
//init your button
Button btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
}
});
//show popup window after you have done initialization of views
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
}
EDIT:
To add scrollable content you need to change approach of initialization of popup window:
final PopupWindow pwindo = new PopupWindow(layout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
where you need to change width of window to ViewGroup.LayoutParams.MATCH_PARENT.
Then you need to wrap your text view by ScrollView which has root child - LinearLayout:
popup_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_element"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4d4d4d"
android:orientation="vertical"
android:padding="10sp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/txtView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5sp"
android:textColor="#ffffff"/>
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/btn_close_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Close"/>
</LinearLayout>
ScrollView has field with weight:
android:layout_weight="1"
I have added this to avoid hiding of button when text will fill all of visible content.
EDIT 2:
Also you can fix height of scroll view, if you do not want to fill all the screen:
-----
<ScrollView
android:layout_width="match_parent"
android:layout_height="150dp">
-----
this is my onCreate() function:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//inflate the view
View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_loading, null);
TextView loadingMessage = (TextView) view.findViewById(R.id.fragment_loading_message_textview);
loadingMessage.setText("test");
//the dialog
Dialog dialog;
//set the dialog views
int themeId = getDialogThemeId();
//if there is no theme
if (themeId == -1)
dialog = new Dialog(getActivity());
else
dialog = new Dialog(getActivity(),getDialogThemeId());
//remove background
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
//remove title
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
//set the content view
dialog.setContentView(view);
//set cancel properties
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
return dialog;
}
this is the layout i'm inflating:
<?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:layout_gravity="center"
android:orientation="vertical"
android:background="#android:color/black">
<!-- spinner -->
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#android:color/transparent" />
<!-- text -->
<TextView
android:id="#+id/fragment_loading_message_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAppearance="#android:style/TextAppearance.Large"
android:textColor="#android:color/white"
android:text="LOADING"/>
</LinearLayout>
although i'm calling loadingMessage.setText("test") the loading message is keeping showing me "LOADING" in the message... any idea???
ok i found the problem.
in the same DialogFragment I've implemented onCreateDialog and onCreateView as well.
This causes my view to be inflated twice. since onCreateView is called after onCreateDIalog, the view I've loaded in the onCreateDialog was overridden..
hope it will someone else
I want to show a list with a button under it in an AlertDialog. The Button is to close the Dialog itself. I extended AlertDialog and added some LayoutParams to the Window to extend the Dialog of the whole screen width:
//Grab the window of the dialog, and change the width
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = getWindow();
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
ok. But if the list is long (display is full of it), I can scroll the ListView but the button is not shown under it. Here's the ContentView of the Dialog:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/choose_equipment_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_below="#+id/choose_equipment_list"
android:id="#+id/btn_choose_equipment_close"
style="#style/custom_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="close" />
</RelativeLayout>
What can I do to show the button ALWAYS under the list, no matter how long it is? (I read a lot warnings to put a ListView inside a ScrollView, tried it anyway and failed...)
Try it:
<?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:orientation="vertical" >
<ListView
android:id="#+id/choose_equipment_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:id="#+id/btn_choose_equipment_close"
style="#style/custom_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="close" />
</LinearLayout>
If not work, remove the style="#style/custom_button" line. Some style configuration could change the view style and hide the button.
I want to show a list with a button under it in an AlertDialog.
So What you have to do is :
Remove this from from your Button:
android:layout_below="#+id/choose_equipment_list"
Then put this in Button:
android:layout_alignParentBottom="true"
And Put this in ListView:
android:layout_above="#+id/btn_choose_equipment_close"
You are Done.
Hmm. Maybe try to use Fragments. I coded sample for you. Tell me if it is what you are looking for:)
MainActivity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
findViewById(R.id.open_dialog_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SampleDialogFragment dialogFragment = new SampleDialogFragment();
dialogFragment.show(getSupportFragmentManager(), "dialogFragment");
}
});
}
}
main_activity.xml
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="#+id/open_dialog_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open dialog"
android:layout_gravity="center" />
</FrameLayout>
SampleDialogFragment.java
public class SampleDialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle("Timer");
alert.setPositiveButton("OK", null);
View view = View.inflate(getActivity(), R.layout.dialog, null);
String[] array = {"Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, array);
((ListView) view.findViewById(R.id.listView)).setAdapter(adapter);
alert.setView(view);
return alert.create();
}
}
dialog.xml
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
I think it could be possible to get what you need declaring in the XML your button first located in the bottom and then the listview above it with height set to fill_parent
This is should be what you want...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/choose_equipment_list"
android:layout_above="#+id/btn_choose_equipment_close"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn_choose_equipment_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="close" />
</RelativeLayout>