I'm using a custom dialog that I've created in a separate xml file in my project, and I'm coloring the main window a blueish tint,
but the main header still remains the default white color.
Is there no way to change the font color, size, background for the header?
Is the only thing I can change in the header the text?
xml:
<?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"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:background="#3edfbc"
android:orientation="vertical">
<TextView
android:id="#+id/textaligmentManager_loader_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text="Initlizing Wifi"
android:textAppearance="?android:attr/textAppearanceLarge" />
<com.github.ybq.android.spinkit.SpinKitView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/barcodeScanning_spinkit"
style="#style/SpinKitView.Large.FoldingCube"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_below="#+id/textaligmentManager_loader_textview"
android:padding="20dp"
android:layout_centerHorizontal="true"
app:SpinKit_Color="#ffffff" />
</RelativeLayout>
dialog:
Dialog dialog = new Dialog(Scanning_Barcode_Activity.this);
dialog.setContentView(R.layout.aligment_manager_loader_layout);
dialog.setTitle("Loading");
dialog.setCancelable(false);
//set up text
loaderScreenMainText = (TextView) dialog.findViewById(R.id.textaligmentManager_loader_textview);
loaderScreenMainText.setText("Loading Wifi");
//progressBar = (ProgressBar) dialog.findViewById(R.id.barcodeScanning_spinkit);
//DoubleBounce doubleBounce = new DoubleBounce();
//progressBar.setIndeterminateDrawable(doubleBounce);
//now that the dialog is set up, it's time to show it
dialog.show();
You can use AlertDialog which is a subclass of Dialog class. Here you can define a custom layout containing everything such as title, body and buttons. No extra title section will appear. Here is a demo:
AlertDialog.Builder builder = new AlertDialog.Builder(Scanning_Barcode_Activity.this);
builder.setCancelable(false);
LayoutInflater inflater = LayoutInflater.from(Scanning_Barcode_Activity.this);
View dialogView = inflater.inflate(R.layout.aligment_manager_loader_layout, null);
TextView loaderScreenMainText = (TextView) dialogView.findViewById(R.id.textaligmentManager_loader_textview);
loaderScreenMainText.setText("Loading Wifi");
builder.setView(dialogView);
AlertDialog dialog = builder.create();
dialog.show();
Design your custom layout in such a way that the layout itself contains the header part. And then skip this code:
dialog.setTitle("Loading");
Instead add this statement:
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
You can also use AlertDialog. In that case requestWindowFeature() method is not required.
android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(Scanning_Barcode_Activity.this);
LayoutInflater inflater = LayoutInflater.from(Scanning_Barcode_Activity.this);
View customView = inflater.inflate(your_layout, null);
builder.setCancelable(false);
loaderScreenMainText = (TextView) customView.findViewById(R.id.textaligmentManager_loader_textview);
loaderScreenMainText.setText("Loading Wifi");
builder.setView(customView);
AlertDialog dialog = builder.create();
dialog.show();
There are plenty of stylish open source libraries that you can use for customizing header parts.
Here I'm sharing my newly developed open source library : PanterDialog
Hope it will help you.
Related
I am trying to populate an Alert Dialog (with a layout file having an EditText & Button) within a fragment of Activity.
The components are not being found by calling findViewById() in the fragment's corresponding class. I have written this code inside the class :
AlertDialog.Builder builder=new AlertDialog.Builder(context);
View v=getLayoutInflater().inflate(R.layout.layout_alert_dialog,null);
final EditText txtAddNew=v.findViewById(R.id.txtAddNew); //null
final TextView txtErr = v.findViewById(R.id.txtErr); //null
Button btnAdd=v.findViewById(R.id.btnAdd); //null
I think the problem raises from this line of code:
View v=getLayoutInflater().inflate(R.layout.layout_alert_dialog,null);
The alertDialog XML code is below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/txtAddNew"
android:hint="Category name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtErr"
/>
<Button
android:id="#+id/btnAdd"
android:text="Add Category" />
</LinearLayout>
Can anyone find what the actual problem is? Thanks!
Yes, that's correct
Do something like this :
LayoutInflater layout = LayoutInflater.from(this);
final View view = layout.inflate(R.layout.dialog_layout, null);
final AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setView(view);
dialog.show();
You can do it like below.
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getActivity());
alertBuilder.setView(getLayoutInflater().inflate(R.layout.layout_alert_dialog,null));
AlertDialog alertDialog = alertBuilder.create();
EditText txtAddNew = alertDialog.findViewById(R.id.txtAddNew);
TextView txtErr = alertDialog.findViewById(R.id.txtErr);
Button btnAdd = alertDialog.findViewById(R.id.btnAdd);
alertDialog.show();
Try using dialog.setContentView(v)
See the difference between setView(v) and setContentView(v) here:
What is difference between Dialog.setContentView( View ) & AlertDialog.setView( View )
I created a custom alert dialog to show in my app. It has an imageView, two textViews and a button. I am trying to show this custom alert dialog once my main activity launches.I am calling this custom alert dialog.show() in a separate method according to my requirement.The custom alert dialog is showing up but on click of a button it is not getting dismissed, also the custom alert dialog is showing a extra white background.
showCustomAlertDialog() Method
public void showCustomAlertDialog() {
LayoutInflater layoutInflater = LayoutInflater.from(this);
final View promptView =layoutInflater.inflate(R.layout.reminder_alert_dialog, null);
final AlertDialog builder = new AlertDialog.Builder(this).create();
Button recordButton = (Button)promptView.findViewById(R.id.record_button);
recordButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println("Dismiss the dialog");
builder.dismiss();
}
});
builder.setView(promptView);
builder.show();
}
reminder_alert_dialog Layout
<?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="wrap_content">
<ImageView
android:id="#+id/image_view"
android:layout_width="wrap_content"
android:layout_height="500dp"
android:background="#drawable/ic_alert" />
<TextView
android:id="#+id/greetings_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GOOD MORNING, KEVIN"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginTop="200dp"
android:layout_centerHorizontal="true"/>
<TextView
android:id="#+id/medicines_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Have you taken your medicines today?"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="normal"
android:layout_below="#id/greetings_text"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true"/>
<Button
android:id="#+id/record_button"
android:layout_width="160dp"
android:layout_height="40dp"
android:background="#3BC4B8"
android:text="Record"
android:textSize="20sp"
android:textColor="#ffffff"
android:layout_below="#id/medicines_text"
android:layout_marginTop="60dp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
Coming to the extra white background, you have to apply the Window Background colour to transparent for the AlertDialog with this code
Dialog alertDialog = new Dialog(this);
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.setContentView(R.layout.tabs);
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alertDialog.show();
And the issue where dialog isn't showing, is the System.out.println() called? I would also recommend you setting the view for the AlertDialog.Builder before setting the listeners like,
final AlertDialog builder = new AlertDialog.Builder(this).create();
builder.setView(promptView);
Button recordButton = (Button)promptView.findViewById(R.id.record_button);
recordButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println("Dismiss the dialog");
builder.dismiss();
}
});
Try like this...
//class variables
AlertDialog dialog;
Button dismiss;
//Add your other views if required
//in onCreate() activity method
makeDialog();
//makeDialog() method code
void makeDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title");
View root = getLayoutInflater().inflate(R.layout.your_layout,null);
dismiss = root.findViewById(R.id.your_button_id);
//another views initialization goes here...
dismiss.setOnClickListener(new View.OnClickListener(){
dialog.dismiss();
//your logic or job...
});
builder.setView(root);
dialog = builder.create();
}
finally show the dialog whenever you want
dialog.show();
I am having an issue with a custom view in a dialog on android API 10.
I use AlertDialog.Builder to construct my dialog. I include a custom view panel using the setView command on the builder.
This works on most of the API's I've tested with. The style changes somewhat from device to device, but that is what I want, for the style to match the device default.
My problem is that on API 10, any text that is in my custom view shows up as black on a black background.
Any text I insert using AlertDialog.Builder.setMessage() appears correctly.
What magical attribute/style is the dialog builder using to determine text appearance?
My app's theme is Theme.AppCompat.Light.
Here is my onCreateDialog method:
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = inflater.inflate(R.layout.fragment_status_dialog, null);
mStatusTextView = (TextView) view.findViewById(R.id.text_status);
mConnectedDeviceTextView = (TextView) view.findViewById(R.id.text_connected_device);
MainService.ServiceState state = null;
if (getArguments().containsKey(SERVICE_STATUS_ARG_KEY)) {
state = (MainService.ServiceState) getArguments().getSerializable(SERVICE_STATUS_ARG_KEY);
}
setState(state);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(view);
builder.setMessage("This will show up just fine.");
builder.setTitle(getString(R.string.status_title));
builder.setNegativeButton(R.string.dialog_back_button_text, null);
builder.setNeutralButton(R.string.dialog_connect_to_text, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
mListener.onDialogConnectTo();
}
});
// Create the AlertDialog object and return it
return builder.create();
}
Here's my fragment_status_dialog layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="18dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/status_starting"
android:id="#+id/text_status"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/status_connected_to_unknown"
android:paddingStart="4dp"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:id="#+id/text_connected_device"
android:layout_toRightOf="#+id/text_status"
android:layout_toEndOf="#+id/text_status"/>
</RelativeLayout>
Note, I've tried https://stackoverflow.com/a/24505312/2350083 but it didn't fix it.
Try calling AlertDialog#setInverseBackgroundForced(true).
What about simply setting the color of the text? Ex:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white"
android:text="#string/status_starting"
android:id="#+id/text_status"/>
The TextView is using the default text color of the device (or the app). If you set the color specifically to the TextView it will be overriden on devices irrespective of the API.
Following my previous question I still have problems managing hyperlinks on my custom AlertDialog but I believe I narrowed down the problem because I think it only not works because it's a custom dialog.
I have followed all the instructions here and here but can't seem to bypass this situation
On strings.xml I have a string defined this way:
<string name="link_text_manual"><b>text2:</b> This is some other
text, with a link specified
via an <a> tag. Use a \"tel:\" URL
to dial a phone number.
</string>
If I create my dialog this way:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Data")
.setIcon(R.drawable.info)
.setMessage(R.string.link_text_manual)
.setCancelable(true)
.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog welcomeAlert = builder.create();
welcomeAlert.show();
// Make the textview clickable. Must be called after show()
((TextView)welcomeAlert.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
it shows like this:
This works as expected and all clicks are clickable.
Now I want the same thing but on a custom layout.
Created a TextView on my info.xml like this:
<TextView
android:id="#+id/infoDetailText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/infoVersionTitle"
android:autoLink="all"
android:linksClickable="true"
android:padding="4dp" />
And this is the code:
Context ctx = this;
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.info, (ViewGroup) findViewById(R.id.infoLayout));
AlertDialog.Builder about = new AlertDialog.Builder(ctx);
about.setView(layout);
about.setTitle("Data");
about.setIcon(R.drawable.info);
AlertDialog displayInfo = about.create();
displayInfo.show();
// Make the textview clickable. Must be called after show()
TextView textView = (TextView) displayInfo.findViewById(R.id.infoDetailText);
textView.setText(R.string.link_text_manual);
textView.setMovementMethod(LinkMovementMethod.getInstance());
With the above code it shows like this (links not clickable and even not marked as links):
If I remove
textView.setMovementMethod(LinkMovementMethod.getInstance());
it shows like the bellow image (Links marked as links but not clickable):
I've also tried to use Linkify and android:autoLink="web" but the result is always the same.
setMessage() works but with a custom layout with a TextView can't make it to work.
Probably this is simple but can't seem to make it work. Any suggestions?
Thanks
You defined the TextView without android:autoLink="all" , android:clickable="true" & android:linksClickable="false"
For info.xml TextView as below
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/infoDetailText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp" />
Anyway, your code works perfectly,...
Code for AlertDialog:
LayoutInflater inflater = LayoutInflater.from(ctx);
View layout = inflater.inflate(R.layout.info, null);
AlertDialog.Builder about = new AlertDialog.Builder(ctx);
about.setTitle("Data");
about.setIcon(R.drawable.info);
// Make the textview clickable. Must be called after show()
TextView textView = (TextView) layout.findViewById(R.id.infoDetailText);
textView.setText(R.string.link_text_manual);
textView.setMovementMethod(LinkMovementMethod.getInstance());
about.setView(layout);
AlertDialog displayInfo = about.create();
displayInfo.show();
Code of TextView XML:
<TextView
android:id="#+id/infoDetailText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/infoVersionTitle"
android:padding="4dp" />
Removed two attributes..
android:autoLink="all"
android:linksClickable="true"
Try this
setText(Html.fromHtml(String));
I am building a Custom dialog box using the Android Developer docs link , for this i made a layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rotatelayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#80000000"
>
<ImageView
android:id="#+id/dialogimage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
and i am inflating this dialog,
AlertDialog dialog;
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(TabsActivity.this);
LayoutInflater inflator = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflator.inflate(R.layout.rotate_dialog_layout, (ViewGroup) findViewById(R.id.rotatelayout));
ImageView image = (ImageView) view.findViewById(R.id.dialogimage);
image.setOnClickListener(new RotateLockListener());
image.setBackgroundColor(Color.parseColor("#80000000"));
if(locked)
{
image.setImageResource(R.drawable.lock_icon);
}
else
{
image.setImageResource(R.drawable.unlock_icon);
}
builder.setView(view).setCancelable(true);
dialog = builder.create();
dialog.setView(view, 0, 0, 0, 0);
timerDelayRemoveDialog(time, dialog);
dialog.show();
but still it appears as
I have tried all the help provided at the stack over flow
Setting ImageView background to transparent,
Setting transparency by #80000000 and
Setting Dialog window transparency
But none of them worked, it still showing up.
Or you can use Dialog class for this,
Dialog dialog = new Dialog(this,
android.R.style.Theme_Translucent_NoTitleBar);
Window window = dialog.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
window.setLayout(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
dialog.setContentView(R.layout.temp);
dialog.show();
Try using this as the style of your View :
<style name="Dialog_Fullscreen">
<item name="android:windowFullscreen">true</item>
<item name="android:windowNoTitle">true</item>
</style>