Defining the Layout Files Contd Lets start with the landscape mode layout

Defining the layout files contd lets start with the

This preview shows page 31 - 37 out of 55 pages.

Defining the Layout Files (Cont’d) Let’s start with the landscape mode layout resource, called res/layout- land/simple_fragments_layout.xml . Note that we store this activity_simple_fragments.xml resource file in a special resource directory for landscape mode use only. For now, this layout will be automatically loaded whenever the device is in landscape mode. Here, we have a fairly straightforward LinearLayout control wrapping another LinearLayout that has two child controls. One is a static Fragment component that references the custom ListFragment class we implemented. For the second region, where we want to put the WebViewFragment , we include a FrameLayout region that we will replace with our specific VeggieGardenWebViewFragment instance programmatically at runtime.
Image of page 31

Subscribe to view the full document.

Defining the Layout Files (Cont’d) The resources stored in the normal layout directory will be used whenever the device is not in landscape mode (in other words, portrait mode). Here, we need to define two layout files. First, let’s define our static ListFragment in its own res/layout/activity_simple_fragments.xml file.
Image of page 32
Defining the Layout Files (Cont’d) <LinearLayout xmlns:android="" android:orientation=“vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <include android:id=”@+id/toolbar” layout=”@layout/tool_bar” /> <fragment android:name= "com.androidintro.simplefragments.VeggieGardenListFragment" android:id="@+id/list" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> </LinearLayout>
Image of page 33

Subscribe to view the full document.

Defining the Activity Classes Now you need to define your Activity classes to host your Fragment components. You’ll need two Activity classes: A primary class Let’s call the primary Activity class SimpleFragmentsActivity . A secondary class that is used only to display the VeggieGardenWebViewFragment when in portrait mode Let’s call the secondary Activity class VeggieGardenViewActivity . As mentioned earlier, moving all your user interface logic to Fragment components greatly simplifies your Activity class implementation.
Image of page 34
Defining the Activity Classes (Cont’d) public class SimpleFragmentsActivity extends AppCompatActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout .activity_simple_fragments); Toolbar toolbar; Toolbar = (Toolbar) findViewById(R.id.toolbar); getSupportActionBar(toolbar); } } Here is the complete implementation for the SimpleFragmentsActivity class. That’s it.
Image of page 35

Subscribe to view the full document.

Defining the Activity Classes (Cont’d) public class VeggieGardenViewActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { finish(); return; } if (savedInstanceState == null) { setContentView(R.layout.activity_simple_fragments); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); getSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Image of page 36
Image of page 37
  • Summer '16

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes