Reputation: 80
I am new to Fragments and i can't define my textbuttons etc in my Fragment i dont know why, i've tried getView() etc... but nothing works, i also dont know what things should go inside onCreateView or what should go on onCreate everythin just looks really confusing. If anyone can help me it woul mean lots, i've tried everything from youtube to stackoverflow, to google!
I dont even know if im using mContext right, the onAttach
method.
P.S ive transferred eveyrthing from a Activity to a Fragment so i might have messed up where things go as i havent seen OnCreate View before.
import android.content.Context;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import android.content.Intent;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Random;
public class HomeFragment extends Fragment implements View.OnClickListener {
private Context mContext;
public String user_answer = "";
public String name= "";
public String current_answer = "";
public int score = 0;
public int current_question_num = 0;
public HomeFragment() {
}
Random random = new Random();
Button btnOption1, btnOption2, btnOption3, btnOption4;
Button btnSubmit;
TextView txtQuestion, txtScore;
public MediaPlayer right, wrong;
Questions questions = new Questions();
@Override
public void onAttach(Context context) {
super.onAttach(context);
mContext=context;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
right = MediaPlayer.create(mContext, R.raw.right);
wrong = MediaPlayer.create(mContext, R.raw.wrong);
//TODO: GET NAME, SCORE FOR ALL FRAGMENTS
init_textViews();
gameLoop();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home, container, false);
}
public void init_textViews() {
btnOption1 = (Button)getView().findViewById(R.id.btnOption1);
btnOption2 = (Button)getView().findViewById(R.id.btnOption2);
btnOption3 = (Button)getView().findViewById(R.id.btnOption3);
btnOption4 = (Button)getView().findViewById(R.id.btnOption4);
btnSubmit = (Button)getView().findViewById(R.id.btnSubmit);
txtQuestion = (TextView) getView().findViewById(R.id.txtQuestion);
txtScore = (TextView) getView().findViewById(R.id.txtScore);
}
public void gameLoop() {
btnSubmit.setText("SUBMIT");
init_textViews();
if (current_question_num < questions.getCurrent_question().length) {
set_answers_questions();
btnOption1.setOnClickListener(this);
btnOption2.setOnClickListener(this);
btnOption3.setOnClickListener(this);
btnOption4.setOnClickListener(this);
btnSubmit.setOnClickListener(this);
txtQuestion.setOnClickListener(this);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
validateAnswer();
}
});
} else {
//TODO: MAKE IT GO TO THE LEADERBOARD FRAGMENT
}
}
public void set_answers_questions() {
btnOption1.setBackgroundColor(Color.rgb(109, 212, 126));
btnOption2.setBackgroundColor(Color.rgb(109, 212, 126));
btnOption3.setBackgroundColor(Color.rgb(109, 212, 126));
btnOption4.setBackgroundColor(Color.rgb(109, 212, 126));
String new_question = questions.getCurrent_question()[current_question_num].toString();
txtQuestion.setText(new_question);
int random_num1 = random.nextInt(questions.getAnswers()[current_question_num].length);
btnOption1.setText(questions.getAnswers()[current_question_num][random_num1]);
int random_num2 = random.nextInt(questions.getAnswers()[current_question_num].length);
while (random_num2 == random_num1) {
random_num2 = random.nextInt(questions.getAnswers()[current_question_num].length);
}
btnOption2.setText(questions.getAnswers()[current_question_num][random_num2]);
int random_num3 = random.nextInt(questions.getAnswers()[current_question_num].length);
while (random_num3 == random_num1 || random_num3 == random_num2) {
random_num3 = random.nextInt(questions.getAnswers()[current_question_num].length);
}
btnOption3.setText(questions.getAnswers()[current_question_num][random_num3]);
int random_num4 = random.nextInt(questions.getAnswers()[current_question_num].length);
while (random_num4 == random_num1 || random_num4 == random_num2 || random_num4 == random_num3) {
random_num4 = random.nextInt(questions.getAnswers()[current_question_num].length);
}
btnOption4.setText(questions.getAnswers()[current_question_num][random_num4]);
current_answer = questions.getCorrect_answer()[current_question_num].toString();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnOption1:
btnOption1.setBackgroundColor(Color.GREEN);
btnOption2.setBackgroundColor(Color.rgb(109, 212, 126));
btnOption3.setBackgroundColor(Color.rgb(109, 212, 126));
btnOption4.setBackgroundColor(Color.rgb(109, 212, 126));
user_answer = btnOption1.getText().toString();
break;
case R.id.btnOption2:
btnOption2.setBackgroundColor(Color.GREEN);
btnOption1.setBackgroundColor(Color.rgb(109, 212, 126));
btnOption3.setBackgroundColor(Color.rgb(109, 212, 126));
btnOption4.setBackgroundColor(Color.rgb(109, 212, 126));
user_answer = btnOption2.getText().toString();
break;
case R.id.btnOption3:
btnOption3.setBackgroundColor(Color.GREEN);
btnOption2.setBackgroundColor(Color.rgb(109, 212, 126));
btnOption1.setBackgroundColor(Color.rgb(109, 212, 126));
btnOption4.setBackgroundColor(Color.rgb(109, 212, 126));
user_answer = btnOption3.getText().toString();
break;
case R.id.btnOption4:
btnOption4.setBackgroundColor(Color.GREEN);
btnOption2.setBackgroundColor(Color.rgb(109, 212, 126));
btnOption3.setBackgroundColor(Color.rgb(109, 212, 126));
btnOption1.setBackgroundColor(Color.rgb(109, 212, 126));
user_answer = btnOption4.getText().toString();
break;
default:
user_answer = "";
break;
}
}
public void validateAnswer() {
if (user_answer.equals(current_answer)) {
right.start();
score++;
if (!(btnOption1.getText().toString().equals(questions.getCorrect_answer()[current_question_num]))) {
btnOption1.setBackgroundColor(Color.WHITE);
}
if (!(btnOption2.getText().toString().equals(questions.getCorrect_answer()[current_question_num]))) {
btnOption2.setBackgroundColor(Color.WHITE);
}
if (!(btnOption3.getText().toString().equals(questions.getCorrect_answer()[current_question_num]))) {
btnOption3.setBackgroundColor(Color.WHITE);
}
if (!(btnOption4.getText().toString().equals(questions.getCorrect_answer()[current_question_num]))) {
btnOption4.setBackgroundColor(Color.WHITE);
}
txtScore.setText("Score: " + score);
btnSubmit.setText("NEXT QUESTION");
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
current_question_num++;
gameLoop();
}
});
} else {
wrong.start();
if (!(btnOption1.getText().toString().equals(questions.getCorrect_answer()[current_question_num]))) {
btnOption1.setBackgroundColor(Color.WHITE);
}
if (!(btnOption2.getText().toString().equals(questions.getCorrect_answer()[current_question_num]))) {
btnOption2.setBackgroundColor(Color.WHITE);
}
if (!(btnOption3.getText().toString().equals(questions.getCorrect_answer()[current_question_num]))) {
btnOption3.setBackgroundColor(Color.WHITE);
}
if (!(btnOption4.getText().toString().equals(questions.getCorrect_answer()[current_question_num]))) {
btnOption4.setBackgroundColor(Color.WHITE);
}
if (btnOption1.getText().toString().equals(user_answer)) {
btnOption1.setBackgroundColor(Color.RED);
} else if (btnOption2.getText().toString().equals(user_answer)) {
btnOption2.setBackgroundColor(Color.RED);
} else if (btnOption3.getText().toString().equals(user_answer)) {
btnOption3.setBackgroundColor(Color.RED);
} else if (btnOption4.getText().toString().equals(user_answer)) {
btnOption4.setBackgroundColor(Color.RED);
}
btnSubmit.setText("NEXT QUESTION");
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
current_question_num++;
gameLoop();
}
});
}
}
}
Upvotes: 2
Views: 3463
Reputation: 1665
You configure the fragment instance in Fragment.onCreate(Bundle)
, but you create and configure the fragment’s view in public View onCreateView(LayoutInflater, ViewGroup, Bundle)
.
This method is where you inflate the layout for the fragment’s view and return the inflated View
to the hosting activity. The LayoutInflater
and ViewGroup
parameters are necessary to inflate the layout. The Bundle
will contain data that this method can use to re-create the view from a saved state.
Getting references in Fragment.onCreateView(…)
works nearly the same as in Activity.onCreate(Bundle)
. The only difference is that you call View.findViewById(int)
on the fragment’s view. The Activity.findViewById(int)
method that you used before is a convenience method that calls View.findViewById(int)
behind the scenes. The Fragment class does not have a corresponding convenience method, so you have to call the real thing.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.id, container, false);
Button mButton = view.findViewById(R.id.button_id);
.
.
.
return view;
}
Upvotes: 4