ProgressDialog is depracted thats why I create a layout and put a progressbar and a textview and put this layout into a dialog. Until now, everything works well but in runtime I cannot change the textview's text.
Here is my code (in OnCreate Method was called)
View progressLayout = LayoutInflater.FromContext(this).Inflate(Resource.Layout.progressbar,null);
var txtMessage = progressLayout.FindViewById<TextView>(Resource.Id.loading_msg);
txtMessage.Text = "My message is this";
txtMessage.SetText("my tesst", TextView.BufferType.Normal);
progressLayout.FindViewById<TextView>(Resource.Id.loading_msg)
.SetText("hobaa",TextView.BufferType.Normal);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.SetView(Resource.Layout.progressbar);
Dialog dialog = builder.Create();
dialog.SetTitle("My Title");
dialog.SetCancelable(false);
dialog.Show();
and my progress.axml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:padding="13dp"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ProgressBar
android:id="#+id/loader"
android:layout_marginEnd="5dp"
android:layout_width="45dp"
android:layout_height="45dp"
/>
<TextView
android:layout_width="wrap_content"
android:text="Loading..."
android:textAppearance="?android:textAppearanceSmall"
android:layout_gravity="center_vertical"
android:id="#+id/loading_msg"
android:layout_toEndOf="#+id/loader"
android:layout_height="wrap_content" />
</LinearLayout>
Change this
builder.SetView(Resource.Layout.progressbar);
to
builder.SetView(progressLayout);
Related
I have set a custom layout resourse file as View for Alert Dialog but im unalbe to fetch/ get values from views in the layout. Can someone point out what im doing wrong here? or is there another way to do achieve this?
some_layout.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText android:inputType="textAutoComplete"
android:textSize="20sp"
android:id="#+id/title"
android:scrollHorizontally="true"
android:layout_height="70dp"
android:hint="Title / Username"
android:layout_width="250dp"
android:layout_gravity="center"
android:autofillHints="username" />
<EditText android:inputType="textVisiblePassword"
android:textSize="20sp"
android:id="#+id/password"
android:scrollHorizontally="true"
android:layout_height="70dp"
android:hint="Password"
android:layout_width="250dp"
android:layout_gravity="center"
android:autofillHints="password" />
</LinearLayout>
Code :
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("Credentials");
alertDialog.setView(R.layout.some_layout);
EditText title = (TextView) some_layout.findViewById(R.id.title);
Error : ( 'some_layout' in EditText definition is highlighted in red and it has this message)
Cannot resolve symbol 'password_item_dialog'
I solved it myself
LayoutInflater layoutInflater = LayoutInflater.from(context);
final View alertView = layoutInflater.inflate(R.layout.some_layout, null);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setView(alertView);
EditText title = (EditText) alertView.findViewById(R.id.title);
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();
}
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 ;)
I created a custom dialog but I got an unexpected result.
The final appearance of the dialog is:
As you can see, my custom dialog is shown with a kind of padding in both top and bottom of the dialog, and I don't code it to do this...
The code is the next:
#Override
protected Dialog onCreateDialog(int id){
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View newAsig = inflater.inflate(R.layout.nuevaasignatura_act, null);
//Also, down here I tried to shape the dialog by setting a functional resource, but
//the stroke should be blue, not grey.
RelativeLayout re = (RelativeLayout)newAsig.findViewById(R.id.layoutCrearAsig1);
re.setBackgroundResource(R.drawable.borde);
Typeface tf = Typeface.createFromAsset(getAssets(), "Roboto-Light.ttf");
ImageView ico = (ImageView)newAsig.findViewById(R.id.ivicono);
ico.setBackgroundResource(R.drawable.colorprincipal);
LinearLayout ly = (LinearLayout)newAsig.findViewById(R.id.layoutCrearAsig2);
ly.setBackgroundColor(Color.rgb(214, 217, 224));
TextView txt = (TextView)newAsig.findViewById(R.id.textviewnuevaasig);
txt.setTypeface(tf);
TextView txt1 = (TextView)newAsig.findViewById(R.id.textViewCarpeta);
txt1.setTypeface(tf);
txt1.setBackgroundResource(R.drawable.colorprincipal);
Button btn = (Button)newAsig.findViewById(R.id.buttonCrearAsig);
btn.setTypeface(tf);
switch (id){
case 0:
return new AlertDialog.Builder(this)
.setView(newAsig).create();
}return null;
And the XML file...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/layoutCrearAsig1">
<ImageView
android:id="#+id/ivicono"
android:layout_width="50dp"
android:layout_height="50dp"
android:paddingLeft="4dp"
android:background="#color/DarkGray"
android:contentDescription="Icono ClassMarks"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:src="#drawable/icono_cm" />
<TextView
android:id="#+id/textViewCarpeta"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_toRightOf="#id/ivicono"
android:background="#color/DarkGray"
android:clickable="true"
android:gravity="center"
android:text="Carpeta"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:id="#+id/layoutCrearAsig2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/ivicono"
android:orientation="vertical">
<TextView
android:id="#+id/textviewnuevaasig"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:text="Crear nueva asignatura"
android:textSize="20sp" />
<EditText
android:id="#+id/edittextAsig"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp" >
</EditText>
<Button
android:id="#+id/buttonCrearAsig"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="20sp"
android:text="Crear"/>
</LinearLayout>
</RelativeLayout>
I hope you can help me. Thanks!
The idea is AlertDialog always has 5dp top and bottom padding.
Try something like this:
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setView(newAsig, 0, 0, 0, 0);
Try this validation.
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
builder = new AlertDialog.Builder(getActivity());
builder.setInverseBackgroundForced(true);
} else {
builder = new AlertDialog.Builder(getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
}
}
I am using a custom Dialog that contains a text field, an image, and a button. The text can contain HTML. Sometimes the bottom of the dialog gets chopped off the bottom when the text is long enough. How can I prevent this? I want Android to determine the size of the dialog but it doesn't seem to be doing that. DO I need to size the Dialog myself in this case?
Here is the layout...
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/alert_root_incorrect"
style="#style/AlertDialogTheme"
android:background="#drawable/rounded_alert"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/rounded_alert"
>
<TableLayout
android:stretchColumns="0"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:textStyle="bold"
android:text="Sorry, that's wrong!"
android:textColor="#color/gray_dark" />
<ImageView
android:id="#+id/check"
android:background="#drawable/xmark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dp" />
</TableRow>
</TableLayout>
<TextView
android:id="#+id/alert_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="none"
android:text="In fact, this is where the explanation will go. Something about how this passage related to the topic"
android:textColor="#000000" />
<Button
android:id="#+id/okay_button"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:background="#drawable/rounded_alert_button"
android:text="Okay"
android:textColor="#color/white"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
And here is the code I am using to load it...
if ( null == layout ) {
this.layout = inflater.inflate(R.layout.alert_incorrect, (ViewGroup) findViewById(R.id.alert_root_incorrect));
}
TextView message = (TextView) layout.findViewById(R.id.alert_text);
message.setText(Html.fromHtml(card.getConclusion()));
((Button) layout.findViewById(R.id.okay_button)).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dismissDialog(INCORRECT_DIALOG);
nextQuestion();
}
});
layout.requestLayout();
dialog = new Dialog(this, R.style.AlertDialogTheme);
dialog.setContentView(layout);
dialog.setCancelable(false);
return dialog;
And here's a snap of what I mean..
Thanks,
John
This is not perfect but I've corrected it by setting the layout of the dialog relative to the default display.
dialog = new Dialog(this, R.style.AlertDialogTheme);
dialog.setContentView(layout);
Window window = dialog.getWindow();
window.setLayout(
(int)(window.getWindowManager().getDefaultDisplay().getWidth() * .90),
(int)(window.getWindowManager().getDefaultDisplay().getHeight() * .90 ));
dialog.setCancelable(false);
Just tweak the ".90" values until it feels right.
Here is the solution:
You should add a Linearlayout at the outside of your dialog's xml file
Then set this Linearlayout's gravity as "center"
the last step is creating a LayoutParams and set it to the dialog
android.view.WindowManager.LayoutParams lp = new android.view.WindowManager.LayoutParams();
lp.width = android.view.WindowManager.LayoutParams.FILL_PARENT;
lp.height = android.view.WindowManager.LayoutParams.FILL_PARENT;
alertDialog.getWindow().setAttributes(lp);