Question: This is java android development class. Introduction The goal of this lab is to understand how to nest one fragment inside another fragment and how

This is java android development class.

Introduction

The goal of this lab is to understand how to nest one fragment inside another fragment and how to use the child fragment manager to manage fragment transactions.

Activities

The BeerAdviser app is completed in lab2.

The Stopwatch app is completed in lab3

Change the FindBeerActivity into a Fragment called FindBeerFragment with associated FindBeerFragment layout xml file.

Add mainActivity to the project FindBeer, and the mainActivity only contains one fragment element which is the FindBeerFragment.

Change the Stopwatch activity to a fragment

Nest the stopwatch fragment into the FindBeerFragment.

Make sure everything in the Stopwatch fragment works well

Some source codes are attached as following you might make a reference to.

import android.os.Bundle;

import android.os.Handler;

import android.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.TextView;

import android.widget.Button;

public class StopwatchFragment extends Fragment implements View.OnClickListener {

//Number of seconds displayed on the stopwatch.

private int seconds = 0;

//Is the stopwatch running?

private boolean running;

private boolean wasRunning;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

if (savedInstanceState != null) {

seconds = savedInstanceState.getInt("seconds");

running = savedInstanceState.getBoolean("running");

wasRunning = savedInstanceState.getBoolean("wasRunning");

if (wasRunning) {

running = true;

}

}

}

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

View layout = inflater.inflate(R.layout.fragment_stopwatch, container, false);

runTimer(layout);

Button startButton = (Button) layout.findViewById(R.id.start_button);

startButton.setOnClickListener(this);

Button stopButton = (Button) layout.findViewById(R.id.stop_button);

stopButton.setOnClickListener(this);

Button resetButton = (Button) layout.findViewById(R.id.reset_button);

resetButton.setOnClickListener(this);

return layout;

}

@Override

public void onPause() {

super.onPause();

wasRunning = running;

running = false;

}

@Override

public void onResume() {

super.onResume();

if (wasRunning) {

running = true;

}

}

@Override

public void onSaveInstanceState(Bundle savedInstanceState) {

savedInstanceState.putInt("seconds", seconds);

savedInstanceState.putBoolean("running", running);

savedInstanceState.putBoolean("wasRunning", wasRunning);

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.start_button:

onClickStart(v);

break;

case R.id.stop_button:

onClickStop(v);

break;

case R.id.reset_button:

onClickReset(v);

break;

}

}

public void onClickStart(View view) {

running = true;

}

public void onClickStop(View view) {

running = false;

}

public void onClickReset(View view) {

running = false;

seconds = 0;

}

private void runTimer(View view) {

final TextView timeView = (TextView) view.findViewById(R.id.time_view);

final Handler handler = new Handler();

handler.post(new Runnable() {

@Override

public void run() {

int hours = seconds / 3600;

int minutes = (seconds % 3600) / 60;

int secs = seconds % 60;

String time = String.format("%d:%02d:%02d",

hours, minutes, secs);

timeView.setText(time);

if (running) {

seconds++;

}

handler.postDelayed(this, 1000);

}

});

}

}

import android.app.Fragment;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.TextView;

import android.app.FragmentTransaction;

public class WorkoutDetailFragment extends Fragment {

private long workoutId;

@Override

public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

if (savedInstanceState != null) {

workoutId = savedInstanceState.getLong("workoutId");

} else {

FragmentTransaction ft = getChildFragmentManager().beginTransaction();

StopwatchFragment stopwatchFragment = new StopwatchFragment();

ft.replace(R.id.stopwatch_container, stopwatchFragment);

ft.addToBackStack(null);

ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

ft.commit();

}

}

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

return inflater.inflate(R.layout.fragment_workout_detail, container, false);

}

@Override

public void onStart() {

super.onStart();

View view = getView();

if (view != null) {

TextView title = (TextView) view.findViewById(R.id.textTitle);

Workout workout = Workout.workouts[(int) workoutId]; title.setText(workout.getName());

TextView description = (TextView) view.findViewById(R.id.textDescription);

description.setText(workout.getDescription());

}

}

@Override

public void onSaveInstanceState(Bundle savedInstanceState) {

savedInstanceState.putLong("workoutId", workoutId);

}

public void setWorkout(long id) {

this.workoutId = id;

}

}

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/time_view"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true"

android:layout_marginTop="0dp"

android:text=""

android:textAppearance="?android:attr/textAppearanceLarge"

android:textSize="92sp" />

android:id="@+id/start_button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/time_view"

android:layout_centerHorizontal="true"

android:layout_marginTop="20dp"

android:text="@string/start" />

android:id="@+id/stop_button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/start_button"

android:layout_centerHorizontal="true"

android:layout_marginTop="10dp"

android:text="@string/stop" />

android:id="@+id/reset_button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/stop_button"

android:layout_centerHorizontal="true"

android:layout_marginTop="10dp"

android:text="@string/reset" />

android:layout_height="match_parent"

android:layout_width="match_parent"

android:orientation="vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textAppearance="?android:attr/textAppearanceLarge"

android:text=""

android:id="@+id/textTitle" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text=""

android:id="@+id/textDescription" />

android:id="@+id/stopwatch_container"

android:layout_width="match_parent"

android:layout_height="match_parent" />

class="com.hfad.workout.WorkoutListFragment"

android:id="@+id/list_frag"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!