I have multiple card views in my Main Fragment that go to different fragments within the app usin onClickListener functionality.
However, when the cardviews are selected the fragment that shows is always the last replaced.
Ex: In main activity all the card views, when selected, show the affirmations fragment. No matter if they have been connected to their proper fragment.
MainActivity.java
#Override
public void onCardSelected() {
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_fragment, new ProfileFragment());
fragmentTransaction.replace(R.id.container_fragment, new BibleFragment());
fragmentTransaction.replace(R.id.container_fragment, new ShopFragment());
fragmentTransaction.replace(R.id.container_fragment, new NotiFragment());
fragmentTransaction.replace(R.id.container_fragment, new CalFragment());
fragmentTransaction.replace(R.id.container_fragment, new AffFragment());
fragmentTransaction.commit();
}
MainFragment.java
public class MainFragment extends Fragment {
private onFragmentBtnSelected listener;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_main,container,false);
CardView clickCardProfile = view.findViewById(R.id.cardProfile);
CardView clickCardBible = view.findViewById(R.id.cardBible);
CardView clickCardShop = view.findViewById(R.id.cardShop);
CardView clickCardNoti = view.findViewById(R.id.cardNotifications);
CardView clickCardCalendar = view.findViewById(R.id.cardCalendar);
CardView clickCardAffirmations = view.findViewById(R.id.cardAffirmations);
/*Button clickme = view.findViewById(R.id.clickHF);
clickme.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onBtnSelected();
}
});
return view;*/
clickCardProfile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onCardSelected();
}
});
//return view;
clickCardBible.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onCardSelected();
}
});
//return view;
clickCardShop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onCardSelected();
}
});
clickCardNoti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onCardSelected();
}
});
clickCardCalendar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onCardSelected();
}
});
clickCardAffirmations.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onCardSelected();
}
});
return view;
}
#Override
public void onAttach(#NonNull Context context){
super.onAttach(context);
if(context instanceof onFragmentBtnSelected ){
listener =(onFragmentBtnSelected) context;
}
else {
throw new ClassCastException(context.toString() + " must implement listiner");
}
}
public interface onFragmentBtnSelected{
//public void onBtnSelected();
public void onCardSelected();
}
Would I need to create an interface method for each of the buttons, and separate the respective replace statements or is there a way to have them all in the same method and switch from fragment to fragment?
Related
Below code of adapter of RecyclerView
public interface SelectMarket {
void selectMarket(DataMarket dataMarket);
}
public class viewHolder extends RecyclerView.ViewHolder{
TextView marketTitleName;
public viewHolder(#NonNull View itemView) {
super(itemView);
marketTitleName = itemView.findViewById(R.id.market_title_custom);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
selectMarket.selectMarket(marketName.get(getAdapterPosition()));
Toast.makeText(ctx, "id "+getAdapterPosition(), Toast.LENGTH_SHORT).show();
}
});
}
}
In the code I shows you the ViewHolder
where I set onClickListener
I want to click on that and then go to other Fragment called SelectRoomFragment
and in Method of SelectMarket() I wrote inside the codes which will help me to go the the Next Fragment like this :
#Override
public void selectMarket(DataMarket dataMarket) {
SelectRoomFragment selectRoomFragment= new SelectRoomFragment();
Bundle args = new Bundle();
args.putString("market",dataMarket.getTitle());
selectRoomFragment.setArguments(args);
getFragmentManager().beginTransaction().add(R.id.fragment_container,
selectRoomFragment).commit();
}
The Output Is
Which is not looking Good like I want.
move the code below to onBindViewHolder
marketTitleName = itemView.findViewById(R.id.market_title_custom);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
selectMarket.selectMarket(marketName.get(getAdapterPosition()));
Toast.makeText(ctx, "id "+getAdapterPosition(),Toast.LENGTH_SHORT).show();
}
});
I am trying to pass information from my alert dialog to my second fragment (I am using tabbed activity layout).
I want to pass information from alert dialog to fragment when I click on my ImageView, but my app keep crashing until I implement my interface inside MainActivity.java. My main mission here is to open alert dialog which contains several buttons. When I click first button I want to print "Test br 3" but it does not work inside my Fragment, it only works inside my MainActivity.java where method prints "Test br 2".
My Main Activity
public class MainActivity extends AppCompatActivity implements ExercisesAlertDialog.DataTransfer {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
}
#Override
public void ApplyData() {
System.out.println("Test br 2");
}
My Fragment
public class Frag2 extends Fragment implements ExercisesAlertDialog.DataTransfer {
ImageView plusbtn;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.frag2, container, false);
plusbtn = (ImageView) v.findViewById(R.id.plusbtn);
plusbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ExercisesDialog();
}
});
return v;
}
public void ExercisesDialog()
{
ExercisesAlertDialog exercisesAlertDialog = new ExercisesAlertDialog();
exercisesAlertDialog.show(getFragmentManager(), "Exercises Dialog");
}
#Override
public void ApplyData() {
System.out.println("Test br 3");
}
My Alert Dialog
public class ExercisesAlertDialog extends AppCompatDialogFragment {
ImageButton one, two;
private DataTransfer listener;
public int first;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.workout_custom_menu, null);
builder.setView(v);
builder.setTitle("Choose your Workout");
builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
});
one = (ImageButton) v.findViewById(R.id.first);
two = (ImageButton) v.findViewById(R.id.second);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
first = 1;
if(first ==1)
{
listener.ApplyData();
}
}
});
return builder.create();
}
public interface DataTransfer
{
void ApplyData();
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
listener = (DataTransfer) context;
}
catch (ClassCastException e)
{
throw new ClassCastException(toString() + "Must implement DataTransfer");
}
}
}
I changed getChildFragmentManager() instead of getFragmentManager() in the show() call. Second thing is onAttach() to listener = (DataTransfer) getParentFragment(); instead of context.
I created my test project where i code to communicate between two fragments but actully I want to access activity from fragment.
Here is code to connect fragment to fragment, its working absolutely right without any error but now i want to change this code to connect activity from fragment instead of fragment to fragment communication.
So Please change this code to access activities from fragment. I stuck on this issue for than a week.So Guys please resolve this.
here is my mainaactivity:
public class MainActivity extends AppCompatActivity implements FragmentA.FragmentAListener, FragmentB.FragmentBListener {
private FragmentA fragmentA;
private FragmentB fragmentB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentA = new FragmentA();
fragmentB = new FragmentB();
getSupportFragmentManager().beginTransaction()
.replace(R.id.container_a, fragmentA)
.replace(R.id.container_b, fragmentB)
.commit();
}
#Override
public void onInputASent(CharSequence input) {
fragmentB.updateEditText(input);
}
#Override
public void onInputBSent(CharSequence input) {
fragmentA.updateEditText(input);
}
Here is my FragmentA.java:
public class FragmentA extends Fragment {
private FragmentAListener listener;
private EditText editText;
private Button buttonOk;
public interface FragmentAListener {
void onInputASent(CharSequence input);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_a, container, false);
editText = v.findViewById(R.id.edit_text);
buttonOk = v.findViewById(R.id.button_ok);
buttonOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CharSequence input = editText.getText();
listener.onInputASent(input);
}
});
return v;
}
public void updateEditText(CharSequence newText) {
editText.setText(newText);
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof FragmentAListener) {
listener = (FragmentAListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement FragmentAListener");
}
}
#Override
public void onDetach() {
super.onDetach();
listener = null;
}
}
Here is my FragmentB.java:
public class FragmentB extends Fragment {
private FragmentBListener listener;
private EditText editText;
private Button buttonOk;
public interface FragmentBListener {
void onInputBSent(CharSequence input);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_b, container, false);
editText = v.findViewById(R.id.edit_text);
buttonOk = v.findViewById(R.id.button_ok);
buttonOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CharSequence input = editText.getText();
listener.onInputBSent(input);
}
});
return v;
}
public void updateEditText(CharSequence newText) {
editText.setText(newText);
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof FragmentBListener) {
listener = (FragmentBListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement FragmentBListener");
}
}
#Override
public void onDetach() {
super.onDetach();
listener = null;
}
}
Here is my Fertilizers.java file which i want to access from FragmentA.:
public class Fertilizers extends AppCompatActivity {
RecyclerView mRecyclerView;
List<FertilizerData> myFertilizersList;
FertilizerData mFertilizersData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fertilizers);
mRecyclerView = (RecyclerView)findViewById(R.id.recyclerView);
GridLayoutManager gridLayoutManager;
gridLayoutManager = new GridLayoutManager(Fertilizers.this, 1);
mRecyclerView.setLayoutManager(gridLayoutManager);
myFertilizersList = new ArrayList<>();
mFertilizersData = new FertilizerData("Urea Fertilizer","Urea is a concent","Rs.1900",R.drawable.urea);
myFertilizersList.add(mFertilizersData);
myFertilizersList.add(mFertilizersData); }
}
please write here a block of code to call Fertilzers Activity from FragmentA, I,ll be very thankful to you.
Calling getActivity() in your fragment gives you the calling activity so if MainActivity started your fragment then you would do
(MainActivity(getActivity())).something_from_your_main_activity
Solution found by itself regarding this issue.
FragmentHome.java class should look like this:
public class FragmentHome extends Fragment {
private Button button;
public FragmentHome(){
}
public interface OnMessageReadListener
{
public void onMessageRead(String message);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home, container, false);
button = (Button)v.findViewById(R.id.bn);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), Fertilizers.class);
intent.putExtra("some"," some data");
startActivity(intent);
}
});
return v;
}
}
FertilizersActivity.java should look like this:
public class Fertilizers extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fertilizers);
Bundle bundle = getIntent().getExtras();
if (bundle != null){
if(bundle.getStringArrayList("some") !=null){
Toast.makeText(getApplicationContext(),"data:" + bundle.getStringArrayList("some"),Toast.LENGTH_LONG).show();
}
}
}
}
Hello need help with a class trying to figure out how to create a first and last button in a fragment that will jump to the first item in a ViewPager and the last item in a ViewPager I'm a bit lost with how to implement. Here is the fragment.
public class CrimeFragment extends Fragment {
private static final String ARG_CRIME_ID = "crime_id";
private static final String DIALOG_DATE = "DialogDate";
private static final int REQUEST_DATE = 0;
private Crime mCrime;
private EditText mTitleField;
private Button mDateButton;
private CheckBox mSolvedCheckBox;
private Spinner mSpinner;
private Button mFirstButton;
private Button mLastButton;
public static CrimeFragment newInstance(UUID crimeId) {
Bundle args = new Bundle();
args.putSerializable(ARG_CRIME_ID, crimeId);
CrimeFragment fragment = new CrimeFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK) {
return;
}
if (requestCode == REQUEST_DATE) {
Date date =
(Date) data.getSerializableExtra(DatePickerFragment.EXTRA_DATE);
mCrime.setDate(date);
updateDate();
}
}
private void updateDate() {
mDateButton.setText(mCrime.getDate().toString());
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
UUID crimeId = (UUID) getArguments().getSerializable(ARG_CRIME_ID);
mCrime = CrimeLab.get(getActivity()).getCrime(crimeId);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_crime, container, false);
mTitleField = (EditText)v.findViewById(R.id.crime_title);
mTitleField.setText(mCrime.getTitle());
mTitleField.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(
CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(
CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
mDateButton = (Button) v.findViewById(R.id.crime_date);
updateDate();
mDateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager manager = getFragmentManager();
DatePickerFragment dialog = DatePickerFragment.newInstance(mCrime.getDate());
dialog.setTargetFragment(CrimeFragment.this, REQUEST_DATE);
dialog.show(manager, DIALOG_DATE);
}
});
mSpinner = (Spinner) v.findViewById(R.id.crime_severity);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this.getActivity(),
R.array.severity_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adapter);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
mCrime.setSeverity((String) mSpinner.getSelectedItem());
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
mSolvedCheckBox = (CheckBox) v.findViewById(R.id.crime_solved);
mSolvedCheckBox.setChecked(mCrime.isSolved());
mSolvedCheckBox.setOnCheckedChangeListener(
new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
mCrime.setSolved(isChecked);
}
});
mFirstButton = (Button) v.findViewById(R.id.first_crime_button);
mFirstButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
mLastButton = (Button) v.findViewById(R.id.last_crime_button);
mLastButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
return v;
}
}
Here is the Activity with the ViewPager.
public class CrimePagerActivity extends FragmentActivity{
private static final String EXTRA_CRIME_ID = "com.martin.android.criminalintent.crime_id";
private ViewPager mViewPager;
private List<Crime> mCrimes;
public static Intent newIntent(Context packageContext, UUID crimeId) {
Intent intent = new Intent(packageContext, CrimePagerActivity.class);
intent.putExtra(EXTRA_CRIME_ID, crimeId);
return intent;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crime_pager);
UUID crimeId = (UUID) getIntent().getSerializableExtra(EXTRA_CRIME_ID);
mViewPager = (ViewPager) findViewById(R.id.activity_crime_pager_view_pager);
mCrimes = CrimeLab.get(this).getCrimes();
FragmentManager fragmentManager = getSupportFragmentManager();
mViewPager.setAdapter(new FragmentStatePagerAdapter(fragmentManager) {
#Override
public Fragment getItem(int position) {
Crime crime = mCrimes.get(position);
return CrimeFragment.newInstance(crime.getId());
}
#Override
public int getCount() {
return mCrimes.size();
}
});
for (int i = 0; i < mCrimes.size(); i++) {
if (mCrimes.get(i).getId().equals(crimeId)) {
mViewPager.setCurrentItem(i);
break;
}
}
}
}
For the First button Im pretty sure I just need to set item to 0? For last button I need to set item to size - 1 just not sure how to do that.
In your CrimePagerActivity, write two method's to jump to the ViewPager first page and last page.
CrimePagerActivity.java:
public void jumpToFirstPage() {
mViewPager.setCurrentItem(0);
}
public void jumpToLastPage() {
mViewPager.setCurrentItem(mCrimes.size() - 1);
}
Call those method's from mFirstButton and mLastButton click listener method:
CrimeFragment.java:
mFirstButton = (Button) v.findViewById(R.id.first_crime_button);
mFirstButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((CrimePagerActivity)getActivity()).jumpToFirstPage();
}
});
mLastButton = (Button) v.findViewById(R.id.last_crime_button);
mLastButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((CrimePagerActivity)getActivity()).jumpToLastPage();
}
});
Hope this will work perfectly.
Ok this was a answer that I was given by the teacher incase anyone is interested.
mFirstButton = (Button) v.findViewById(R.id.first_crime_button);
mFirstButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ViewPager pager = (ViewPager) getActivity().findViewById(R.id.activity_crime_pager_view_pager);
pager.setCurrentItem(0);
}
});
mLastButton = (Button) v.findViewById(R.id.last_crime_button);
mLastButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ViewPager pager = (ViewPager) getActivity().findViewById(R.id.activity_crime_pager_view_pager);
pager.setCurrentItem(CrimeLab.get(getActivity()).getCrimes().size()-1);
}
});
Create two button and on click set
viewPager.setCurrentItem(0); // for top
and
viewPager.setCurrentItem(mCrimes.size()); // for last
I am trying to implement a quiz whereby the user selects one from four buttons. When they click on the button it becomes selected, but then what I want to do is make any other button that was selected before to be reset to normal. This is my class and code, but it doesn't disable the other buttons:
public class QuizOne extends Fragment{
Button one;
Button two;
Button three;
Button four;
boolean selected = false;
public QuizOne(){
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.quiz, container, false);
one = (Button) rootView.findViewById(R.id.ones);
two = (Button) rootView.findViewById(R.id.twos);
three = (Button) rootView.findViewById(R.id.threes);
four = (Button) rootView.findViewById(R.id.fours);
selectedButton();
return rootView;
}
public void selectedButton(){
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(selected){
one.setBackgroundColor(Color.YELLOW);
}
else {
one.setBackgroundColor(Color.GRAY);
selected = true;
}
}
});
two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(selected){
two.setBackgroundColor(Color.YELLOW);
}
else {
two.setBackgroundColor(Color.GRAY);
selected = true;
}
}
});
three.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(selected){
three.setBackgroundColor(Color.YELLOW);
}
else {
three.setBackgroundColor(Color.GRAY);
selected = true;
}
}
});
four.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(selected){
four.setBackgroundColor(Color.YELLOW);
}
else {
four.setBackgroundColor(Color.GRAY);
selected = true;
}
}
});
}
}
So its the selectedButton() method that I am struggling on. Because say if a user clicks on a button and then changes their mind after and selects another button then the previous button should be disabled. Also after clicking the button I want to go to the next tab
You might want to rethink your whole design. There's easier ways with way less code. Try something like this :
public class QuizOne extends Fragment implements View.OnClickListener {
....
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.quiz, container, false);
one = (Button) rootView.findViewById(R.id.ones);
one.setOnClickListener(this);
// and so on ...
#Override
public void onClick(View v) {
setButtonsGray();
v.setBackgroundColor(COLOR.Yellow);
}
private void setButtonsGray(){
one.setBackgroundColor(COLOR.Gray);
two.setBackgroundColor(COLOR.Gray);
// and so on ...
}
If i clearly understand your problem , one way to do this if you want to follow your first write logic is when you click your button to set yellow color to it and gray color to evryone exept clicket button
public void selectedButton() {
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
one.setBackgroundColor(Color.YELLOW);
two.setBackgroundColor(Color.GRAY);
three.setBackgroundColor(Color.GRAY);
four.setBackgroundColor(Color.GRAY);
}
});
two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
two.setBackgroundColor(Color.YELLOW);
one.setBackgroundColor(Color.GRAY);
three.setBackgroundColor(Color.GRAY);
four.setBackgroundColor(Color.GRAY);
}
});
three.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
three.setBackgroundColor(Color.YELLOW);
one.setBackgroundColor(Color.GRAY);
two.setBackgroundColor(Color.GRAY);
four.setBackgroundColor(Color.GRAY);
}
});
four.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
four.setBackgroundColor(Color.YELLOW);
one.setBackgroundColor(Color.GRAY);
two.setBackgroundColor(Color.GRAY);
three.setBackgroundColor(Color.GRAY);
}
});
}
but there are many ways to achive this for sure..