Can I add a button onClick handler via Andriod Studio GUI? - android

In Android Studio (3.5.3), is it possible to add a button onClick handler via the GUI?
When the onClick dropdown (red box) is clicked, there are no options. When I typed in something manually (as in the picture) no code was generated.
I can add a button handler to my activity in the code, which works fine, but nothing is populated in the "Common Attributes" on this screen.

You have multiple options, but in any one you have to do some effort:
First: you can type anything in onClick attribute and press Enter then will place below in your layout
<Button
android:onClick="onClick"
Your effort: you've either to
Switch to the Text tab of your layout, press ALT + ENTER on the
warning on "onClick" attribute, and then choose Create onClick
Handler, choose the activity and hit OK.
Switch to your activity behavior class and add below method.
public void onClick(View view){
}
Second: you can reverse the order of the first method, so you can first create your click handler callbacks in your activity, and then choose them for buttons on the design tab, and here you can use the drop-down menu of the onClick().
Here I created a couple of callbacks
public class MainActivity extends AppCompatActivity {
...
public void onClick(View view) {
}
public void onClick2(View view) {
}
}
And then you can choose any from the designer tab onClick attribute

Related

Adding functionality to onClick in Android

I want to run a function each time a button is pressed in my app. I know I can make a new button which extends Android's button and override onClick() but I have already made my entire app and want this new functionality as an afterthought. Can I somehow add this functionality without changing the class of all buttons in my app?
Lets say I want to add something like
runthisfunction();
before every onClick() code executes. There are many buttons in various activities and so they have their own onClick functions.
Is there a way that this function is run before each and every onClick() without the need to make a new class and changing all the existing buttons.
Add to your xml resource for whatever button/view you want to have an onClick listener:
android:onClick="fncClick"
For example:
<Button
android:id="#+id/btnClicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="fncClick" />
Then in the activity that utilizes that xml resource, include:
public void fncClick(View view) {
//TODO: Code here
}
This ONLY attaches an onClick listener for this specific button - not all buttons

View OnClickListner at Application level Android

I am developing an android application where I stuck at some point. The problem is that I want to track the last action performed in app by tracking onClickListner. So is there any way to set OnclickListner for whole application and then track the last event time. Please Suggest me the way to do that. My assumption is to have a class which extends View class and this class should implement onClickListner. Then all of my buttons should set OnClickListner to Object of this class. but my application have more than 100 buttons so it will increase the complexity of the class. one more problem is that all buttons are performing their activity specific operations.doing all operations from one class will increase complexity.
I am looking to capture onclicklistner throughout the app and then log the event time then transfer the event to the activity where onclickListner was implemented.
An idea might be to create your own OnClickListener, say
class LoggingOnClickListener implements OnClickListener {
public void onClick(View view) {
doLog();
}
}
Now you just have to add super.onClick(view) to add logging to the Button clicks
button.setOnClickListener(new LoggingOnClickListener() {
public void onClick(Button button) {
//handle the click
super.onClick(button);
}
}
The code might very well be flawed as I do not have my IDE open, but should just show the general idea.

Click Listener in Android button isn't working

I'm with some really strange trouble with my android click listener button! I've already done that several times, I'm getting crazy not founding an solution (neither an logical explanation) for this error.
error
The event handler for 2 buttons on my activity are not being executed. There is no error, it just not performance the handler action at runtime. This is the code for one of the buttons:
btnNext = (Button) findViewById(R.listclient.btnnext);
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(MyActivityClassName.this, "Flag 01", 1).show();
btnNext.setText("CLICKED!");
}
});
And that's the button on xml layout:
<Button android:id="#+listclient/btnnext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
style="#style/Widget.TextViewInfo"
android:text="Next"
/>
info
There is also an ImageButton inside this activity, this imageButton works perfectly with an inner OnClickListener class (just like this one).
I have already tried to make my activity class inherit OnClickListener and set it as the click listener for the button with no success.
I have also created an class inside my Activity class, and set it as the button click listener, no success too.
I'm compiling for Android 2.1 + Google API (SDK 7)
------------EDITED-----------------
If I put in my code:
btnNext.performClick();
It's executed! I'm getting even crazy right now!
And the button is in fact clicked when I touch it, I can see the button "animation", and the click is logged in LogCat.
You can't use listclient when specifying or using an id. The first part is the type of the resource, which has to be id in your case.
Change android:id="#+listclient/btnnext" to android:id="#+id/btnnext". Also adjust your code:
btnNext = (Button) findViewById(R.id.btnnext);
At a quick glance over you code, I noticed when you don't call findViewById correctly. Change the id of your Button to "test" then try: findViewById(R.id.test). Make sense?

Show Menu Option Automatically At First Activity Of Application

I develop a application
and in which i have a Menu option which i invoke from onCreateOptionMenu()
But this is called only when any user press the menu button
so now i want that my application start and first Activity is Welcome.java
then in onCreate(Bundle b)
can i write sone line from which the menu is invoked automatically without pressing Menu button
i used openOptionMenu() but it not works.
2) can i create a Button and simulate it as Menu button and then write button.performClick() so it act as a Menu Button and menu option will visible
So give me some suggestion on this
Thanks
You can request the menu be opened with an Activity method
openOptionsMenu();
If you want to show a menu immediately, you'll have to wait for the window focus to change, rather than using onResume:
#Override
public void onWindowFocusChanged(boolean hasFocusFlag) {
super.onWindowFocusChanged(hasFocusFlag);
if (hasFocusFlag) {
openOptionsMenu();
}
}
See openOptionsMenu()
Hi I will like to provide answer for your question
(" can i create a Button and simulate it as Menu button and then write button.performClick() so it act as a Menu Button and menu option will visible")
Answer:
Step 1-Create a button/ image button in your layout
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/a"
android:onClick="expand"
android:src="#drawable/button" />
Here I have set onClick method as "expand"
Step 2-Now in your MainActivity.java class define your "expand" method which will be call once user click on your button
public void expand(View v)
{
ImageButton imgButton=(ImageButton)findViewById(R.id.imageButton1) ;
imgButton.setVisibility(View.GONE);
openOptionsMenu();
}
In this code I have set visibility as "gone", as I want the button to disappear once menu is shown
Step 3-*(In case you are setting visibility for the button)* You can also write code to set visibility as "visible" once menu is closed using below method
public void onOptionsMenuClosed(Menu menu) {
super.onOptionsMenuClosed(menu);
ImageButton imgButton=(ImageButton)findViewById(R.id.imageButton1) ;
imgButton.setVisibility(View.VISIBLE);
}
Hope this will help you.....

how to change the view of a button onClick in android

In my app I am trying to calculate an operation using timer. For controlling those operations I am using four buttons as Start, Stop, Pause and resume.
But I want to show only 2 buttons. At the beginning I have only two buttons Start and Pause.
When the start button is clicked timer gets started and immediately in Start button's place I want to show the Stop button.
I want to do the same for the other stop and pause buttons. How to do this please help me......
Using ToggleButton is a good solution for you. Do something like:
ToggleButton first = new ToggleButton(getContext());
ToggleButton second = new ToggleButton(getContext());
first.setTextOff("start");
first.setTextOn("stop");
second.setTextOff("pause");
second.setTextOn("resume");
and use setOnCheckedChangeListener() to implement your actions.
In your onClick(View v), v is the button that gets clicked. You can cast it like:
Button b = (Button) v;
so you can change its text with setText(), and set another listener. You can declare the alternate listeners once as members of the activity, and set them without re-declaring them each time.
Your application needs to maintain states, such as "Idle/Stopped", "In Progress", "Paused", etc. If you want to hide buttons, you can use View.setVisibility, and dynamically show and hide the buttons when your state changes (when other buttons are pressed). You would need to set your layout appropriately so that the buttons display nicely as they are shown/hidden dynamically
Or, you can change the text of the buttons, and their associated click listeners dynamically. This method is not very ideal becuase you may run in to cases where you want different amount of buttons for all your different states, and also, you're associating variable behavior with a single control. Also, you must manage your click listeners, adding and removing them dynamically.
here is a simple implementation
public class Demo extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
button.setText("stop");
}
});
}
}
In the main.xml have a Button widget like this,
<Button android:id="#+id/button"
android:text="start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

Categories

Resources