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);
Related
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 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.
}
}
}
}
How to get onclick of a view in custom dialog in an activity in android?
My XML is:
<?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:background="#color/default_green"
android:orientation="vertical" >
<TextView
android:id="#+id/tvSetNotificationsHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:text="Set Notifications On"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/white" />
<RadioButton
android:id="#+id/rbFriendRequest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:text="Friend Request"
android:textColor="#android:color/white" />
<RadioButton
android:id="#+id/rbMessages"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:text="Messages"
android:textColor="#android:color/white" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:onClick="saveSetNotificationsDialog"
android:text="Save" />
</LinearLayout>
My code is:
public void showSetNotificationsDialog(View v) {
dialog = new Dialog(Activity_Settings.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_setnotificationsdialog);
rbFriendRequest = (RadioButton) findViewById(R.id.rbFriendRequest);
rbMessages = (RadioButton) findViewById(R.id.rbMessages);
dialog.show();
}
I am able to show dialog view but no able to get on click of dialog save button in activity class. Error was no method found exception.
use instance of dialog for findViewById()
Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
declineButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
use as following code
dialog = new Dialog(Activity_Settings.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
View view = View.inflate(getActivity(), R.layout.dialog_setnotificationsdialog, null);
dialog.setContentView(view);
//always find your view view.findViewbyid
yourView= (RadioButton) view.findViewById(R.id.rbFriendRequest);
//then you can add on click listner to any of your view i.e.
yourView.setonClickListener(this);(new OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
I am looking for an easy popupmenu I could use for phones that operate on 2.3.3 and up.
Something like this, and works differently from a ContextMenu in that it doesn't require a listview:
Try the following :
Create a file dialog.xml in your layout folder and add the following code :
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/ErrorMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Do you want to un-install this app?" />
<LinearLayout
android:layout_below="#+id/ErrorMsgDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button android:id="#+id/Cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="15sp"
android:text="Cancel" />
<Button android:id="#+id/Ok"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="15sp"
android:text="OK" />
</LinearLayout>
and then in your class file use following code to display the dialog :
AlertDialog.Builder builder = new AlertDialog.Builder(context);
View view = LayoutInflater.from(context).inflate(
R.layout.dialog, null);
builder.setView(view);
dialog = builder.create();
dialog.setCanceledOnTouchOutside(true);
Button cancel = (Button) view.findViewById(R.id.Cancel);
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
Button ok = (Button) view.findViewById(R.id.Ok);
ok.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Do something here for OK action
}
});
dialog.show();
The easy way to do this is just create an AlertDialog.Builder object and configured its positive and negative buttons and then set the Title and the Messages and done. Removing the need for any layout files.
I have one AlertDialog which is working fine.I have set some background images to it with following code:
Button buttonPositive = (Button)dialog.getButton(DialogInterface.BUTTON_POSITIVE);
Button buttonNegative = (Button)dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
buttonPositive.setBackgroundResource(R.drawable.custom_button);
buttonPositive.setTextColor(Color.WHITE);
buttonNegative.setBackgroundResource(R.drawable.custom_button);
buttonNegative.setTextColor(Color.WHITE);
Now after setting image the two buttons are touching each other, i mean they have no space between them.I have tried with setPadding(...),it's not working.Even if i am changing the image size(i.e. width) it is not working.Any help !!!
I think its better to create layout xml file what you want ...
and set Like alertDialog.setContentview(R.layout.mylayout);
try this code
private Dialog myDialog;
myDialog = new Dialog(ShowReportActivity.this);
myDialog.setContentView(R.layout.alert);// your xml
myDialog.setTitle("Send Email");
myDialog.setCancelable(true);
Button set = (Button) myDialog .findViewById(R.id.alert_bnt_send_email);
Button exit = (Button) myDialog.findViewById(R.id.alert_bnt_exit);
set.setTextColor(Color.WHITE);
set.setBackgroundResource(R.drawable.custom_button);
getMailId = (EditText) myDialog.findViewById(R.id.alert_editT_email_Id);
send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
.........
myDialog.dismiss();
});
exit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
myDialog.dismiss();
}
});
myDialog.show();
use custome dialog using code like given below
Dialog windialog = new Dialog(YourActivity.this);
windialog.setContentView(R.layout.win_dialog);
windialog.setTitle("Congratulation");
windialog.setCancelable(true);
final EditText et_emailverification=EditText)windialog.findViewById(R.id.et_emailveri);
et_emailverification.setText(UserEmailOrName);
Button submit=(Button)windialog.findViewById(R.id.bt_sub_que);
submit.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//write here your code what you want onclick
}
});
Button cancel=(Button)windialog.findViewById(R.id.bt_sq_cancel);
cancel.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
windialog.cancel();
});
windialog.show();
and xml like win_dialog.xml
<?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:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email "
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/et_emailveri"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_weight="1" >
</EditText>
</LinearLayout>
<RelativeLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/bt_sub_que"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
<Button
android:id="#+id/bt_sq_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="Cancel" />
</RelativeLayout>
</LinearLayout>