Can Fragments use the logic from main_activity.java? - java

So, I am using Activity Recognition API to know the state of the user, and implementing it like Google FIT does.
I have written all the logic for the app in main_activity.java and a class I made for ActivityRecognition. And I have my XML in activity_main.xml
PRetty usual stuff till now.
Well the problem arises now when I want to integrate the code in Tab layout material design which uses fragments and viewpager to display.
If I copy my activity_main.xml to Fragment_one.xml, Do I have to copy the logic for ActivityRecognition API to Fragment_one.java too or not ?
Very confused, any help will do.

Related

Android app: 2 problems with layout

After ionic and angular i'm trying to develop apps in native java. After setting up a toolbar and a drawer-menu i wasn't able to make it work:
First problem: How can i use myitems in my drawer to show different views (in separate .xml files) in the main view.
Second problem: How can i create a view, similar to a full-height snackbar, whitin a text input field?
thank you for your time.
You should use fragment replacement. So in your main activity layout file create Frame Layout and onNavigationDrawerClick change fragments. Take a look at this as example.
2.I recommend you to use Bottom Sheet in it. Sample
If you need some code recommendations comment here. Good luck

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.

How to use Fragments in Android properly

I asked a question a while back:
Replacing Fragments isn't working/Am I executing this the proper way?
Because I was having a really tough time wrapping my head around the entire concept. Past Google I/O videos have also helped, but I still didn't really have a reason to use fragments. I have encountered my problem again, and with more searching and Google I/O 2013 videos, I've hit another fork in the road. After starting a bounty to get a more expert opinion on this subject, I was guided to use FrameLayouts, yet in this I/O 13 video at 19:05 into the video it shows <fragment>. Can someone clear up any discrepencies or misconceptions I have?
Do I use <fragment> or <frameLayout>? Bonus points if you look at my original question to know where I'm coming from.
There are 2 seperate questions I see in here; (1) what is the <fragment> element? (2) am I (meaning you) doing what I tried to do in my previous question in the best way possible by using a FrameLayout?.
I will try to answer both.
The <fragment> XML tag is just another way to implement a fragment. As a refresher, a Fragment is like a part of an activity. It is a stand alone class, however, referring back to the fragment documentation, because a Fragment is intended to be modular, all fragments must be housed within a seperate FragmentActivity. Think of this as FragmentActivity as a container. By loading your Fragment into the FragmentActivity the FragmentActivity provides other valuable functions that the Fragment class does not have access to on its own. It also offers you the chance to swap out fragments dynamicly, allowing for a more flexable user experience.
How you load your Fragment into its containing FragmentActivity depends on what you are trying to acomplish. One way to do this is to use the <fragment> tag. With this approach, rather than declaring the Fragment in the activity java code and then loading it with a FragmentTransaction as you did in your earlier question, the Fragment class is loaded via the XML layout for the containing FragmentActivity.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:name="com.example.android.fragments.HeadlinesFragment"
android:id="#+id/headlines_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
You can see the fragment declared here in the example taken from the android docs. The fragment is then loaded by the FragmentActivty when setContentView() is called onCreate.
public class MainActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_articles);
}
}
The other way to display fragments is as you have done it in your prior example. Using a FragmentManager you can create a new instance of your Fragment class and load it programatically within the java code.
So which technique makes sense when? As the example from the android docs demonstraits, the XML fragment method makes sense when you are using stationary fragments, i.e. you are not dynamically swapping fragments in and out. This is because you cannot edit the XML layout you create on the fly the same way you can the java code. Also, from what I can tell, using the XML approach treats the fragments more like layout elements alowing for different control. In the android example, they use the XML approach to display 2 fragmens side by side on a tablet. By controling the fragments more like layout elements, e.g. having the ability to adjust layout_weight you can achieve a better result for that purpose.
However, if you are designing a highly dynamic UX, say with a ViewPager or some other feature where fragments will be rotated regularly, it makes more sense to use seperate Fragment classes and replace those Fragments as they become necessary.
Based on your prior question and the need to swap fragments dynamicly I think you made the right choice for that implementation.

Android App Development : How do change content on screen depending on event

Just starting out developing some android apps. Coming from a web development background I'm wondering if the idea behind changing whats displayed on screen is similar to linking html files.
Say I had a button that once clicked would then display a completely new page, button gone and completely new content in its place. I had thought at first that this was done just by having conditional statements in the main activity class. But I don't see how this would work with the xml layout file.
So I have come to the conclusion that you have to define multiple xml files and switch between them using logic in the main class.
If this is correct whats the best way to go about this, and if not could some suggest how this is achieved normally?
Thanks.
I think it wise to follow the following tutorial: http://developer.android.com/training/basics/firstapp/index.html
Have you tried visiting Android developers' website?.The solution to your question can be obtained taking the Android training module in that website. You have said you want to go to a new page, you can use Activities here.
Let me explain you this in simple terms.
In Android for every page(Activity) you need to make a separate xml file. for example main_activity.xml.
And for each page(Activity) there is a java class. For ex MainActivity.java. This class may contain event handling and business logic part.
Now let's go to your question about switching between multiple pages.
Suppose you have 2 activities: MainActivity and SecondActivity.
Now in MainActivity you have a button then you set its onClick attribute to its event handling method. This can be done in xml file.
android:onClick="goToSecond"
Now in MainActivity.java you need to create a method which looks like this.
public void goToSecond(View v)
{
Intent i=new Intent(MainActivity.this,SecondActivity.class);
startActivity(i);
}
This is a code snippet for switching to second activity.
And I also agree with other answers that you should check out developers.android.com
Hope it helps.
There is no need to switch between the XML files for portrait and landscape mode. Android does that for you. If you are writing an app that uses both orientation, then you have to write separate layout files and keep them in layout(for portrait), layout-land(for landscape) folders. This is not at all necessary if your design looks same in both orientation.

managing Persistent Visual Data (event driven buttons and ImageViews) throughout Activities of an application

I need some advice for those who are experienced making Android applications. What I would really like to have, for my application's appearance: at the top, a title-bar which is a ImageView (content is a png), and at the bottom a series of custom buttons which make up a tab-bar like thing. In between the title and the tab-bar is the Content, which may be anything... (most likely buttons)
I have been doing this by making a RelativeLayout which specifies LeftMargin and UpperMargin for x,y coordinates--
Currently all of my activities are inheriting a custom MyActivity class, which rebuilds the title and the tab-bar at the time of onCreate. This seems bad to me!
PART1)
---A solution to Persistent data
Since the "tab-bar" and the title are persistent no matter what screen you're on during this application's run-time, it makes the most sense to store them somewhere... How should I do this? Make a singleton object that the Activity's ask for?
I thought a little about the singleton object, and I'm not even sure what I would store, since the Views that are on displayed during Activity A have activity A as context, and not Activity B.
PART2)
---Animation Aesthetics
I would really like to have the "Content" (the view in the middle between title and tabbar) slide out to the left, and the new content slide in from the right. I.e, I'd like the tab-bar and the title to remain fixed while the "activities" change. is this at all possible? What should I do to achieve it?
one idea I had, was to make all of the program in one activity! I would create an animation for the Custom View in the middle, and I would override the "back" button to navigate correctly to the previous Custom View. Is that a horrible idea?
Anyone have any advice?
Read http://developer.android.com/design. Most of the design principles can be applied to apps that run on legacy releases; it's not just limited to Honeycomb and Ice Cream Sandwich. Do consider the Action Bar and Dashboard design patterns.
I don't really recommend using just one Activity -- generally, an Activity should be a separate, encapsulated, pretty well defined chunk of functionality that can execute independently of other Activities.
To avoid duplication of your UI, consider reusing XML layouts.
To avoid duplication of your logic, consider using Fragments. You should be able to mix and match them in your activities.
To achieve the animation you describe, consider implementing a ViewPager.
Using the ActionBarCompat sample app and Android Support Library, you can enjoy modern goodies like Action Bar, fragments, tabs, and horizontal sliding transitions on devices running Android all the way back to Donut (1.6).

Categories

Resources