How to call a button from another activity in Android? - java

I'm new to android/java and I'm not sure how to solve the following problem:
Basically, I am creating a fitness tracker app and I need to be able to add details about an exercise (a text view and a couple text input fields) to my 'activity_weights_main' when the relevant exercise button is clicked in my 'activity_exercises'page.
At the minute I am trying to call the exercise buttons from 'activity_exercises' in my 'WeightsMain' java class via the code below:
btnCableCrunches = (Button) findViewById(R.id.btnCableCrunches);
After I'm able to call the 'btnCableCrunches' in my 'WeightsMain' java class, I'm planning on creating a method which will add the textviews/input fields that I need. I'm aware this is probably a stupid question but I've been trying to research how to do this for quite a while now with no success.
So I guess my question is: How do I call this button from another activity (activity_exercises) so that I can create a method (in 'WeightsMain.java') to add textviews/input fields (in 'activity_weights_main').
I don't need to know how to create the onClick method, just need to know how to call the button from other activity so I can do so myself. Thanks in advance for any feedback!

Actually you are looking for " Callback Implementation".
You can follow this example for assistance.

Related

How to know which button called certain activity?

For example, I have activityMain which contains the main logic of the Application, but it should be opened from several buttons that stored in other activities, and some activities contains multiple buttons too. And they should call activityMain. So i need to pass different data from db depending on which button called activityMain
Help please, there weren't similar topics at all.
If i Understand your question correctly, you can achieve this functionality with android jetpack navigation component. I add the link of documentation below
Get started with the Navigation component

How do ListActivities work with ListViews in Android?

As a C# developer I'm trying to get familiar with Java and more specifically, the Android framework.
I've created some very basic intro Activities, but I'm now trying trying to get a ListView up and running with 'databinding'. I found this sample code: http://developer.android.com/guide/topics/ui/layout/listview.html
I don't think I've fully grasped the concept of these views yet, because initially I figured that the ListActivity class that I created would be built as some sort of a user-control that I could simply replace my ListView with. When I look at the code though, it appears as though it's somehow trying to hook into an existing view which confuses me. (How do I call this ListActivity from my current activity, and how do I use it with my ListView?).
getListView().setEmptyView(progressBar);
Could someone please clarify what's going on? Perhaps I've become too .NET paradigm orientated and I'm not understanding the picture properly.
A ListActivity is an activity that assumes that you will set a layout with a list view in it with a certain id. It will then store that view in a variable so getListVew() will return it. Basically its a tiny bit of syntactic sugar to do what you can do yourself with 3 lines of code.
Really I don't think its worth using. You can use listviews in a regular activity, it takes 1-3 lines of code to do everything the ListActivity does, and you don't have problems if you name your listview with a different id or if you even decide to not have a listview down the road.

add a new button from another fragment class

My problem is that I have two classes that extend Fragment.Now I have a button(its name is save) in 1 fragment class.I want to add a new button in another fragment class when 'save' button is clicked.I know I need to have an onClickListener for the 'save' button but I don't know how to go further from there.I also want an onClickListener for the new created button.
Any help would be much appreciated.
use interface to communicate from one fragment to another.
follow the below link. You will find out something:
onItemClickListener between two fragments
There are a number of ways to do this, depending on the relationships between fragments, whether they are nested etc.
1) Use SharedPreferences. That means you would write to the apps defaultSharedPreferences some flag which says "save has been pressed", and then in the other fragment any time you call createView you would check this flag in preferences. IF save has been pressed you would then show the button.
This approach has a few issues though depending on how long you want to show this button for, if it should be shown forever etc.
2) The interface approach mentioned is valid, but it has coupling issues, and may not be suited to the framework you have in place.
3) Broadcasts - you can use intents and send messages between fragments. This runs into some vaugeness issues (You need to be careful when documenting broadcasts and intents) and can be somewhat opaque to other readers.

Why do i have to cast a Button?

This must be a really dumb question because I cant find an answer online.... I know that casting is changing one datatype to another. How is this button ever changing it's data dype? Button button = (Button)findViewById(R.Bla.Bla) Why cant we just write Button button = New Button() And then assign the xml to it another way? Please explain, I'm lost.
You can set a Button to a new button.
But findViewById returns a view. If you want to access any of its Buttonosity, you must cast, otherwise the reference isn't a button. There are times that may be okay, of course.
See In Android You can create the UI Elements in two ways:
1. create UI elements through layouts (.xml) files.
And to use them in java class map them to their corresponding class.
And to do so we have to call method findViewById(int id); which returns the view of that perticuler element with given id.and thus we have to type cast it to respective component.
And thus if you have created a element already in xml why will you create a different object again at java end. so just map the element created with xml file.
2. crate UI elements through java end.
To use this feature use have to create the elements in java with new keywords ex. Button button = new Button(); and then set the all properties on that object.
But But But,
According to android philosophy you should create UI in xml, and write your core business logic in java end. And with this concept you can write neet and clean application code.
But it is only recommended not compulsory at all. now its up to you....
and i think at starting you feel it different but after some time you will start loving it...
Thats the beauty of android.
Thanks. i hope, i answered your question throughly.
Also, remember that Button is a subclass of View. The findViewById() method returns a generic View (any View or subclass of View that you put in a layout file). The cast to Button is saying "It's okay - I know this is a Button, not just a regular View," which allows you to access properties and methods of the Button that aren't available in the View superclass.
final Button callButton = (Button) findViewById(R.id.callButton);
I believe that when finding an XML view using findViewbyId(), it returns the view in the UI, but the returned view must be cast in order to be used as a button within the Java code, and have access to the button methods.
There are ways to create a button in the Java code without specifying it in the XML, but this practice differentiates the UI from the logic.
Plus, declaring UI elements in the XML is better because it is makes the process changing entire layouts easy through usage of of setContentView().
You have two options to create View component in android including Button
1- Define it in a layout XML file and access it using (Button) findViewById(R.id.button)
2- Create it dynamically in the code e.g. Button button = new Button();
both has their own advantages and disadvantages, for example, defining the UI in layout xml makes your Activity concise and and give you more flexibility by separating the UI from the actual code
Dynamic UI creation is useful in many applications that needs to create Views on-the-fly

Triggering a method from one class to an other using a button in Android SDK

I'm sorry if this is asked too much but I couldn't find something clear enough.
I'm developing a little game engine for android.
I've already implemented a gamepad class to control the touch screen.
The game pad class draws an analog stick if someone touches the right area and also draws to buttons on the side.
The problem is , making it simple, i have the main class GAME that as the LEVEL class and GAMEPAD class.
Inside the GAMEPAD code i can trigger things when someone pressed\releases a button.
The problem is I wan't to trigger something to the main GAME class by pushing a button witch is handled in the GAMEPAD class.
In C# i would use events and delegated.
In Java i have no clue.. i want the gpad to invoke a method to who is registered.
This is what i'm hoping to achieve on the main GAME class.
.. void onGpadClick (e EventArgument)
{
if (e.Button==Button.A&&e.ButtonState==ButtonState.Pressed) Shoot();
}
Thanks in advance
You can achieve this using Intents.
http://www.vogella.de/articles/AndroidIntent/article.html
What you could do is this:
Let your GAMEPAD-class recieve an instance GAME-class (either in its constructor or by a set-method)
In your GAME-class, you add methods containing what ever you want done for each button e.g. onButtonAClicked(), onButtonBClicked() and onPadClicked(). (If those are the options - add more if you have more options).
Detect the clicks from your GAMEPAD-class by setting a OnClickListener on each button e.g. buttonA.setOnClicklistner( new OnClickListener() { //override the onClick-method here } ) Then override the onClick-method and add your implementation there e.g. myGame.onButtonAClicked().
Hope it helps.

Categories

Resources