Home > Enterprise >  How to add a function for the button in android studio?
How to add a function for the button in android studio?

Time:11-25

I am beginner in android development, i am developing one conference room meeting app. I have a doubt in that. There is two buttons with activity(kind of open and close button). If i click the first button means it needs to show that the meeting is going on and also needs to update in the main server . The second button, if i click that means it needs to show that the meeting is ended and also needs to update in the server also. So that, other people can able to book the meeting room in future Please help me in this doubt.

CodePudding user response:

I guess what you mean to ask is how to check whether an activity is running or not. If that's indeed the case a simple solution would be using a static variable that is updated as true or 1 within the onStart() function and set to 0 or false within the onStop() function..

(For more detailed solutions I advise you to look separately for the solution for every simple/smallest step/functionality that your goal program is composed of.)

CodePudding user response:

It seems like you're asking two different questions here.

The first one, about the button:

You can create a layout and add the button within your layout file like this

<Button
    android:id="@ id/yourIDhere"
    android:text="@string/yourbuttonlabel" />

You can use a hardcoded string for "yourbuttonlabel" but it is not recommended if you intend to localize your application to different languages.

You can also create a button in code by using something like this

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    RelativeLayout layout = new RelativeLayout(this);
    layout.setLayoutParams(params);


    Button button = new Button(this);
    layout.addView(button);

And to make something happen when the button is clicked you can use this

//if your button is in xml, you can call this after your layout is inflated
Button button = findViewById(R.id.yourIDhere);

//otherwise if your button is in code you should already have a reference to it
//make sure to call setOnClickListener() only AFTER adding your button to a layout

button.setOnClickListener(v-> {
    //do something
});

The second question you seem to be asking is how to update the server, which is a big enough question that it should be asked in a separate question.

CodePudding user response:

Initially inside onCreate ,

        Button btn_goingON = findViewById(R.id.btn_goingON);
        Button btn_update = findViewById(R.id.btn_update);

Then

btn_goingON.setOnClickListener(view -> {
        // Do something
        // Set your requirement here 
    });
btn_update.setOnClickListener(view -> {
        // Do something
        // call your update function in here
    });
  • Related