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

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>

Related

Common method for activity and fragment

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!

Complex Android UI design guidance needed (fragments)

I am developing an applications that is aimed at Tablets and Google TVs. It will be like many standard Google TV applications with a LeftNavBar and a top Search bar that is common to all application screens. It will look something like the following image:
Main Screen
The RED area will be different for all other screens. It may contain data like following screens mockups:
Activity One loaded into main container
Activity Two loaded into main container
So you can see that completely different sections can be loaded in the main area.
Screen 3 can be loaded as a detailed section when selecting any list item in Screen 2 (say in fragment list) OR it can be loaded as a result of selecting a tab (which will appear in LeftNavBar).
Here is how I am trying to implement it.
Step 1. I Created a main Activity with the following XML:
<?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="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#9ccc" >
<!-- Top Bar -->
</LinearLayout>
<FrameLayout
android:id="#+id/mainContainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!-- main Red Container that will load other Activities -->
</FrameLayout>
</LinearLayout>
mainContainer is the RED container where I want to load the Activities. LeftNavBar will be added to this Activity as its the parent of All.
Step 2 I created ActivityOne & ActivityTwo with two & three Fragments in them respectively (as shown in above second & third image).
*Step 3 I am trying to load the ActivityOne in main page's mainContainer FrameLayout... But I cannot add it.
I tried by adding the ActivityOne to mainContainer as follows:
View v = (new ActivityOne()).getWindow().getDecorView();
FrameLayout mainContainer = (FrameLayout) findViewById(R.id.mainContainer);
mainContainer.addView(v);
but the getWindow() returns null....
Other issue occurs because all the data comes from a remote services .. so please also suggest how would I be able to hold references to all the loaded Activities in mainContainer in a some kind of stack ... so I can just reload the already loaded activity instead of creating its new instance.. This will be used on BACK button press.
OR
Instead of loading an activity into the above RED container, I should create two Activities each with their own Fragments & a LeftNavBar. This might be easier than the aforementioned approach. or this might be the only solution.... however I feel that saving state for BACK buttons might get messy .. but I will try implementing this
What would you do if you had to create this type of application?
How would you design the UI layout for best performance/practice?
Your suggestions in helping me setting this app's layout are much appreciated.
Disclaimer
This is where fragments can get tricky. The problem would be simple if Activity 1 & 2 had identical layouts so that you could simply attach/detach fragments and use the fragment back stack to unwind.
Because you want 2 unique layouts to house your fragments, things are going to be a little more involved. If at all possible I would try to use the same layout so that you can take the easy path.
As another option, you could use two activities as you outline above and send data back and forth with Intents.
That said, if I really had to implement this solution as written, here is what I would do. Note that I am not advocating this solution but myself do not know of a better way of doing things.
The Solution
Create a FragmentActivity whose view would be Main Screen as you've defined above. The layout for the Main Screen would contain:
Left nav bar
Top bar
2 layouts. layout1 and layout2. These would be contained in a parent layout i.e. RelativeLayout or LinearLayout and would contain the necessary FrameLayout elements for your fragments.
Example using your XML (note, tags are a bit brief):
<?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="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#9ccc" >
<!-- Top Bar -->
</LinearLayout>
<LinearLayout android:id="#+id/layout1">
<FrameLayout android:id="#+id/listFragment" />
<FrameLayout android:id="#+id/contentFragment" />
</LinearLayout>
<LinearLayout android:id="#+id/layout2">
<FrameLayout android:id="#+id/imageFragment" />
<FrameLayout android:id="#+id/boxFragment1" />
<FrameLayout android:id="#+id/boxFragment2" />
<FrameLayout android:id="#+id/boxFragment3" />
</LinearLayout>
</LinearLayout>
The main idea is that you then show/hide layout1 & layout2 i.e. set android:visibility="gone" based on the state of your app.
The disadvantages of this method are:
Using fragment backstack may be impossible, instead you'll have to track where the user is in your UI flow and manage the back button to show/hide layout
You may need to take special care to attach/detach fragments when you show/hide their parent view to reduce resource consumption while the fragments are invisible
The advantages are:
Easy communication between fragments and the base activity since only 1 activity is used
Re: The nested Fragments problem
To get around the 'nested Fragments' problem in our application where (as you correctly note) Fragments cannot add Fragments I hosted a single templating Fragment under the activity whose only purpose was to define a set of place holders for other fragments to anchor to. When adding further Fragments to the activity past this point I used the templating Fragment's view place holder +#ids to identify the "root" or parent view Id for the Fragment being added.
getSupportFragmentManager().beginTransaction().add(#someIdFromTheTemplateFrag, fragment, fragmentTag).commit();
The Fragment I was adding then knew where to anchor itself in the current layout and of course then went about it's merry way of add it's view. This had the effect of attaching a Fragment to another Fragment hence achieving the desired visual 'nesting'...

Android: fixed button in each view

Is there a way to make a button fix in the whole application views? I mean instead of adding the button to every xml file and code it.
Thank you.
You can keep your button xml code a different xml file. And to every other activity xml layout you can use the xml tag include like
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent">
<include layout="#layout/okcancelbar_button"/>
</LinearLayout>
Or like jack said, create a base activity that creates the buttons and the rest of the activities can extend from it.
you can create a custom activity in which you create one time button and every activity you will call, extended to your custom activity will have that button

Sharing data with custom canvas view

I'm a beginner at android programming, so excuse me if my wording is slightly incorrect.
I have a custom canvas view along with a TextView inside a linear layout, defined in the layout file as
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="#+id/blah"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:gravity="top|center_horizontal"
/>
<com.*.Overlay android:id="#+id/combined"
android:layout_width="fill_parent"
android:layout_height="100dp"
/>
</LinearLayout>
I need to be able to read the text contained in the TextView from within the Overlay custom class that I created.
(The overlay class takes in 2 bitmaps and puts one on top of the other. The bitmaps used will depend on the text in the TextView.)
I considered using intents, but the Overlay class doesn't have an onCreate method.. All my code is within the onDraw method. I also added the necessary constructors.
I'm not sure what to try next, perhaps try accessing the parent linearlayout and then its child textview?
Hope I managed to explain everything in a non-confusing manner
Ok, managed to fix the issue... sort of
I found out that Views need to be contained in Activities.. so I created a new Activity with my custom view as an inner class, passed an intent with the necessary data to the activity and was able to use it successfully in my custom canvas view.
I was a bit surprised I didn't get any responses, but I guess that's because I'm new here

Android: Instantiating Multiple Widgets From One Layout Declaration

I am creating a custom widget to display a rotating list of items. I have declared the layout in newsview.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/tblLink"
style="#style/newsviewstyle"
android:layout_alignParentBottom="true">
<ImageView
android:id="#+id/newsviewimg"
style="#style/newsviewimgstyle"
android:layout_margin="5dp" />
<TextView
android:id="#+id/newsviewtitle"
android:layout_toRightOf="#id/newsviewimg"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:textColor="#000000" />
<TextView
android:id="#+id/newsviewurl"
android:layout_toRightOf="#id/newsviewimg"
android:layout_below="#id/newsviewtitle"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_width="fill_parent"
android:layout_height="15dp"
android:textColor="#000000" />
</RelativeLayout>
The idea is in my main.xml, I have a ViewFlipper that I would like to use animate the transitions between each item. How could I instantiate "n" unique items declared above, so they can bee added to the ViewFlipper. How would I be able to get access to the layout declared from a class outside the main activity (and also outside the package containing the main application, i.e. a view controller).
As a note, collecting the list of items takes place in a controller outside the main activity of my application. I would like to keep everything as much as possible in the controller for possible reuse later on.
How could I instantiate "n" unique items declared above, so they can bee added to the ViewFlipper.
Use getLayoutInflater().inflate(), the same way you would in a custom Adapter. In fact, on Honeycomb, there is AdapterViewFlipper to handle this very scenario.
How would I be able to get access to the layout declared from a class outside the main activity (and also outside the package containing the main application, i.e. a view controller).
I'm sorry, but this sentence did not parse for me.

Categories

Resources