I am trying to create an android app that has multiple "pages" ,
for example : you can choose any city in the world (e.g : Rome,Berlin,New york..), and by click on it you move to the specific city activity and get specific information, such as: country, nubmer of people.. etc (All the categories are the same, and the information changes for each city )
the specific information should be stored on mySQL database
I would like to ask - how can i implement this on an android application?
thanks!
You have to open a new Activity (CityActivity) adding the selected city to the Intent:
Intent cityIntent = new Intent(this, CityActivity.class);
cityIntent.putExtra("city", selectedCity);
startActivity(cityIntent);
Then in City activity you have to get the value that you sent via Intent:
String city = getIntent().getStringExtra("city");
Now you are able to get the information of the corresponding city and draw the information in a common view.
Use Fragment instead. just create a dynamic fragment that runs on one activity. its can handle almost anything an activity can handle.
A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).
Click Here on how to create Fragment
Related
Let's say I have an Arraylist named PersonList stored in Firebase FireStore. Every Person has a bunch of attributes (name, contact number, email, etc.).
I have RecyclerView displaying each person's name. That RecyclerView is stored in Fragment A. When I click on an Item in Recycler View, I use Fragment Manager to replace Fragment A with Fragment B. Fragment B lets me view all of that person's data (not just his name), plus maybe add or edit some data as well. Currently, I am able to get that person's name from Fragment A and pass it off as an argument and display it in Fragment B.
Now I plan to get the position of that Person Object onClick in Fragment A, call another instance of PersonList and use position to get all of a particular person's data (not just his name) displayed in Fragment B. I also want to be able to edit that data in Fragment B and have the changes reflected in my Firestore. My question is this: should I keep on replacing Fragment A with Fragment B? Or should I use intent instead? I am new to Java and AndroidStudio and am just feeling my way through, any other insights or opinions with my plan are welcome.
To answer your questions:
My question is this: should I keep on replacing Fragment A with Fragment B?
That's a way to go from one fragment to another. However, a more convenient way would be to use navigation between fragments, which is a component of Android Architecture Components.
If you understand Kotlin, here is a useful article where you can see how to interact with Firestore:
How to make a clean architecture Android app, using MVVM, Firestore, and Jetpack Compose?
Or should I use intent instead?
Intents are used for navigation between activities, not fragments.
I want to make a recipes app, the basic layout is the same for all the recipes the only thing that changes are the images, times and ingredients.
The problem is, I could make 40 activities, one for each recipe and performance wouldn't be a problem because the user is only interacting with one activity at the time. However, writing the same code and going on a copy paste spree feels wrong.
I would have to repeat the same code over 40 activities and it would work (I guess), but it would be much easier to create one activity with the functionalities I want like a timer and the layout and in some way make smaller files that insert the data for the selected recipe in that "pre-made template".
There's must be a way of doing it, although I'm not experienced enough
Here is an example layout
It is usually good practice to have a base activity that implements all the code common to several activities then those activities can simply inherit from the base activity like this public class ChildActivity extends BaseActivity.
This will allow you to call methods that are in the BaseActivity from any of the child activities. You can have a read up on Java Inheritance here and here is a blog post with some examples of using a base activity.
You can create only one activity that will receive the Receipt data as extra using Intent. The layout for this activity should contain an image view(or a recycler view to hold all your images), a recyclerview to show your steps/ingredients and a textview for the time.
Receiving these data from the activity(that one that the user selected which receipt he wants too check) that created this new activity, all you need to do is to setup your layout with this data.
Check this question to get how to pass data between activities
Click here to see how to create recycler views.
I have two different activities :
one having department list and another activity having employee list.
I want to add search functionality to my app such that it can search data for all activities from a single activity.
Please suggest if it is possible and how ?
For code you can refer How to pass data collected in arraylist from json asset file in one activity having recyclerview to another activity again having recycler view?
That means you need to search data of all Activity in single Activity.
So you need to add include conman SerchView in xml file of all Activities. Also Implement BaseActivity that holds search Implementation for all of your Acclivities.
I'm developing an Android app and for this I want to have a fragment with which the user can insert an amount. The fragment has a couple methods, like inserting the correct currency symbol on the basis of the country.
I now want the user to be able to insert several amounts on the same screen. As far as I understand I can reuse one fragment several times for this. Every time the fragment is used in the main xml it gets an id, and every fragment contains a couple EditTexts which each have an id as well.
I now wonder how I can get the value of a certain EditText within a certain fragment. So lets say I want to get the result of edit_text_2 from within fragment_3 (both are their respective id's). How would I do this?
You can get result from fragment via callback. Please check following documents:
http://developer.android.com/training/basics/fragments/communicating.html
Check these links out:
http://developer.android.com/training/basics/fragments/communicating.html
http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity
Fragments are made to be self-contained, so that they don't know what is outside of them, and their containing activity doesn't necessarily know what is inside of them. You need to create an interface for the Activity to call to get the reference to your EditText.
I have developed one application in which i need to register a user in SIP account as well as Chat account. To register the user i need pass through three classes: setting.java (sharedpreferences), sipchat.java (registeration to SIP account), xmppclient.java (to register in chat).
Now i am calling settings.class to register user in application in settings.java I am calling siochat.java and in sipchat.java i am calling xmppclient.java.
This is the way the user registers in application:
code:
Intent i = new Intent(Welcome_screen.this, Settings.class);
startActivity(i);
finish();
When using this application, it takes lots of time to register and it blinks as it passes through different activities.
So how do i call all three of these classes in a single activity? (Because it very weird that at the main screen the application blinks thrice.)
Thanks
I give you some solutions:
Use startActivityForResult(), pass through 3 activities and handle the result in your main activty.Example: http://rahulonblog.blogspot.com/2010/05/android-startactivityforresult-example.html
Change the content view of one activity. In this case, we have 3 views. Example: How to use view flipper with three layouts?
I don't understand why you need to start all these activities. Isn't it possible to just call static functions in the target activities?
Otherwise could you further explain why it's crucial that you start all these different activities and not just handle the functions in one activity?