RecyclerView If you press a button, a sound is played
Use the custom RecyclerView application, consisting of one textView and three radioButton.
When you press any radioButton, an audio file is played.
Notes:
When I use MeidaPlayer
Inside RecyclerView
The application does not work
Code class MainActivity
public class MainActivity extends AppCompatActivity {
private String question = {
"The capital of France",
"The capital of India",
"The capital of Iraq"
};
private String answer1 = {"Paris", "New Delhi", "Baghdad" };
private String answer3 = {"Canberra", "Manama", "Brasilia" };
private String answer2 = {"Havana", "Doha", "Moscow" };
///////////////////////////////
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
addQuestion();
}
//////////////////////////////
public void addQuestion() {
List<Question> questionsList = new ArrayList<>();
RecyclerView recyclerView = findViewById(R.id.recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
for (int i = 0; i < question.length; i++) {
String item0 = question[i];
String item1 = answer1[i];
String item2 = answer2[i];
String item3 = answer3[i];
Question questionClass = new Question(item0, item1, item2, item3);
questionsList.add(questionClass);
}
QuesAdapter adapter = new QuesAdapter(questionsList);
recyclerView.setAdapter(adapter);
}
}
This class is intended to add menu items
public class Question {
final String question, answer_1, answer_2, answer_3;
public Question(String question, String answer_1, String answer_2, String answer_3) {
this.question = question;
this.answer_1 = answer_1;
this.answer_2 = answer_2;
this.answer_3 = answer_3;
}
}
This class is for the RecyclerView custom list
Here's the problem
If the button is pressed
I want to play a sound file
public class QuesAdapter extends RecyclerView.Adapter<QuesAdapter.QuesHolder>
{
private List<Question> questionList;
public QuesAdapter(List<Question> questionList) {
this.questionList = questionList;
}
@Override
public QuesHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View row = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.question, viewGroup, false);
QuesHolder holder = new QuesHolder(row);
return holder;
}
@Override
public void onBindViewHolder(QuesHolder viewHolder, int i) {
Question question = questionList.get(i);
TextView ques = viewHolder.question;
final RadioButton answer1 = viewHolder.answer1;
RadioButton answer2 = viewHolder.answer2;
RadioButton answer3 = viewHolder.answer3;
ques.setText(question.question);
answer1.setText(question.answer_1);
answer2.setText(question.answer_2);
answer3.setText(question.answer_3);
/////////////////////////////
// Start: this is the problem
// Press the button An audio file is playing
answer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("TAG", answer1.getText() + "");
}
});
// End: this is the problem
///////////////////////////
}
@Override
public int getItemCount() {
return questionList.size();
}
class QuesHolder extends RecyclerView.ViewHolder {
private TextView question;
private RadioButton answer1, answer2, answer3;
public QuesHolder(View itemView) {
super(itemView);
question = itemView.findViewById(R.id.question);
answer1 = itemView.findViewById(R.id.answer_1);
answer2 = itemView.findViewById(R.id.answer_2);
answer3 = itemView.findViewById(R.id.answer_3);
}
}
}
android android-recyclerview
add a comment |
Use the custom RecyclerView application, consisting of one textView and three radioButton.
When you press any radioButton, an audio file is played.
Notes:
When I use MeidaPlayer
Inside RecyclerView
The application does not work
Code class MainActivity
public class MainActivity extends AppCompatActivity {
private String question = {
"The capital of France",
"The capital of India",
"The capital of Iraq"
};
private String answer1 = {"Paris", "New Delhi", "Baghdad" };
private String answer3 = {"Canberra", "Manama", "Brasilia" };
private String answer2 = {"Havana", "Doha", "Moscow" };
///////////////////////////////
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
addQuestion();
}
//////////////////////////////
public void addQuestion() {
List<Question> questionsList = new ArrayList<>();
RecyclerView recyclerView = findViewById(R.id.recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
for (int i = 0; i < question.length; i++) {
String item0 = question[i];
String item1 = answer1[i];
String item2 = answer2[i];
String item3 = answer3[i];
Question questionClass = new Question(item0, item1, item2, item3);
questionsList.add(questionClass);
}
QuesAdapter adapter = new QuesAdapter(questionsList);
recyclerView.setAdapter(adapter);
}
}
This class is intended to add menu items
public class Question {
final String question, answer_1, answer_2, answer_3;
public Question(String question, String answer_1, String answer_2, String answer_3) {
this.question = question;
this.answer_1 = answer_1;
this.answer_2 = answer_2;
this.answer_3 = answer_3;
}
}
This class is for the RecyclerView custom list
Here's the problem
If the button is pressed
I want to play a sound file
public class QuesAdapter extends RecyclerView.Adapter<QuesAdapter.QuesHolder>
{
private List<Question> questionList;
public QuesAdapter(List<Question> questionList) {
this.questionList = questionList;
}
@Override
public QuesHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View row = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.question, viewGroup, false);
QuesHolder holder = new QuesHolder(row);
return holder;
}
@Override
public void onBindViewHolder(QuesHolder viewHolder, int i) {
Question question = questionList.get(i);
TextView ques = viewHolder.question;
final RadioButton answer1 = viewHolder.answer1;
RadioButton answer2 = viewHolder.answer2;
RadioButton answer3 = viewHolder.answer3;
ques.setText(question.question);
answer1.setText(question.answer_1);
answer2.setText(question.answer_2);
answer3.setText(question.answer_3);
/////////////////////////////
// Start: this is the problem
// Press the button An audio file is playing
answer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("TAG", answer1.getText() + "");
}
});
// End: this is the problem
///////////////////////////
}
@Override
public int getItemCount() {
return questionList.size();
}
class QuesHolder extends RecyclerView.ViewHolder {
private TextView question;
private RadioButton answer1, answer2, answer3;
public QuesHolder(View itemView) {
super(itemView);
question = itemView.findViewById(R.id.question);
answer1 = itemView.findViewById(R.id.answer_1);
answer2 = itemView.findViewById(R.id.answer_2);
answer3 = itemView.findViewById(R.id.answer_3);
}
}
}
android android-recyclerview
2
Please read "How to create a Minimal, Complete, and Verifiable example". Then use the edit link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you.
– GhostCat
Nov 18 '18 at 20:12
where is the mediaplayer? what's the problem actually
– Jins Lukose
Nov 19 '18 at 4:36
I want when I press the button. A sound file is played I think my question is very clear
– Mustafa Max
Nov 19 '18 at 14:01
add a comment |
Use the custom RecyclerView application, consisting of one textView and three radioButton.
When you press any radioButton, an audio file is played.
Notes:
When I use MeidaPlayer
Inside RecyclerView
The application does not work
Code class MainActivity
public class MainActivity extends AppCompatActivity {
private String question = {
"The capital of France",
"The capital of India",
"The capital of Iraq"
};
private String answer1 = {"Paris", "New Delhi", "Baghdad" };
private String answer3 = {"Canberra", "Manama", "Brasilia" };
private String answer2 = {"Havana", "Doha", "Moscow" };
///////////////////////////////
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
addQuestion();
}
//////////////////////////////
public void addQuestion() {
List<Question> questionsList = new ArrayList<>();
RecyclerView recyclerView = findViewById(R.id.recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
for (int i = 0; i < question.length; i++) {
String item0 = question[i];
String item1 = answer1[i];
String item2 = answer2[i];
String item3 = answer3[i];
Question questionClass = new Question(item0, item1, item2, item3);
questionsList.add(questionClass);
}
QuesAdapter adapter = new QuesAdapter(questionsList);
recyclerView.setAdapter(adapter);
}
}
This class is intended to add menu items
public class Question {
final String question, answer_1, answer_2, answer_3;
public Question(String question, String answer_1, String answer_2, String answer_3) {
this.question = question;
this.answer_1 = answer_1;
this.answer_2 = answer_2;
this.answer_3 = answer_3;
}
}
This class is for the RecyclerView custom list
Here's the problem
If the button is pressed
I want to play a sound file
public class QuesAdapter extends RecyclerView.Adapter<QuesAdapter.QuesHolder>
{
private List<Question> questionList;
public QuesAdapter(List<Question> questionList) {
this.questionList = questionList;
}
@Override
public QuesHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View row = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.question, viewGroup, false);
QuesHolder holder = new QuesHolder(row);
return holder;
}
@Override
public void onBindViewHolder(QuesHolder viewHolder, int i) {
Question question = questionList.get(i);
TextView ques = viewHolder.question;
final RadioButton answer1 = viewHolder.answer1;
RadioButton answer2 = viewHolder.answer2;
RadioButton answer3 = viewHolder.answer3;
ques.setText(question.question);
answer1.setText(question.answer_1);
answer2.setText(question.answer_2);
answer3.setText(question.answer_3);
/////////////////////////////
// Start: this is the problem
// Press the button An audio file is playing
answer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("TAG", answer1.getText() + "");
}
});
// End: this is the problem
///////////////////////////
}
@Override
public int getItemCount() {
return questionList.size();
}
class QuesHolder extends RecyclerView.ViewHolder {
private TextView question;
private RadioButton answer1, answer2, answer3;
public QuesHolder(View itemView) {
super(itemView);
question = itemView.findViewById(R.id.question);
answer1 = itemView.findViewById(R.id.answer_1);
answer2 = itemView.findViewById(R.id.answer_2);
answer3 = itemView.findViewById(R.id.answer_3);
}
}
}
android android-recyclerview
Use the custom RecyclerView application, consisting of one textView and three radioButton.
When you press any radioButton, an audio file is played.
Notes:
When I use MeidaPlayer
Inside RecyclerView
The application does not work
Code class MainActivity
public class MainActivity extends AppCompatActivity {
private String question = {
"The capital of France",
"The capital of India",
"The capital of Iraq"
};
private String answer1 = {"Paris", "New Delhi", "Baghdad" };
private String answer3 = {"Canberra", "Manama", "Brasilia" };
private String answer2 = {"Havana", "Doha", "Moscow" };
///////////////////////////////
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
addQuestion();
}
//////////////////////////////
public void addQuestion() {
List<Question> questionsList = new ArrayList<>();
RecyclerView recyclerView = findViewById(R.id.recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
for (int i = 0; i < question.length; i++) {
String item0 = question[i];
String item1 = answer1[i];
String item2 = answer2[i];
String item3 = answer3[i];
Question questionClass = new Question(item0, item1, item2, item3);
questionsList.add(questionClass);
}
QuesAdapter adapter = new QuesAdapter(questionsList);
recyclerView.setAdapter(adapter);
}
}
This class is intended to add menu items
public class Question {
final String question, answer_1, answer_2, answer_3;
public Question(String question, String answer_1, String answer_2, String answer_3) {
this.question = question;
this.answer_1 = answer_1;
this.answer_2 = answer_2;
this.answer_3 = answer_3;
}
}
This class is for the RecyclerView custom list
Here's the problem
If the button is pressed
I want to play a sound file
public class QuesAdapter extends RecyclerView.Adapter<QuesAdapter.QuesHolder>
{
private List<Question> questionList;
public QuesAdapter(List<Question> questionList) {
this.questionList = questionList;
}
@Override
public QuesHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View row = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.question, viewGroup, false);
QuesHolder holder = new QuesHolder(row);
return holder;
}
@Override
public void onBindViewHolder(QuesHolder viewHolder, int i) {
Question question = questionList.get(i);
TextView ques = viewHolder.question;
final RadioButton answer1 = viewHolder.answer1;
RadioButton answer2 = viewHolder.answer2;
RadioButton answer3 = viewHolder.answer3;
ques.setText(question.question);
answer1.setText(question.answer_1);
answer2.setText(question.answer_2);
answer3.setText(question.answer_3);
/////////////////////////////
// Start: this is the problem
// Press the button An audio file is playing
answer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("TAG", answer1.getText() + "");
}
});
// End: this is the problem
///////////////////////////
}
@Override
public int getItemCount() {
return questionList.size();
}
class QuesHolder extends RecyclerView.ViewHolder {
private TextView question;
private RadioButton answer1, answer2, answer3;
public QuesHolder(View itemView) {
super(itemView);
question = itemView.findViewById(R.id.question);
answer1 = itemView.findViewById(R.id.answer_1);
answer2 = itemView.findViewById(R.id.answer_2);
answer3 = itemView.findViewById(R.id.answer_3);
}
}
}
android android-recyclerview
android android-recyclerview
edited Nov 19 '18 at 4:14
Mustafa Max
asked Nov 18 '18 at 18:25
Mustafa MaxMustafa Max
124
124
2
Please read "How to create a Minimal, Complete, and Verifiable example". Then use the edit link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you.
– GhostCat
Nov 18 '18 at 20:12
where is the mediaplayer? what's the problem actually
– Jins Lukose
Nov 19 '18 at 4:36
I want when I press the button. A sound file is played I think my question is very clear
– Mustafa Max
Nov 19 '18 at 14:01
add a comment |
2
Please read "How to create a Minimal, Complete, and Verifiable example". Then use the edit link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you.
– GhostCat
Nov 18 '18 at 20:12
where is the mediaplayer? what's the problem actually
– Jins Lukose
Nov 19 '18 at 4:36
I want when I press the button. A sound file is played I think my question is very clear
– Mustafa Max
Nov 19 '18 at 14:01
2
2
Please read "How to create a Minimal, Complete, and Verifiable example". Then use the edit link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you.
– GhostCat
Nov 18 '18 at 20:12
Please read "How to create a Minimal, Complete, and Verifiable example". Then use the edit link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you.
– GhostCat
Nov 18 '18 at 20:12
where is the mediaplayer? what's the problem actually
– Jins Lukose
Nov 19 '18 at 4:36
where is the mediaplayer? what's the problem actually
– Jins Lukose
Nov 19 '18 at 4:36
I want when I press the button. A sound file is played I think my question is very clear
– Mustafa Max
Nov 19 '18 at 14:01
I want when I press the button. A sound file is played I think my question is very clear
– Mustafa Max
Nov 19 '18 at 14:01
add a comment |
1 Answer
1
active
oldest
votes
Put your sound file inside the raw folder in res directory, you probably would have to make raw folder in res because it is not present when you create project and
final MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
answer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp.start();
Log.i("TAG", answer1.getText() + "");
}
});
This way did not help me thank you
– Mustafa Max
Nov 19 '18 at 15:57
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53364138%2frecyclerview-if-you-press-a-button-a-sound-is-played%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Put your sound file inside the raw folder in res directory, you probably would have to make raw folder in res because it is not present when you create project and
final MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
answer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp.start();
Log.i("TAG", answer1.getText() + "");
}
});
This way did not help me thank you
– Mustafa Max
Nov 19 '18 at 15:57
add a comment |
Put your sound file inside the raw folder in res directory, you probably would have to make raw folder in res because it is not present when you create project and
final MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
answer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp.start();
Log.i("TAG", answer1.getText() + "");
}
});
This way did not help me thank you
– Mustafa Max
Nov 19 '18 at 15:57
add a comment |
Put your sound file inside the raw folder in res directory, you probably would have to make raw folder in res because it is not present when you create project and
final MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
answer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp.start();
Log.i("TAG", answer1.getText() + "");
}
});
Put your sound file inside the raw folder in res directory, you probably would have to make raw folder in res because it is not present when you create project and
final MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
answer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp.start();
Log.i("TAG", answer1.getText() + "");
}
});
answered Nov 19 '18 at 14:52
Taha wakeelTaha wakeel
896
896
This way did not help me thank you
– Mustafa Max
Nov 19 '18 at 15:57
add a comment |
This way did not help me thank you
– Mustafa Max
Nov 19 '18 at 15:57
This way did not help me thank you
– Mustafa Max
Nov 19 '18 at 15:57
This way did not help me thank you
– Mustafa Max
Nov 19 '18 at 15:57
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53364138%2frecyclerview-if-you-press-a-button-a-sound-is-played%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Please read "How to create a Minimal, Complete, and Verifiable example". Then use the edit link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you.
– GhostCat
Nov 18 '18 at 20:12
where is the mediaplayer? what's the problem actually
– Jins Lukose
Nov 19 '18 at 4:36
I want when I press the button. A sound file is played I think my question is very clear
– Mustafa Max
Nov 19 '18 at 14:01