How to Make Android Apps 13

Android Fragment ExampleIn this tutorial I’ll finish up the Android Fragment example I started in part 12. Watch part 12 first. Here is the entire package available for download.

This time will cover how to switch to either an Activity if we are in portrait mode, or how to change the hero data dynamically on the screen if we are in horizontal mode. This is a very useful technique to learn. All of the code follows the tutorial below.

If you like videos like this, it helps if you share on Google Plus with a click here

Code from the Video

package com.newthinktank.derekbanas.fragmentlayout;

import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;

public class DetailsFragment extends Fragment {

    // Create a DetailsFragment that contains the hero data for the correct index
    public static DetailsFragment newInstance(int index) {
        DetailsFragment f = new DetailsFragment();

        // Bundles are used to pass data using a key "index" and a value
        Bundle args = new Bundle();
        args.putInt("index", index);

        // Assign key value to the fragment

        return f;

    public int getShownIndex() {

        // Returns the index assigned
        return getArguments().getInt("index", 0);

    // LayoutInflator puts the Fragment on the screen
    // ViewGroup is the view you want to attach the Fragment to
    // Bundle stores key value pairs so that data can be saved
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Create a ScrollView to put your hero data in
        ScrollView scroller = new ScrollView(getActivity());

        // TextView goes in the ScrollView
        TextView text = new TextView(getActivity());

        // A TypedValue can hold multiple dimension values which can be assigned dynamically
        // applyDimensions receives the unit type to use which is COMPLEX_UNIT_DIP, which
        // is Device Independent Pixels
        // The padding amount being 4
        // The final part is information on the devices size and density
        int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                4, getActivity().getResources().getDisplayMetrics());

        // Set the padding to the TextView
        text.setPadding(padding, padding, padding, padding);

        // Add the TextView to the ScrollView

        // Set the currently selected heros data to the textView
        return scroller;

package com.newthinktank.derekbanas.fragmentlayout;

import android.content.res.Configuration;
import android.os.Bundle;

public class DetailsActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {

        // Check if the device is in landscape mode
        if (getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_LANDSCAPE) {
            // If the screen is now in landscape mode, we can show the
            // dialog in-line with the list so we don't need this activity.

        // Check if we have any hero data saved
        if (savedInstanceState == null) {

            // If not then create the DetailsFragment
            DetailsFragment details = new DetailsFragment();

            // Get the Bundle of key value pairs and assign them to the DetailsFragment

            // Add the details Fragment
            getFragmentManager().beginTransaction().add(, details).commit();


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    package="com.newthinktank.derekbanas.fragmentlayout" >

        android:theme="@style/AppTheme" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

        <activity android:name=".DetailsActivity"
                <action android:name="android.intent.action.VIEW" />


16 Responses to “How to Make Android Apps 13”

  1. David says:

    What is a general rule of thumb concerning new classes? What constitutes creating and not creating a new class.

  2. Hrishi says:

    This is my first day and is not showing preview.there are messages like
    Error:connection timed out:if you are behind an HTTP proxy,please configure the proxy settings either in IDE ot Gradle

  3. Nikolay says:

    Hi Derek,
    Probably you have answered these questions in some comments but would you tell me when you will continue your android tutorial and should I learn PHP in order to develop android apps.

    • Derek Banas says:

      I’ll upload a video on MySQL and then web services. Then I’ll get into how to use web services with Android. Then I’ll make a bunch of Android tutorials in a row. Sorry about the wait. I have been overwhelmed with work for the past month.

  4. JniTut says:

    Make a good android jni tutorial!

  5. idan says:

    great job!!! is it the last part of the tutorial? if not , what come next?

  6. idan says:

    Hi , a little question :
    why do you import in details fragment code and not as we did last time?
    is there a difference between the two imports ? when do we use each one ?

  7. Martshal Kohn says:

    Hey Derek:

    Have you tried building any “androidTest” modules in Android Studio.

    I have to admit that right from the git-go I find the testing structure under A.S. very confusing. All of the testing documentation assumes that you are building a new package just at the /src level, but it seems that A.S. is only building a new .apk at the /app level.

    Also A.S. doesn’t automatically build new AndroidManifest.xml or gradle or other template structures, and I’m wandering if I’m interpreting correctly that I must manually build them, or if there are ‘as yet’ undiscovered switches in the build files that I should use to construct my test files from a different target in the same build files.

    Just writing this has given me some ideas, but any insights you might have would be great.

  8. Suryakiran says:

    Hello Derek,

    Need Help!
    Can you please provide me with some hints how can I add icons and color to the listview you have created in the above code.


  1. Android Apps 13 Tutorial | IT Hangout - […] HOW TO MAKE ANDROID APPS 13 […]

Leave a Reply

Your email address will not be published.