How to Use ViewPager Button On MainActivity Page? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How do save an Android Activity state using save instance state?Why is the Android emulator so slow? How can we speed up the Android emulator?How do I fix android.os.NetworkOnMainThreadException?ViewPager PagerAdapter not updating the ViewHow do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically?How to determine when Fragment becomes visible in ViewPagerChange Text of TextView in a ViewpagerAdding Social Media Share Logic From Firebase in AndroidHow to get the data saved in a USER_ID?How to add child(Product) under a child(Store) in Firebase Database using RecyclerView
Import keychain to clean macOS install?
IC on Digikey is 5x more expensive than board containing same IC on Alibaba: How?
Like totally amazing interchangeable sister outfit accessory swapping or whatever
What could prevent concentrated local exploration?
Can 'non' with gerundive mean both lack of obligation and negative obligation?
Converting a text document with special format to Pandas DataFrame
Can this water damage be explained by lack of gutters and grading issues?
xkeyval -- read keys from file
Are Flameskulls resistant to magical piercing damage?
false 'Security alert' from Google - every login generates mails from 'no-reply@accounts.google.com'
Why isn't everyone flabbergasted about Bran's "gift"?
How do I deal with an erroneously large refund?
Weaponising the Grasp-at-a-Distance spell
Why do C and C++ allow the expression (int) + 4*5?
Can a Knight grant Knighthood to another?
Do chord progressions usually move by fifths?
Why "Go Out and Learn"
/bin/ls sorts differently than just ls
What is the definining line between a helicopter and a drone a person can ride in?
Assertions In A Mock Callout Test
Alternative to "rest in peace" (RIP)
When does Bran Stark remember Jamie pushing him?
Why does my GNOME settings mention "Moto C Plus"?
How to get a single big right brace?
How to Use ViewPager Button On MainActivity Page?
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How do save an Android Activity state using save instance state?Why is the Android emulator so slow? How can we speed up the Android emulator?How do I fix android.os.NetworkOnMainThreadException?ViewPager PagerAdapter not updating the ViewHow do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically?How to determine when Fragment becomes visible in ViewPagerChange Text of TextView in a ViewpagerAdding Social Media Share Logic From Firebase in AndroidHow to get the data saved in a USER_ID?How to add child(Product) under a child(Store) in Firebase Database using RecyclerView
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
How can I use Viewpager Adapter Button into MainActivity? I am developing a quiz app and I am comparing button value into MainActivity. How can I access ViewPagerAdapter class buttons In MainActivity?
@Override
protected void onStart() {
super.onStart();
Ref.child("Exam_Category").child("PRACTICE TEST").child("PREVIOUS YEAR PAPER'S").child(ct1).child(ct2).child("ENGLISH COMPREHENSION").addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
if (dataSnapshot.exists())
for (DataSnapshot data : dataSnapshot.getChildren())
key = data.getKey().toString();
Ref.child("Exam_Category").child("PRACTICE TEST").child("PREVIOUS YEAR PAPER'S").child(ct1).child(ct2).child("ENGLISH COMPREHENSION").child(String.valueOf(key))
.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
if (dataSnapshot.exists())
final questions studentDetails = dataSnapshot.getValue(questions.class);
list.add(studentDetails);
adapter = new RecyclerViewAdapter(Previous_Yr_Quiz.this, list);
viewPager.setAdapter(adapter);
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
);
//total_q.setText(Question_No +" / "+key);
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
);
This is My ViewPager Adapter
public class RecyclerViewAdapter extends PagerAdapter
// Declare Variables
Context context;
LayoutInflater inflater;
public List<questions> list;
public RecyclerViewAdapter(Context context, List<questions> TempList)
this.context = context;
this.list = TempList;
@Override
public int getCount()
return list.size();
@Override
public boolean isViewFromObject(View view, Object object)
return view == ((RelativeLayout) object);
@Override
public Object instantiateItem(ViewGroup container, final int position)
// Declare Variables
TextView question;
final Button option1,option2,option3,option4;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.quiz_layout_listview, container,
false);
// Locate the TextViews in viewpager_item.xml
question = (TextView)itemView.findViewById(R.id.question);
option1 = (Button) itemView.findViewById(R.id.optiona);
option2 = (Button) itemView.findViewById(R.id.optionb);
option3 = (Button) itemView.findViewById(R.id.optionc);
option4 = (Button) itemView.findViewById(R.id.optiond);
// Capture position and set to the TextViews
questions studentDetails = list.get(position);
question.setText(studentDetails.getQuestion());
option1.setText(studentDetails.getOption1());
option1.setId(position);
option2.setText(studentDetails.getOption2());
option3.setText(studentDetails.getOption3());
option4.setText(studentDetails.getOption4());
option1.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
option1.setBackgroundResource(R.drawable.wrong);
);
((ViewPager) container).addView(itemView);
return itemView;
@Override
public void destroyItem(ViewGroup container, int position, Object object)
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);
private int getItem(int i)
return list.size();
How can used button options A, B, C, D OnClick in MainActivity because I compare button value into A, B, C, D buttons....
android firebase-realtime-database android-viewpager
add a comment |
How can I use Viewpager Adapter Button into MainActivity? I am developing a quiz app and I am comparing button value into MainActivity. How can I access ViewPagerAdapter class buttons In MainActivity?
@Override
protected void onStart() {
super.onStart();
Ref.child("Exam_Category").child("PRACTICE TEST").child("PREVIOUS YEAR PAPER'S").child(ct1).child(ct2).child("ENGLISH COMPREHENSION").addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
if (dataSnapshot.exists())
for (DataSnapshot data : dataSnapshot.getChildren())
key = data.getKey().toString();
Ref.child("Exam_Category").child("PRACTICE TEST").child("PREVIOUS YEAR PAPER'S").child(ct1).child(ct2).child("ENGLISH COMPREHENSION").child(String.valueOf(key))
.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
if (dataSnapshot.exists())
final questions studentDetails = dataSnapshot.getValue(questions.class);
list.add(studentDetails);
adapter = new RecyclerViewAdapter(Previous_Yr_Quiz.this, list);
viewPager.setAdapter(adapter);
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
);
//total_q.setText(Question_No +" / "+key);
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
);
This is My ViewPager Adapter
public class RecyclerViewAdapter extends PagerAdapter
// Declare Variables
Context context;
LayoutInflater inflater;
public List<questions> list;
public RecyclerViewAdapter(Context context, List<questions> TempList)
this.context = context;
this.list = TempList;
@Override
public int getCount()
return list.size();
@Override
public boolean isViewFromObject(View view, Object object)
return view == ((RelativeLayout) object);
@Override
public Object instantiateItem(ViewGroup container, final int position)
// Declare Variables
TextView question;
final Button option1,option2,option3,option4;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.quiz_layout_listview, container,
false);
// Locate the TextViews in viewpager_item.xml
question = (TextView)itemView.findViewById(R.id.question);
option1 = (Button) itemView.findViewById(R.id.optiona);
option2 = (Button) itemView.findViewById(R.id.optionb);
option3 = (Button) itemView.findViewById(R.id.optionc);
option4 = (Button) itemView.findViewById(R.id.optiond);
// Capture position and set to the TextViews
questions studentDetails = list.get(position);
question.setText(studentDetails.getQuestion());
option1.setText(studentDetails.getOption1());
option1.setId(position);
option2.setText(studentDetails.getOption2());
option3.setText(studentDetails.getOption3());
option4.setText(studentDetails.getOption4());
option1.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
option1.setBackgroundResource(R.drawable.wrong);
);
((ViewPager) container).addView(itemView);
return itemView;
@Override
public void destroyItem(ViewGroup container, int position, Object object)
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);
private int getItem(int i)
return list.size();
How can used button options A, B, C, D OnClick in MainActivity because I compare button value into A, B, C, D buttons....
android firebase-realtime-database android-viewpager
add a comment |
How can I use Viewpager Adapter Button into MainActivity? I am developing a quiz app and I am comparing button value into MainActivity. How can I access ViewPagerAdapter class buttons In MainActivity?
@Override
protected void onStart() {
super.onStart();
Ref.child("Exam_Category").child("PRACTICE TEST").child("PREVIOUS YEAR PAPER'S").child(ct1).child(ct2).child("ENGLISH COMPREHENSION").addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
if (dataSnapshot.exists())
for (DataSnapshot data : dataSnapshot.getChildren())
key = data.getKey().toString();
Ref.child("Exam_Category").child("PRACTICE TEST").child("PREVIOUS YEAR PAPER'S").child(ct1).child(ct2).child("ENGLISH COMPREHENSION").child(String.valueOf(key))
.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
if (dataSnapshot.exists())
final questions studentDetails = dataSnapshot.getValue(questions.class);
list.add(studentDetails);
adapter = new RecyclerViewAdapter(Previous_Yr_Quiz.this, list);
viewPager.setAdapter(adapter);
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
);
//total_q.setText(Question_No +" / "+key);
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
);
This is My ViewPager Adapter
public class RecyclerViewAdapter extends PagerAdapter
// Declare Variables
Context context;
LayoutInflater inflater;
public List<questions> list;
public RecyclerViewAdapter(Context context, List<questions> TempList)
this.context = context;
this.list = TempList;
@Override
public int getCount()
return list.size();
@Override
public boolean isViewFromObject(View view, Object object)
return view == ((RelativeLayout) object);
@Override
public Object instantiateItem(ViewGroup container, final int position)
// Declare Variables
TextView question;
final Button option1,option2,option3,option4;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.quiz_layout_listview, container,
false);
// Locate the TextViews in viewpager_item.xml
question = (TextView)itemView.findViewById(R.id.question);
option1 = (Button) itemView.findViewById(R.id.optiona);
option2 = (Button) itemView.findViewById(R.id.optionb);
option3 = (Button) itemView.findViewById(R.id.optionc);
option4 = (Button) itemView.findViewById(R.id.optiond);
// Capture position and set to the TextViews
questions studentDetails = list.get(position);
question.setText(studentDetails.getQuestion());
option1.setText(studentDetails.getOption1());
option1.setId(position);
option2.setText(studentDetails.getOption2());
option3.setText(studentDetails.getOption3());
option4.setText(studentDetails.getOption4());
option1.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
option1.setBackgroundResource(R.drawable.wrong);
);
((ViewPager) container).addView(itemView);
return itemView;
@Override
public void destroyItem(ViewGroup container, int position, Object object)
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);
private int getItem(int i)
return list.size();
How can used button options A, B, C, D OnClick in MainActivity because I compare button value into A, B, C, D buttons....
android firebase-realtime-database android-viewpager
How can I use Viewpager Adapter Button into MainActivity? I am developing a quiz app and I am comparing button value into MainActivity. How can I access ViewPagerAdapter class buttons In MainActivity?
@Override
protected void onStart() {
super.onStart();
Ref.child("Exam_Category").child("PRACTICE TEST").child("PREVIOUS YEAR PAPER'S").child(ct1).child(ct2).child("ENGLISH COMPREHENSION").addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
if (dataSnapshot.exists())
for (DataSnapshot data : dataSnapshot.getChildren())
key = data.getKey().toString();
Ref.child("Exam_Category").child("PRACTICE TEST").child("PREVIOUS YEAR PAPER'S").child(ct1).child(ct2).child("ENGLISH COMPREHENSION").child(String.valueOf(key))
.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
if (dataSnapshot.exists())
final questions studentDetails = dataSnapshot.getValue(questions.class);
list.add(studentDetails);
adapter = new RecyclerViewAdapter(Previous_Yr_Quiz.this, list);
viewPager.setAdapter(adapter);
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
);
//total_q.setText(Question_No +" / "+key);
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
);
This is My ViewPager Adapter
public class RecyclerViewAdapter extends PagerAdapter
// Declare Variables
Context context;
LayoutInflater inflater;
public List<questions> list;
public RecyclerViewAdapter(Context context, List<questions> TempList)
this.context = context;
this.list = TempList;
@Override
public int getCount()
return list.size();
@Override
public boolean isViewFromObject(View view, Object object)
return view == ((RelativeLayout) object);
@Override
public Object instantiateItem(ViewGroup container, final int position)
// Declare Variables
TextView question;
final Button option1,option2,option3,option4;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.quiz_layout_listview, container,
false);
// Locate the TextViews in viewpager_item.xml
question = (TextView)itemView.findViewById(R.id.question);
option1 = (Button) itemView.findViewById(R.id.optiona);
option2 = (Button) itemView.findViewById(R.id.optionb);
option3 = (Button) itemView.findViewById(R.id.optionc);
option4 = (Button) itemView.findViewById(R.id.optiond);
// Capture position and set to the TextViews
questions studentDetails = list.get(position);
question.setText(studentDetails.getQuestion());
option1.setText(studentDetails.getOption1());
option1.setId(position);
option2.setText(studentDetails.getOption2());
option3.setText(studentDetails.getOption3());
option4.setText(studentDetails.getOption4());
option1.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
option1.setBackgroundResource(R.drawable.wrong);
);
((ViewPager) container).addView(itemView);
return itemView;
@Override
public void destroyItem(ViewGroup container, int position, Object object)
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);
private int getItem(int i)
return list.size();
How can used button options A, B, C, D OnClick in MainActivity because I compare button value into A, B, C, D buttons....
android firebase-realtime-database android-viewpager
android firebase-realtime-database android-viewpager
edited Mar 22 at 14:00
Zoe
13.8k85586
13.8k85586
asked Mar 22 at 13:57
punitpunit
136
136
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You have two options:
1) Make Buttons as class members and access them from your Activity by get methods.
2) Write an Interface that will have the callback methods so you know which Button is being clicked. Once you write the interface you pass it as a constructor parameter or by a set method. Something like:
interface QuizListener
void answered(Button whichButton);
...
[RecyclerViewAdapter.java]
public RecyclerViewAdapter(Context context, List<questions> TempList, QuizListener quizListener)
this.context = context;
this.list = TempList;
this.quizListener = quizListener;
...
option1.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
if (quizListener != null)
quizListener.answered(option1);
option1.setBackgroundResource(R.drawable.wrong);
);
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%2f55301221%2fhow-to-use-viewpager-button-on-mainactivity-page%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
You have two options:
1) Make Buttons as class members and access them from your Activity by get methods.
2) Write an Interface that will have the callback methods so you know which Button is being clicked. Once you write the interface you pass it as a constructor parameter or by a set method. Something like:
interface QuizListener
void answered(Button whichButton);
...
[RecyclerViewAdapter.java]
public RecyclerViewAdapter(Context context, List<questions> TempList, QuizListener quizListener)
this.context = context;
this.list = TempList;
this.quizListener = quizListener;
...
option1.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
if (quizListener != null)
quizListener.answered(option1);
option1.setBackgroundResource(R.drawable.wrong);
);
add a comment |
You have two options:
1) Make Buttons as class members and access them from your Activity by get methods.
2) Write an Interface that will have the callback methods so you know which Button is being clicked. Once you write the interface you pass it as a constructor parameter or by a set method. Something like:
interface QuizListener
void answered(Button whichButton);
...
[RecyclerViewAdapter.java]
public RecyclerViewAdapter(Context context, List<questions> TempList, QuizListener quizListener)
this.context = context;
this.list = TempList;
this.quizListener = quizListener;
...
option1.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
if (quizListener != null)
quizListener.answered(option1);
option1.setBackgroundResource(R.drawable.wrong);
);
add a comment |
You have two options:
1) Make Buttons as class members and access them from your Activity by get methods.
2) Write an Interface that will have the callback methods so you know which Button is being clicked. Once you write the interface you pass it as a constructor parameter or by a set method. Something like:
interface QuizListener
void answered(Button whichButton);
...
[RecyclerViewAdapter.java]
public RecyclerViewAdapter(Context context, List<questions> TempList, QuizListener quizListener)
this.context = context;
this.list = TempList;
this.quizListener = quizListener;
...
option1.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
if (quizListener != null)
quizListener.answered(option1);
option1.setBackgroundResource(R.drawable.wrong);
);
You have two options:
1) Make Buttons as class members and access them from your Activity by get methods.
2) Write an Interface that will have the callback methods so you know which Button is being clicked. Once you write the interface you pass it as a constructor parameter or by a set method. Something like:
interface QuizListener
void answered(Button whichButton);
...
[RecyclerViewAdapter.java]
public RecyclerViewAdapter(Context context, List<questions> TempList, QuizListener quizListener)
this.context = context;
this.list = TempList;
this.quizListener = quizListener;
...
option1.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
if (quizListener != null)
quizListener.answered(option1);
option1.setBackgroundResource(R.drawable.wrong);
);
answered Mar 23 at 23:54
Danilo LemesDanilo Lemes
19018
19018
add a comment |
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%2f55301221%2fhow-to-use-viewpager-button-on-mainactivity-page%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