This is what is working correctly
>>The user swipes on a part of the main activity ui we will call the MainTitleBar.
>>This creates a dialog with stuff in it
=----------------------------------------------------------------------------=
Now I need to make it so when someone clicks on the buttons or imageviews within that dialog STUFF happens. I cannot figure out for the life of me how to handle this.
Code I have currently inside the maintitlebar.java class (which extends view) and is the view they are swiping on to create the dialog box.
--- bunch of stuff like ui stuff and swipe calculations etc
public void onDownSwipe() {
// custom dialog object created
Dialog ntd = new Dialog(getContext(),R.style.lightbox_dialog);
ntd.setContentView(R.layout.create_task_dialog);
ntd.show();
ImageView cancelbutton = (ImageView) ntd.findViewById(R.id.cancelbutton);
cancelbutton.setOnClickListener(new OnclickListener(){
public void onClick(View v) {
ntd.dismiss();
}
}
);
In the xml for the dialog, the create_task_dialog is the overall xml for the dialog.
the cancelbutton (id) is the id for the cancel button (which is properly added with + sign into resource file).
Should I be putting the onclicklistener and clicky stuff somewhere else in my code?
So confused as to how to make my dialog buttons/imageviews clickable.
THANKS!
Related
Is it possible when any users clicks on button which I created on Application, and code some numbers like *123#. When click happens then it automatically execute this without showing dialpad to User and show its responce in popup like we get.
May be it little bit confusing because I do not know how to clearly explain it. If any person know how to do that in Android Studio.
You can add this a method in your button listener dialToNumberStr("0123456789")
public void dialToNumberStr(String numberToDial){
//String numberToDial = "0123456789"
Intent dialIntent = new Intent(Intent.ACTION_DIAL);
dialIntent.setData(Uri.parse("tel:".concat(numberToDial)));
startActivity(dialIntent);
}
your listener should be (using Lambda Expression):
myButton.setOnClickListener(v->{
dialToNumberStr("0123456789");
});
your listener should be (or the old way):
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialToNumberStr("0123456789");
}
});
Hi im a begginer on Android development. I knew basic core java. So i have a question on this particular code:
button.setOnClickListener(new View.OnClickListener()
{
#Override public void onClick(View v)
{
// do something when the button is clicked
}
});
I know that this is anonymous class and OnClickListener is an interface. But what i dont understand is the onClick(View v) method, v is the button that was clicked but under the hood how was this method AUTOMATICALLY executed? I mean isnt that to be able to call a method you must first create an object then a method beside it? I just need to understand this concept, thank you.
In simple words when you create a Button object it has some listener objects:
Example:
class Button extends View{
private OnClickListener clickListener;
public void setOnClickListener(OnClickListener clickListener){
this.clickListener = clickListener;
}
}
when you call this:
button.setOnClickListener();
basically you assign the value to clickListener in Button class and then each time you click the button it triggers
clickListener.onClick(this)
and perform your defined stuff.
Your listener is provided to the Button object, and by clicking the button, the Android framework will try to invoke the OnClickListener (if any) by calling the onClick method you provide.
So it is not really automatically. Your action triggers the click, and Android framework calls your onClick.
I am following a youtube tutorial and I've got most of the works done, but I still got some problems.
I have my custom layout for my custom dialog, all I wanted to do is to set the custom dialog on a button. Once we click the button ,the dialog shows, that's it. I've already set the onclicklistener on the button, here's my code.
Credit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Dialog credit = new Dialog(Main.this);
credit.setContentView(R.layout.creditdialog);
credit.setTitle(" ");
credit.show();
}
});
I followed all of this on a tutorial, but I don't know that the "MAIN" is about, I got an error there. Please tell me what to do. Sorry for my poor English.
new Dialog(Main.this);
The above line creates a new dialog object and associates it with the context of your Activity. SO you have to pass the context of your activity in the paranthesis..
Eg:
If you are calling the dialog from Activity "ActivityMain".. then use:
new Dialog(ActivityMain.this);
try this
Credit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Dialog credit = new Dialog(getApplicationContext());
credit.setContentView(R.layout.creditdialog);
credit.setTitle(" ");
credit.show();
}
});
it is common question! but you can extend dialog box and can set custom layout for your dialog than on button click just call dialog like this:
new CustomDialog(activity).show();
also you can follow this tutorial on custom dialog to understand how to customise dialog and how to use it on button click.
http://www.shaikhhamadali.blogspot.com/2013/09/types-of-dialogbox-part-two-custom.html
I have a listActivity with many items.
For each item, I want to open the same popup while sending an item id\position\other
info object unique to that item.
But basically all the time I open the exact same popup.
Its buttons will send the extra unique data to the server.
I have read few tutorials, and saw a dialog is usually opened like this:
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
I think it's more readable to create a different file to the dialog.
like this:
public class SocialActionsDialog extends Dialog {
public SocialActionsDialog(Context context) {
super(context);
mContext = context;
}
Context mContext;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.social_actions);
setTitle("Title...");
}
but then I get a syntax error on the OnCreate.
Is it common and good practice to create new file? and if so- how to do it properly?
is it more efficient somehow - just showing the same dialog instead of initializing a new one each time? or is both ways the same?
For the syntax error, make sure you are importing the R class from your app and not the Android SDK class R resources.
For an experienced programmer your instinct is to move the dialog to a separate file, especially if it contains a lot of code. And that can be the right decision at times.
However, it is often convenient to have the dialog as an embedded class or to just create it inline (as in your example) because then it is within the scope of your activity and can access your state variables and protected methods on the Activity itself.
So you end up passing all this information to the dialog if you have it in a separate file, and at some point it just isn't worth it because the code becomes more complex and less maintainable.
my app have action bar on top of windows. Where are some buttons. Buttons count and there functions is changing depending on activity user are.
I want to write a class with methods addFirstButton, removeFirstButton and so on.
So i other classes i want to do this:
MyButtons myButtons = new MyButtons();
myButtons.addFirstButton();
So there is everything alright, but how to create a listener button if i want to do this ?
Normally i would do this:
Button backButton = (Button) customNav.findViewById(R.id.back);
backButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(Action_Bar_TestingActivity.this, "BACK", Toast.LENGTH_SHORT).show();
}
});
But i want that this would be in MyButtons class and method somehow would return a listener to that action.
So any ideas if this possible ?
Thanks.
If you're programming an Action Bar, then you can handle its "buttons" in onOptionsItemSelected(). For more information, see here: http://developer.android.com/guide/topics/ui/menus.html
If you are supporting Android 1.6-2.x, you can make a copy of the ActionBarCompat sample app. It will use some of the same XML flags as >=3.x ActionBar, but not all functionality is emulated. You may also consider using Action Bar Sherlock.
If you want to set and get your onClickListeners, you can. Nothing says you have to instantiate the click listener inside the button. But you'll have to do some bookkeeping. At the least, instantiate the listener outside your button array and pass it in.
Here's how I make a standalone click listener:
Button.OnClickListener mTakePicOnClickListener =
new Button.OnClickListener() {
public void onClick(View v) {
dispatchTakePictureIntent(ACTION_TAKE_PHOTO_B);
}
};
And here's where I attach it to a button (trivial example):
private void setBtnListener(
Button btn,
Button.OnClickListener onClickListener ) {
btn.setOnClickListener(onClickListener);
}
(If you want to see what this function really looks like, it's part of the Capturing Photos sample app.)
But I think you can see how you could use this function internal to MyButtons.
Or the hard way to code:
final Button backButton = null;
final LinearLayout navBar = (LinearLayout) customNav.findViewById(R.id.root);
Button addButton = (Button) customNav.findViewById(R.id.add_button);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
if (backButton == null)
{
backButton = new Button(this);
backButton.setText("Back");
backButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Toast.makeText(Action_Bar_TestingActivity.this, "BACK", Toast.LENGTH_SHORT).show();
}
});
navBar.addView(backButton);
addButton.setText("Remove Back button");
}
{
navBar.removeView(backButton);
backButton = null;
addButton.setText("Add Back button");
}
}
});