i am trying to show fragment from a custom alertdilog's button click event, alert dialog is in fragment's adapter, i wanted to show fragment from alertdialog button click but i am unable to do so.
alertdialog code:
holder.commentButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder=new AlertDialog.Builder(context);
final AlertDialog alertDialog=builder.create();
View viewTmp_comment=LayoutInflater.from(context).inflate(R.layout.comment_show_layout,null,false);
RecyclerView recyclerView_commentShow=(RecyclerView)viewTmp_comment.findViewById(R.id.recyclerViewId_commentShow);
TextView textViewId_commentShow_layout=(TextView)viewTmp_comment.findViewById(R.id.textViewId_commentShow_layout);
ImageButton commentSendButtonId_Post=(ImageButton)viewTmp_comment.findViewById(R.id.commentSendButtonId_Post);
ImageButton imageUpload_CommentButton=(ImageButton)viewTmp_comment.findViewById(R.id.imageUploadButtonId_comment);
ImageButton smileyUpload_CommentButton=(ImageButton)viewTmp_comment.findViewById(R.id.smileyButtonId_comment);
editTextId_CommentPost=(EditText) viewTmp_comment.findViewById(R.id.editTextId_CommentPost);
// RelativeLayout smileyLoadLayoutId_CommentAdapter=(RelativeLayout)
final CommentShowAdapter commentShowAdapter=new CommentShowAdapter(context,newsFeedClassArrayList.get(position).getCommentShowClassArrayList(),"NewsFeed");
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(context);
recyclerView_commentShow.setLayoutManager(linearLayoutManager);
if(newsFeedClassArrayList.get(position).getCommentShowClassArrayList()!=null) {
recyclerView_commentShow.setAdapter(commentShowAdapter);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(context, linearLayoutManager.getOrientation());
recyclerView_commentShow.addItemDecoration(dividerItemDecoration);
commentShowAdapter.notifyDataSetChanged();
recyclerView_commentShow.onChildDetachedFromWindow(null);
}else {
builder.setMessage("No comments yet");
}
//comment show alert dismiss
textViewId_commentShow_layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
if(alertDialog.isShowing()){
alertDialog.dismiss();
}
}catch (Exception e){}
}
});
**//here i am trying to lauch fragment**
smileyUpload_CommentButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ServiceClass.setSmileyKey("comment");
smileyFragment=new SmileyShowFragment();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
Fragment fragment = fragmentManager.findFragmentById(R.id.smileyLoadLayoutId_CommentAdapter);
if (!(fragment instanceof SmileyShowFragment)) {
fragmentTransaction.replace(R.id.smileyLoadLayoutId_CommentAdapter, smileyFragment, "Smiley").commit();
} else {
fragmentTransaction.remove(smileyFragment).commit();
}
}
});
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = alertDialog.getWindow(); lp.copyFrom(window.getAttributes());
lp.width = context.getResources().getDisplayMetrics().widthPixels;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity=Gravity.BOTTOM; window.setAttributes(lp);
alertDialog.setView(viewTmp_comment);
alertDialog.show();
}
});
alert xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ddd"
>
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="#+id/textViewId_commentShow_layout"
android:text="Comments"
android:textSize="16sp"
android:paddingLeft="20dp"
android:textStyle="bold"
android:paddingTop="10dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/underline"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/recyclerViewId_commentShow"
>
</android.support.v7.widget.RecyclerView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
android:weightSum="4"
android:background="#1470a6"
>
<ImageButton
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/camera"
android:id="#+id/imageUploadButtonId_comment"
android:layout_gravity="center"
android:layout_marginLeft="8dp"
android:layout_marginRight="5dp"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/smileyButtonId_comment"
android:src="#drawable/smiley"
android:layout_gravity="center"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="60dp"
android:id="#+id/editTextId_CommentPost"
android:layout_weight="3"
android:background="#FFFFFF"
android:hint="Your comment"
android:textSize="16sp"
/>
<ImageButton
android:layout_width="30dp"
android:layout_height="match_parent"
android:id="#+id/commentSendButtonId_Post"
android:src="#drawable/send_comment"
android:background="#1470a6"
android:gravity="right"
android:layout_gravity="right"
android:layout_weight="1"
/>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/smileyLoadLayoutId_CommentAdapter"
>
</FrameLayout>
</LinearLayout>
how to solve this problem? is it possible to show fragment inside alertdialog.i have searched on google but no similar solution
You should create a separate customized dialog. To do so you can extend DialogFragment and on onCreateDialog() set up your views and proper assignments. Create a layout for your customized dialog fragment class with a fragment inside. You can create an interface on your customized dialog to listen for actions like button clicks or others. Then, onClick of your button use fragment manager to show the fragment within your dialogfragment. Something like below:
//Give it a fragment with a FrameLayout to hold the fragment if you
need to hold more than one or with just add a fragment in your layout
if you do not need a holder.
public class MyCustomizedDialog extends DialogFragment{
private TextView dialogTitle;
private TextView dialogText;
private FrameLayout myFragmentContainer;
private Button myButton;
private CustomDialogListener customDialogListener;
public interface CustomDialogListener{
//set listeners here, i.e. for buttons clicked by user
public void onBtnClick();
}
public setDialogFragmentListener(CustomDialogListener customDialogListener){
this.customDialogListener = customDialogListener;
}
#override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//initialize dialog
setUpMyCustomDialog(dialog);
return dialog;
}
public void setUpMyCustomDialog(Dialog dialog){
dialogTitle = (TextView)dialog.findViewById(R.id.dialog_title_tv);
dialogText = (TextView)dialog.findViewById(R.id.dialog_text_tv);
myFragmentContainer=(FrameLayout)dialog.findViewById(R.id.child_fragment_container);
myButton = (Button)dialog.findViewById(R.id.myButton);
myButton.setOnClickListener(new OnClickListener(){
public void onClick(View view) {
//initialize your fragment here.
}
}
}
}
Related
I'm working on android project. I want to show a popup dialog inside my activity. I have implemented it (All the codes attached below). But my question is, when I run this project in android the popup shows like this. I need to increase the width of this popup dialog. I tried a few solutions, but didn't work for me. Please help me to solve this matter. Thanks in advance.
Here is my code
xml file--
<?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="wrap_content"
android:id="#+id/lv_main">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="22sp"
android:text="MSGP3006"
android:textColor="#color/Black"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="QTY:2 | Weight : 500g | Discount::25%"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_marginBottom="#dimen/dimen_5dp"
android:layout_gravity="bottom">
<TextView
android:id="#+id/amount"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Rs.360.00"
android:gravity="end"
android:textColor="#color/Sky_Dark_Blue"
android:textSize="22sp"/>
</LinearLayout>
</LinearLayout>
java file--
public class AddInvoiceDialog extends Dialog {
public Activity activity;
public Dialog dialog;
public Button btn_add;
public AddInvoiceDialog(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.activity = a;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.inv_add_new_invoice_popup);
btn_add = (Button) findViewById(R.id.btn_add);
btn_add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
activity.finish();
}
});
}
}
Add below line after setContentView(R.layout.inv_add_new_invoice_popup);
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
You will get popup window matching device width.
You also need to add android:orientation="vertical" in your parent LinearLayout, it contains multiple child.
You can set custom view in Alert Dialog as mentioned below :
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogView = inflater.inflate(R.layout.inv_add_new_invoice_popup, null);
alertDialog.setView(dialogView);
alertDialog.setCancelable(false);
alertDialog.show();
Somehow the scroll view is not working . The message alert dialog box is showing is really big and so I need to implement vertical scrollbar. I tried to get data from previous asked question but it isn't solving my issue please help.
I need to show the alert dialog on button click event.
benefits.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog ad = new AlertDialog.Builder(Panchgavya.this).create();
ad.setCancelable(false); // This blocks the 'BACK' button
ad.setMessage(getString(R.string.benefits));
ad.setTitle("Benefits");
ad.setButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ad.show();
TextView textView = (TextView) ad.findViewById(android.R.id.message);
textView.setScroller(new Scroller(Panchgavya.this));
textView.setVerticalScrollBarEnabled(true);
textView.setMovementMethod(new ScrollingMovementMethod());
}
});
My XML File Code:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Design.Panchgavya"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#color/Green"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Panchgavya"
android:textColor="#color/white"/>
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6"
android:orientation="vertical">
<TextView
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:text="#string/panchgavya"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="vertical">
<TextView
android:id="#+id/panchgavya_tv_cow_dung"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cow Dung"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textStyle="bold"
android:textColor="#color/black"/>
<TextView
android:id="#+id/panchgavya_tv_cow_urine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Cow Urine"
android:textStyle="bold"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#color/black"/>
<TextView
android:id="#+id/panchgavya_tv_cow_milk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Cow Milk"
android:textStyle="bold"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#color/black"/>
<TextView
android:id="#+id/panchgavya_tv_ghee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:text="Ghee"
android:textStyle="bold"
android:textColor="#color/black"/>
<TextView
android:id="#+id/panchgavya_tv_dahi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:text="Dahi"
android:textStyle="bold"
android:textColor="#color/black"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
<Button
android:id="#+id/panchgavya_btn_benefits"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Benefits"
android:background="#color/Green"
android:textColor="#color/white"
/>
</LinearLayout>
</LinearLayout>
You can create custom layout for your dialog and set any property easily.
For your requirement, Crate a layout for your required Dialog. Put android:scrollbars = "vertical" in your textView inside your layout. And textview.setMovementMethod(new ScrollingMovementMethod());.
You can set custom layout on your by following method.
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();
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Textview
android:id="#+id/txtDescription"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/margin_16dp"
android:paddingRight="#dimen/margin_16dp"
android:paddingBottom="#dimen/margin_16dp"
android:paddingTop="#dimen/margin_10dp"
android:text="#string/dummy_text_"
android:textSize="#dimen/font_14dp"
android:textColor="#color/colorPrimary"
android:layout_below="#+id/imgClose"/>
<ImageView
android:id="#+id/imgClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_close"
android:layout_alignParentRight="true"
android:layout_marginRight="#dimen/margin_10dp"
android:layout_marginTop="#dimen/margin_10dp" />
</RelativeLayout>
</ScrollView>
// in your java file put below code
private void showPopup() {
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.dialog_terms_services,
null);
dialogBuilder.setView(dialogView);
TextView txtDescription = dialogView.findViewById(R.id.txtDescription);
ImageView imgClose = dialogView.findViewById(R.id.imgClose);
txtDescription.setText(message);
final AlertDialog b = dialogBuilder.create();
b.show();
imgClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
b.dismiss();
}
});
}
// after than call that showPopup() methos on button click.Hope it works for you
class extending DialogFragment can have UI elements as per your requirement.
public class OrderDetailFragment extends DialogFragment{
#Override
public void onStart() {
super.onStart();
Dialog d = getDialog();
if (d!=null){
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.WRAP_CONTENT;
d.getWindow().setLayout(width, height);
}
}
public static OrderDetailFragment getInstance(GeneralListDataPojo dataList){
OrderDetailFragment orderDetailFragment=new OrderDetailFragment();
Bundle bundle = new Bundle();
bundle.putParcelable(DATA, dataList);
orderDetailFragment.setArguments(bundle);
return orderDetailFragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root_view = inflater.inflate(R.layout.fragment_order_detail, container, false);
GeneralListDataPojo mDataList = getArguments().getParcelable(DATA);//My class which holds data implemented Parcelable
//population of data from mDataList
return root_view;
}
void closeDialog(){
this.dismiss();
}
}
fragment_order_detail is having my requirement specific element's (ScrollView/LinearLayouts/Buttons etc)
GeneralListDataPojo is the class which holds data implements Parcelable for transferring data between components
Now you can invoke this DialogFragment from your Fragment like this. (Im invoking from a Fragment. Change the FragmentManager retrieval accordingly if you are using from Activity)
OrderDetailFragment orderDetailFragment=OrderDetailFragment.getInstance((GeneralListDataPojo) responseObj);
FragmentManager fragmentManager=getFragmentManager();
orderDetailFragment.show(fragmentManager,"OrderDetailFragment");
Create The Custom Dialog layout Using Scroll View in your own custom Layout
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog= new Dialog(getApplicationContext());
dialog.setContentView(R.layout.activity_dialog);
Button click= dialog.findViewById(R.id.click);
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
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 am trying to implement a share action that on user's choice, he gets a popup dialog with specific share actions (over specific distinct apps - desired screen:desired layout).
I would like to have 6 ImageButtons.
Till now, I could get the popup window (but the onclick listenes - were like dead) or (on the other hand) the app would crash.
My Layout for the popup is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorAccent">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/viber"
android:id="#+id/viber_share_button"
android:layout_below="#+id/twitter_share_button"
android:background="#color/colorAccent"
android:layout_alignEnd="#+id/twitter_share_button"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_toEndOf="#+id/facebook_share_button" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/twitter"
android:id="#+id/twitter_share_button"
android:background="#color/colorAccent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/facebook_messenger"
android:id="#+id/messanger_share_button"
android:background="#color/colorAccent"
android:layout_marginLeft="10dp"
android:layout_alignTop="#+id/viber_share_button"
android:layout_alignEnd="#+id/facebook_share_button" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/instagram"
android:id="#+id/instagram_share_button"
android:background="#color/colorAccent"
android:layout_marginLeft="10dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/twitter_share_button"
android:layout_marginStart="31dp"
android:layout_marginTop="10dp"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/whatsapp"
android:id="#+id/whatsapp_share_button"
android:background="#color/colorAccent"
android:layout_alignTop="#+id/viber_share_button"
android:layout_alignEnd="#+id/instagram_share_button"
android:layout_toEndOf="#+id/viber_share_button"
android:layout_alignStart="#+id/instagram_share_button" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/facebook"
android:id="#+id/facebook_share_button"
android:background="#color/colorAccent"
android:layout_marginLeft="10dp"
android:layout_alignParentTop="true"
android:layout_toStartOf="#+id/twitter_share_button"
android:layout_marginEnd="39dp"
android:layout_marginTop="10dp"/>
</RelativeLayout>
the code that I use is:
private void displayIntentOptions(final Bitmap bitmap){
LayoutInflater layoutInflater = LayoutInflater.from(this);
View promptView = layoutInflater.inflate(R.layout.share_buttons, null);
final AlertDialog alertD = new AlertDialog.Builder(this).create();
alertD.setView(promptView);
alertD.show();
ImageButton facebook_button = (ImageButton)findViewById(R.id.facebook_share_button);
ImageButton twitter_button = (ImageButton)findViewById(R.id.twitter_share_button);
ImageButton intagram_button = (ImageButton)findViewById(R.id.instagram_share_button);
ImageButton messanger_button = (ImageButton)findViewById(R.id.messanger_share_button);
ImageButton viber_button = (ImageButton)findViewById(R.id.viber_share_button);
ImageButton whatsapp_button = (ImageButton)findViewById(R.id.whatsapp_share_button);
facebook_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
twitter_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
shareTwitter(v, bitmap);//just a sample of what I want to do
}
});
intagram_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
messanger_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
viber_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
whatsapp_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
This code doesn't work at all. I would be thankful, If someone could help me with this. Since on a regular dialog I cannot have all those ImageButtons, I tried to make a custom one.
Furthermore, I would like to know if this is a good practice, or I should implement a new Intent for this.
Thanks in Advance.
You need to change one little thing
Was
final AlertDialog alertD = new AlertDialog.Builder(this).create();
alertD.setView(promptView);
alertD.show();
Now
final AlertDialog.Builder alertD = new AlertDialog.Builder(this);
alertD.setView(promptView);
alertD.show();
Update
Also there is a problem in this
ImageButton facebook_button = (ImageButton)findViewById(R.id.facebook_share_button);
It will be null, because you'r looking at Activity(or fragments) views, but not in AlertDialog ones. You need to set it like this
ImageButton facebook_button = (ImageButton)promptView.findViewById(R.id.facebook_share_button);
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);