Android dashboard pattern as fragment - android

Is is possible to use the android dashboard pattern from Google I/O as a fragment so that in landscape mode on a tablet the dashboard is shown on the left while the content of the current category is shown on the right?
I've tried something that looks like the code below but it doesnt work.
main.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Include dashboard -->
<include layout="#layout/fragment_dashboard"/>
<!-- Include details -->
<fragment
android:id="#+id/fragment_details"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.example.fragments.Details" />
</LinearLayout>
Details.java:
public class Details extends Fragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.details, container, false);
return view;
}
}

Related

Using Clock-Widget in a Fragment possible?

I have a Fragment in my MainActivity, that displays a Digital Clock. It works: It shows the current system time. But it looks rather simple. So before I start trying to style this clock I wanted to ask, if there is a way, to use the systems Digital-Clock Widget?
Screenshot of the widget, that I'd like to use in my Fragment:
FragmentClock.java:
public class FragmentClock extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = inflater.inflate(R.layout.fragment_clock, container, false);
return view;
}
}
fragment_clock.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayoutClockDisplay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000000">
<DigitalClock
android:id="#+id/digital_clock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"/>
</LinearLayout>

How to insert existing fragment to Mortar architecture

Let's consider this situation. We are building an app using Mortar and Flow to simplify things and then all of the sudden we see that we can use some of the old fragments. This fragments are a little bit complex so we would like to somehow inject them to our Views.
Would that be possible if we have our Mortar View like this:
<?xml version="1.0" encoding="utf-8"?>
<com.foo.myMortarView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="+id/mortar_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:orientation="horizontal" >
<fragment
android:id="#+id/myFragment"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
class="com.example.MyFragment" >
</fragment>
</com.foo.myMortarView>
And our Fragment like this:
public class MyFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
/**
* Inflate the layout for this fragment
*/
return inflater.inflate(
R.layout.my_fragment, container, false);
}
}
This of course doesn't work out of the box so how can we accomplish that? Thanks!

Can two Android Fragments use the same ids?

I want to have two or more Fragments which will be processed similarly. Can I reuse ids in each Fragment?
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
<Fragment
android:id="#+id/fragment_1"
class="com.example.DoesSomethingToTitle"
<TextView android:id="#+id/title" />
</Fragment>
<Fragment
android:id="#+id/fragment_2"
class="com.example.DoesSomethingToTitle"
<TextView android:id="#id/title" /> <!-- same id -->
</Fragment>
</FrameLayout>
And use it like this:
public class DoesSomethingToTitle {
private String titletext;
public DoesSomethingToTitle(String titletext) {
this.titletext = titletext;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.feed, container, false);
// find the title TextView and mutate it
((TextView) v.findViewById(R.id.title)).setText(titletext);
return v;
}
}
(I realize for something as simple as this, I could just specify the title string in the XML; my actual usage is more complex.)
Can I reuse ids in each Fragment?
Yes you can.

android google map v2 - inflate map fragment layout take too long and results in lagging

As title said, it takes too long to inflate the layout containing a google map fragment (around 4xx ms) and results in a lagging. How to solve this problem?
fragment_google.map.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment
android:id="#+id/google_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
/>
</FrameLayout>
MapFragment
public class MyMapFragment extends Fragment {
...
//this method consumes 4xx ms to done
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_google_map, container, false);
}
...
}

Different fragment wasn`t loaded when orientation changed (android)

I created a test project that has two different fragments to be shown in the same activity. One fragment is for landscape and the other for portrait.
# My unique activity
public class MainActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
# First Fragment
public class LandscapeFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView v = (TextView) inflater.inflate(R.layout.fragment, container, false);
v.setText("LANDSCAPE");
return v;
}
}
# Other Fragment
public class PortraitFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView v = (TextView) inflater.inflate(R.layout.fragment, container, false);
v.setText("PORTRAIT");
return v;
}
}
And I have two main.xml, one in layout/ and the other in layout-land/. Each main.xml points to the correct Fragment to be used.
<!-- layout-land/main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment"
android:name="br.luckcheese.test.LandscapeFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp" />
<!-- layout/main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment"
android:name="br.luckcheese.test.PortraitFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp" />
<!-- layout/fragment.xml -->
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30sp" />
When I open the app on landscape the LandscapeFragment is shown, and when I open in Portrait the PortraitFragment is shown. So far, so good.
But if I open in landscape and rotate the device to portrait then the LandscapeFragment is reloaded and shown.
Which is not the predicted behavior. The PortraitFragment should have been loaded.
And the same thing happens the other way around, if the device starts at the portrait orientation and then I rotate it to landscape: the PortraitFragment just gets reloaded, instead of having the LandscapeFragment loaded.
What am I doing wrong?
Make sure that you don't have: android:config="orientation" in your manifest.xml
The problem is in your 2 layouts, the fragment ids are the same.
Just change these fragment ids, like :
fragmentportrait for layout/main.xml
fragmentlanscape for layout-land/main.xml.
(And change also:
Fragment frag = getSupportFragmentManager().findFragmentById(R.id.fragment);
frag.setRetainInstance(false);
)

Categories

Resources