I am using a Master/Detail flow. I have a button on each detail, and I want a count down timer to start when that button is clicked.

This is my code for the OnClickListener, which worked in a test run on separate a blank activity project, where all I did was click a button to start the timer:

Button btnStart = (Button) findViewById(R.id.btnTimer);
    btnStart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            {
                new CountDownTimer(30000, 1000) {

                    public void onTick(long millisUntilFinished) {
                        Toast.makeText(ItemListActivity.this, "seconds remaining: " + millisUntilFinished / 1000, Toast.LENGTH_LONG).show();
                    }

                    public void onFinish() {
                        Toast.makeText(ItemListActivity.this, "Finished!", Toast.LENGTH_LONG).show();
                    }
                }.start();


            }

        }
    });

I'm not sure what .java file to place this in; I do not have a clear understanding of Master/Detail despite several days of research.

I have tried putting this code in the ItemListActivity.java, but the app crashes as soon as it starts:

public class ItemListActivity extends FragmentActivity
    implements ItemListFragment.Callbacks {

/**
 * Whether or not the activity is in two-pane mode, i.e. running on a tablet
 * device.
 */
private boolean mTwoPane;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_item_list);
    Button btnStart = (Button) findViewById(R.id.btnTimer);
    btnStart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            {
                new CountDownTimer(30000, 1000) {

                    public void onTick(long millisUntilFinished) {
                        Toast.makeText(ItemListActivity.this, "seconds remaining: " + millisUntilFinished / 1000, Toast.LENGTH_LONG).show();
                    }

                    public void onFinish() {
                        Toast.makeText(ItemListActivity.this, "Finished!", Toast.LENGTH_LONG).show();
                    }
                }.start();


            }

        }
    });






    if (findViewById(R.id.item_detail_container) != null) {
        // The detail container view will be present only in the
        // large-screen layouts (res/values-large and
        // res/values-sw600dp). If this view is present, then the
        // activity should be in two-pane mode.
        mTwoPane = true;

        // In two-pane mode, list items should be given the
        // 'activated' state when touched.
        ((ItemListFragment) getSupportFragmentManager()
                .findFragmentById(R.id.item_list))
                .setActivateOnItemClick(true);
    }

    // TODO: If exposing deep links into your app, handle intents here.
}

/**
 * Callback method from {@link ItemListFragment.Callbacks}
 * indicating that the item with the given ID was selected.
 */
@Override
public void onItemSelected(String id) {
    if (mTwoPane) {
        // In two-pane mode, show the detail view in this activity by
        // adding or replacing the detail fragment using a
        // fragment transaction.
        Bundle arguments = new Bundle();
        arguments.putString(ItemDetailFragment.ARG_ITEM_ID, id);
        ItemDetailFragment fragment = new ItemDetailFragment();
        fragment.setArguments(arguments);
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.item_detail_container, fragment)
                .commit();

    } else {
        // In single-pane mode, simply start the detail activity
        // for the selected item ID.
        Intent detailIntent = new Intent(this, ItemDetailActivity.class);
        detailIntent.putExtra(ItemDetailFragment.ARG_ITEM_ID, id);
        startActivity(detailIntent);
     }
  }


}

As I understand it, the ItemListActivity is the equivalent of the MainActivity. Is this correct? Which .java file should I place the OnClickListener code in?

Thank you.

1

There are 1 answers

0
Tyler Kiser On

The ItemListActivity is what generates the list on the left-hand side of the screen (or the slide out). The ItemDetailActivity is was handles the main part of the screen. The ItemDetailFragment is where you load your view with your information. Assuming your button is on the detail view as you stated, and that view is called "fragment_item_detail", try putting it in the onCreateView method in the ItemDetailFragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_item_detail, container, false);

    //Your button goes here
    Button btnStart = (Button) rootView.findViewById(R.id.btnTimer);
    btnStart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new CountDownTimer(30000, 1000) {

                public void onTick(long millisUntilFinished) {
                    Toast.makeText(getActivity(), "seconds remaining: " + millisUntilFinished / 1000, Toast.LENGTH_LONG).show();
                }

                public void onFinish() {
                    Toast.makeText(getActivity(), "Finished!", Toast.LENGTH_LONG).show();
                }
            }.start();
        }
    });

    // Show the dummy content as text in a TextView.
    if (mItem != null) {
        ((TextView) rootView.findViewById(R.id.item_detail)).setText(mItem.content);
    }



    return rootView;
}

Note that to get the activity needed for a Toast from a fragment, you must call "getActivity()" instead of "ClassName.this" as the first param.