I'm trying to make my first app which has a set of 15 buttons. when you press a button the color changes between two numbers.Then if you press a specificity different "commit" button the buttons won't change colors any more.
My question right now is how would I be able to iterate through the buttons on the screen? I believe I need a assign the buttons a "name", "type", or something like it, and then find all instances where that happens but I cannot find the relevant getter/setter methods.
Here is my code so far:
public void clickHandler(View view) {
Button btn = (Button) view;
int id = btn.getId();
boolean clicked = isChosen(btn);
if( clicked == true && id != 1) {
btn.setBackgroundColor(-3355444);
}
else{
btn.setBackgroundColor(-16777216);
}
}
public boolean isChosen(Button btn){
ColorDrawable buttonColor = (ColorDrawable) btn.getBackground();
int colorId = buttonColor.getColor();
if(colorId == -16777216){
return true;
}
else return false;
}
public boolean player = true;
public void changeTurn(View view) {
TextView t = (TextView) findViewById(R.id.textView3);
if(player == false) {
t.setText("Player 2's turn");
player = true;
}
else{
t.setText("Player 1's turn");
player = false;
}
Hope this piece of code helps
final int buttonCount = 15;
LinearLayout mLayout = (LinearLayout) findViewById(R.id.pager);
for (int i = 0; i < buttonCount; i++) {
TextView textView = new TextView(this);
textView.setTag(String.valueOf(i));
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int buttonID = Integer.valueOf(view.getTag().toString());
updateColorForButtonAtPosition(buttonCount);
}
});
mLayout.addView(textView);
}
private void updateColorForButtonAtPosition (int buttonCount){
// add your functionality here
}
Related
I want select only one radio button in recyclerView and get its data in its Activity.
I have gone through following solutions :
Single selection in RecyclerView
Select only one radiobutton in a recyclerview
And I made one solution :
private static int lastCheckedPos = -1;
binding.radioButton.setChecked(mImagesList.get(position).isSelected());
binding.radioButton.setTag(new Integer(position));
//for default check in first item
if(position == 0 && mImagesList.get(0).isSelected() && binding.radioButton.isChecked())
{
radioButton = binding.radioButton;
lastCheckedPos = 0;
}
binding.radioButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
RadioButton cb = (RadioButton)v;
int clickedPos = ((Integer)cb.getTag()).intValue();
if(cb.isChecked())
{
if(radioButton != null)
{
radioButton.setChecked(false);
mImagesList.get(lastCheckedPos).setSelected(false);
}
radioButton = cb;
lastCheckedPos = clickedPos;
}
else
radioButton = null;
mImagesList.get(clickedPos).setSelected(cb.isChecked());
}
});
I have written this in onBindViewHolder method.
Now to get data i have written this in adapter :
public String getUserId() {
if(lastCheckedPos == -1)
{
return null;
} else {
return mImagesList.get(lastCheckedPos).getUser_code();
}
}
And get this in activity :
userId = adapter.getUserId();
But I am not able to get any data in activity. It is always showing null. Also if I click twice on radio button then it deselected.
Please ask if anything unclear. Any help would be appreciated.
Thank you :)
Replace int clickedPos = ((Integer)cb.getTag()).intValue(); by int clickedPos =position;
I am trying to change ImageView background when clicked(like Duolingo)
Here is my code from fragment :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.frag_repeat, container, false);
final int[] a1 = {0};
final int[] a2 = {0};
final int[] a3 = {0};
final int[] a4 = {0};
TypedArray itemsIcon = getResources().obtainTypedArray(R.array.nav_drawer_icons);
ImageView wer1 = (ImageView) rootView.findViewById(R.id.imageView);
ImageView wer2 = (ImageView) rootView.findViewById(R.id.imageView2);
ImageView wer3 = (ImageView) rootView.findViewById(R.id.imageView23);
ImageView wer4 = (ImageView) rootView.findViewById(R.id.imageView43);
TextView textView1 = (TextView) rootView.findViewById(R.id.textView711);
textView1.setText(ss[i]);
wer1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a1[0] = 1;
a2[0] = 0;
a3[0] = 0;
a4[0] = 0;
}
});
wer2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a2[0] = 1;
a1[0] = 0;
a3[0] = 0;
a4[0] = 0;
}
});
wer3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a3[0] = 1;
a2[0] = 0;
a1[0] = 0;
a4[0] = 0;
}
});
wer4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a4[0] = 1;
a2[0] = 0;
a3[0] = 0;
a1[0] = 0;
}
});
if(a1[0] > 0){
wer3.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer2.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer1.setBackgroundColor(Color.parseColor("#42A5F5"));
wer4.setBackgroundColor(Color.parseColor("#FFFFFF"));
} else if(a2[0] > 0){
wer1.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer3.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer2.setBackgroundColor(Color.parseColor("#42A5F5"));
wer4.setBackgroundColor(Color.parseColor("#FFFFFF"));
}else if(a3[0] > 0){
wer1.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer2.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer3.setBackgroundColor(Color.parseColor("#42A5F5"));
wer4.setBackgroundColor(Color.parseColor("#FFFFFF"));
} else if(a4[0] > 0){
wer1.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer2.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer4.setBackgroundColor(Color.parseColor("#42A5F5"));
wer3.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
if(i1 == 1){
wer1.setImageResource(itemsIcon.getResourceId(i, -1));
} else if(i1 == 2){
wer2.setImageResource(itemsIcon.getResourceId(i, -1));
} else if(i1 == 3){
wer3.setImageResource(itemsIcon.getResourceId(i, -1));
} else if(i1 == 4){
wer4.setImageResource(itemsIcon.getResourceId(i, -1));
}
return rootView;
}
But ImageView background doesn't change! I have CardViews inside RelativeLayout and inside RelativeLayout I have ImageView.
Please share xml file. Maybe you don't set background for ImageView in xml
Each of your onClick listeners is just toggling values in an array. I suspect what you really want is to invoke the setBackground color code inside each handler.
From your code, I think what you are trying to do is this:
Whichever button was clicked - change it's background color from white to #42A5F5.
For the other three buttons not clicked, change the background color back to white.
I don't know what the name of your class is (I'll refer to it as "YourClass"). But make your 4 ImageViews member variables.
class YourClass extends Fragment // I'm guessing this is the declaration, it doesn't matter - use whatever you have now
{
ImageView _wer1;
ImageView _wer2;
ImageView _wer3;
ImageView _wer4;
Then inside onCreateView, assign each one of these member variables exactly where you assign the local variables:
_wer1 = (ImageView) rootView.findViewById(R.id.imageView);
_wer2 = (ImageView) rootView.findViewById(R.id.imageView2);
_wer3 = (ImageView) rootView.findViewById(R.id.imageView23);
_wer4 = (ImageView) rootView.findViewById(R.id.imageView43);
Now set each of your click listeners to invoke a "ToggleBackgroundColors". Notice that there's a different index value passed to this function in each callback handler.
_wer1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
YourClass.this.ToggleBackgroundColors(0);
}
});
_wer2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
YourClass.this.ToggleBackgroundColors(1);
}
});
_wer3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
YourClass.this.ToggleBackgroundColors(2);
}
});
_wer4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
YourClass.this.ToggleBackgroundColors(3);
}
});
Then implement ToggleBackgroundColors:
void ToggleBackgroundColors(int which)
{
int highlight = Color.parseColor("#42A5F5");
ImageView [] views = {_wer1, _wer2, _wer3, _wer4};
for (int x = 0; x < views.length; x++)
{
int background = (which == x) ? highlight : Color.WHITE;
views[x].setBackgroundColor(background);
}
}
In your onClickListener, you can also set colorFilter on your imageView when you onClick it.
Consider using the onTouchListener instead though as you would like the imageView to change color when you touch the button and not when you click on it.
ImageView iv = (ImageView)findViewById(resIdOfImageToFilter);
iv.setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
I'm a beginner Android developer. I have list text when clicked next change text in one textview. I created imagelist how can when click next change imageview same time textview
public class Home extends Fragment implements View.OnClickListener {
int stringIdList[] = {R.string.one,R.string.two,R.string.three,R.string.four,R.string.five};
int imageIdList[] = {R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four,R.drawable.five};
int stringListCounter = 0;
int imageListCounter = 0;
TextView text21;
ImageView image2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home, container, false);
text21=(TextView) rootView.findViewById(R.id.dyktext);
image2=(ImageView) rootView.findViewById(R.id.imagedyk);
ImageButton next = (ImageButton)rootView.findViewById(R.id.next);
ImageButton previous = (ImageButton) rootView.findViewById(R.id.previous);
#Override
public void onClick(View v) {
int id = v.getId();
if(id == R.id.next && stringListCounter < stringIdList.length - 1 ) {
stringListCounter++;
}else if (id == R.id.previous && stringListCounter > 0 ) {
stringListCounter--;
}
text21.setText(stringIdList[stringListCounter]);
}
}
Try this if I understood you correctly...
#Override
public void onClick(View v) {
int id = v.getId();
boolean updateText = false,
updateImage = false;
if(id == R.id.next) {
if (stringListCounter < stringIdList.length - 1 ) {
stringListCounter++;
updateText = true;
}
if (imageListCounter < imageIdList.length - 1 ) {
imageListCounter++;
updateImage = true;
}
}else if (id == R.id.previous) {
if (stringListCounter > 0 ) {
stringListCounter--;
updateText = true;
}
if (imageListCounter > 0 ) {
imageListCounter--;
updateImage = true;
}
}
if (updateText) {
text21.setText(stringIdList[stringListCounter]);
}
if (updateImage) {
image2.setImageResource(imageIdList[imageListCounter]);
}
}
I'm not sure, if I understand your question correctly.
You would like to change the image in the ImageView at the same moment as you change yout text in the TextView, right?
In this case, you simply can add the command for setting a image resource below your setText() command.
In your case:
...
text21.setText(stringIdList[stringListCounter]);
image2.setImageResource(imageIdList[stringListCounter]);
Another hint: you should give your variables meaningful names (for example "textViewTitle" or "descriptionTV" ... instead of "text21")
I hope that I could help you!
I am using a recyclerView inside my navigation drawer and I am using this library Twoway-view to get click and selection support.
It works perfectly and I can change the color of the text and the icons without problems inside OnClick method for each position:
itemClickSupport.setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
#Override
public void onItemClick(RecyclerView parent, View view, int position, long id) {
TypedValue typedValue = new TypedValue();
MainActivity.this.getTheme().resolveAttribute(R.attr.colorAccent, typedValue, true);
final int color = typedValue.data;
//TODO Icon and text colors
for (int i = 0; i < drawerTitles.length; i++){
if (i == position){
ImageView imageViewDrawerIcon = (ImageView) recyclerViewDrawer.getChildAt(i).findViewById(R.id.imageViewDrawerIcon);
TextView textViewDrawerTitle = (TextView) recyclerViewDrawer.getChildAt(i).findViewById(R.id.textViewDrawerItemTitle);
imageViewDrawerIcon.setColorFilter(color);
if(Build.VERSION.SDK_INT > 15){
imageViewDrawerIcon.setImageAlpha(255);
}else{
imageViewDrawerIcon.setAlpha(255);
}
textViewDrawerTitle.setTextColor(color);
RelativeLayout relativeLayoutDrawerItem = (RelativeLayout) recyclerViewDrawer.getChildAt(i).findViewById(R.id.relativeLayoutDrawerItem);
relativeLayoutDrawerItem.setFocusableInTouchMode(true);
}else{
ImageView imageViewDrawerIcon = (ImageView) recyclerViewDrawer.getChildAt(i).findViewById(R.id.imageViewDrawerIcon);
TextView textViewDrawerTitle = (TextView) recyclerViewDrawer.getChildAt(i).findViewById(R.id.textViewDrawerItemTitle);
imageViewDrawerIcon.setColorFilter(getResources().getColor(R.color.md_text));
if(Build.VERSION.SDK_INT > 15){
imageViewDrawerIcon.setImageAlpha(138);
}else{
imageViewDrawerIcon.setAlpha(138);
}
textViewDrawerTitle.setTextColor(getResources().getColor(R.color.md_text));
RelativeLayout relativeLayoutDrawerItem = (RelativeLayout) recyclerViewDrawer.getChildAt(i).findViewById(R.id.relativeLayoutDrawerItem);
relativeLayoutDrawerItem.setFocusableInTouchMode(false);
}
}
//TODO Fragments (closedrawers before setfragment)
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// Do something after some time
}
}, 250);
mDrawerLayout.closeDrawers();
}
});
The problem is that I want to access to the first item like in that method, but before it (to set the first item selected, with different color, etc).
But I get a crash (null reference) when I try to get the first item outside OnClick method:
ImageView imageViewDrawerIcon = (ImageView) recyclerViewDrawer.getChildAt(0).findViewById(R.id.imageViewDrawerIcon);
All code:
// Setup RecyclerView inside drawer
recyclerViewDrawer = (RecyclerView) findViewById(R.id.recyclerViewDrawer);
recyclerViewDrawer.setHasFixedSize(true);
recyclerViewDrawer.setLayoutManager(new LinearLayoutManager(MainActivity.this));
ArrayList<DrawerItem> drawerItems = new ArrayList<>();
final String[] drawerTitles = getResources().getStringArray(R.array.drawer);
final TypedArray drawerIcons = getResources().obtainTypedArray(R.array.drawerIcons);
for (int i = 0; i < drawerTitles.length; i++) {
drawerItems.add(new DrawerItem(drawerTitles[i], drawerIcons.getDrawable(i)));
}
drawerIcons.recycle();
adapterDrawer = new DrawerAdapter(drawerItems);
recyclerViewDrawer.setAdapter(adapterDrawer);
// Here is the problem
ImageView imageViewDrawerIcon2 = (ImageView) recyclerViewDrawer.getChildAt(0).findViewById(R.id.imageViewDrawerIcon);
// RecyclerView item listener.
ItemClickSupport itemClickSupport = ItemClickSupport.addTo(recyclerViewDrawer);
itemClickSupport.setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
#Override
public void onItemClick(RecyclerView parent, View view, int position, long id) {
TypedValue typedValue = new TypedValue();
MainActivity.this.getTheme().resolveAttribute(R.attr.colorAccent, typedValue, true);
final int color = typedValue.data;
//TODO Icon and text colors
for (int i = 0; i < drawerTitles.length; i++){
if (i == position){
ImageView imageViewDrawerIcon = (ImageView) recyclerViewDrawer.getChildAt(i).findViewById(R.id.imageViewDrawerIcon);
TextView textViewDrawerTitle = (TextView) recyclerViewDrawer.getChildAt(i).findViewById(R.id.textViewDrawerItemTitle);
imageViewDrawerIcon.setColorFilter(color);
if(Build.VERSION.SDK_INT > 15){
imageViewDrawerIcon.setImageAlpha(255);
}else{
imageViewDrawerIcon.setAlpha(255);
}
textViewDrawerTitle.setTextColor(color);
RelativeLayout relativeLayoutDrawerItem = (RelativeLayout) recyclerViewDrawer.getChildAt(i).findViewById(R.id.relativeLayoutDrawerItem);
relativeLayoutDrawerItem.setFocusableInTouchMode(true);
}else{
ImageView imageViewDrawerIcon = (ImageView) recyclerViewDrawer.getChildAt(i).findViewById(R.id.imageViewDrawerIcon);
TextView textViewDrawerTitle = (TextView) recyclerViewDrawer.getChildAt(i).findViewById(R.id.textViewDrawerItemTitle);
imageViewDrawerIcon.setColorFilter(getResources().getColor(R.color.md_text));
if(Build.VERSION.SDK_INT > 15){
imageViewDrawerIcon.setImageAlpha(138);
}else{
imageViewDrawerIcon.setAlpha(138);
}
textViewDrawerTitle.setTextColor(getResources().getColor(R.color.md_text));
RelativeLayout relativeLayoutDrawerItem = (RelativeLayout) recyclerViewDrawer.getChildAt(i).findViewById(R.id.relativeLayoutDrawerItem);
relativeLayoutDrawerItem.setFocusableInTouchMode(false);
}
}
//TODO Fragments (closedrawers before setfragment)
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// Do something after some time
}
}, 250);
mDrawerLayout.closeDrawers();
}
});
In my case it,is work like a charm
View viewItem = recycleView.getLayoutManager().findViewByPosition(position);
View icon = viewItem.findViewById(R.id.view);
I solved finally with this (source):
recyclerViewDrawer.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
ImageView imageViewDrawerIcon = (ImageView) recyclerViewDrawer.getChildAt(0).findViewById(R.id.imageViewDrawerIcon);
TextView textViewDrawerTitle = (TextView) recyclerViewDrawer.getChildAt(0).findViewById(R.id.textViewDrawerItemTitle);
imageViewDrawerIcon.setColorFilter(color);
if (Build.VERSION.SDK_INT > 15) {
imageViewDrawerIcon.setImageAlpha(255);
} else {
imageViewDrawerIcon.setAlpha(255);
}
textViewDrawerTitle.setTextColor(color);
RelativeLayout relativeLayoutDrawerItem = (RelativeLayout) recyclerViewDrawer.getChildAt(0).findViewById(R.id.relativeLayoutDrawerItem);
relativeLayoutDrawerItem.setFocusableInTouchMode(true);
// unregister listener (this is important)
recyclerViewDrawer.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
By the way, if someone have a better solution I would like to know it.
This worked for me.
recyclerView.post(new Runnable() {
#Override
public void run() {
View viewItem = recycleView.getLayoutManager().findViewByPosition(position);
View icon = viewItem.findViewById(R.id.view);
}
});
I would use findViewHolderForAdapterPosition() or findViewByPosition() instead to find the root view at that index. The way you ensure the data is loaded is up to you, however.
findViewByPosition is a method of LayoutManager, which works with all LayoutManagers.
So you can use recycleView.getLayoutManager() and findViewByPosition(pos).
Also recyclerView.findViewHolderForAdapterPosition(pos) works too.
For more information, look at this RecyclerView - Get view at particular position
I am trying to populate data in UI fetched through REST service. The data comes up everytime but the UI doesn't get populated sometimes. The UI is kind of listing but listview is not being used. There's a ScrollView which has LinearLayout as its child and then row views are added to the linearlayout. There are times when UI just doesn't get updated even if the data is passed to it.
private void showData(List list) {
if(list != null && list.isEmpty()) {
mNoData.setVisibility(View.VISIBLE);
} else {
mNoData.setVisibility(View.GONE);
final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.details_layout);
findViewById(R.id.details_progress).setVisibility(View.GONE);
linearLayout.removeAllViews();
LayoutInflater layoutInflater = getLayoutInflater();
View rowView = null;
TextView txtName = null;
Button buttonPlay = null;
for (int i = 0; i < list.size(); i++) {
rowView = layoutInflater.inflate(R.layout.row_view, null);
txtName = (TextView) rowView.findViewById(R.id.row_view_name);
buttonPlay = (Button) rowView.findViewById(R.id.row_view_button_play);
final Item item = (Item) list.get(i);
rowView.setTag(i) ;
txtName.setText(item.getName());
final RecentItem recentItem = RecentsManager.getInstance().getRecentItemFromRefID(item.getId());
if (recentItem !=null) {
buttonPlay.setText(getString("Resume"));
buttonPlay.requestFocus();
}
buttonPlay.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (recentItem!=null) {
//do some action
} else {
//do some action
}
finish();
}
});
final int tag = (Integer) rowView.getTag() ;
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do some action
}
}) ;
txtName.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do some action
}
});
buttonPlay.setId(i);
if(i < list.size())
buttonPlay.setNextFocusDownId(i+1);
linearLayout.addView(rowView);
if (i == 0) {
buttonPlay.requestFocus();
buttonPlay.setNextFocusUpId(mButtonReminder.getId());
if(mButtonReminder != null) {
mButtonReminder.setNextFocusDownId(buttonPlay.getId());
}
} else {
buttonPlay.setNextFocusUpId(i-1);
}
}
}
}
I have even checked the linearlayout children's count and the count is always equal to list size which means rows are also being added to it but it just doesn't show up on the UI.
What can be the issue?
you are using condition
if(list != null && list.isEmpty()) {
mNoData.setVisibility(View.VISIBLE);
}
change it to
if(list == null || list.isEmpty()) {
mNoData.setVisibility(View.VISIBLE);
}
I was able to resolve the issue by doing certain changes as adding fillViewPort="true" for scrollview. Also, there was a background thread which was trying to update the UI because of which UI thread was breaking. This didn't resulted in a crash but the UI updation didn't happened. With these two changes it's working fine.