i'm defining my own list adapter and i want an image inside it to be shown OR hidden based on a value what i've noticed that its always invisible or visible disregarding the value
Here's my code , this code is inside the getView method
singleRow=data.get(position);
readit = singleRow.getRead();
Log.i("readit","" + readit );
//NotificationID=singleRow.getId();
holder.title.setText(singleRow.getAttach_title());
holder.date.setText( singleRow.getAttach_created());
holder.dueDate.setVisibility(ImageView.INVISIBLE);
holder.course.setText(singleRow.getCourse_title());
if(readit==1)
{
//holder.read.setImageResource(IGNORE_ITEM_VIEW_TYPE);
holder.read.setVisibility(ImageView.INVISIBLE);
}
else
{
holder.read.setImageResource(R.drawable.unread);
}
holder.dueDate.setVisibility(View.GONE);
You're never setting your read image back to VISIBLE after you once set it to INVISIBLE. In else you, probably, should have setVisibility(VISIBLE)
Use this code.It will solve your problem
singleRow=data.get(position);
readit = singleRow.getRead();
Log.i("readit","" + readit );
//NotificationID=singleRow.getId();
holder.title.setText(singleRow.getAttach_title());
holder.date.setText( singleRow.getAttach_created());
holder.dueDate.setVisibility(ImageView.INVISIBLE);
holder.course.setText(singleRow.getCourse_title());
holder.read.setImageResource(R.drawable.unread);
if(readit==1) {
//holder.read.setImageResource(IGNORE_ITEM_VIEW_TYPE);
holder.read.setVisibility(View.INVISIBLE);
}
Related
I have a view. The view might become visible at some time in the future. When this view is visible I want to call a method. How to do this?
val editText = findViewById<EditText>(R.id.editText)
// editText might become invisible in some time in future
// and in some in future it might visible
if(editText.isVisible(){
// code to be executed
}
Code for View.isVisible() :
fun View.isVisible() = this.visibility == View.VISIBLE // check if view is visible
Is there anything like View.setOnClickListener which could be applied and triggered when the view is visible-
editText.setOnClickListener { view ->
}
click listener is callback when the view is being clicked. it has no concern with its visibility. There is no method like isVisible(). to check Visibility
if(yourView.getVisiblity()==View.VISIBLE){ //your task}
for kotlin:
if(youView.visibility==View.VISIBLE){//your task}
I might initialize a variable int status of visibility and set it to 0 with the view invisible.
Now I would create a function instead directly setting the visibility of the view.
For example a function named onVisibilityChanged();
In that function add the set visibility code followed by setting the int to 0 or 1 as per the visibility an if-else block.
If you just set the view to visible, set the int to 1.
The reason for adding if-else block is to configure your actions based on the visibility status.
So that gives you the freedom to do whatever you want bases on the visibility.
Make sure you add this code in such a way that it is executed anytime you want.
Use the performClick() function to click any button or any other view.
I hope you understand. Or comment any query. I would have posted the code for the same but it looks like you are using Kotlin. So I'll try to post it in Kotlin if possible.
The main intention of doing such a thing is when the value of int changes, the app knows what to do and also knows that visibility has changed.
Just call the function wherever you want. It will be easy.
So this is what I am trying to do:
int visibilityStatus;
textview = findViewById(R.id.textview);
getInitialVisibility();
funcOnVisibilityChange();
}
private void getCurrentVisibility() {
if (textview.getVisibility() == View.VISIBLE) {
visibilityStatus = 1;
} else {
visibilityStatus = 0;
}
}
private void funcOnVisibilityChange() {
//Now change the visibility in thia function
textview.setVisibility(View.VISIBLE);
int currentVisibilityStatus;
if (textview.getVisibility() == View.VISIBLE) {
currentVisibilityStatus = 1;
} else {
currentVisibilityStatus = 0;
}
if (visibilityStatus != currentVisibilityStatus) {
//Visibility status has changed. Do your task
else {
//visibility status not changed
}
}
}
So all that we are doing is getting visibility of the view when the app is started and when you somehow change its visibility. Here in the example, I've directly changed the visibility. So wherever you know that visibility is going to change just put the funcOnVisibilityChange() and it will do your job... hope it helps. Let me know if you need more clarification.
I am trying to collapse all sections in recyclerview except one ( currently selected)..
I can collapse all items successfully.. but I want to reverse the arrow direction in headerholder.. I Using the library as https://github.com/luizgrp/SectionedRecyclerViewAdapter..
while reversing the arrow it is giving null pointer exception for headerholders out of window ( not visible in present screen)..
{
for (int i = 0; i < deviceInfoList.size(); i++) {
ExpandableDeviceSection section = (ExpandableDeviceSection) sectionAdapter.getSection(deviceInfoList.get(i).getdName());
if (section.expanded && !section.dName.equals(dName)) {
section.expanded = false;
HeaderViewHolder headerViewHolder1=(HeaderViewHolder)recyclerView.findViewHolderForAdapterPosition(sectionAdapter.getHeaderPositionInAdapter(section.dName));
//getting null for sectionHeader which is not available.
if(headerViewHolder1!=null)
headerViewHolder1.imgArrow.setImageResource(section.expanded ? R.drawable.ic_expand_less : R.drawable.ic_expand_more);
}
}
// sectionAdapter.notifyDataSetChanged();
}
here is my all collapse code in onclick listener.. I hope this clarifies.. let me know if anything else required..
Problem solved.. forgot to set image view (imagearrow) in onbindheaderview holder..
No need to set it in for loop now .
Why myView.findViewsWithText(..) method not returns any views with Visible.GONE? (i use View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION flag as the last parameter).
findViewById method returns them but i need to get them base on their contentDescription attribute.
public ArrayList<View> Dest=new ArrayList<View>();
wrapperView.findViewsWithText(Dest, "MyContentDescription", View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
And now Dest.size() is zero; if visible of them equals View.Visible, then size will be 2 (or etc).
thanks.
First try cleaning the project .
if problem is there then use the below code.
Please use the below code
String mButtonName = "descreption";
int resID = getResources().getIdentifier(mButtonName , "id", getPackageName());
That's the way you get in int(id) from string, but you can do the same using reflection which is supposed to be way faster.
public static int getId(String resourceName, Class<?> c) {
try {
Field idField = c.getDeclaredField(resourceName);
return idField.getInt(idField);
} catch (Exception e) {
throw new RuntimeException("No resource ID found for: "
+ name + " / " + c, e);
}
}
You can use it like this
getId("button", R.id.class);
I had to use a trick for solve this problem. (because i have used the contentDescription so much in my project.)
my usage was in items of a RecyclerView. in this class per item creates once and binding process of it occures when that item appears on screen.
i use first character of value of tag attribute for determine Visibility of my views (that have contentDescription). then in Constructor of my custom ViewHolder, i found all desired views (that have specific contentDescription) and checked first character.
for example if it was '*', i set its visibility to GONE or if was '&' i set it to INVISIBLE.
this process happens before draw view on screen, so everything is great.
Thanks for all replies. Thanks you #Sagar.
I have RecyclerView where every list item has an ImageButton, thee image of which I define in the adapter's onBindViewHolder():
int myVote = getMyVote();
if (myVote != 0) {
Log.d("dbg", myVote + "");
holder.ratingButton.setImageResource(R.drawable.ic_star_grey600_36dp);
}
So ratingButton is a star in the right bottom corner of the list item layout. Its shape is filled with gray color (and accordingly, a log record is pushed) if the condition (myVote != 0) is satisfied.
The problem is that when I scroll the list down I can watch other stars became filled even though I can see the only one record in the log window (for the correct list item). Moreover, this list items with incorrectly changed buttons repeat every 5 rows, and that's what's confusing me. If I changemListView.setItemViewCacheSize(0); the repeat period changes to 3, so we can assume it somehow connected with the RecyclerView's caching and recycling mechanism.
Please, help me to work the problem out. Thanks!
Don't forget to implement
public long getItemId(int position) {}
otherwise, you'll see repeated items in RecyclerView.
Try to change your code to:
if (myVote != 0) {
Log.d("dbg", myVote + "");
holder.ratingButton.setImageResource(R.drawable.ic_star_grey600_36dp);
} else {
holder.ratingButton.setImageResource(int another resource);
}
}
You may also have to write else part of main condition with some another resource like:
if (myVote != 0) {
Log.d("dbg", myVote + "");
holder.ratingButton.setImageResource(R.drawable.ic_star_grey600_36dp);
} else {
holder.ratingButton.setImageResource(int another_resource);
}
It is worked for me.
I wolud like to set button's gravity value from default center to right using TextView.setGravity() method, but when I call it nothing happens. In LogCat output using getGravity() method I still see the first value - 17 (center).
Tell me please, how can I change button's gravity value, using setGravity() method?
Here is code snippet which describes setting the view's gravity:
public void setGravity(String gravitation) {
if(controlledView != null) {
String gravityValues[] = gravitation.split("|");
for(String gravity : gravityValues) {
if(gravity.equals("center")) {
controlledView.setGravity(Gravity.CENTER);
}
else if(gravity.equals("top")) {
controlledView.setGravity(Gravity.TOP);
}
else if(gravity.equals("bottom")) {
controlledView.setGravity(Gravity.BOTTOM);
}
else if(gravity.equals("right")) {
controlledView.setGravity(Gravity.RIGHT);
}
else if(gravity.equals("left")) {
controlledView.setGravity(Gravity.LEFT);
}
}
}
}
where controlledView is instance of Button class.
TextView textView = (TextView)findViewById(R.id.mytextviewid);
textView.setGravity(Gravity.RIGHT);
I'm assuming you weren't using Gravity.RIGHT; other valid values can be found in the Gravity constants.
I'm sory for this post, found my mistake myself. I pass to split() method Java Regex API's metacharacter '|' and get all symbols of string. Should choose any other split symbols for string.
You can use in XML layout's like > (RightClick->Properties->Gravity->"your option")