Hi am am working on android.
I have a layout which was used in every activity.
I mean I have a layout which has footer and header.
On each activity, header and footer same and has same actions.
I want to use a general layout for header and footer.
I mean in a activity, I will put the content area layout to general layout.
I find someting but not enough.
How can I do this?
Is there a dummy docuument for do this?.
sorry for bad english.
What you are talking about is a new Android design pattern called Fragments. Since 3.0 fragments are small activity like views that can be combined to form a screen.
So you would create a Header and Footer fragment and then include these across all activities that require them.
The other pattern you might want to look at is the Action bar pattern, which is used to place a bar at the top of screens with common content and functions, similar to your header.
Also another way would be using xml files to define your header and footer then instantiate these as views in code and add them programmatically to your content views xml definition. The problem with this is that the code behind the header and footer would need to be replicated in each controller. Your best bet is using Fragments, I'll put some useful links below:
http://developer.android.com/guide/topics/ui/actionbar.html
http://developer.android.com/guide/topics/fundamentals/fragments.html
http://mobile.tutsplus.com/tutorials/android/android-compatibility-working-with-fragments/
https://stackoverflow.com/questions/5710573/need-a-fragments-example
You could use includes for the header & footer or add them dynamically from a base class, but I think a better approach is to use a single Activity to host the app, and then use Fragments for your screen content.
http://android-developers.blogspot.co.uk/2011/02/android-30-fragments-api.html
I have nothing against fragments, and Yes, they're the way to go, but for the beginner android developer, you can do achieve what you're trying to do with <include>s and base activities.
This article explains nicely the use of <include>s, but to sum it up, you can have a layout xml file that you can "include" to another layout, instead of rewriting the same stuff over and over.
For the functionality of the headers and footers (assuming they do something when clicked), you can create a base activity that you can extend instead of the normal android Activity.
Define the logic for the header and footer clicks in this base activity, such as with this sample code:
public class MyBaseActivity extends Activity {
...
public void onHeaderClick(View view) {
// when header is clicked, do this.
}
public void onFooterClick(View view) {
// when footer is clicked, do this.
In your layout (the one you have as a separate xml), add an onClick attribute to your header/footer, assigning the name of the method in the base activity.
such as
android:onClick="onHeaderClick"
Then it's just a matter of extending MyBaseActivity for all your activities that have headers and footers.
Check this out, you can indeed reuse your layout whenever you want it to.
Related
My application has several screens, and each of them has almost the same layout.
I know I can use the "include" tag in xml to avoid rewriting the same layout again, but I also do not want to duplicate the java code of setting the behavior/properties of views and layouts.
What is the best practice to be more organized in this case?
Edit: For example in several screens, the first half of the layout is a gallery that scrolls horizontally with text below it. This is the same throughout my app
If the screens are the same for example. Activity/Fragment
You can create a "generic" parent that handles the logic and use include for the layout.
Or
Create a custom view that uses a layout as content and you can write your logic there.
I create a library that uses this principle.
here is a tutorial on how to create a view with a layout file
I have some problems with the layout and activity and I don't know are they different,are they related?
I think the layout is a place we can add or remove our views and activity is just a place that shows any thing in our layout, is this true?
An activity:
is an instance of Activity, a class in the Android SDK. An activity is responsible for
managing user interaction with a screen of information.
You write subclasses of Activity to implement the functionality that your app requires. A simple application may need only one subclass; a complex application can have many.
A layout:
defines a set of user interface objects and their position on the screen. A layout is made up of definitions written in XML. Each definition is used to create an object that appears on screen, like a button or some text.
A layout deals with the user interface. Its where you set all your views that will be visible on the user interface.
The code behind (.java) sets the layout you created as the content view and manipulates the behavior of the views you have set. For example, sets the text for a text view.
The activity then is the whole thing, the layout and the code behind.
An activity is the java code which attaches actions and puts content to/in a layout. For this the Activity loads the layout.
Briefly,
Activity is the java part of your projects. The program and any kind of algorithms are implemented here. Also layout views come to life in an activity.
Layout is where you organize the views in your page. But without activity, they have no meaning. Because in activity, you have to get these views and use them programmaticaly.
All together, you load views from layout to activity and in activies you implement your whole program.
A layout defines all the appearance of an app and this is of no use without a java program which helps in real functioning of that visual display.
Thus we define what an app does by writing its java code and a special java class called activity decides which layout to use at a particular instant and tells the app how to respond to the user.
I am building a two column application for Android and I'm wondering how to do the navigation. The left column is the navigation bar. The right one is the content view. Both of them are LinearLayouts.
I have a different activity for all the options. How do I implement my menu into these? It is the same for all the activities (except the current one is highlighted), so copying the same code multiple times seems waste and makes it harder to change it later because I would have to change all the files.
I also have to change the title for every activity.
The typical answer would be Fragments. Here's a great tutorial on that topic.
However, depending on the triviality of your requirements, you could also consider using a horizontal LinearLayout containing your two original LinearLayouts.
in my opinion you should use fragments for your columns.
http://developer.android.com/guide/components/fragments.html
you Should use Fragment control for this. you can call content Fragments on right side (Content View) area with the click on leftSide(Content item/Index) .
I feel you should follow this link.
http://developer.android.com/training/multiscreen/screensizes.html
Am not sure but hopes u asked for the same.
Thanks..
Okay, So I just started Android development (I am average at VB.Net, so I understand basic code even if its not in VB). I've made a couple play around apps which used Text-To-Speech, Async Tasks and Reading/Writing files. However, I now wish to make a tabbed Android app.
I started the project in Eclipse Juno and filled in all the details. I then selected (For navigation) the Tabs/Swipe layout. I now have the default code for that layout type (Link to Tabs on developer.android.com - http://developer.android.com/design/building-blocks/tabs.html).
I understand mostly what the default code is doing. The only problem I am having, is determining the individual layout of my Tabs. I wish to have 2 Tabs, 1 in which the user selects an option, and the other, in which an image is shown depending on the selection in Tab 1.
So the question is: How do I create a .xml file in Layout to determine what is shown on the Fragment?
If you want to do this in XML the answer is simple, it can't be done just with XML, you must create a class that's implementing a ActionBar.TabListener.
Than you can override the onTabSelected method in which you can exchange the content.
A proper solution would be:
Use a LinearLayout as root container, and implement two Fragments for each of your Tabs (there you can design an individual XML-layout). Now you can add one fragment initially to the root-container and implement the exchange of the layouts inside the onTabSelected method and you are done.
This may be a dumb question, so my apologies if so; I'm fairly new to Android.
But anyway - I have a working ViewStub, which is replaced by different layouts depending different situations. It's working fine with regards to showing the correct layout when I call the setLayoutResource() method, and then setVisibility to VISIBLE. However, I now need some of the content in this view that is being shows to be dynamic (i.e. I need to set it via code rather than just show a static layout).
Is this even possible? The setLayoutResource() method only takes a static layout-resource ID, but I need that layout XML file to be able to have it's TextViews contain non-static text that comes from some code that I have ready to utilize. How should this be approached if possible? I understand the concept of having a Java class, and inflating the XML to attach itself to it to update the fields, but I can't see how that relates to my code at hand, since it's simply a layout resource int I need to set for the setLayoutResource() method in ViewStub.
I can post existing code if needed, but I'm not sure it do much more than clutter up the post. For reference - All I have is a simple layout XML file with some TextViews, and then my main XML containing the ViewStub, which is part of a custom Dialog. The user is able to instantiate the Dialog and set the layout, which in turn sets the layout of the ViewStub. This is the layout in which I need the dynamic content to be used.
Any advice would be greatly appreciated. Thanks!
Turns out this wasn't too difficult to accomplish. I just needed to use the ID of the TextView layouts after inflating the ViewStub to get a copy of the actual TextViews, then I was easily able to set their text to whatever kind of dynamic/custom text I desired.
I also needed to comment out the code that shows it via the .VISIBLE call, and instead do the following (the .inflate() line of code accomplishes the same thing as setting it to VISIBLE):
View inflatedView = dialog.myStubView.inflate();
TextView myTextView = (TextView) inflatedView.findViewById(R.id.my_text_view);
myTextView.setText("Dynamic/Custom Text");