Common method for activity and fragment - android

I have a layout section which is part of a Fragment and its a layout for the activity.
fragment.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none”>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp">
<include layout="#layout/sectional_component"/>
<include layout="#layout/list_component" />
<include layout="#layout/usage_component" />
</RelativeLayout>
</ScrollView>
list_component.xml
<LinearLayout
android:id="#+id/list"
style="#style/VerticalMatchParent">
// programatically added top 3 customer list
</LinearLayout>
activity_list.xml
<LinearLayout
android:id="#+id/list"
style="#style/VerticalMatchParent">
// programmatically render all customer list
</LinearLayout>
I'm new to android. I wanted to use a common method for rendering the list based on the request (3 or all). For now, I have written a separate method in Fragment and in an activity to handle this.
I want to make it as a single method which can be used by the fragment as well as an activity since the functionality is nearly same. I tried to make it as a separate util method, but the issue I'm facing is inside the method I have do manipulations for the view elements not sure how to do it without inflating the view element layouts. Already the layouts having the view elements are inflated in the respective Fragment and Activity.
Kindly provide a solution for this.

There are multiple approaches to do this, one of them would be to create an static method inside a separated class, pass the required parameters and call it wherever you want.
Class MyClass:
public class MyClass {
public static View myMethod(View myView){
// Do whatever you want with your View
return myView;
}
}
Call to myMethod from MyClass:
View modifiedView = MyClass.myMethod(myView);
If you need to handle some Views send them to the method as a list, array or whatever

If you plan on populating a list, you can go for listView. And write an adapter class to populate your list on it.
Note: You need a separate layout file with design for listView saved under you layout folder.
Hope this helps!

Related

How to add multiple rows of one linear layout into a single parent layout dynamically in program

I have a row.xml file which define a row whose content and number of rows will be dynamic.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/room_detail_row_in_hotel_detail">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/name"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/sz_blue"
android:id="#+id/price"/>
</LinearLayout>
The best solution as suggested by #Hell is to use a Listview.
Say you are operating within a fragment, add a listview in the xml for the same.
The fragment (or activity) containing the listview will dynamically create list(preferrable)/array of data.
In your case the dataset will essentially either be a list of class object which uniquely have two objects, price and name.
Create an adapter, could be a BaseAdapter or any other form depending on your specific use-case. This adapter would essentially inflate your row layout (as specified in your question) and dynamically add the data.
Add the data into the adapter using it's constructor. Following which assign this adapter as the listview's adapter and call the "notify" method for the adapter indicating that a new dataset has been loaded and the list needs to be refreshed.
Also note, it's highly suggestible to add smoothScroll to your listview for obvious reasons.
I guess what you are looking for is a RecyclerView.
You should use the Viewholder-Pattern to reuse your layouts

How to share common layout between activities without fragment

Is there any possible way to share layout(part) between activities? For example, in my app, all activities have similar layout, the top part is long operation indicator (a progress bar, hidden when no operation is being executed), the bottom part is for showing errors. Only the middle part is different for all activities. See the picture below.
so my question is, is it possible to reuse the common layout(loading and error part) for all activities in my app? (currently I don't want to use fragment to do it for some reasons)
maybe the layout resources should like this:
layoutfolder
activity_common.xml
activity_one_content.xml
activity_two_content.xml
thanks
You can create an abstract 'base' activity that all your activities extend from, overriding setContentView to merge the base, and sub activity layouts.
This way you can handle all the loading/error code in the base activity, and simply toggle between hiding and showing the views in the sub activities.
The abstract activity:
public abstract class BaseActivity extends Activity {
protected RelativeLayout fullLayout;
protected FrameLayout subActivityContent;
#Override
public void setContentView(int layoutResID) {
fullLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.activity_base, null); // The base layout
subActivityContent = (FrameLayout) fullLayout.findViewById(R.id.content_frame); // The frame layout where the activity content is placed.
getLayoutInflater().inflate(layoutResID, subActivityContent, true); // Places the activity layout inside the activity content frame.
super.setContentView(fullLayout); // Sets the content view as the merged layouts.
}
}
the layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="#+id/loading_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="#+id/error_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
You could use include in XML to, well.. include the re-useable part of your layout code.
As an example, here's my layout file for the Toolbar I used in my app:
// /res/layout/component_toolbar.xml
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:taggr="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/primary"
android:minHeight="?attr/actionBarSize"
taggr:popupTheme="#style/ThemeOverlay.AppCompat.Light"
taggr:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
Now, say if I want to use that Toolbar again in a different Activity, this is all I'd have to write:
// /res/layout/whatever_layout_this_might_be.xml
<include layout="#layout/component_toolbar" />
Bear in mind that this would only copy the layout - not the actual behavior of said widget/component.
If you want to actually copy all of the aspects (layout, behaviour) I'm afraid Fragment is the only way to go.
Although ActivityGroup is deprecated fro API 13 but if you don't wish to go with fragments then this can be your best choice.
According to documentation, an ActivityGroup is:
A screen that contains and runs multiple embedded activities.
You can find a tutorial here and here Although the mentioned tutorial uses a Tablayout you can replace that with your common layout in XML.
A second Approach could be Reuse the layout with include tag, in this approach you could just reuse your once created common layout everywhere in the app.

Android Can we add a footer view to a content view

I have a 'ParentActivity'
public class ParentActivity extends Activity
and all the activities of my application extends this ParentActivity
public class MainActivity extends ParentActivity
I have roughly 20 activities all extending ParentActivity and all activities have different layouts from each other(like some have LinearLayout ,some RelativeLayout,some ScrollView)
My app is almost complete,but my client now requires to have an adsense ad display at the end of each activity.
One general solution is to include following layout to all of my activity layouts
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="30dip"
android:orientation="vertical"
android:background="#color/header_bgcolor"
android:gravity="bottom"
android:layout_gravity="bottom">
<com.google.ads.GoogleAdView
android:id ="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
But what I want to do is on ParentActivity I will define a function which automatically adds the following view to end of the content view as follows
ViewGroup view =(ViewGroup)this.findViewById(android.R.id.content);
View adSenseView = View.inflate(this, R.layout.ad_sense_layout,view);
GoogleAdView adView =(GoogleAdView)adSenseView.findViewById(R.id.adView);
AdSenseSpec adSenseSpec = new AdSenseSpec("CLIENT_ID")
.setCompanyName("COMPANY NAME")
.setAppName("APP NAME")
.setChannel("Channel Id")
.setAdType(AdType.TEXT);
adView.showAds(adSenseSpec);
In this way I am able to add the adsense view at the bottom but it overlaps the lower end of the content view.So is there any way ,I can dynamically add a footerview to a content view of an activity.
I am still working on solution,and if I find any I will surely post.
Thanks in advance
Also, I wonder if some of the setters you are using have an xml equivilent, removing some of the need for the code?
Put your last code snippet in ParentActivity::onCreate and define a_child_activity::onCreate as { /* my create stuff */; super.onCreate(); }
Just a beginner so probably rubbish though.

TextView and ListView in a LinearLayout

I searched for similar questions but didn't get the proper answer, so posting it as a new question.
Can a LinearLayout have two TextViews and one list view? I learn that listview can be used to display array elements only if the class extends ListActivity. The Class in my app extends Activity and uses listView.
I am trying to build an RSS Reader based on IBM tutorial. On running the project I am getting parsed text in both the Text views but ListView is not displaying. I can post the code and Logcat if required.
Linear Layout can have any number of children.
ListActivity is an Activity that Android have added for convenience when dealing with activities comprised of list.
You can use ListView on a regular Activity and just implement an adapter that will populate the list items with data from your model.
Android has some ready-made adapters that can be used for simple use-cases.
of course if you need a more complicated beahviour you can extend them.
Have a look on BaseAdapter, ArrayAdapter and CursorAdapter to get a better understanding of how to use adapters.
You can have a ListView in any activity, using a ListActivity just makes your life easier (usually). See here for details.
You can have a Linear layout with different views and list view inside for example:
?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/background_new_search_activity_1">
<TextView
android:id="#+id/list_item_amount_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"/>
</LinearLayout>
And yes you can use the ListViewin all the activities, it is not necessary to extend ListActivity
Make sure the orientation of the ListView is set to vertical. Otherwise, it will try to display all items side by side, and those that fall outside the available view area won't be visible.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"> <!-- << orientation setting here -->

Passing values from one activity to a class that extends View class

I have a need where i must pass some values from an Activity to
another file which extends View class (instead of Activity) .... Its
not the normal passing of values from one activity to another...
In my Activity,i will pass some co-ordinate values to the class that
extends View... In this class,i will draw an image and place points
over the image on the co-ordinates that were passed from the activity... But , the problem is ,i cant send values using Intent ...
Is there any way to do this??
If you want to show the View, you must use another Activity (you cannot just show a View without an activity holding it), thus this is actually the normal passing of values from one activity to another... the only difference is that once your second activity has received the values, it will have to configure your custom view with them.
Take a look at these links: Building Custom Components, Creating custom Views. In order to use a custom View, you just put it in your XML as you normally do for android Views. For instance:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this is a normal view"/>
<!-- this is a custom View -->
<your.package.YourCustomView
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

Categories

Resources