Question
I am converting my activity to a Fragment. I need help manipulating my activity code to run as a Fragment. I know I have to
I am converting my activity to a Fragment. I need help manipulating my activity code to run as a Fragment.
I know I have to convert the on Create() to be onCreateView() method now. I changed something but I am missing something and I am not sure what I am missing.
My code for the onCreate()...
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreate(savedInstanceState); View v = inflater.inflate(R.layout.activity_fragment, container, false); //mQuoteTextView.setText("This should generate an error. Do you see why?"); //Re-display the same quote we were on when activity destroyed if(savedInstanceState != null){ mCurrentIndex = savedInstanceState.getInt(KEY_QUOTE_INDEX); } Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); //setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); //Display that text for the quote mQuoteTextView = (TextView) findViewById(R.id.quoteTextView); int quote = mQuoteList[mCurrentIndex].getQuote(); mQuoteTextView.setText(quote); mQuoteTextView.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { displayAuthorFact(); } }); //Display the author of the quote mAuthorTextView = (TextView) findViewById(R.id.authorTextView); int author = mQuoteList[mCurrentIndex].getAuthor(); mAuthorTextView.setText(author); mAuthorTextView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { displayAuthorFact(); } }); //Display image mImageView = (ImageView) findViewById(R.id.imageView); mImageView.setImageResource(R.drawable.mountain_pic); //set up listener to handle next button presses mNextButton = (Button) findViewById(R.id.nextButton); mNextButton.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { // move to the next quote in the list //if index reaches end array, //reset index to zero (first quote) mCurrentIndex++; if(mCurrentIndex == mQuoteList.length){ mCurrentIndex = 0; } updateQuote(); } }); return v; }
I need to use this next codes structure
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ View v = inflater.inflate(R.layout.fragment_bug_list, container, false); //Setup floating action button to add a new bug final View addButton = v.findViewById(R.id.add_button); addButton.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View view){ addBug(); } }); return v; }
What am I missing? I can't figure it out. (Using Android Studio)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started