I have an alarm symbol on my interface. I want it like if anyone clicks on this then he/she can get another interface.
so how to write a click event of Imageview. plz give me answer. Thank you
You can add an OnClickListener to ImageView using setOnClickListener method on your ImageView and write your code in the listener's onClick method.
imageView.setOnClickListener(clickListener);
OnClickListener clickListener = new OnClickListener() {
public void onClick(View v) {
if (v.equals(imageView)) {
// Write your awesome code here
}
}
};
Faster.
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Your code.
}
});
with lambda expression
img_drawer_open_icon.setOnClickListener(v -> Home.drawerLayout.openDrawer(GravityCompat.START));
Also just do it in some easy steps visually:
1- Click your Image
2- From Attribute, Select OnClick and Type a Name Such as onCkick_btnST
3- In code of Your xml, find and select the above verb
4- using yellow bulb menu, select "Create OnClick Event Handler"
5- Fill your Code in created area.
The benefit of this methode is that U`LL see that event title in structure tab of Android Studio!
Related
I'm trying to do something like this:
When I go to this activity I have what is in black and some objects like EditText boxes.
Once I press the button I want those EditBoxes an other stuff that is up there to stay visible but unable to be edited (that's easy to do from code overriding onClick).
But at the same time I also want to load some layout down inside the same activity (from an xml) and change the button function to act over the objects of the new layout.
Could anyone give me an idea on how to do this two things staying in the same activity?
Update:
public void createButton(){
create_button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
editText1.setEnabled(false);
editText2.setEnabled(false);
hidden_layout.setVisibility(View.VISIBLE);
create_button.setText("New text");
}
});
}
On the first click I want the button to do that. But once it's pressed I want it to do another thing. How could I do that?
(that's easy to do from code overriding onClick).
Actually I would recommend enable or disable which is easier to trace by using
view.setEnabled(bool);
as for the other question I'd recommend adding the layout from the start with setting visibility to GONE and when needed set the visibility to VISIBLE
view.setVisibility(View.VISIBLE);
view.setVisibility(View.GONE);
Ok, I've realized it was a dumb question, just add a flag an edit it:
public void createButton(){
create_button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!button_pressed) {
editText1.setEnabled(false);
edittext2.setEnabled(false);
hidden_layout.setVisibility(View.VISIBLE);
create_button.setText("New text");
button_pressed=true;
}
else{
create_button.setText("Second click");
create_button.setEnabled(false);
}
}
});
}
}
I have just started to learn Android Application Development from thenewboston.com tutorials.
I had a confusion with setting the onClickListener event handler. When setting it for a button in the main activity, they used the Button class.
redButton.setOnClickListener(
new Button.onClickListener(){
public void onClick(View v){
// Do Something
}
}
);
But when setting it for a fragment they used the view class.
redButton.setOnClickListener(
new View.onClickListener(){
public void onClick(View v){
// Do Something
}
}
);
What is the difference between the two ? And when to use them ?
Please Help !!
In Button also, you can use View also as you are using in fragment.
Actually both works identically.It is just the way you want to go for better solution than other.
For more details you could refer:
setOnclickListener vs OnClickListener vs View.OnClickListener
Difference between specifying the view.onclicklistener and just having onclicklistener
Button.onClickListener() could only be used for Button. But View.onClickListener() could be used for any view. So what is the right way of doing it?
Well, what you have currently done is called 'setting an anonymous listener`. That's bad. More info here: https://softwareengineering.stackexchange.com/a/110113
The best way to do is let your Activity or Fragment implement the View.onClickListener() and the override the onClick() method.
Button is derived class of View, so onClickListener of Button is overridden from onClickListener for View.
And there is no difference between two implementations.
In the documentation for button example uses view.onClickListener
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
so that seems the recomanded way.
I'm a beginner in Android programming.
How do you execute a task when a button is clicked? I have heard you can use the onclick method but I'm not quite sure how to use it.
i think this will be useful:
Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Put some code here for response to button click
}
});
Write your code in the Activity's class as a function:
public void myFunction(View v){
//all codes here for the click
}
Then in the design, select the Button, and in the property list look for the property "onclick". In the dropdown you will find your method listed there, choose it.
This will link you button click to the function being triggered.
By default, there is a checkedbox in CheckedTextView, but now I want to replace it by an button.
I have tried to setCheckMarkDrawable which does exactly what I want it look like.
But now the problem is how can I add a listener to this. I want this button to remove current item in the ListView. How can I achieve this?
You could set your button by calling a ressource id like that:
setCheckMarkDrawable (R.id.myListButton);
Then set your listener on that button with:
(Button)findViewById(R.id.myListButton)
.setOnclickListener(new onClickListener()
{
public void onClick(View v)
{
//Do somethting here
}
});
I want to change the image of a button in my code. I found this can be done in xml:
please check this link
but the image will not stay on after I released the button. I want to click the button
and the button change to a new image. Can this be done?
in onClick method you need to change the Button's image, this way..
public void onClick(View v) {
if(v==buttonName){
buttonName.setBackgroundResource(R.drawable.imageName);
}
}
Assuming an ImageButton...
You can change the background image in an onClick listener, similar to the following:
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//set button image
myButton.setImageBitmap(myBitmapFile)
}
});