How to connect button to main activity in Android studio? - android

I think that my question is easy but I have no idea how to solve it.
I made a java class (test_class) and xml file for this class. In xml file I made a button and its work correctly in test_class, but I have to use it in MainActivity.
I made this button public and made object of this class, but the button is null. I also tried to make button from main activity
Button btn = (Button) findViewById(R.id.logButton)
but its also null.
How can I solve this problem?
thanks in advance!

In MainActivity.java make a public void method that takes a View parameter.
e.g.
public void buttonPressed(View view) {
// do stuff
}
In activity_main.xml - in your button node add
<Button
...
android:onClick="buttonPressed"
...
/>
And now they're wired.

Related

Navigate through different Android xml activities

I am new to android development and am using android studio in ubuntu to write a simple Android app.
Right now the main activity activity_go is an xml file which describes the first set of objects being displayed. By adding a click listener to a button object described on activities_go.xml
I am able to change the activity to another set of objects, 'two' which corresponds with two.xml
The Following is the java code I am currently using to switch between xml files
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_go);
Button nxtButton = (Button) findViewById(R.id.nextButton);
nxtButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View arg0){
setContentView(R.layout.two);
}
});
}
Upon running this I am able to display two.xml once the button is clicked.
How Can I do this again, upon clicking a button on two.xml change to three.xml ?
or is there a better method to shift between the layout xml for android applications?
I guess, you need to view trainings by Google. This article is about your case.

Android XML Graphical Layout Not Updating

When I first ran the sample HelloWorld app, it displays the hello world text on the emulator. I decided then to delete that and make a button. What I wanted is that when I click the button, it will show a text "This is the second activity". I made another XML file and another class to handle the second activity to display the text. But when I ran again, I cannot see the changes on the UI for the emulator. The text "This is the second activity" does not show after I clicked the button. I saved everything. How would I automatically update the UI of the emulator after some of the changes made on the design? I am new to android development. Please help me. Btw I cannot post images so it requires 10 reputation that's why I used online image viewing. Sorry for that.
Here is my Graphical layout on eclipse: activity_main.xml
http://s16.postimg.org/wusm4qrp1/image.png
second.xml
http://s29.postimg.org/qft17p5on/image.png
Running the emulator:
http://s28.postimg.org/f9dn32ku5/image.png
After clicking the button (in which case the text I edit does not show):
http://s16.postimg.org/75r62dodx/image.png
Because you didn't post your code, I'll try to explain it from the beginning.
I don't know if you can do it in an other way, but the following answer assumes we aim the good programming practice.
Both of the activities have their distinct layouts set in the following way, right?
public class MyFirstActivity extends Activity{
...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
...
public class MySecondActivity extends Activity{
...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
...
Then, in your first activity, define onClickListener of your button
...
setContentView(R.layout.activity_first);
Button myButton = (Button)findViewById(R.id.my_button);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MyFirstActivity.this, MySecondActivity.class);
startActivity(intent);
}
});
That being said, I don't recommend you to have two activities for this functionality. You should have different activities when you need different layouts. Putting the button and the textview in the same layout and updating textview inside the button's onclicklistener is a better solution.

A button which goes onto an Activity

I have been trying to figure this thing out with no luck in eclipse. I have created 3 screens. One being the main menu which a button leading to another button which then leads to an Activity. I can get the button from the main menu to lead onto the button onto the second screen but i can't get the second button to lead onto the third screen.
Can anybody help me?
appproject
You should really post some code so we can help you...but it seems like you need to look at your onClick() method for the second button.
Providing you have three separate Activities for each of these three screens (I'll call them ActivityA, ActivityB, and ActivityC), you'll also probably need an XML layout for each. There are ways of doing it without an XML layout, but for now just stick with that.
The next thing is you want to make sure your Button is initialized properly. ActivityB should look like:
public class ActivityB extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
Button b = new (Button) findViewById(R.id.button2);
b.setOnClickListener(new OnClickListener() {
#Override
protected void onClick() {
startActivity(new Intent(this, ActivityC.class);
}
});
}
The first thing you should do is check to make sure your button functions in a way similar to how I've described here. If that doesn't change anything, make sure you are initializing your Button in accordance to how it is defined in the layout XML. You must use an id for a Button that is in the same XML layout as the one you set in setContentView(). If not, it will do nothing, no matter what you put in the onClick() method.
I hope that helps!!

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?

Why is there no getOnClickListener in the Button class? (Android)

Why is there no getOnClickListener in the Button class? I think this is really strange considering there is a getOnFocusChangeListener function. Why make it for the FocusChangeListener and not for the ClickListener?
Added comment:
For those below that are wondering why I need this: We are developing a large application with a lot of viewgroups on the screen. I want to add some code to a button on the screen but not replace the complete OnClickListener. I want to implement a new OnClickListener that will run some code and call the old OnClickListener. But for that I need to retrieve the old one.
I don't know why there is not, but you can do what you want to do by extending the button class:
public class Button extends android.widget.Button implements OnClickListener {
public void onClick(View v) {
/* Your code here...*/
super().onClick(v);
}
}
I think it's a question to Google :D
Why do you need to get a onClickListener back? If you are so desperate, store it in a tag (Views.setTag(...));

Categories

Resources