Android DialogFragment TextView setText() doesn't work - android

this is my onCreate() function:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//inflate the view
View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_loading, null);
TextView loadingMessage = (TextView) view.findViewById(R.id.fragment_loading_message_textview);
loadingMessage.setText("test");
//the dialog
Dialog dialog;
//set the dialog views
int themeId = getDialogThemeId();
//if there is no theme
if (themeId == -1)
dialog = new Dialog(getActivity());
else
dialog = new Dialog(getActivity(),getDialogThemeId());
//remove background
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
//remove title
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
//set the content view
dialog.setContentView(view);
//set cancel properties
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
return dialog;
}
this is the layout i'm inflating:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:background="#android:color/black">
<!-- spinner -->
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#android:color/transparent" />
<!-- text -->
<TextView
android:id="#+id/fragment_loading_message_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAppearance="#android:style/TextAppearance.Large"
android:textColor="#android:color/white"
android:text="LOADING"/>
</LinearLayout>
although i'm calling loadingMessage.setText("test") the loading message is keeping showing me "LOADING" in the message... any idea???

ok i found the problem.
in the same DialogFragment I've implemented onCreateDialog and onCreateView as well.
This causes my view to be inflated twice. since onCreateView is called after onCreateDIalog, the view I've loaded in the onCreateDialog was overridden..
hope it will someone else

Related

How to customise android dialogs to material theme?

I wanted to customise android Dialog to make it look like a floating dialog as below which is suggested in https://material.google.com/components/dialogs.html#dialogs-simple-dialogs
I tried creating a DialogFragment in which onCreateDialog returns a Dialog with a view whose root is a CardView.But couldn't get the expected result.Can I have some suggestions on how to achieve this?
My layout looks like this,
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center|top"
card_view:cardElevation="5dp"
card_view:cardPreventCornerOverlap="false">
//Contents come here
</android.support.v7.widget.CardView>
and in code,
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new Dialog(getActivity());
View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_font_selection,null,false);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(view);
float width = (I am getting screen width here) * .80f;
dialog.getWindow().setLayout((int) width, ViewGroup.LayoutParams.WRAP_CONTENT);
return dialog;
}`
but the result looks like this,
Instead of getting elevation I was getting a black shade
AlertDialog.Builder mydialog = new AlertDialog.Builder( context);
mydialog.setTitle("Set backup accounts");
mydialog.setItems(CharSequence[] items, OnClickListener);
mydialog.show();
mydialog.setContentView(R.layout.mydialogLayout);
This in your activity/fragment. And so declare your layout as follow:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView
android:id="#+id/myID1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
After that, if you want to handle the clicks just do that:
TextView myView1 = (TextView) mydialog.findViewById(myID1);
myView1.setOnClickListener(...);
MaterialAlertDialogBuilder is now available for Android:
material.io/develop/android/components/dialog

how to change the alertdialog box style in android

I am just trying to build a alertdialog for my android application.
what I actually want is a dialog box like this:
whose positive and negative button are filled across dialog box
but I always get something like this:
and these two buttons stick together at the right corner of dialog box
can anybody tell me what to do to change the alertdialog from the second one to first one?
many thanks
For that You need to create custom dilogbox Like this
Dialog xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/image"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="#drawable/ic_launcher"
android:src="#drawable/ic_launcher" />
<Button
android:id="#+id/button"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_below="#+id/image"
android:layout_centerHorizontal="true"
android:text="Dismiss" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_above="#+id/button"
android:layout_toRightOf="#+id/image"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
And In your Activity :
final Dialog dialog = new Dialog(MainActivity.this);
//setting custom layout to dialog
dialog.setContentView(R.layout.cusotm_dialog_layout);
dialog.setTitle("Custom Dialog");
//adding text dynamically
TextView txt = (TextView) dialog.findViewById(R.id.textView);
txt.setText("Put your dialog text here.");
ImageView image = (ImageView)dialog.findViewById(R.id.image);
image.setImageDrawable(getResources().getDrawable(android.R.drawable.ic_dialog_info));
//adding button click event
Button dismissButton = (Button) dialog.findViewById(R.id.button);
dismissButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
You can set customized layout to alert dialog by using setView(view) method like this:
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alert_layout, null);
dialogBuilder.setView(dialogView);
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();

Make dialog smaller on screen

I want my dialog to not fit the whole screen like the image shown. How should i make it so it is not as long (height).
public class SidemenuInfoDialogFragment extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.sidemenu_info_main, container, true);
return layout;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new Dialog(new ContextThemeWrapper(getActivity(), R.style.Theme_Window_NoMinWith));
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
}
The View:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#+id/sidemenu_info_main_relative">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/text1"
android:text="TEST TEXT "/>
</RelativeLayout>
Let me know if there is something else needed to be able to help me get on the right path of fixing this.
Give your textview inside your dialog view height and width as "wrap_content".Like:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#+id/sidemenu_info_main_relative">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text1"
android:text="TEST TEXT "/>
</RelativeLayout>
Try changing you xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#+id/sidemenu_info_main_relative">
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:id="#+id/text1"
android:text="TEST TEXT "/>
</RelativeLayout>
If that does not work with you here is another approach that I usually do :
public class PopUp {
public void poupUP(final Activity activity) {
final Dialog dialog = new Dialog(activity, R.style.dialogwithoutTitle);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
//here add the layout
dialog.setContentView(R.layout.pop_up);
// The comments are to change the position of the alert dialogu
// WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();
//wmlp.gravity = Gravity.TOP | Gravity.END;
//wmlp.x = 100; //x position
//wmlp.y = 100; //y position
dialog.show();
}
}
Then wherever you want it to show do this :
PopUp p = new PopUp();
p.popUP();
RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.sidemenu_info_main, container, false);
Try this:
alert.getWindow().setLayout(dialogWidth, LayoutParams.WRAP_CONTENT);
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(alert.getWindow().getAttributes());
layoutParams.x = xPosition;
layoutParams.y = yPosition;
alert.getWindow().setAttributes(layoutParams);

Adding buttons programatically to DialogFragment are never displayed

So I am trying to add a button programatically to a DialogFragment but the button is never shown.
I have the following layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#99000000"
android:padding="10dp">
<RelativeLayout
android:id="#+id/notificationLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/logo_background_shape"
android:padding="2dp"
android:layout_centerVertical="true"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/input_layout"
android:paddingBottom="20dp">
<TextView
android:id="#+id/start_date_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Some text"
android:paddingLeft="10dp"
android:textColor="#color/my_blue"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/clickables_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/input_layout"
android:paddingBottom="10dp">
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
Here is the way I create my dialog's view:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
/**
* Setup the dialog and its styles
*/
final Dialog dialog = new Dialog(getActivity(), android.R.style.Theme_Black_NoTitleBar_Fullscreen);
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
final View view = getActivity().getLayoutInflater().inflate(R.layout.notification_fragment_layout, null);
final RelativeLayout relLayout = (RelativeLayout)getActivity().findViewById(R.id.clickables_layout);
RelativeLayout.LayoutParams paramsButton1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
paramsButton1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
Button button1 = (Button) mDynamicNotification.getClickableList().get(0).getElementView(getActivity(), paramsButton1);
relLayout.addView(button1);
/**
* Code block handling blurring of the background
*/
final Drawable d = new ColorDrawable(Color.TRANSPARENT);
dialog.getWindow().setBackgroundDrawable(d);
dialog.getWindow().setContentView(view);
//allow the dialog to be canceled when touching outside of the dialog
dialog.setCanceledOnTouchOutside(false);
final Activity activity = getActivity();
final View content = activity.findViewById(android.R.id.content).getRootView();
if (content.getWidth() > 0) {
Bitmap image = BlurrBuilder.blur(content);
dialog.getWindow().setBackgroundDrawable(new BitmapDrawable(activity.getResources(), image));
} else
content.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Bitmap image = BlurrBuilder.blur(content);
dialog.getWindow().setBackgroundDrawable(new BitmapDrawable(activity.getResources(), image));
}
});
return dialog;
}
Problem:
The problem is that the button that I am adding is never shown in the layout once launched. It almost seems like the layout gets created before I add the button so the button is never shown...
The problem is that the button that I am adding is never shown in the
layout once launched
Because forget to add relLayout in Dialog layout which passing in dialog.getWindow().setContentView.
Use view.addView method to add Button in view :
relLayout.addView(button1);
view.addView(relLayout);

Button under ListView in AlertDialog is hidden

I want to show a list with a button under it in an AlertDialog. The Button is to close the Dialog itself. I extended AlertDialog and added some LayoutParams to the Window to extend the Dialog of the whole screen width:
//Grab the window of the dialog, and change the width
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = getWindow();
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
ok. But if the list is long (display is full of it), I can scroll the ListView but the button is not shown under it. Here's the ContentView of the Dialog:
<?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">
<ListView
android:id="#+id/choose_equipment_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_below="#+id/choose_equipment_list"
android:id="#+id/btn_choose_equipment_close"
style="#style/custom_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="close" />
</RelativeLayout>
What can I do to show the button ALWAYS under the list, no matter how long it is? (I read a lot warnings to put a ListView inside a ScrollView, tried it anyway and failed...)
Try it:
<?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" >
<ListView
android:id="#+id/choose_equipment_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:id="#+id/btn_choose_equipment_close"
style="#style/custom_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="close" />
</LinearLayout>
If not work, remove the style="#style/custom_button" line. Some style configuration could change the view style and hide the button.
I want to show a list with a button under it in an AlertDialog.
So What you have to do is :
Remove this from from your Button:
android:layout_below="#+id/choose_equipment_list"
Then put this in Button:
android:layout_alignParentBottom="true"
And Put this in ListView:
android:layout_above="#+id/btn_choose_equipment_close"
You are Done.
Hmm. Maybe try to use Fragments. I coded sample for you. Tell me if it is what you are looking for:)
MainActivity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
findViewById(R.id.open_dialog_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SampleDialogFragment dialogFragment = new SampleDialogFragment();
dialogFragment.show(getSupportFragmentManager(), "dialogFragment");
}
});
}
}
main_activity.xml
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="#+id/open_dialog_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open dialog"
android:layout_gravity="center" />
</FrameLayout>
SampleDialogFragment.java
public class SampleDialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle("Timer");
alert.setPositiveButton("OK", null);
View view = View.inflate(getActivity(), R.layout.dialog, null);
String[] array = {"Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, array);
((ListView) view.findViewById(R.id.listView)).setAdapter(adapter);
alert.setView(view);
return alert.create();
}
}
dialog.xml
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
I think it could be possible to get what you need declaring in the XML your button first located in the bottom and then the listview above it with height set to fill_parent
This is should be what you want...
<?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">
<ListView
android:id="#+id/choose_equipment_list"
android:layout_above="#+id/btn_choose_equipment_close"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn_choose_equipment_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="close" />
</RelativeLayout>

Categories

Resources