I have two apps on different devices - Rider & Driver. I am sending a notification from the Driver app to the Rider app once the driver accepts the request:
private void acceptBooking(String customerId) {
Token token = new Token(customerId);
Map<String, String> content = new HashMap<>();
content.put("title", "Accept");
content.put("message", "Your request is accepted, Please make your payment!");
DataMessage dataMessage = new DataMessage(token.getToken(), content);
mFCMService.sendMessage(dataMessage)
.enqueue(new Callback<FCMResponse>() {
#Override
public void onResponse(Call<FCMResponse> call, Response<FCMResponse> response) {
if (response.body().success == 1) {
Toast.makeText(CustomerCall.this, "Ride Accepted",
Toast.LENGTH_SHORT).show();
finish();
}
}
#Override
public void onFailure(Call<FCMResponse> call, Throwable t) {}
});
}
Once acceptBooking() is run, the Driver receives a toast "Ride Accepted" and the Rider receives a toast "Your request is accepted, Please make your payment!"
What I need to do is position the toast in the Rider App in the mid centre of the screen, change the colour and the background colour.
I have tried the following suggestion, but doesn't seem to work with this type of notification.
Try this. Add an xml file like this:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:id="#+id/custom_toast_container"
android:background="#drawable/complete_round_light_green_fill">
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginHorizontal="24dp"
android:layout_marginVertical="15dp"
android:layout_gravity="center_horizontal"
android:fontFamily="#font/ubuntu"
android:textSize="14sp"
android:lineSpacingExtra="7sp"
android:textColor="#color/white"
tools:text="abcd"
/>
</LinearLayout>
Add this in the Java Code. As you can see, we can add the background, set gravity to the toast:
LayoutInflater inflater = LayoutInflater.from(this);
View layout = inflater.inflate(R.layout.layout_toast_green, null);
LinearLayout customContainer = (LinearLayout) layout.findViewById(R.id.custom_toast_container);
customContainer.setBackgroundResource(R.drawable.complete_round_mgred_fill);
TextView text = (TextView) layout.findViewById(R.id.message);
text.setText("Your request is accepted, Please make your payment!");
Toast toast = new Toast(AppController.getContext());
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0,0);
toast.setView(layout);
toast.show();
Xml code for complete_round_light_green_fill:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="#78cd96" />
<corners android:radius="100dip"/>
</shape>
</item>
</layer-list>
Related
Below is the code for main activity
**public class TwitterMain extends Activity {
private LinearLayout myLayout;
private TwitterLoginButton loginButton;
private LinearLayout mainLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tweet_layout);
//find the id of main layout
mainLayout = (LinearLayout)findViewById(R.id.main_layout);
//find id of TweetView
TweetView tweetView = (TweetView)findViewById(R.id.tweet_view);
//set callback action to TweetView to listen favorite event
tweetView.setOnActionCallback(actionCallback);
//find id of CompactTweetView
CompactTweetView compactTweetView = (CompactTweetView)findViewById(R.id.compact_tweet_view);
//set callback action to CompactTweetView to listen favorite event
compactTweetView.setOnActionCallback(actionCallback);
//load runtime Tweet
loadTweet();
}
private void loadTweet() {
//use any TweetID
final long tweetId = 952933941702545408L;
//start loading tweet and pass TweetID
TweetUtils.loadTweet(tweetId, new Callback<Tweet>() {
#Override
public void success(Result<Tweet> result) {
//create TweetView or CompactTweetView with "result.data"
final TweetView tweetView = new TweetView(TwitterMain.this, result.data,
R.style.tw__TweetDarkWithActionsStyle);
//set action callback
tweetView.setOnActionCallback(actionCallback);
//finally add created Tweet to LinearLayout
mainLayout.addView(tweetView);
}
#Override
public void failure(TwitterException exception) {
//show toast if some exception occurs
Toast.makeText(TwitterMain.this, "Failed to show Tweet.", Toast.LENGTH_SHORT).show();
}
});
}
// launch the login activity when a guest user tries to favorite a Tweet
final Callback<Tweet> actionCallback = new Callback<Tweet>() {
#Override
public void success(Result<Tweet> result) {
// Intentionally blank
}
#Override
public void failure(TwitterException exception) {
if (exception instanceof TwitterAuthException) {
Toast.makeText(TwitterMain.this, "User is guest.", Toast.LENGTH_SHORT).show();
//launch login activity from here
//ask user to login
}
}
};
}**
code for adding config for twitter in application class--
TwitterConfig config = new TwitterConfig.Builder(this)
.logger(new DefaultLogger(Log.DEBUG))
.twitterAuthConfig(new TwitterAuthConfig(*************", "**********************************"))
.debug(true)
.build();
Twitter.initialize(config);
below is the xml for main activity-----
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tweet view"
android:textColor="#android:color/black"
android:textSize="18sp" />
<!-- TweetView with Dark Style theme, here pass any Tweet Id in
place of tw__tweet_id -->
<com.twitter.sdk.android.tweetui.TweetView
android:id="#+id/tweet_view"
style="#style/tw__TweetDarkWithActionsStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tw__tweet_id="954329648879587328" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Compact tweet view"
android:textColor="#android:color/black"
android:textSize="18sp" />
<!-- CompactTweetView with Light Style theme, here pass any
Tweet Id in place of tw__tweet_id -->
<com.twitter.sdk.android.tweetui.CompactTweetView
android:id="#+id/compact_tweet_view"
style="#style/tw__TweetLightWithActionsStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tw__tweet_id="953599133318819843" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="tweet view runtim"
android:textColor="#android:color/black"
android:textSize="18sp" />
<!-- Here the runtime Tweet will be shown and will be added in runtime -->
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Note--always getting error code 400 from twitter sdk, if anyone can kindly help.looks like some authorisation error for consumer key and secret key , but i am passing them correctly and also tried generating new key, but that also didn't work.
Can a Toast be styled in style.xml like we do for Activity themes?
I want to style the following:
Text color
Text Size
Text font
Background color/opacity
Background Radius of corners and sizes
I can't find anything that relates to Toast on the web or in the style.xml
I have solved it by making an StyleableToast class which you can easily use to style your Toasts in almost any way! See answer here: https://stackoverflow.com/a/39591755/5366495
Since there was not an easy and a non messy way (layouts, inflating etc) to style a Toast, I decided then to make a complete Styleable Toast class with a lot of styling possibilities!
I will keep improving the Styleable Toast class and make it feature rich and release it in the jCenter() so it can be added as an dependency
Here is it. Just a single class you put in your project: https://github.com/Muddz/StyleableToast
Examples of toasts made with StyleableToast:
All feedback and feature requests is welcome!
why you dont try to make your own toast layout :
LayoutInflater inflater = getLayoutInflater();
View customToastroot = inflater.inflate(R.layout.custom_toast, null);
TextView msg = (TextView) customToastroot.findViewById(R.id.toastMsg);
msg.setText("Speed up !");
msg.setTypeface(tf);
Toast customtoast = new Toast(getApplicationContext());
customtoast.setView(customToastroot);
customtoast.setDuration(Toast.LENGTH_SHORT);
customtoast.show();
and here is the custom_toast.xml :
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/game_on">
<TextView
android:id="#+id/toastMsg"
android:layout_margin="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:text="Your text"
android:textSize="16dp"
android:layout_gravity="center_vertical"/>
</LinearLayout>
Hope this will help you .
I think you should stop using Toast and look up SnackBar. This is the new standard in Material Design guideline way of displaying Toast-type messages.
You can use it similar to a toast but you can also set a layout for how content should be displayed.
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "Welcome to AndroidHive", Snackbar.LENGTH_LONG);
snackbar.show();
Not only that, you can also set custom interactions like button clicks in the SnackBar. Eg:
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "Message is deleted", Snackbar.LENGTH_LONG)
.setAction("UNDO", new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar snackbar1 = Snackbar.make(coordinatorLayout, "Message is restored!", Snackbar.LENGTH_SHORT);
snackbar1.show();
}
});
snackbar.show();
Here is some links to help you apart from documentation.
1) Material Design guidelines for SnackBar
2) SnackBar examples
I think this is the best way to go as it allows you to do everything that you have asked in your question.
I created my own class for that. Because it prevents me from trouble I experienced from other peoples solutions.
This is how it looks:
You can show a Toast simple in a single row:
MyToast.showShort(context, getString(R.string.verworfen));
MyToast.showLong(context, getString(R.string.verworfen));
//Code
public class MyToast{
private static Toast currentToast;
public static void showShort(Context context, String message){
if(currentToast != null) currentToast.cancel();
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup) ((Activity) context).findViewById(R.id.root));
TextView text = (TextView) layout.findViewById(R.id.message);
text.setText(message);
Toast toast = new Toast(context);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();
currentToast = toast;
}
public static void showLong(Context context, String message){
if(currentToast != null) currentToast.cancel();
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup) ((Activity) context).findViewById(R.id.root));
TextView text = (TextView) layout.findViewById(R.id.message);
text.setText(message);
Toast toast = new Toast(context);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
currentToast = toast;
}
public static Toast getCurrentToast(){
return currentToast;
}
}
//Layout
<LinearLayout
android:id="#+id/root"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/custom_toast">
<TextView
android:id="#id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="12dp"
android:layout_gravity="center_horizontal"
android:textColor="#color/white"
android:textSize="14sp" />
</LinearLayout>
//Drawable
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid
android:color="#color/primary_dark" >
</solid>
<stroke
android:width="2dp"
android:color="#color/primary_light" >
</stroke>
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" >
</padding>
<corners
android:radius="11dp" >
</corners>
</shape>
Styling Toast, or more specifically setView() was deprecated in Android 11 (API 30). Google does not want every app to have different toast style.
If you want a custom toast, you will have to implement and show your custom view, that looks like a toast.
Imo, you shouldn't do it and use default toast without any custom style, like it was intended by Google.
I've an Android activity that has some buttons in the bottom of it, these buttons have a one pressed and a three released as a default,
and when any of them pressed it's background supposed to change to the background of the pressed buttons and the others background supposed to change to the background of the released buttons, but when clicking any button I got that result:
this is my code:
xml:
<LinearLayout
android:id="#+id/GrandThree"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal"
android:background="#android:color/transparent">
<Button
android:id="#+id/BedRoom_bottom"
android:layout_width="200dp"
android:layout_height="fill_parent"
android:background="#drawable/light_bottom_buttons_pressed"
android:text="Bedroom"
android:textSize="20dp"
android:textColor="#FFFFFF"
android:layout_gravity="center"
android:layout_marginTop="3dp"
android:layout_marginLeft="3dp"
android:layout_marginBottom="3dp"
android:layout_marginRight="3dp"
android:tag="pressed"
android:clickable="true" />
<Button
android:id="#+id/livingroom"
android:layout_width="200dp"
android:layout_height="fill_parent"
android:background="#drawable/light_bottombuttons"
android:text="Living Room"
android:layout_gravity="center"
android:textSize="20dp"
android:padding="20px"
android:textColor="#FFFFFF"
android:layout_marginTop="3dp"
android:layout_marginLeft="3dp"
android:layout_marginBottom="3dp"
android:layout_marginRight="3dp"
android:tag="released"
android:clickable="true" />
<!.. then two buttons the same way ..!>
</LinearLayout>
JAVA:
// the onClickListener method for the Navigation buttons
public OnClickListener onNavigationClick = new OnClickListener() {
#Override
public void onClick(View v) {
if(v == bedRoom){
String tag = bedRoom.getTag().toString().trim();
if (tag.equals("released")) {
bedRoom.setBackgroundColor(R.drawable.light_bottom_buttons_pressed);
bedRoom.setTag("pressed");
livingRoom.setBackgroundColor(R.drawable.light_bottombuttons);
livingRoom.setTag("released");
masterBedroom.setBackgroundColor(R.drawable.light_bottombuttons);
masterBedroom.setTag("released");
kitchen.setBackgroundColor(R.drawable.light_bottombuttons);
kitchen.setTag("released");
}
}
//then another 3 blocks the same way for the other buttons
}
};
Hint: light_bottombuttons & light_bottom_buttons_pressed are shapes with gradient colors:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="30dp"/>
<gradient
android:startColor="#353535"
android:centerColor="#212121"
android:endColor="#121212" <!.. the values of the other is just upside down these values ..!>
android:angle="270"/>
</shape>
Create a file called button-drawable.xml in your drawable folder containing this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:#drawable/light_bottom_buttons_pressed" />
<item
android:drawable="#drawable/light_bottom_buttons_pressed" />
</selector>
Add a tag to all the buttons in the layout file.
android:background="#drawable/button_drawable"
Now, in your button's click listener set the button's selected state to true any time a new button is chosen.
Example code :
public OnClickListener onNavigationClick = new OnClickListener() {
#Override
public void onClick(View v) {
if(v == bedRoom){
String tag = bedRoom.getTag().toString().trim();
if (tag.equals("released")) {
bedRoom.setTag("pressed");
bedRoom.setSelected(true);
livingRoom.setTag("released");
livingRoom.setSelected(false);
masterBedroom.setTag("released");
masterBedroom.setSelected(false);
kitchen.setTag("released");
kitchen.setSelected(false);
}
}
//then another 3 blocks the same way for the other buttons
}
};
Use radio group and radio button for this.
Your radio group will contain multiple buttons and at a time only one can be selected.
When you get your drawable you should do it like the following
bedRoom.setBackgroundColor(MyActivity.this.getResources().getDrawable(R.drawable.light_bottom_buttons_pressed));
MyActivity Would change to be whatever your activity name is
I'm trying to make a custom android dialog with rounded corners. My current attempts have given me this result.
As you can see, the corners are rounded, but it leaves the white corner still intact.
Below is the xml that I put in the drawable folder to create the blue dialog with the red border with the rounded corners.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle">
<solid android:color="#color/transparent_black" />
<corners android:radius="#dimen/border_radius"/>
</shape>
</item>
<item
android:left="#dimen/border_width"
android:right="#dimen/border_width"
android:top="#dimen/border_width"
android:bottom="#dimen/border_width" >
<shape android:shape="rectangle">
<solid android:color="#color/blue" />
<corners android:radius="#dimen/border_radius"/>
</shape>
</item>
</layer-list>
Below is the layout of the dialog.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/fill"
android:orientation="vertical"
android:layout_margin="#dimen/spacing_normal"
android:padding="#dimen/spacing_normal"
android:background="#drawable/border_error_dialog" >
<RelativeLayout
style="#style/block"
android:layout_gravity="center" >
<ImageView
android:id="#+id/imageView1"
style="#style/wrap"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
android:contentDescription="#string/content_description_filler"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
style="#style/error_text"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/imageView1"
android:text="#string/error_login" />
</RelativeLayout>
<Button
android:id="#+id/button1"
style="#style/wrap"
android:layout_gravity="center"
android:text="Button" />
</LinearLayout>
And below is the Activity in which I create the dialog.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
View child = getLayoutInflater().inflate(R.layout.dialog_custom_tom, null);
alertDialogBuilder.setView(child);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
}
The only solution I have found is here. Use Dialog instead of AlertDialog and set transparent background:
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
Therefore you can't use the builder. But you can use new Dialog() also in onCreateDialog callback of DialogFragment if you follow to best guidelines.
This works also for Gingerbread.
Besides the layered drawable can be simplified to one shape with xml element <stroke> for the border.
I had similar issue when made dialog extending DialogFragment and to fix this used:
dialog.setStyle(DialogFragment.STYLE_NO_FRAME, 0);
Like this:
public class ConfirmBDialog extends DialogFragment {
public static ConfirmBDialog newInstance() {
ConfirmBDialog dialog = new ConfirmBDialog();
Bundle bundle = new Bundle();
dialog.setArguments(bundle);
return dialog;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// This removes black background below corners.
setStyle(DialogFragment.STYLE_NO_FRAME, 0);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.confirm_dialog, container, true);
getDialog().setCanceledOnTouchOutside(true);
return view;
}
Hope this helps.
Just try
myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
The below code solved the issue
MyDialog mydialog = new MyDialog(this, "for testing",
new myOnClickListener() {
#Override
public void onPositiveButtonClick() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"I am positive button in the dialog",
Toast.LENGTH_LONG).show();
}
#Override
public void onNegativeButtonClick() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"I am negative button in the dialog",
Toast.LENGTH_LONG).show();
}
});
// this will remove rectangle frame around the Dialog
mydialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
mydialog.show();
Thanks,
Nagendra
In you java file keep below code and change your layout name
View mView =LayoutInflater.from(mContext).inflate(R.layout.layout_pob,null);
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
just try using this, this worked for me
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
Use 9-patch PNG with transparency in those corners.
public void initDialog() {
exitDialog = new Dialog(this);
exitDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
View view = View.inflate(this, R.layout.dialoglayout, null);
exitDialog.setContentView(view);
AdSize adSize = new AdSize(300, 250);
dialogAdview = new AdView(this);
dialogAdview.setAdUnitId(getResources().getString(R.string.banner_id));
dialogAdview.setAdSize(adSize);
RelativeLayout adLayout = (RelativeLayout) view.findViewById(R.id.adLayout);
adLayout.addView(dialogAdview);
AdRequest adRequest = new AdRequest.Builder()
.build();
dialogAdview.loadAd(adRequest);
dialogAdview.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
Log.d("Tag", "adLoaded");
super.onAdLoaded();
}
});
view.findViewById(R.id.yes_btn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
exit = true;
onBackPressed();
}
});
view.findViewById(R.id.no_btn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
exit = false;
exitDialog.dismiss();
}
});
}
dialoglayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/custom_dialog_round"
android:orientation="vertical">
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:text="Do you want to exit?"
android:textColor="#000"
android:textSize="18dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/text"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/yes_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background_draw"
android:padding="8dp"
android:text="Yes"
android:textAlignment="center"
android:textColor="#9fa8da"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/no_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="#drawable/background_draw"
android:padding="8dp"
android:text="No"
android:textAlignment="center"
android:textColor="#d50000"
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
`
custom_dialog_round.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#fff"/>
<corners
android:radius="10dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
reference http://techamongus.blogspot.com/2018/02/android-create-round-corner-dialog.html
UPDATE
I understood that activity's background makes sense. So use #robert's answer with these changes.
in DialogFragment layout set width and height or add minimum sizes:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content" // Or match_parent, 300dp.
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:background="#drawable/white_round_corner_background"
android:gravity="center"
android:minWidth="300dp"
android:minHeight="200dp"
android:orientation="vertical"
android:padding="15dp"
>
...
Remove <item name="android:background">#color/...</item> from styles of needed activities and set these backgrounds in activity's layouts.
In DialogFragment write:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// This removes black background below corners.
setStyle(DialogFragment.STYLE_NO_FRAME, 0);
}
Old variant
According to robert answer, you should apply setStyle(STYLE_NO_FRAME, 0), but there appear new problems. If you have a narrow DialogFragment like in Custom dialog too small, then you should follow this guide.
Add to styles.xml these 3 lines for dialog size:
<style name="ErrorDialogTheme" parent="#android:style/Theme.Dialog">
<item name="android:minWidth" type="dimen">300dp</item>
<!-- This option makes dialog fullscreen and adds black background, so I commented it -->
<!-- <item name="android:minHeight" type="dimen">200dp</item> -->
<!-- This option doesn't work, so I commented it -->
<!-- <item name="android:layout_width">match_parent</item> -->
</style>
In layout of your DialogFragment add style:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
android:minWidth="300dp" // Optional, remove this line.
android:minHeight="200dp" // Optional, remove this line.
style="#style/ErrorDialogTheme"
android:theme="#style/ErrorDialogTheme"
>
In code of your DialogFragment write:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// This removes black background. If not 0 as a parameter, black background will appear.
setStyle(STYLE_NO_FRAME, 0)
}
// If you want a fullscreen dialog, use this, but it doesn't remove a black background.
override fun onStart() {
super.onStart()
dialog.window?.setLayout(WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT)
}
Look at AndroidManifest.xml and find all activities that can show these dialogs, check android:theme="..." themes and go to styles.xml. Now take a look at <item name="android:background">#color/...</item> items of these themes. There should be a transparent color or these items might not exist. If you have these background items, whole activities will have those backgrounds and dialogs too! So, if you have a camera activity with DialogFragment above it, you will see this.
Remove background items of needed styles. Also maybe background is set in code, check it.
In Dialog with transparent background in Android and many pages it is written to add one of these:
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
probably in onViewCreated() or onCreateDialog(), but it didn't help me, because the background of the Activity was set in styles.
Tested on Samsung Galaxy S4 running Android 5.0.1.
Use CardView and make
app:cardCornerRadius="dp"
According shape xml.
I will post my solution here because it may be helpful. The solution that worked for me was to set the drawable resource in the layout xml and also in the activity that starts the dialog, without switching from AlertDialog to Dialog.
This would mean that in the layout where we create our design for the dialog alert_dialog_design.xml we will have the property android:background filled with our own defined background alert_dialog_shape.xml:
android:background="#drawable/alert_dialog_shape"
But also inside the activity that starts the dialog:
alert.getWindow().setBackgroundDrawableResource(R.drawable.alert_dialog_shape);
This way the parent (the alert itself) of your custom layout will have the shape you desire. Using this method I achieved the following:
https://i.stack.imgur.com/drCw3.png
I'm using an AlertDialog with custom layout. The color of TextView in the layout is black, so when opening the dialog on Android 4.0 with Holo.Light, the text is visible. However if you open the dialog in Android 2.2 the text is not visible because of the gray background. Is there a way to change the background color?
However if you open the dialog in Android 2.2 the text is not visible because of the gray background. Is there a way to change the background color?
Yes it is possible, I used it on my app using DialogBuilder.
Just put inverseBackgroundForced to true
builder.setInverseBackgroundForced(true);
AlertDialog dialog = builder.create();
dialog.show();
on your dialog builder. It will force the background to white color (instead of dark grey) on android version before Froyo.
Just define the background of the root view in the layout.xml file for your dialog to a color that you want.
Like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/dialog_background" >
...
Thank you very much to StinePike and Artjom B.
The idea of StinePike is very good.
I put a TextView in AlertDialog having a customized background.
I show how to use solid and gradient background to customize objects.
Please let me to present you the context in which I applied StinePike's Idea.
// location: MainActivity.java
AlertDialog mAlertDialog_With_Radio_Buttons;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ini();
}
public void onAlert_With_Radio_Buttons_Close_Click(View view) {
mAlertDialog_With_Radio_Buttons.dismiss();
} // onAlert_With_Radio_Buttons_Close_Click
public void alert_with_radio_buttons(){
AlertDialog.Builder
mAlertDialog_Builder = new AlertDialog.Builder(this);
mAlertDialog_Builder
.setView(getLayoutInflater()
.inflate(R.layout.alert_with_radio_buttons, null));
mAlertDialog_Builder
.setTitle("Select The Directory");
mAlertDialog_With_Radio_Buttons = mAlertDialog_Builder.create();
mAlertDialog_With_Radio_Buttons.show();
} // public void alert_with_radio_buttons(){
// location: alert_with_radio_buttons.xml in layout
<LinearLayout
android:id="#+id/alert_with_radio_buttons_tv_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/turquoise1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<TextView
android:id="#+id/mAlert_With_Radio_Buttons_TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:background="#color/turquoise2"
android:textSize="#dimen/main_wiz_size"
android:text = "#string/alert_with_rb_tv_text" />
</LinearLayout>
// Location: colors in values
<color name="turquoise1">#FF00ABAB</color>
<color name="turquoise2">#FF00BCBC</color>
// Location: strings in values
<string name="alert_with_rb_tv_text">Directory Names</string>
// Close Definition
// location: alert_with_radio_buttons.xml in layout
<Button
android:id="#+id/alert_with_radio_buttons_close_btn"
android:text="#string/alert_with_radio_buttons_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/btn_decor"
android:onClick="onAlert_With_Radio_Buttons_Close_Click" />
// location: btn_decor.xml in drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true">
<gradient
android:startColor="#700000ff"
android:endColor="#70009B80"
android:angle="-90"/>
</shape>
location: strings.xml in values
<string name="alert_with_radio_buttons_close">Close</string>
"Is there a way to change the background color?"
Yes there are several ways for different contexts.
Please let me to "provide details and share my research" to you.
My code shows how to get customized TextView Background for items of ListView incorporated in Alert Dialog.
Let's start with the model for the item of ListView
// location: customized_tv_for_list_view.xml from layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/layer_border">
<TextView
android:id="#+id/text_view_for_lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity ="center"
android:padding ="5dip"
android:background="#color/turquoise2"
android:textSize="#dimen/lv_text_size"
android:textColor="#color/blue0"/>
</LinearLayout>
// location: main_activity.xml in layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/decor"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/main_activity_files_btn_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/layer_border" >
<Button
android:text="Files"
android:id="#+id/files_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/btn_decor"
android:onClick="onMainActivity_Files_Click" />
</LinearLayout>
</LinearLayout>
// location: colors.xml in values
<color name="blue0">#0000FF</color>
<color name="turquoise2">#FF00BCBC</color>
// location: dimens.xml in values
<dimen name="lv_text_size">24dp</dimen>
// location: layer_border.xml in drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="1dp" android:color="#9999FF" />
<solid android:color="#CCCCFF" />
<padding android:left ="4dp" android:top="4dp"
android:right="4dp" android:bottom="4dp" />
<corners android:radius="4dp" />
</shape>
// location: decor.xml in drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true">
<gradient
android:startColor="#aa0000ff"
android:endColor="#aa009B80"
android:angle="-90"/>
</shape>
// location: MainActivity.java
ListView mListView;
AlertDialog mAlertDialog;
ArrayAdapter<String> mArrayAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = new ListView(this);
ArrayList<String>
mArrayList_Days = new ArrayList<>();
for(int i = 0; i< 32; i++)
mArrayList_Days.add("Day " + String.valueOf(i));
mArrayAdapter = new ArrayAdapter<>(
this, R.layout.customized_tv_for_list_view,
R.id.text_view_for_lv, mArrayList_Days);
mListView.setAdapter(mArrayAdapter);
mListView
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String sel_item = (String) mListView
.getItemAtPosition(position);
Toast.makeText(MainActivity.this, sel_item, Toast.LENGTH_SHORT).show();
mAlertDialog.cancel();
} // onItemClick
}); // .setOnItemClickListener
build_files_alert_dialog();
}
public void build_files_alert_dialog() {
AlertDialog.Builder
mAlertBuilder = new AlertDialog.Builder(MainActivity.this);
mAlertBuilder.setTitle("Days");
mAlertBuilder.setView(mListView);
mAlertDialog = mAlertBuilder.create();
WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams();
mLayoutParams.copyFrom(mAlertDialog.getWindow().getAttributes());
}
public void onMainActivity_Files_Click(View view) {
mAlertDialog.show();
} // onMainActivity_Files_Click
AlertDialog.Builder.setView(getLayoutInflater().inflate(R.layout.your_layout, null));
by using this function you can inflate a layout to your dialogue. now do whatever you want in the layout xml. for example see the following code.
AlertDialog.Builder about = new AlertDialog.Builder(this);
about.setTitle(getString(name));
about.setIcon(R.drawable.icon);
about.setView(getLayoutInflater().inflate(R.layout.your_layout, null));