I want to hide element/Views programmatically in android - android

I got JSON response which have elementId and flag for hide/show that element
Call function using this(From JSON Response)
displayView(templateDefinationItem.getTemplateDefinationId(), templateDefinationItem.isActive());
I have created one function for hide the views
public void displayView(final int elementId, boolean isVisible) {
try {
View view = findViewById(elementId);
if (isVisible) {
view.setVisibility(View.VISIBLE);
} else {
view.setVisibility(View.GONE);
}
} catch (Exception e) {
e.printStackTrace();
}
}
In above code i passed elementId and true/false value for the operation, where
elementId of(EditTextId,TextView,LinearLayout,Buttons etc.)
Error
i got error in this line View view = findViewById(elementId); getting null.
What i want
is there any way to bind any type of element? Or any generic view for same?
in my case i used this View view = findViewById(elementId); for binding but i got null.

Rather passing view id you should pass view in display method that is more convenient.
First Views Ids are generated automatically so if things you are storing this ids some Where and later used to get views it not right thing because Ids are generated and it different device to device and might change any time when application closed and start again.
You can do it by getIdentifier()
try {
String buttonID = elementId;//String name of id
int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
View view = findViewById(resID);
if (isVisible) {
view.setVisibility(View.VISIBLE);
} else {
view.setVisibility(View.GONE);
} catch (Exception e) {
e.printStackTrace();
}
as above we are passing view id with combination of i and j values and then using getIdentifier() method to make Views objects .
I thing above code is solution towards your problem.

I would suggest using Kotlin instead of Java, and additionally using the core ktx library (it's a library of usefull Kotlin extensions for Android).
With it, you can do something like this:
view.isVisible = true sets the view to View.VISIBLE, whereas view.isVisible = false sets it to View.GONE
Similarly you have view.isInvisible which toggles between Invisible and Visible, and view.isGone which toggles between Gone and Visible.
In case you need the documentation of these methods, you can find it here.
Also, if you're using Kotlin instead of Java, you don't need to do findViewById(R.id.xxx), you can simply do a static import of any View.

Related

How to check if a view is visible in future?

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.

why view.findViewsWithText(..) method not returns any gone views?

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.

Create an "if statement" or "switch statement" based off a floating action button image resource [duplicate]

I have one ImageView and set a drawable on it. Now I need to get the ID of the drawable on click event of ImageView dynamically. How can I get it?
imgtopcolor = (ImageView) findViewById(R.id.topcolor);
imgtopcolor.setImageResource(R.drawable.dr); // How do I get this back?
Now on touch event of imgtopcolor i want to need drawable id because I am setting different drawable each time and want to compare the drawable with other
I think if I understand correctly this is what you are doing.
ImageView view = (ImageView) findViewById(R.id.someImage);
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
ImageView imageView = (ImageView) view;
assert(R.id.someImage == imageView.getId());
switch(getDrawableId(imageView)) {
case R.drawable.foo:
imageView.setDrawableResource(R.drawable.bar);
break;
case R.drawable.bar:
default:
imageView.setDrawableResource(R.drawable.foo);
break;
}
});
Right? So that function getDrawableId() doesn't exist. You can't get a the id that a drawable was instantiated from because the id is just a reference to the location of data on the device on how to construct a drawable. Once the drawable is constructed it doesn't have a way to get back the resourceId that was used to create it. But you could make it work something like this using tags
ImageView view = (ImageView) findViewById(R.id.someImage);
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
ImageView imageView = (ImageView) view;
assert(R.id.someImage == imageView.getId());
// See here
Integer integer = (Integer) imageView.getTag();
integer = integer == null ? 0 : integer;
switch(integer) {
case R.drawable.foo:
imageView.setDrawableResource(R.drawable.bar);
imageView.setTag(R.drawable.bar);
break;
case R.drawable.bar:
default:
imageView.setDrawableResource(R.drawable.foo);
imageView.setTag(R.drawable.foo);
break;
}
});
I answered something like this in another question already, but will change it just a little for this one.
Unfortunately, there is no getImageResource() or getDrawableId(). But, I created a simple workaround by using the ImageView tags.
In onCreate():
imageView0 = (ImageView) findViewById(R.id.imageView0);
imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView2 = (ImageView) findViewById(R.id.imageView2);
imageView0.setTag(R.drawable.apple);
imageView1.setTag(R.drawable.banana);
imageView2.setTag(R.drawable.cereal);
Then, if you like, you can create a simple function to get the drawable id:
private int getDrawableId(ImageView iv) {
return (Integer) iv.getTag();
}
Too easy.
As of today, there is no support on this function. However, I found a little hack on this one.
imageView.setImageResource(R.drawable.ic_star_black_48dp);
imageView.setTag(R.drawable.ic_star_black_48dp);
So if you want to get the ID of the view, just get it's tag.
if (imageView.getTag() != null) {
int resourceID = (int) imageView.getTag();
//
// drawable id.
//
}
Digging StackOverflow for answers on the similar issue I found people usually suggesting 2 approaches:
Load a drawable into memory and compare ConstantState or bitmap itself to other one.
Set a tag with drawable id into a view and compare tags when you need
that.
Personally, I like the second approach for performance reason but tagging bunch of views with appropriate tags is painful and time consuming. This could be very frustrating in a big project. In my case I need to write a lot of Espresso tests which require comparing TextView drawables, ImageView resources, View background and foreground. A lot of work.
So I eventually came up with a solution to delegate a 'dirty' work to the custom inflater. In every inflated view I search for a specific attributes and and set a tag to the view with a resource id if any is found. This approach is pretty much the same guys from Calligraphy used. I wrote a simple library for that: TagView
If you use it, you can retrieve any of predefined tags, containing drawable resource id that was set in xml layout file:
TagViewUtils.getTag(view, ViewTag.IMAGEVIEW_SRC.id)
TagViewUtils.getTag(view, ViewTag.TEXTVIEW_DRAWABLE_LEFT.id)
TagViewUtils.getTag(view, ViewTag.TEXTVIEW_DRAWABLE_TOP.id)
TagViewUtils.getTag(view, ViewTag.TEXTVIEW_DRAWABLE_RIGHT.id)
TagViewUtils.getTag(view, ViewTag.TEXTVIEW_DRAWABLE_BOTTOM.id)
TagViewUtils.getTag(view, ViewTag.VIEW_BACKGROUND.id)
TagViewUtils.getTag(view, ViewTag.VIEW_FOREGROUND.id)
The library supports any attribute, actually. You can add them manually, just look into the Custom attributes section on Github.
If you set a drawable in runtime you can use convenient library methods:
setImageViewResource(ImageView view, int id)
In this case tagging is done for you internally. If you use Kotlin you can write a handy extensions to call view itself. Something like this:
fun ImageView.setImageResourceWithTag(#DrawableRes int id) {
TagViewUtils.setImageViewResource(this, id)
}
You can find additional info in Tagging in runtime
I recently run into the same problem. I solved it by implementing my own ImageView class.
Here is my Kotlin implementation:
class MyImageView(context: Context): ImageView(context) {
private var currentDrawableId: Int? = null
override fun setImageResource(resId: Int) {
super.setImageResource(resId)
currentDrawableId = resId
}
fun getDrawableId() {
return currentDrawableId
}
fun compareCurrentDrawable(toDrawableId: Int?): Boolean {
if (toDrawableId == null || currentDrawableId != toDrawableId) {
return false
}
return true
}
}
A simple solution might be to just store the drawable id in a temporary variable. I'm not sure how practical this would be for your situation but it's definitely a quick fix.
Even easier: just store the R.drawable id in the view's id: use v.setId(). Then get it back with v.getId().

Toggling View background

I have a View object on my Activity and I'd like to change the background resource of the view. More specifically, I'd like to toggle it.
So I'll need some logic like this:
if (myView.getBackgroundResource() == R.drawable.first) {
myView.setBackgroundResource(R.drawable.second);
}
else {
myView.setBackgroundResource(R.drawable.first);
}
The issue here being that there is no getBackgroundResource() method.
How can I obtain the resource a View is using for its background?
I don't think the View remembers what resource it is using after it gets the Drawable from the resource.
Why not use an instance variable in your Activity, or subclass the View and add an instance variable to it?
Wouldn't it be easier to just have a control variable that maintains the state? Lets you be flexible and allows you any number of drawables.
int[] backgrounds = {
R.drawable.first,
R.drawable.second,
R.drawable.third
};
int currentBg;
void switch() {
currentBg++;
currentBg %= backgrounds.length;
myView.setBackgroundResource(backgrounds[currentBg]);
}
You could use a flag to keep track of which was last set
private static final int FIRST_BG = 0;
private static final int SECOND_BG = 1;
private int mCurrentBg;
...
if (mCurrentBg == FIRST_BG) {
myView.setBackgroundResource(R.drawable.second);
mCurrentBg = SECOND_BG;
}
else {
myView.setBackgroundResource(R.drawable.first);
mCurrentBg = FIRST_BG;
}
You would have to initialize mCurrentBg wherever the background is initially set though.
You can get the ID of a resource via the getResources().getIdentifier("filename", "drawable", "com.example.android.project"); function. As you can see you will need the filename, the type of resource (drawable, layout or whatever) and the package it is in.
EDIT: Updated my logic fail.
I think you might be able to put the setTag() and getTag() methods to use here:
//set the background and tag initially
View v = (View)findViewById(R.id.view);
v.setBackgroundResource(R.drawable.first);
v.setTag(R.drawable.first);
if(v.getTag().equals(R.drawable.first)) {
v.setBackgroundResource(R.drawable.second);
v.setTag(R.drawable.second);
} else {
v.setBackgroundResource(R.drawable.first);
v.setTag(R.drawable.first);
}
I have not tested this, but I think it should work, in theory. The downside is that you add a little overhead by having to manually tag it the first time, but after the initial tagging, you shouldn't have to worry about keeping track of flags.

Get the ID of a drawable in ImageView

I have one ImageView and set a drawable on it. Now I need to get the ID of the drawable on click event of ImageView dynamically. How can I get it?
imgtopcolor = (ImageView) findViewById(R.id.topcolor);
imgtopcolor.setImageResource(R.drawable.dr); // How do I get this back?
Now on touch event of imgtopcolor i want to need drawable id because I am setting different drawable each time and want to compare the drawable with other
I think if I understand correctly this is what you are doing.
ImageView view = (ImageView) findViewById(R.id.someImage);
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
ImageView imageView = (ImageView) view;
assert(R.id.someImage == imageView.getId());
switch(getDrawableId(imageView)) {
case R.drawable.foo:
imageView.setDrawableResource(R.drawable.bar);
break;
case R.drawable.bar:
default:
imageView.setDrawableResource(R.drawable.foo);
break;
}
});
Right? So that function getDrawableId() doesn't exist. You can't get a the id that a drawable was instantiated from because the id is just a reference to the location of data on the device on how to construct a drawable. Once the drawable is constructed it doesn't have a way to get back the resourceId that was used to create it. But you could make it work something like this using tags
ImageView view = (ImageView) findViewById(R.id.someImage);
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
ImageView imageView = (ImageView) view;
assert(R.id.someImage == imageView.getId());
// See here
Integer integer = (Integer) imageView.getTag();
integer = integer == null ? 0 : integer;
switch(integer) {
case R.drawable.foo:
imageView.setDrawableResource(R.drawable.bar);
imageView.setTag(R.drawable.bar);
break;
case R.drawable.bar:
default:
imageView.setDrawableResource(R.drawable.foo);
imageView.setTag(R.drawable.foo);
break;
}
});
I answered something like this in another question already, but will change it just a little for this one.
Unfortunately, there is no getImageResource() or getDrawableId(). But, I created a simple workaround by using the ImageView tags.
In onCreate():
imageView0 = (ImageView) findViewById(R.id.imageView0);
imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView2 = (ImageView) findViewById(R.id.imageView2);
imageView0.setTag(R.drawable.apple);
imageView1.setTag(R.drawable.banana);
imageView2.setTag(R.drawable.cereal);
Then, if you like, you can create a simple function to get the drawable id:
private int getDrawableId(ImageView iv) {
return (Integer) iv.getTag();
}
Too easy.
As of today, there is no support on this function. However, I found a little hack on this one.
imageView.setImageResource(R.drawable.ic_star_black_48dp);
imageView.setTag(R.drawable.ic_star_black_48dp);
So if you want to get the ID of the view, just get it's tag.
if (imageView.getTag() != null) {
int resourceID = (int) imageView.getTag();
//
// drawable id.
//
}
Digging StackOverflow for answers on the similar issue I found people usually suggesting 2 approaches:
Load a drawable into memory and compare ConstantState or bitmap itself to other one.
Set a tag with drawable id into a view and compare tags when you need
that.
Personally, I like the second approach for performance reason but tagging bunch of views with appropriate tags is painful and time consuming. This could be very frustrating in a big project. In my case I need to write a lot of Espresso tests which require comparing TextView drawables, ImageView resources, View background and foreground. A lot of work.
So I eventually came up with a solution to delegate a 'dirty' work to the custom inflater. In every inflated view I search for a specific attributes and and set a tag to the view with a resource id if any is found. This approach is pretty much the same guys from Calligraphy used. I wrote a simple library for that: TagView
If you use it, you can retrieve any of predefined tags, containing drawable resource id that was set in xml layout file:
TagViewUtils.getTag(view, ViewTag.IMAGEVIEW_SRC.id)
TagViewUtils.getTag(view, ViewTag.TEXTVIEW_DRAWABLE_LEFT.id)
TagViewUtils.getTag(view, ViewTag.TEXTVIEW_DRAWABLE_TOP.id)
TagViewUtils.getTag(view, ViewTag.TEXTVIEW_DRAWABLE_RIGHT.id)
TagViewUtils.getTag(view, ViewTag.TEXTVIEW_DRAWABLE_BOTTOM.id)
TagViewUtils.getTag(view, ViewTag.VIEW_BACKGROUND.id)
TagViewUtils.getTag(view, ViewTag.VIEW_FOREGROUND.id)
The library supports any attribute, actually. You can add them manually, just look into the Custom attributes section on Github.
If you set a drawable in runtime you can use convenient library methods:
setImageViewResource(ImageView view, int id)
In this case tagging is done for you internally. If you use Kotlin you can write a handy extensions to call view itself. Something like this:
fun ImageView.setImageResourceWithTag(#DrawableRes int id) {
TagViewUtils.setImageViewResource(this, id)
}
You can find additional info in Tagging in runtime
I recently run into the same problem. I solved it by implementing my own ImageView class.
Here is my Kotlin implementation:
class MyImageView(context: Context): ImageView(context) {
private var currentDrawableId: Int? = null
override fun setImageResource(resId: Int) {
super.setImageResource(resId)
currentDrawableId = resId
}
fun getDrawableId() {
return currentDrawableId
}
fun compareCurrentDrawable(toDrawableId: Int?): Boolean {
if (toDrawableId == null || currentDrawableId != toDrawableId) {
return false
}
return true
}
}
A simple solution might be to just store the drawable id in a temporary variable. I'm not sure how practical this would be for your situation but it's definitely a quick fix.
Even easier: just store the R.drawable id in the view's id: use v.setId(). Then get it back with v.getId().

Categories

Resources