[Q] Each view a new activity? - Android Software Development

I'm learning how to program in android and want to make a musicplayer. The problem I have now is I have no idea how to build the views for the application. For example if you start the application you have the options, all songs, artists, albums. If you then select artists you get all the albums from that artist, if you select an album you get the songs etc..
Now my problem. Do I have to make a different activity per view (all songs view, artist view, album view), because they all do almost the same, just fill a listview with some data.
Can sombody point me to a tutorial of give me a hint how to do this because I'm trying to find a solution for 2 weeks now and all 101 "noob" basic tutorial I found don't cover this.
Thanks in advance.

Nobody who can point me to the right direction?

I would create a new activity for each, but create a base class that has the listview in it. Then in each new activity, just inherit from that base class and call a method to fill in that list view with the data specific to that particular activity.
-frank
Sent from my PC36100 using XDA App

Hello!
I'm only new to android development too but I'd suggest two things. Firstly you need to make sure the music playing is being done by a service so it works in the background. Secondly, I don't think you'll need a different activity for each view. List views are populated by adapters if I remember right so maybe one view to choose the music filter and the next view will choose the correct adapter and populate the list....
Hope this helps a little.
Sent from my X10i using XDA App

@kaediil, I think I try that, I'm open for other suggestion tho.
@Keithod, the mechanics to play a song are already done, the vies are the only problem. And I tried it with adapters but how am I'm going to handle the onclick then? That would be one hell of a switch statement.
Is there a tutorial that handles these kind of problems?

If your music player created a database of the metadata, artist, album etc. Then your adapter could use the ID of the element that was clicked as the criteria for the select statement... No switch required and only one activity...
Sent from my X10i using XDA App

I now get the id3 tag from the song and build the views that way, I could dump it in a database when the application start. Let me try that.

After a couple of days I am still stuck, I prefer a tutorial or a snippet how I could fill 1 listview and when I click a specific item another listview or the same will be filled with other items.
Hope somebody can help me with this.

What went wrong with the database idea?
Sent from my X10i using XDA App

Then I still have to display it, or it's from an arraylist or database.

Try using a ViewFlipper: http://developer.android.com/intl/zh-CN/reference/android/widget/ViewFlipper.html

You don't need to have one Activity for every view, you can choose to display different info depending on the previous Screen or if the user has clicked a different button.
An example:
- An user clicks on a button that goes to PSN and recover a list of latest games played by the user with his playstation.
- You put a flag on the Bundle Extras to indicate to the next ListView activity that you want a PSN info.
- In the ListView Activity you recover info from PSN. If the user clicks on the Xbox Live button you use the same Activity to query Microsoft's website. You can even load a different XML Layout for PSN or Xbox Live in that same Activity and have 2 completely different Layouts for a single Activity:
Code:
if (xbox)
setContentView(R.layout.achievements_xbl);
else
setContentView(R.layout.trophies_psn);
Anyway, if the methods are not shared to display different info in the Activities is a good practice to have different Activities Classes, you can have one XML Layout if the List Rows are similar and use in two different Activities.
Helper Classes, to separate business code from Activities are also a great tool.

Related

[Q] Android app auto starts again when device rotated

Hello all,
I've written a simple audio player app and testing it in Samsung Galaxy S.
It is a simple app with can create playlist and some buttons to play the mp3 file.
However, there's a funny bug in it.
After starting the app, whenever I rotate the phone, the running app will launch another instance of the app. If I rotate again, another instance (3rd) would be started. So u can hear 3 copies of the same audio being played simultaneously.
What could be wrong with it?
Is there some code which can prevent program re-entry?
Thanks.
It's not a bug, that's the way andorid OS works. You need to account for this in OnCreate for your activity. I assume you are spawning off the actual playing of the MP3 in a thread or service. You need to check if the service is already running in OnCreate and attach, else spawn a new one.
Hello Gene,
How do I check if an activity is already running?
I could not find the answer on the android developer page (the topic on application fundamentals).
And no, I'm not converting it to a service yet as this is my first app.
Haven't explore service yet.
Thanks.
I read on another article, a simple way to prevent this is to add overwrite the onConfigurationChanged function
@Override
public void onConfigurationChanged(Configuration newConfig) {
//ignore orientation change
super.onConfigurationChanged(newConfig);
}
and modify the androidmanifest.xml
<activity android:name="selectCategories" android:configChanges="orientation|keyboardHidden"></activity>
But it still launch multiple instance of the app.
Thanks.
This is a good question and should be in an FAQ somewhere.
As already mentioned, changing the display orientation basically restarts your app. Read the Android dev page on Activity lifecycle for more info.
A short answer for your question is: the Bundle parameter for onCreate() will be null when your app is first run. When your app is paused and restarted, that Bundle will be non-null. You can store data in that Bundle by overriding onSaveInstanceState(), then check for that data in onCreate(). It's a good idea to learn how to do this (save/read app data on pause/restart). Once you start testing apps by rotating the display at various times, you'll find a lot of them FC at unexpected places.
This is indeed a topic that keeps surprising people who are new to android (ahem, like me 4-5 months ago ).
The solutions above are perfect, however in certain situations there's another trick that might make your life much easier. I suspect it won't help you in this case, but it might help others who tackle the problem and see this thread.
Certain applications, mainly games, should be fixed in a single orientation. I.E. you won't be playing angry birds on portrait - that game is locked to landscape, as it should be. In order to lock your activity in a certain orientation you add this attribute to the manifest under the Activity tag. So a standard activity might look something like that, combined with the ignore tag from the previous posts:
Code:
<activity android:screenOrientation="landscape" android:configChanges="orientation|keyboardHidden"
android:name="whatever">
The great thing about this combination is that your activity will not restart at all when the phone is rotated. Of course, for a standard activity you'd usually want to support both landscape and portrait, but if you need your app to be in a fixed orientation this is the way to go - no weird FCs or annoying bugs
Hi,
How do I check the bundle?
And also, what to do if the bundle is not null? Just return?
Thanks.
regards
r_p_ang said:
This is a good question and should be in an FAQ somewhere.
As already mentioned, changing the display orientation basically restarts your app. Read the Android dev page on Activity lifecycle for more info.
A short answer for your question is: the Bundle parameter for onCreate() will be null when your app is first run. When your app is paused and restarted, that Bundle will be non-null. You can store data in that Bundle by overriding onSaveInstanceState(), then check for that data in onCreate(). It's a good idea to learn how to do this (save/read app data on pause/restart). Once you start testing apps by rotating the display at various times, you'll find a lot of them FC at unexpected places.
Click to expand...
Click to collapse

[Q] ListView with huge data from backend-server

Hi,
What's the best approach to load a listview contents from a backend-server (internet)? I mean, I want to populate on-the-go a listview that will retrieve data from a internet server. For example, I want to know all orders from my e-commerce from my app, and I have like 5000 orders in the last week, I won't load this 5000 orders because my app will have a great load time and memory consumation.
My idea is load more when user get close to the last element of list, but I don't know how to do this, and about memory consumation? Is better to continue with the data in memory (when user goes back to upper), or save it on a file-cache (better than memory?) and load when user scroll back to the top?
Thanks,
While the user is the activity you must hold each of the list item in memory anything other than that will cause serious lag in your app
While at other times you may cache them to save resources
Sent from my GT-S5302 using Tapatalk 2
You definetely need ASyncTask here. You can define a class as asynctask, which lets you implement a few methods to use as a background thread. So whenever you wan to load more items you can call this background thread which does not block the UI.
Also, if I remember correctly, you can implement a method which lets you know which list item is at the top of the screen, the total number of items in the listview, and the number of items on the screen.So with this method you can find out whrn you reached the last item in the listview.
I hope this helped a little, at least you got some stuff to google
Sent from my NexusHD2 using xda app-developers app
Ya as he said use a global variable and update it in getView if adapter hence ull know the last loaded view
Sent from my GT-S5302 using Tapatalk 2

Practice App Ideas

I am new to Android App development. I need some simple application ideas to practice. please give me some ideas. I just made Rock, Paper, Scissor game. So easy games like this will be fine(I think )
Build a notepad app that lets you write and save notes. Build it steps where you increase the difficulty as you go on:
Make an app with a textbox that lets you enter text
Add a save button and learn how to execute code when the button is hit. Whenever you encounter any problems or don't know what to do, use Google! There are a lot of other running the same path as you and me who have encountered problems. The solution is almost always a search away!
Implement a save function that saves the content when the button is hit. When the user reopens the app load the text file and put the content in the textbox. Tip: Use SharedPreferences
Time to add support for multiple notes! Start off by learning how to move between activities (windows), in your notepad app add a button labeled "All notes" that sends you to another activity when clicked. Use Google to finds articles on how to do this.
Once you've figured out how to move between windows, it's time to create a list with all the notes. To display a list you use ListViews, before jumping in to showing the actual notes create a mock list containing your favorite cars, cats, Justin Bieber songs, whatever! Once you get a hang of it, move on to the next step
Learn how to store multiple strings instead of just one. Check out JSON, it's a simple data structure that lets you store.. data. Tip: store the notes in JSONObjects in a JSONArray. The final JSON-string can be stored in SharedPreferences.
Once you get a grip of JSON, add an add button, preferably in the ActionBar, that lets you add notes. Save the new note in the JSONArray.
Replace the cars or cats that are currently shown by the created lists.
VoilĂ , you have a fully functional notepad app and you've learned how to: move between activities, store data, ListViews, find solutions with Google etc. Good luck!
What are you aiming for, eventually ? Do you want to be a game developer, utility developer, are you aiming for a job developing apps ?
That might help you decide what kind of test apps to make.
If you go for a game you could spend a fair bit of time dealing with the graphical elements and gameplay without understanding the basics... so before you do that I'd suggest getting familiar with the building blocks (Activity, Intent, Service, Handler, Looper and so on) first.
jokernw said:
I am new to Android App development. I need some simple application ideas to practice. please give me some ideas. I just made Rock, Paper, Scissor game. So easy games like this will be fine(I think )
Click to expand...
Click to collapse
I started with memory game, you can go for it as well

Way to store Preferences

Hey Guys,
I am creating a app to rename other apps via xposed and the main code is written. Now I simply want to make a interface where you can define a app to rename and then enter a new name. For the beginning I thought about a Activity where you enter the Package Name and the desired name. Later on I want to use a list view to show all apps, from where you can choose one to rename(Like App Settings etc.). To show the renamed apps I want to use a ListView. Now I am stuck with a problem: My xposed code works with an array to check and rename. The list view can be used with an array or an arraylist, but I need a way to store the preferences(which are stored in a array).
Do you have a good idea or a sample how to realise this? And does somebody know a nice App List type of thing(I googled, but the project there aren't usable with my kind of approach.)
GalaxyInABox said:
Hey Guys,
I am creating a app to rename other apps via xposed and the main code is written. Now I simply want to make a interface where you can define a app to rename and then enter a new name. For the beginning I thought about a Activity where you enter the Package Name and the desired name. Later on I want to use a list view to show all apps, from where you can choose one to rename(Like App Settings etc.). To show the renamed apps I want to use a ListView. Now I am stuck with a problem: My xposed code works with an array to check and rename. The list view can be used with an array or an arraylist, but I need a way to store the preferences(which are stored in a array).
Do you have a good idea or a sample how to realise this? And does somebody know a nice App List type of thing(I googled, but the project there aren't usable with my kind of approach.)
Click to expand...
Click to collapse
Ah the data storage problem... I think we all came across this at least once . I suppose you read this guide on the different options available? Well there are actually three options: SharedPreferences, text or csv file and SQL.
The first one would need some work around and is probably the slowest. You would save a separate string directly into the SharedPreferences (maybe in a new file to avoid collisions?) with the array name and its index somehow in the key. That's just two methods of coding but not the nicest way to do it.
The text or csv file however is the more common way, here you'd save your array in one line of the file, each item separated with a ; or some other char. Needs a bit more coding and also the WRITE_EXTERNAL_STORAGE permission on preKitKat if I remember correctly.
The third one is the nicest and most modular one. Because it makes use of SQL it needs quite a bit of knowledge and some coding (but less than the text file).
I guess for simple things that you want to do it is better to stay away from SQL for now unless you know how to use it and use the SharedPreferences.
Edit: take a look at the answers to this question, they give you the code as well... And you can use StringSets in ICS and above if the order of your list doesn't matter !
I'd say go down the SQLite DB way, chances are that if you plan on expanding your coding knowledge and want to keep creating apps you'll be needing to learn this in the future anyway so why delay?
Google AndroidHive and look at their SQL tutorial - I used it when learning and found it very informative
Sent from my HTCSensation using Tapatalk
Thank you very much! As coming from windows, first of all I thought about SQL as well, but it seems oversized for the set of data i want to store. I also took a look at the stackoverflow thread you linked. It looks easy to implement, but the need to use an external class file made me look for another way. I found this one, which works pretty well for me, as I look forward to interchange the method of saving the data with a better one using the SharedPreferences(which actually should be really easy with my code).
Maybe you can tell me yet another thing: Is there a way of declaring an object(like the ArrayList) to make it accessible from every class except from giving every class(activity) it's own "load the preferences" and "save the preferences" code block or sending intents all over the place? This would make saving much easier and allow me to update the preferences during runtime and without a reboot
Edit: This was my answer to.SimplicityApks ^^ I'll take a closer look at SQL now, since you, Jonny, told me that it' nevertheless necessary.
GalaxyInABox said:
Thank you very much! As coming from windows, first of all I thought about SQL as well, but it seems oversized for the set of data i want to store. I also took a look at the stackoverflow thread you linked. It looks easy to implement, but the need to use an external class file made me look for another way. I found this one, which works pretty well for me, as I look forward to interchange the method of saving the data with a better one using the SharedPreferences(which actually should be really easy with my code).
Maybe you can tell me yet another thing: Is there a way of declaring an object(like the ArrayList) to make it accessible from every class except from giving every class(activity) it's own "load the preferences" and "save the preferences" code block or sending intents all over the place? This would make saving much easier and allow me to update the preferences during runtime and without a reboot
Edit: This was my answer to.SimplicityApks ^^ I'll take a closer look at SQL now, since you, Jonny, told me that it' nevertheless necessary.
Click to expand...
Click to collapse
Welcome
You mean you want to make your ArrayList, which is an instance variable in the activity, accessible to every other class within your package without having an instance of your activity at hands? Well the basic solution would be to make the ArrayList static. But that is not recommended because it won't be created and garbage collected at the same time as your activity and also it's not a nice way .
If you had an instance of the activity it would be just using a public getter for it, but without I'd put your ArrayList into a separate class following the Singleton pattern. That way you have only one global instance which contains the ArrayList.
Thanks again for your reply! I changed my mind about the ArrayList and created a method, where everything is stored in the SharedPreferences and the ArrayList's only purpose is the use with the ListView and Adapter. This way I don't have to write the ArrayList to the SharedPreferences and changes will be much easier to control. Although I had to implement another type of save/load method to interact with the class thats being loaded by xposed. That was needed because of the restriction that you can only load SharedPreferences with a context, which my class doesn't have. It's an inconvenient way, but it works

[Q] Andriod Quiz App - Radio Buttons

Okay, so I've been surfing the web for a few weeks/months now but I can't seem to find any decent tutorial for my query.
With high hopes, can anyone show me how to do it on a single activity with at least 10 questions each?
I'm currently developing an M-Learning application. So aside for all the reading materials and somewhat simulations,
I need to present a quiz that will ask users at least 10 - 15 items per chapter.
I'll be presenting it on a graph as well for the user's profile. (But that's another issue I'd tackle later on)
Currently, all I have now was moving from one activity to another and vice versa.
Can anyone please guide me? I'd modify this if it doesn't clarify anything.
Please take interest. Am still a student though.
iamreverie said:
Okay, so I've been surfing the web for a few weeks/months now but I can't seem to find any decent tutorial for my query.
With high hopes, can anyone show me how to do it on a single activity with at least 10 questions each?
I'm currently developing an M-Learning application. So aside for all the reading materials and somewhat simulations,
I need to present a quiz that will ask users at least 10 - 15 items per chapter.
I'll be presenting it on a graph as well for the user's profile. (But that's another issue I'd tackle later on)
Currently, all I have now was moving from one activity to another and vice versa.
Can anyone please guide me? I'd modify this if it doesn't clarify anything.
Please take interest. Am still a student though.
Click to expand...
Click to collapse
Since you have a quiz app I'd recommend a swipe view with 4 radio buttons and a spinner with the chapter in the action bar or chapter list in the navigation drawer. Use 1 fragment and dynamically change the text for each fragment and inflate the swipe views. That way lesser resources.
Sent from my Moto G using XDA Free mobile app
Hi there once again.
I was able to pull off the Swipe View. However, I wanted to ask if this scenario is possible.
I wanted to build the quiz in a single activity only. Is this possible?
Button A ---> QuizActivity(Set1)
Button B ---> QuizActivity(Set2)
I want the quiz to happen on a single activity only but with different set of questions as triggered by different buttons.
Is this possible? And how will I pull this one.
Thanks.
So you want to have a set of buttons and depending on the button (category of questions) clicked you want to start a quiz of that category?
Simple answer to your question though is yes, u can have a quiz in a single activity if you wish.
Sent from my GT-I9300 using XDA Free mobile app

Categories

Resources