Need transparent background like this [duplicate] - android

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"

Related

Android Pop Up during opening of Application

I am developing an Android application, where I need to show a popup containing some message on application opening (Image attached for reference).
I have tried to achieve this using toast message and snack bar but could not do exact same.
Please suggest which component in Android to be used to achieve this functionality?
You can achieve the desired results using a dialog. I am sharing the code, you can modify it as you want but make sure you put it inside the onCreate method of the launcher activity.
This is the code for a simple dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Look at this dialog!")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do things
}
});
AlertDialog alert = builder.create();
alert.show();
The one shown in the picture shared by you is a custom dialog where you have to make your own layout. Here is an example,
1. Create your own layout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffffff">
<ImageView
android:layout_width="match_parent"
android:layout_height="120dp"
android:id="#+id/a"
android:gravity="center"
android:background="#DA5F6A"
android:src="#drawable/dialog_cross"
android:scaleType="fitCenter" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEXT"
android:id="#+id/text_dialog"
android:layout_below="#+id/a"
android:layout_marginTop="20dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="20dp"
android:textSize="18sp"
android:textColor="#ff000000"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="OK"
android:id="#+id/btn_dialog"
android:gravity="center_vertical|center_horizontal"
android:layout_below="#+id/text_dialog"
android:layout_marginBottom="20dp"
android:background="#drawable/btn_flat_red_selector"
android:layout_centerHorizontal="true"
android:textColor="#ffffffff" />
</RelativeLayout>
2. Create a class for the custom dialog
public class ViewDialog {
public void showDialog(Activity activity, String msg){
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.dialog);
TextView text = (TextView) dialog.findViewById(R.id.text_dialog);
text.setText(msg);
Button dialogButton = (Button) dialog.findViewById(R.id.btn_dialog);
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
3. Make an object in your launcher activity of the custom dialog class and call the function showDialog
ViewDialog alert = new ViewDialog();
alert.showDialog(getActivity(), "Thank you for installing the Paytm
App");
Or you could use an external library for getting the desired results
Pop.on(this).with().title(R.string.title).layout(R.layout.custom_pop).show();
where R.layout.custom_pop is the custom layout of your dialog.
Try this for same result:
dialog.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="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right">
<Button
android:id="#+id/btnClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="Hello, this is your message"
android:textSize="20sp" />
</LinearLayout>
code:
private void showDialog() {
final Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.dialog);
Window window = dialog.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.BOTTOM;
wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(wlp);
dialog.findViewById(R.id.btnClose).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.cancel();
}
});
dialog.show();
}

How to customise android dialogs to material theme?

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

Android DialogFragment with custom layout remove white border [duplicate]

This question already has answers here:
Dialog with transparent background in Android
(25 answers)
Closed 6 years ago.
I have a DialogFragment class with following code:
public class LeavingAppDialog extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.leaving_app_dialog, null);
builder.setView(v);
Typeface segoeuib = Typeface.createFromAsset(getActivity().getApplicationContext().getAssets(), "fonts/segoeuib.ttf");
TextView text = (TextView) v.findViewById(R.id.msg_dialog);
text.setTypeface(segoeuib);
Dialog dialog = builder.create();
return dialog;
}
}
This dialog uses a custom layout, here is its code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00000000">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:id="#+id/imageView"
android:src="#drawable/box_1_fit"
android:scaleType="centerInside"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:background="#00000000" />
<FrameLayout
android:layout_width="275dp"
android:layout_height="150dp"
android:id="#+id/frameLayout"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="LEAVING THIS APP?"
android:id="#+id/msg_dialog"
android:textSize="35dp"
android:layout_gravity="center_horizontal|top"
android:gravity="center_horizontal|top"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
</FrameLayout>
<Button
android:layout_width="100dp"
android:layout_height="37dp"
android:id="#+id/btn_cancel"
android:background="#drawable/cancel_button_fit"
android:singleLine="false"
android:layout_alignRight="#+id/frameLayout"
android:layout_alignEnd="#+id/frameLayout"
android:layout_marginTop="75dp"
android:layout_below="#+id/frameLayout"
android:layout_marginRight="25dp"
android:onClick="ExitApp"/>
<Button
android:layout_width="100dp"
android:layout_height="37dp"
android:id="#+id/btn_accept"
android:background="#drawable/accept_button_fit"
android:layout_below="#+id/frameLayout"
android:layout_alignLeft="#+id/frameLayout"
android:layout_alignStart="#+id/frameLayout"
android:layout_marginTop="75dp"
android:layout_marginLeft="25dp"
android:onClick="ExitApp"/>
</RelativeLayout>
</RelativeLayout>
The problem is on the screenshot. I need to get rid of these white borders. I've tried to set background color to transparent through layout editor and through code. I've tried to set transparent backgound color to all the layouts and to the image itself, but it all had no effect. My source image is a png file and it really has transparent background, so the problem is not in the source image.
Set Background Transparent:
Like this: mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Try this Custom Alert Dialog Class:
class CustomAlertDialog extends AlertDialog {
CustomAlertDialog(Context context) {
super(context);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
setContentView(R.layout.view_custom_alert_dialog);
RMTextView txtTitle = (RMTextView) findViewById(R.id.txt_alert_title);
txtTitle.setText("Title");
}
}
try this way.
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
// mDialog.getWindow().setBackgroundDrawableResource("#0069ad");
mDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

What should I use instead of custom dialog in android api less 20

I wrote an application which have to call dialog to get name. I need the custom view in dialog so I used .setContentView(). But in android with api less 20 I have problems with displaying it.
This is calling dialog:
public void setName(View v) {
nameDialog = new Dialog(this);
//using our dialog
nameDialog.setContentView(R.layout.new_name);
EditText text = (EditText) nameDialog.findViewById(R.id.form2_dialog_name);
//if we have previous name we show it
if (haveName) {
text.setText(((Button) findViewById(R.id.form2_name_button)).getText());
}
//request focus and call keyboard for input name
text.requestFocus();
text.selectAll();
nameDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
nameDialog.show();
}
the xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_dark"
tools:context=".CreateNoteActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/form2_dialog_name_of_event"
android:id="#+id/textView11" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="#+id/form2_dialog_name"
android:maxLines="1" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/close_button"
android:id="#+id/button8"
android:onClick="onCancelDialog"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/okay_button"
android:id="#+id/okay_form2_dialog_button"
android:onClick="onFinishDialog" />
</LinearLayout>
</LinearLayout>
How it looks when api upper then 20:
http://i.imgur.com/pJ1oZky.png
Less then 20:
http://i.imgur.com/TJGEwXh.png
OK the answer is quite simple: Use the android.support.v7.app.AlertDialog
Your setName will have to look like this then:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.new_name, null);
builder.setView(dialogView);
EditText text = (EditText) dialogView.findViewById(R.id.form2_dialog_name);
//if we have previous name we show it
if (haveName) {
text.setText(((Button) findViewById(R.id.form2_name_button)).getText());
}
//request focus and call keyboard for input name
text.requestFocus();
text.selectAll();
AlertDialog dialog = builder.create();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
dialog.show();
I guess you will have to play a little with your layout file because the buttons are quite a bit too far away but I think this will be just a little problem ;)

How can I create my own theme for alertDialog?

I want to create a different theme for all of alertDialog instances. I need my own title view instead of the usual black title background. All text should have a blue color, and set the edge of the alertDialog to a round shape.
Is it possible to create our own theme for alert dialogs, using any style or creating a class which extends AlertDialog.Builder? I need a common theme or style for my all instances of alertDialog. I am using alertDialog in many places - one for singleChoice items, one with ArrayAdapter.
My alertDialog with array adapter:
String[] items = {"Edit profile","Change user","Change password","Logout"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Loged.this,
R.layout.my_spinner_layout, items);
settingMenu.setAdapter(adapter, listener);
My alertDialog with single choice items:
alertDelete = new AlertDialog.Builder(getParent());
alertDelete.setTitle("Delete");
alertDelete.setCancelable(true);
CharSequence[] choice = {"this user","All user"};
alertDelete.setSingleChoiceItems(choice, 0,
For my all alertDialog, I need a common theme, like:
please check this link. here am creating my own alertDialog class
and its a simple method if you reuse the alertDialog in many situation in your application
how Create setAdapter() for a AlertDialog
I solved my problem by creating a class which extends Dialog, and I created my own functions. for example setMessage,setTitle,setPositiveButton,setNegativeButton etc.
But am confusing on how we can replace the setAdapter() and setSingleChoice().
Here one example For Dialog:
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Give a Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Put here any custom text!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton = (Button) dialog.findViewById(R.id.ButtonOK);
// if button is clickedthen dialog will closed
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
Custom AlertDialog
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext;
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View urlayoutfile = inflater.inflate(R.layout.custom_dialog_xmlfile,(ViewGroup)findViewById(R.id.Yourlayout_id));
builder = new AlertDialog.Builder(this);
builder.setView(urlayoutfile);
alertDialog = builder.create();
alertDialog.show();
To use a specific style, use
ctw = new ContextThemeWrapper(this, R.style.MyStyle);
new AlertDialog.Builder(ctw)
.setTitle(...)
You can use layout as Different themes, styles, Backgrounds
customdialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
style="#android:style/Theme.Black.NoTitleBar.Fullscreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/layoutsample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/dialoghdrbg"
android:orientation="horizontal" >
<ImageView
android:id="#+id/dialogheaderimage1"
android:layout_width="50dp"
android:layout_height="48dp"
android:scaleType="fitXY"
/>
<TextView
android:id="#+id/dialogheadertext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:layout_marginLeft="1dp"
android:gravity="center_vertical|right"
android:textColor="#000000"
android:textSize="25dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/dialogcontentbg"
android:orientation="vertical" android:gravity="center">
<TextView
android:id="#+id/dialogmessgetext1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#ffffff"
android:textSize="23dp"
android:text=""
android:gravity="center"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|center_horizontal"
android:gravity="center|center_horizontal"
android:layout_marginTop="20dip"
android:orientation="horizontal">
<Button
android:id="#+id/dialogokbutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:background="#drawable/buttonanimation"
android:text="Retry"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="#+id/dialogcancelbutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:background="#drawable/buttonanimation"
android:text="Report"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
customDialog.java
final Dialog favDialog = new Dialog(Myclass.this,
android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
favDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
favDialog.getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
favDialog.setContentView(R.layout.reloadurlpopuplayout);
favDialog.setCancelable(false);
ImageView dialogImage = (ImageView) favDialog
.findViewById(R.id.dialogheaderimage1);
dialogImage.setBackgroundResource(R.drawable.questionmark);
TextView dialogMesage = (TextView ) favDialog
.findViewById(R.id.dialogmessgetext1);
TextView dialogHeader = (TextView) favDialog
.findViewById(R.id.dialogheadertext1);
String descText = getString(R.string.RetryReportMessage);
dialogMesage.setBackgroundColor(0x00000000);
dialogMesage.setText(descText);
dialogHeader.setText(R.string.BrockenLinkHeader);
try {
favDialog.show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Categories

Resources