Same activity but different layouts? - java

I'm kind of new to android java so I started to create a Music Player app just to learn and practice. The main thing was to control the same MediaPlayer from 2 different activities (In MainActivity there's a listview with the music files and PlayerActivity has the MediaPlayer). I tried to use public voids like:
PlayerActivity pa = new PlayerActivity();
//button onClick
pa.songPlay();
and it kind of worked at first but then a lot of errors occurred when using contexts and MediaPlayer.create(), so I started to look for another way to do this but found none so far. Is there a way to make 2 different layouts with a shared element, and with the same class?
For a better explanation:
MainActivity:
PlayerActivity:
And fuse them like:

Okay so I'm answering my own question because I didn't know that FrameLayouts exist, but since I found them I can put two different layouts into the same space and just set the visibility to VISIBLE or GONE whenever I want to switch between them

Related

How to update content of the activity without creating new Activity?

I'm trying to make an app and I have made a blueprint for a specific activity, but I don't know how to implement it. The layout contains few buttons at the top of the activity, and each button features some information, which is displayed inside the view. The view which needs to be updated is present under the buttons. I don't want the activity to be changed, instead it should update the contents of the View, which is different for each category/button.
By doing some research I have realised that the "Tab Layout" can be used to achieve my requirements, but I don't want the tabs and I need some stylish buttons as a replacement.
I know I'm not the best at describing, so I have looked upon Dribble and found one design which is 100% similar to blueprint.
I want to achieve this using XML and Java using Android Studio! Every suggestion will be a great support foy my app.
Thanks a lot.
As far as I know, you could achieve that by using fragments (which is the same concept you would have used on TabLayout). I don't really know how much knowleadge you have on Android, but if you know what a Fragment is, it should be easy for you to recreate the idea.
You have 3 buttons for 3 different fragments, so you must design every fragment by separate and change it depending the button you click.

Viewpager with listviews

Has anybody had any luck with a view pager switching between fragments that contain listviews? In particular, the listviews I am working with inflate two separate layouts to get the desired effect. However, to my understanding this is causing the viewpager to disappear as well as the tabhost. I believe this to be so because it is working with other fragments that only inflate once.
Edit 1:
I was trying to see what would happen if I used one of the fragments that showed the tabhost and used the viewpager first. I would switch views and see the correct next one. However, shortly thereafter one of the fragments that does not show the tabhost or use the viewpager, for some odd reason, would load up.
Edit 2:
It's weird it is not even loading up on the right page. It should load up on 3 but instead it loads up on 2 and replaces the former screen that was actually supposed to be there.
Adding listviews to two fragments is very easy.
In short, you want to have a main activity that's the viewpager itself. Next, the viewpager is going to host two tabs (can be as many as you want, really) which will both contain separate layouts...each with a listview of its own.
Code
The first thing we need to do is add some classes. I've made a GitHub Gist of 4 classes that I'd like you to implement into your project. You'll need to change the package name and R class to meet your project's needs.
Gist: https://gist.github.com/Andrew-Quebe/b3e9f1d0f8223ba2f8df
Second, we need to make our host activity. This is what will show the tabs and toolbar. See this next Gist as I don't want to spam up this answer with tons of code.
Gist: https://gist.github.com/Andrew-Quebe/8add2fc064397ab8efe4
You've probably gotten an error in the MainActivity.java file due to a missing ViewPagerAdapter class. That's up next!
Gist: https://gist.github.com/Andrew-Quebe/fd70ee97c2e00d72f025
And finally, the tabs that'll show our listviews!
Gist: https://gist.github.com/Andrew-Quebe/3e2a87706c98a69e7353
My apologies for taking so long in my response...I actually took the time to build all this code and error check it for you. I had an example of tabs once before but it was outdated...you weren't the only reason I made all this code. The full project can be found on GitHub here: https://github.com/Andrew-Quebe/SlidingTabsExample
Hope this helps!
Edit:
Download the sample APK to see how everything looks: https://github.com/AMQTech/SlidingTabsExample/blob/master/APKs/Sample.apk?raw=true
First of all I would like to apologize. I pointed you all in the wrong way. I did some research and as it turns out you cannot have the fragment container (frame layout) in the activity layout already. All I had to do was put the fragment container into a different layout and inflate it when the time came to switch to another fragment class and that fixed it. Thanks to everyone.

Eclipse ADT automatically creates a fragment for Activities. Why?

I vaguely understand what fragments are used for. However, I don't understand why ADT seems to suggest that we should ALWAYS use them. I am aware that I can simply delete the fragment layout, as well as the code in Activity for the fragment. But is it ACTUALLY recommended to use fragments all the time? What are the benefits of not deleting it?
Is there even a point of an Activity having a single fragment? What is the difference with it having no fragments at all?
Additionally, how do you know whether to create multiple fragments in one Activity, multiple Activities with a single or zero fragments or a combination of Activities and fragments?
For example, if you have a Terms and Conditions button in your Activity, and you want it to open a screen containing a document listing the terms and conditions, should I be starting a new Activity for that? Or should I just move a fragment containing this content to the front?
Using Fragments is how modern apps are developed today.
Some of the benefits are a better performance when switching content (lets say 2 fragments)
because you are not leaving the current Activity.
Also, apps with swipe tabs (for example) are built with an Activity as a container and Fragments as the content itself.
Other benefit is that you can do much more with one activity, distributing work between the fragments. Each Fragment have its own Layout and therefore its own components, so maintaining the code and keeping organized is easier.
Eclipse does this way as suggesting a start point. You can totally ignore this and start from scratch. And that's all.
I suggest you to keep the Fragment to start getting used to it. Even if there is just one.
And, if later you need to work more with that Activity, it will be easier to add new features by creating a new Fragment, without modifying your previous code.
Good luck.

New to android fragments - looking for a few answers

I'm fairly new to android, and have been working on an app for a few weeks. I want the app to run on handhelds and tablets. I decided to implement fragments after running things on my tablet and am having trouble getting my head around it to get the ball rolling. My app is like most: The first page you see is a menu list of various activities you can navigate to, some of which call other activities. I've read countless tutorials, but still have a few questions, I'm hoping someone can help with. I haven't had any luck finding the answers on the internet.
1) Should the main menu be in a fragment or just remain an activity that calls fragments?
(I'm thinking it should be a fragment as well)
2) I've read that the activities are just a portal to the fragment, so in the main menu all of my logic should go in the fragment?
3) Why does eclipse throw a dummy class into the wizard setup? Shouldn't this package just be deleted and the array adapter or whatever be put into the logic of the main menu or do I require another class for a good reason?
4) I also read somewhere that the Main activity must be called Main.java is this true? I don't see why it would be.
I realize these are all probably pretty simple questions to someone that has a grasp on fragments, but I don't!
Thanks for any answers
Ken
1) Yes, if you are handling logic and ui, and you are attempting to do your best and want to have a structure that you can easily support multiple screen sizes, your menu should be a fragment.
I think the activity is the mechanism to handle what fragments to display.
So on a phone the first activity just displays the menu fragment and launches a new activity to handle the selection.
While on a tablet you might have the menu fragment on the left side of the screen and do a fragment transition for the right side of the screen upon user selection
2) Your activity still has work to do to coordinate those fragments as my example in 1.
3) You don't need that class
4) You can name anything at all, anything at all. Pay attention to what's specified in your manifest. Your app can even have multiple starting points / icons in the launcher if you specify it as such in the manifest.
1) If you want different layouts depending on device and/or orientation then yes. Maybe you want the main menu to be located in a pane on the left on a tablet while it's a full screen list on a phone.
2) This is a bit more tricky to answer. It all depends on how you design your application, and there really is not right or wrong here. You could opt for an MVC like approach where you put the actual logic in a controller. You could let your Activity deal with what happens when a menu item is selected. You could use an eventbus (Otto or Guava) to communicate between fragments. It's up to you.
3) Not needed
4) No, you don't have to name it Main.java. I think you may be confusing this with the public static void main(String[] args) entry point of a standard J2SE program?

Android - 2 Activities active at the same time

I have a GameActivity. I also have a transparent ChatActivity running on top if the user presses the Chat options from the Menu (onOptionsItemSelected). The problem is, when a player starts the ChatActivity before I start the game, an odd behavior occurs and the game won't start.
Is there any way I can keep GameActivity active while ChatActivity is visible?
I fired up the ChatActivity using the normal way:
startActivity(new Intent(GameActivity.this, ChatActivity.class));
Thanks for your help.
You can't have two activities in one activity. However; one design you could possibly achieve is introduce a design that allows the user to swipe the screen to bring up the chat view and swipe in the opposite direction to hide the view.
Have the main Activity that is running maintain that view via an async process so that it can be updated as necessary and does NOT interrupt the user.
You could take a look at FLAG_NOT_TOUCH_MODAL (and maybe FLAG_NOT_TOUCHABLE), using 2 activities on top of each other, making the top one transparant and give it that flag/those flags (not sure if you can actually touch windows you create within that activity, didn't try that yet)
you could also take a look at this. It's not exactly the same thing, but maybe you could rework it a bit to fit your needs
I know this is very very late,but this answer could be of use of anyone new.
For achieving the kind of design mentioned here, you could implement fragments instead of having two activities.

Categories