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.
Related
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>
Is there a way to check if there is a toast message being displayed in android?
I am writing an idlingResource and I want to make sure no toast is currently being displayed before I return the resource as being idle.
Create Custom toast.
private Toast myToast = nuul; // Create class Level Object to
Add this code in the method
String message = "Your Message";
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
the (ViewGroup) findViewById(R.id.custom_toast_container));
TextView text = (TextView) layout.findViewById(R.id.custom_toast_text);
text.setText(Message);
myToast = new Toast(getApplicationContext());
myToast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
myToast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
myToast.setDuration(Toast.LENGTH_LONG);
myToast.setView(layout);
myToast.show();
custom_toast.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom_toast_container"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp"
android:background="#80000000">
<TextView
android:id="#+id/custom_toast_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:enabled="true"
android:textSize="16dp"
/>
</LinearLayout>
Dismiss the Toast, if it is not null
if (myToast != null)
myToast.cancel(); // Dismiss the toast
Currently, I have the following dialog, which I will perform expand/ collapse animation on its items.
This dialog is created via the following code
import android.support.v7.app.AlertDialog;
final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
final AlertDialog dialog = builder.setView(view).create();
final ViewTreeObserver vto = view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
ViewTreeObserver obs = view.getViewTreeObserver();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
obs.removeOnGlobalLayoutListener(this);
} else {
obs.removeGlobalOnLayoutListener(this);
}
// http://stackoverflow.com/questions/19326142/why-listview-expand-collapse-animation-appears-much-slower-in-dialogfragment-tha
int width = dialog.getWindow().getDecorView().getWidth();
int height = dialog.getWindow().getDecorView().getHeight();
dialog.getWindow().setLayout(width, height);
}
});
However, when animation being performed, here's the side effect.
Note, the unwanted extra white region at the dialog after animation, is not caused by our custom view. It is the system window white background of the dialog itself.
I tend to make the system window background of the dialog, to become transparent.
final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
final AlertDialog dialog = builder.setView(view).create();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Although the unwanted white background is no longer seen, the original margin of the dialog is gone too. (The dialog width is now full screen width)
How can I make it transparent, without affecting its margin?
There's a pretty easy way to do that:
You need to "modify" the Drawable that is being used as a background of the Dialog. Those sort of Dialogs use an InsetDrawable as a background.
API >= 23
Only SDK API 23+ allows you to get the source Drawable wrapped by the InsetDrawable (getDrawable() method). With this, you can do whatever you want - e.g. change color to something completely different (like RED or something). If you use this approach remember that the wrapped Drawable is a GradientDrawable and not a ColorDrawable!
API < 23
For lower APIs your ("elegant") options are very limited.
Fortunately you don't need to change the color to some crazy value, you just need to change it to TRANSPARENT. For this you can use setAlpha(...) method on InsetDrawable.
InsetDrawable background =
(InsetDrawable) dialog.getWindow().getDecorView().getBackground();
background.setAlpha(0);
EDIT (as a result of Cheok Yan Cheng's comments):
or you can actually skip casting to InsetDrawable and get the same result. Just remember that doing so will cause the alpha to be changed on the InsetDrawable itself and not on the Drawable that is wrapped by the InsetDrawable.
Try to below Theme:
<style name="TransaparantDialog">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowFrame">#null</item>
<item name="android:windowTitleStyle">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:background">#android:color/transparent</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>
Try below code to apply Theme to AlertDialog.Builder:
final AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(activity, R.style.TransaparantDialog));
...
dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
I hope help you !
The background image abc_popup_background_mtrl_mult which is part of the compat library contains already a margin in the picture information.
This is why the margin goes away when you remove the background image. I strongly recommend not to use the ViewTreeObserver, it will been called multiple times and can cause performance issues. Better work with the screen size:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Your problem is properly in the layout try to check the views with the Hierarchy viewer.
just add this line after show dialog. I would prefer using Dialog instedof using AlertDialog
dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Let's start with Google recommendation which says to use DialogFragment instead of a simple Dialog.
#rekire is right that margins set by drawable, going forward it is set by either 9 patch or programmatically depending on theme.
So you either can set your padding to your content view or create dialog using DialogFragment here is an example which changes height of dialog based on it's content, and note you don't need to use tree observer which is as mentioned before may cause performance issue.
So the example
dialog_confirm.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<LinearLayout android:id="#+id/container"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:orientation="vertical"
android:animateLayoutChanges="true"
android:padding="15dp">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:text="A label text"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque mauris mi, dictum a lectus ut, facilisis"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Remove Me"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Remove Me"/>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Remove Me"/>
<!-- as much content as you need -->
</LinearLayout>
</ScrollView>
Note: I wrapped everything into scroll view and set padding you can skip it if you want.
ConfirmDialog.java
//here goes package name and imports
/**
* Created by Vilen - virtoos.com;
* fragment dialog example
*/
public class ConfirmDialog extends DialogFragment implements View.OnClickListener {
private Button button1;
private Button button2;
private Button button3;
private LinearLayout containerLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, 0);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.dialog_confirm, container, false);
containerLayout = (LinearLayout)v.findViewById(R.id.container);
button1 = (Button)v.findViewById(R.id.button1);
button2 = (Button)v.findViewById(R.id.button2);
button3 = (Button)v.findViewById(R.id.button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// make background transparent if you want
//getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return super.onCreateDialog(savedInstanceState);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
containerLayout.removeView(button1);
break;
case R.id.button2:
containerLayout.removeView(button2);
break;
case R.id.button3:
containerLayout.removeView(button3);
break;
}
}
}
and finally you can show your dialog with this piece of code
ConfirmDialog confirmDialog = new ConfirmDialog();
confirmDialog.show(getSupportFragmentManager(), "dialog");
I will not go into details why Fragment dialog is better but one thing is clear that you can encapsulate logic for it and have separate class.
Hope this solves your issue.
What should be there is what you didn't show, I'm not sure it is something you didn't know or it is already there so you don't think it is necessary to show.
Set theme to Dialog, that puts entire activity as one Dialog. I don't think you did it, otherwise AlertDialog would not be there.
I'm a bit lost your description, but there is that little <shape/> XML that is much more powerful then a 9-patch, and use RelativeLayout will help.
I have seen sometimes a Blue toast showing up when i connect my Samsung phone with wifi network. Can anyone help for customising the colour of the toast.
For example:
Try This:-
SpannableString text = new SpannableString("Please Wait !!!!! ");
text.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 41, 0);
Toast.makeText(c.getApplicationContext(),text , Toast.LENGTH_LONG).show();
Another Way:-
Make an xml / customToast.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toast_layout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp"
android:background="#DAAA"
>
<ImageView android:src="#drawable/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
/>
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
/>
</LinearLayout>
In Activity:-
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.customToast,
(ViewGroup) findViewById(R.id.toast_layout));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("This is a custom toast");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
Like this way:
LayoutInflater inflater = youractivity.this.getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
null);
TextView text = (TextView) layout.findViewById(R.id.tvtoast);
text.setText("No Internet Connection");
text.setTextColor(Color.BLACK);
Toast toast = new Toast(getActivity());
toast.setView(layout);
toast.setDuration(100);
toast.show();
Make your own layout custom_toast.xml and set Color to TextView Text as per your need
Output:
You can create a custom Toast view to suit your requirements. See the section titled "Creating a Custom Toast View" at http://developer.android.com/guide/topics/ui/notifiers/toasts.html
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));