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>
Related
I have 2 layout files, one for main layout showing (activity_main.xml) and another is for custom dialog showing (dialog_custom.xml). The 2 files are as follows-
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="#+id/btn_1_option"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Alert with 1 Button"
android:padding="10dip"
android:layout_marginTop="40dip">
</Button>
<Button android:id="#+id/btn_2_option"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Alert With 2 Buttons"
android:padding="10dip">
</Button>
<Button android:id="#+id/btn_3_option"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Alert with 3 Buttons"
android:padding="10dip">
</Button>
<Button android:id="#+id/btn_custom_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Alert with Custom Layout - From XML"
android:padding="10dip">
</Button>
</LinearLayout>
dialog_custom.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/dialog_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="#android:color/holo_orange_dark"
android:text="#string/dialog_text"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="#id/dialog_info">
<Button
android:id="#+id/dialog_cancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.50"
android:background="#3333"
android:text="Cancel"/>
<Button
android:id="#+id/dialog_ok"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.50"
android:background="#888888"
android:text="Agree"/>
</LinearLayout>
</RelativeLayout>
And what I have done in the Main class is -
public class Activity_Main extends Activity
{
Context context = Activity_Main.this;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_main);
dialog.setTitle(R.string.dialog_title);
dialog.show();
}
}
So, I get a dialog like this-
Now I want to add on click option for this CANCEL and AGREE button. But if I want, I am getting forced close. Can anyone please help me, how can I do it?
Thanks for helping.
Do it as follows :
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.layout.dialog_main);
Button btCancel = (Button) dialog.findViewById(R.id.dialog_cancel);
Button btOk = (Button) dialog.findViewById(R.id.dialog_ok);
btCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Cancel Click
}
});
btOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//OK CLICK
}
});
dialog.show();
If you want to add Buttons or any Widget (inside of the Dialog) make sure that you are finding the view with dialog.findViewById(R.id.ID_WIDGET) otherwise it will crash.
I couldn't find your click code cancel and agree, but try this:
Button cancel=(Button)dialog.findViewById(R.id.dialog_cancel);
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// do your work
}
});
Thanks if problem persists then post your error and click code.
You may need to add View.OnClickListener to buttons of dialog.
Button btnCancel = (Button) dialog.findViewById(R.id.dialog_cancel);
Button btnOk = (Button) dialog.findViewById(R.id.dialog_ok);
btnCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Called on click of Cancel button
}
});
btnOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Called on click of Cancel button
}
});
dialog.show();
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 have a custom dialog preference that have bunch of button.each button represent a number.something like clock application when you want to set alarm. how can I get onClick of each button in my dialog preference?
this is part of my dialog layout (time_picker) :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<LinearLayout
android:id="#+id/showTimer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="_ _ : _ _"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
<RelativeLayout
android:id="#+id/Buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_below="#+id/showTimer"
android:layout_marginTop="10dp">
<Button
android:id="#+id/button1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button5"
android:layout_alignBottom="#+id/button5"
android:layout_alignLeft="#+id/button3"
android:layout_centerInParent="true"
android:text="6"
android:textStyle="bold"
android:textSize="20dp"
android:tag="1"
android:background="#android:color/transparent" />
<Button
android:id="#+id/button2"
android:layout_width="100dp"
...
this is Setting layout that launch dialog preference:
<com.example.test.TimerForNoti
android:key = "pref_sync_noti_timer"
android:title = "#string/pref_sync_noti_timer"
android:summary = "#string/pref_sync_noti_timer_summ"
android:dialogMessage = "#string/pref_sync_noti_timer">
</com.example.test.TimerForNoti>
and this is class of dialog preference (TimerForNoti) :
public class TimerForNoti extends DialogPreference
{
public TimerForNoti(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.time_picker);
setPositiveButtonText(R.string.ok);
setNegativeButtonText(R.string.cancel);
Dialog di = new Dialog(context);
di.setContentView(R.layout.time_picker);
OnClickListener test = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String tag = v.getTag().toString();
if(tag == "1"){
Log.v("onOne", "ok");
}
// .....
}
};
Button btn1 = (Button) di.findViewById(R.id.button1);
btn1.setOnClickListener(test);
}
}
If I understand your question right, you can set tag for each button and set one OnClickListener for all of them:
<Button
android:id="#+id/button1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button5"
android:layout_alignBottom="#+id/button5"
android:layout_alignLeft="#+id/button3"
android:layout_centerInParent="true"
android:text="6"
android:textStyle="bold"
android:textSize="20dp"
android:tag="6"
android:background="#android:color/transparent" />
Code:
OnClickListener buttonsListener = new OnClickListener() {
#Override
public void onClick(View v) {
String buttonText = v.getTag().toString();
// your logic here
}
};
Button btn1 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(buttonsListener);
Button btn2 = (Button)findViewById(R.id.button2);
btn2.setOnClickListener(buttonsListener);
Button btn3 = (Button)findViewById(R.id.button3);
btn3.setOnClickListener(buttonsListener);
// your buttons here
Also you can use v.getId() in OnClickListener for determining which button was clicked
As much I understood your question. You want one method to be called on every button clicked.
For that Add this to your every button element in xml:
<Button
android:onClick="OnClick"
.. />
And this method to your .java file where you want to perform task:
public void OnClick(View v){
// do something....
}
Or you can directly call setOnClickListener at button object
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// do something....
}
});
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 an image view, i want to to create two buttons which will enable users to go to next or previous image. When a user taps the image , the buttons should appear and on the second tap , the button should disappear. How to go about doing this ?
Fortunately currently I am working on the same thing and here is my code modify it to suit your needs
xml code
<?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="fill_parent"
android:layout_height="fill_parent" />
<LinearLayout
android:id="#+id/button_holder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:visibility="gone">
<Button
android:id="#+id/buy"
android:text="Buy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="#+id/cancel"
android:text="Cancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
Main code
Button mBuy = (Button) findViewById(R.id.buy);
mBuy.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
}
});
Button mCancel = (Button) findViewById(R.id.cancel);
mCancel.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
finish();
}
});
ImageView mView = (ImageView) findViewById(R.id.image);
mView.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
LinearLayout mLayout = (LinearLayout) findViewById(R.id.button_holder);
if(!isVisible)
{
isVisible = true;
mLayout.setVisibility(View.VISIBLE);
}
else
{
isVisible = false;
mLayout.setVisibility(View.GONE);
}
}
});