I wanted to customise android Dialog to make it look like a floating dialog as below which is suggested in https://material.google.com/components/dialogs.html#dialogs-simple-dialogs
I tried creating a DialogFragment in which onCreateDialog returns a Dialog with a view whose root is a CardView.But couldn't get the expected result.Can I have some suggestions on how to achieve this?
My layout looks like this,
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center|top"
card_view:cardElevation="5dp"
card_view:cardPreventCornerOverlap="false">
//Contents come here
</android.support.v7.widget.CardView>
and in code,
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new Dialog(getActivity());
View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_font_selection,null,false);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(view);
float width = (I am getting screen width here) * .80f;
dialog.getWindow().setLayout((int) width, ViewGroup.LayoutParams.WRAP_CONTENT);
return dialog;
}`
but the result looks like this,
Instead of getting elevation I was getting a black shade
AlertDialog.Builder mydialog = new AlertDialog.Builder( context);
mydialog.setTitle("Set backup accounts");
mydialog.setItems(CharSequence[] items, OnClickListener);
mydialog.show();
mydialog.setContentView(R.layout.mydialogLayout);
This in your activity/fragment. And so declare your layout as follow:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView
android:id="#+id/myID1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
After that, if you want to handle the clicks just do that:
TextView myView1 = (TextView) mydialog.findViewById(myID1);
myView1.setOnClickListener(...);
MaterialAlertDialogBuilder is now available for Android:
material.io/develop/android/components/dialog
Related
I have a custom layout for my dialog box which shows the available product of a store in my app:
popstoreproduct.xml
<?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="wrap_content"
android:orientation="vertical"
android:background="#color/backgroundgrey">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_marginVertical="20dp">
<TextView
android:id="#+id/txtlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P R O D U C T"
android:textSize="16dp"
android:textStyle="bold"
android:layout_marginRight="15dp"
android:layout_marginLeft="20dp"
android:textColor="#color/text1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="L I S T"
android:textSize="16dp"
android:textColor="#color/text1"/>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/poprecycler"
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_gravity="center_horizontal"
android:overScrollMode="never"
android:layout_marginBottom="10dp"/>
</LinearLayout>
I have to use this layout as dialog in different activities so I have created a class that has a function which calls the dialog from MainActivity
Utility.java
public class Utility {
public Utility() {
}
public void popStoreProduct(Context context,ArrayList<Stock> mainlist,ArrayList<Stock> menuList){
final AlertDialog.Builder mBuild = new AlertDialog.Builder(context,R.style.DialogSlideAnim);
LayoutInflater inflater = LayoutInflater.from(context);
View layout = inflater.inflate(R.layout.popstoreproduct,null);
final RecyclerView precycler =(RecyclerView)layout.findViewById(R.id.poprecycler);
LinearLayoutManager playoutManager = new LinearLayoutManager(context);
precycler.setLayoutManager(playoutManager);
precycler.setHasFixedSize(true);
AdapterPopStoreProduct pAdapter = new AdapterPopStoreProduct(context,mainlist,menuList);
precycler.setAdapter(pAdapter);
mBuild.setView(layout);
final AlertDialog dialog = mBuild.create();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().getAttributes().gravity = Gravity.BOTTOM |Gravity.CLIP_HORIZONTAL;
dialog.show();
}
}
When I run the app and call for the function in my main activity, it crashes and gives the following error that points to the dialog.show() line:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at com.example.myapp.Classes.Utility.popStoreProduct(Utility.java:288)
I don't understand what is wrong with the code. Please Help
PS: I have use slide animation in my dialog that slides from the bottom.
show your styles file and Activitys manifest declaration. as the error say your style for Activity must be/extends Theme.AppCompat. for example:
android:theme="#style/Theme.AppCompat.Light"
you can also set this theme for all Activities adding above line to <application tag instead of each Activity declaration
This question already has answers here:
Dialog with transparent background in Android
(25 answers)
Closed 6 years ago.
Need to make a layout like this.
Below is an alert box which has a transparent background containing spinner and a button below.
i ve tried this as far.
this is the layout file.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#fff"
android:paddingBottom="50dp">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/selectCity_xspn"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="6dp"
android:layout_marginTop="75dp" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/selectCity_xspn"
android:layout_alignParentStart="true"
android:layout_marginTop="70dp"
android:id="#+id/selectCtgry_xspn" />
<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button2"
android:layout_marginTop="300dp"
android:layout_alignParentEnd="true" />
This is the java class where i ve used the transparent option at many places but it doesnt work.
accessing the function at the bottom in the following button.
customdialog_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onCreateDialog2().show();
}
});
public Dialog onCreateDialog2() {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(Registeration.this);
View dialog_view = getLayoutInflater().inflate(R.layout.custom_dialog, null);
//dialog_view.setBackgroundResource(android.R.color.transparent);
builder.setView(dialog_view);
selectCity_spn=(Spinner)dialog_view.findViewById(R.id.selectCity_xspn);
selectCtgry_spn=(Spinner)dialog_view.findViewById(R.id.selectCtgry_xspn);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this.getBaseContext(), R.array.selectCity_spn, android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
assert selectCity_spn != null;
selectCity_spn.setAdapter(adapter);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this.getBaseContext(), R.array.selectCtgry_spn, android.R.layout.simple_spinner_dropdown_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
assert selectCtgry_spn != null;
selectCtgry_spn.setAdapter(adapter2);
View v = getWindow().getDecorView();
v.setBackgroundResource(android.R.color.transparent);
return builder.create();
}
This will do
Dialog dialog = builder.create();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
return dialog;
Try this:-
Dialog dialog1 = new Dialog(Main3Activity.this);
dialog1.setCancelable(true);
dialog1.setContentView(view1);
dialog1.show();
you can set background transparent of any layout or any view or any component by adding this code in XML
android:background="#android:color/transparent"
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>
i custom a dialog :
public class CustomizeDialog extends Dialog implements OnClickListener {
Button close;
TextView tv;
public CustomizeDialog(Context context,String Stringcontent) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_diolog_main);
tv=(TextView) findViewById(R.id.content);
tv.setText(Stringcontent);
close = (Button) findViewById(R.id.close);
close.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v == close)
dismiss();
}
}
the xml is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="100dip"
android:orientation="vertical"
android:background="#drawable/custom_diolog_bg"
android:layout_width="250dip">
<TextView android:layout_height="wrap_content"
android:textColor="#000"
android:textStyle="bold"
android:textSize="18sp"
android:id="#+id/content"
android:layout_marginLeft="15dip"
android:layout_marginTop="5dip"
android:layout_alignParentTop="true"
android:layout_width="250dip"
android:text=" Custom Dialog "/>
<Button android:layout_width="70dip"
android:layout_marginLeft="80dip"
android:background="#drawable/custom_dialog_button_bg"
android:layout_alignParentBottom="true"
android:layout_height="40dip" android:text="关闭"
android:id="#+id/close"></Button>
</RelativeLayout>
my dialog is vrry well,but custom_diolog_bg is a rounded rectangle image,and when i show my dialog ,it show a system Frame behide my custom,so i used this.getwindow.setBackgroundDrawable(null),then the system Frame seems have remove but only the Four Corners not remove,we also see dark Four Corners,because i used the rounded rectangle image.so my question how to remove all the Frame so that my dialog seem Very well
the pic is http://i.stack.imgur.com/EG7oz.jpg ,so you can see there is dark frame in the last,how to remove it? thank you
Solution that worked for me
<style name="DialogTheme" parent="#android:style/Theme.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
</style>
Dialog dialog = new Dialog(this, R.style.DialogTheme);
Use following lines before calling setContentView() :-
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
will work perfectly.
Dialog mydialog = new dialog (this,android.R.style.Theme_Translucent_NoTitleBar);
instead of calling
super(context);
call
super(context, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
Update : Use this xml layout instead
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="100dip"
android:orientation="vertical"
android:background="#drawable/custom_diolog_bg"
android:layout_width="250dip">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true">
<TextView
android:layout_height="wrap_content"
android:textColor="#000"
android:textStyle="bold"
android:textSize="18sp"
android:id="#+id/content"
android:layout_marginLeft="15dip"
android:layout_marginTop="5dip"
android:layout_alignParentTop="true"
android:layout_width="250dip"
android:text=" Custom Dialog " />
<Button
android:layout_width="70dip"
android:layout_marginLeft="80dip"
android:background="#drawable/custom_dialog_button_bg"
android:layout_alignParentBottom="true"
android:layout_height="40dip"
android:text="关闭"
android:id="#+id/close"></Button>
</LinearLayout>
</RelativeLayout>
Try this it worked for me like a charm.
ContextThemeWrapper wrapper = new ContextThemeWrapper(this, android.R.style.Theme_Holo);
final LayoutInflater inflater = (LayoutInflater) wrapper.getSystemService(LAYOUT_INFLATER_SERVICE);
AlertDialog.Builder builder = new AlertDialog.Builder(wrapper);