Android get type of a view - java

How can i do this?
something:
final View view=FLall.getChildAt(i);
if (view.getType()==ImageView) {
...
}

If, for some strange reason, you can't use Asahi's suggestion (using tags), my proposition would be the following:
if (view instanceof ImageView) {
ImageView imageView = (ImageView) view;
// do what you want with imageView
}
else if (view instanceof TextView) {
TextView textView = (TextView) view;
// do what you want with textView
}
else if ...

I try the following and it worked:
View view=FLall.getChildAt(i);
Log.i("ViewName", view.getClass().getName());

For Others who check this Question,in some cases instanceof does not work(I do not know why!),for example if your want to check if view type is ImageView or ImageButton(i tested this situation) , it get them the same, so you scan use this way :
//v is your View
if (v.getClass().getName().equalsIgnoreCase("android.widget.ImageView")) {
Log.e("imgview", v.toString());
imgview = (ImageView) v;
} else if (v.getClass().getName().equalsIgnoreCase("android.widget.ImageButton")) {
Log.e("imgbtn", v.toString());
imgbtn = (ImageButton) v;
}

You can use tag for that purpose:see set/getTag methods at http://developer.android.com/reference/android/view/View.html

I am using this solution for KOTLIN code, so going off of Arash's solution:
if(v.javaClass.name.equals("android.widget.ImageView", ignoreCase = true)) ...
using this didn't work for me, but tweaking it to:
if(v.javaClass.name.contains("ImageView", ignoreCase = true)) ...
worked for me!

In Kotlin you can check this by using "is":
val view = findViewById<View>(R.id.a_view)
if (view is ImageView) {
print("This is a ImageView")
} else {
print("This is not a ImageView")
}

In Android Xamarin, This is how Android get type of a view and compare with controller type.
var view = FindViewById<TextView>(Resource.Id.emailText);
if (typeof(TextView).IsInstanceOfType(view))
{
var textView = (TextView)view;
}

Related

Using Overlay of StfalconImageViewer to close imageViewer

I'm trying to implement StfalconImageViewer in my app using these links: here and here. this is my code:
View imageDetail = activity.getLayoutInflater().inflate(R.layout.chat_image_pager, container , false);
ImageView back = imageDetail.findViewById(R.id.back);
TextView totalImages = imageDetail.findViewById(R.id.totalImages);
TextView imagePosition = imageDetail.findViewById(R.id.mediaCounter);
TextView sentDate = imageDetail.findViewById(R.id.sentDate);
TextView sentBy = imageDetail.findViewById(R.id.sentBy);
TextView sentTime = imageDetail.findViewById(R.id.sentTime);
back.setOnClickListener(v -> {
//what should I write here???
});
StfalconImageViewer.Builder<ChatMessageBuilder> builder = new StfalconImageViewer.Builder<>
(activity,ChatImageData.imageMessages, (imageView, image) -> {
Glide.with(fragment)
.load(image.getMediaAddress())
.into(imageView);
});
//some other codes
builder
.withStartPosition(ChatMessageInterfaceFragment.imagePosition)
.withHiddenStatusBar(false)
.allowZooming(true)
.allowSwipeToDismiss(true)
.withImageChangeListener((position) -> {
//some codes
})
.withBackgroundColorResource(R.color.background_light)
.withTransitionFrom(messageImage)
.withOverlayView(imageDetail)
.show();
As you can see there is an Overlay named imageDetail and a button (in Overlay) named back which should close imageViewer and get me back to my RecyclerView but the problem is I can't really put a right code for this. Any help would be appreciated.
Ok after a long test and experiments finally found it. here is the code:
View imageDetail = activity.getLayoutInflater().inflate(R.layout.chat_image_pager, container , false);
ImageView back = imageDetail.findViewById(R.id.back);
TextView totalImages = imageDetail.findViewById(R.id.totalImages);
TextView imagePosition = imageDetail.findViewById(R.id.mediaCounter);
TextView sentDate = imageDetail.findViewById(R.id.sentDate);
TextView sentBy = imageDetail.findViewById(R.id.sentBy);
TextView sentTime = imageDetail.findViewById(R.id.sentTime);
StafalconImageViewer builder = new StfalconImageViewer.Builder<>
(activity,ChatImageData.imageMessages, (imageView, image) -> {
Glide.with(fragment)
.load(image.getMediaAddress())
.into(imageView);
});
//some other codes
builder
.withStartPosition(ChatMessageInterfaceFragment.imagePosition)
.withHiddenStatusBar(false)
.allowZooming(true)
.allowSwipeToDismiss(true)
.withImageChangeListener((position) -> {
//some codes
})
.withBackgroundColorResource(R.color.background_light)
.withTransitionFrom(messageImage)
.withOverlayView(imageDetail)
.show();
now our onClickListener works:
back.setOnClickListener(v -> {
builder.onDismiss();
});
If we use the base Class which mean StafalconImageViewer and not the
StafalconImageViewer.Builder, the onDismiss() method works fine!

How to hide the imageview in RecyclerView permanently through firebase realtime database

This is my first time with recyclerview and I need some help. I am using a firebase and I want to hide an item(imageview) from my recyclerview adapter.
public RecyclerViewHolder(View itemView, final OnItemClickListener listener) {
super(itemView);
image = itemView.findViewById(R.id.listrow_img1);
title = itemView.findViewById(R.id.listrow_tv1);
lock = itemView.findViewById(R.id.lock_accountImg);
}
I want to hide the lock image if my firebase realtime database don't have a child named("password"). if it has that child() the lock image will show up. Thank you in advance for those who help.
To hide or display a view according to a condition, create a method inside the ViewHolder class:
public void hideDisplayImageView(boolean passwordExist) {
if (passwordExist) {
lock.setVisibility(View.VISIBLE);
} else {
lock.setVisibility(View.GONE);
}
}
And call from within the onBindViewHolder method:
String password = list.get(position).getPassword();
if(password == null) {
holder.hideDisplayImageView(true);
} else {
holder.hideDisplayImageView(false);
}
You can try this like
if("your condition for the child is true"){
image.setVsisibility(View.VISIBLE)}
else{
image.setVsisibility(View.GONE)}
Kolin
val firebaseData = list.[position].getChild()
if(!firebaseData == "password")
holder.image.isVisible = false
else holder.image.isVisible = true
Java
String firebaseData = list.getPosition().getChild();
if(!firebaseData.contains("password")){
holder.image.setVisibility(View.GONE)
}else{
holder.image.setVisibility(View.VISIBLE)
}

Java: Stuck inside if statement block

i am starting with creating android apps, i already have some basic experience with C# and Java. Now i'm stuck at a strange problem, see below:
public void pictureSwitch (View view){
ImageView imageView = (ImageView) findViewById(R.id.imgVCat);
boolean switched = false;
if (switched){
imageView.setImageResource(R.drawable.catstart);
Log.i("Status-Start", "Wert: " + switched);
switched = false;
} else {
imageView.setImageResource(R.drawable.catswitch);
Log.i("Status-switched", "Wert: " + switched);
switched = true;
}
}
}
The Problem is, on the first click on the Button it changes to the catswitch drawable. But it never switches back and i dont know why.
Thanks for your help in advance.
Lets have a boolean as instance variable.
private boolean switched = false;
public void pictureSwitch() {
ImageView imageView = (ImageView) findViewById(R.id.imgVCat);
if (switched) {
imageView.setImageResource(R.drawable.catstart);
} else {
imageView.setImageResource(R.drawable.catswitch);
}
switched = !switched;
}
And you do not need a parameter View view if you are not using it .

Button.setClickable(false) is not working

I have set mButton.setClickable(false); in my code but still this button is invoked by global button.setOnClickListener of my code.
EDIT: sorry for the delayed update. Below is the details view where I face the issue.
inside my listview customAdapter class getView method
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
YourWrapper wrapper = null;
HashMap<String, Object> cTa= new HashMap<String, Object>();
cTa= d.getPosition(position)
Button mButton = (Button)convertView.findViewById(R.id.mBtn);
if (row == null)
{
row = inflater.inflate(R.layout.layout, parent, false);
wrapper = new YourWrapper (row);
row.setTag(wrapper);
}
else
wrapper = (YourWrapper) row.getTag();
if(success)
{
// section-1
mButton.setClickable(true);
}
else{
// section-2
mButton.setClickable(false);
mButton.setFocusable(false);
}
wrapper.getButton().setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
//operation
}
});
return row;
}
Above is the current code which working,and on section-2 it makes the mButton clickable- false, and focusable - false but still it's listen the below wrapper.getButton().setOnClickListener() and perform the operation. Please suggest me. Sorry for delayed update. Thanks!
UPDATE: I have made below hot-fixes that solve the problem for now.
// section-2
mButton.setVisibility(View.GONE);
mButton.setClickable(false);
mButton.setFocusable(false);
That seems to be by design. This is from the documentation of the View.setOnClickListener method:
Register a callback to be invoked when this view is clicked. If this view is not clickable, it becomes clickable.
Instead of using setClickable(false) use setEnabled(false)
Put setClickable after setOnClickListener
mBtn.setOnClickListener(this);
mBtn.setClickable(false);
if you put setClickable(false) before setOnClickListener(this), it doesn't work.
Instead of using setClickable(false) use following
button.setFocusableInTouchMode(false);
I had the same problem in my app where i needed to set my button not to clickable in certain conditions. this worked for me. Hope this helps.
Use View.setOnClickListener() before View.setClickable() ,or the method setOnclickLisnter() will set the flag true.
I'm not sure if you're still looking for the answer, but for some weird reason
mBtn.setClickable(true);
stops the view from getting clicked and
mBtn.setClickable(false);
makes it clickable again.
on xml
android:enabled="false"
android:alpha="0.5"
dynamically
yourButtonId.alpha = 0.5f
yourButtonId.isEnabled = false
You can check like if(!view.isClickable()) return;
This will work in case of Imageview as well as the button.
private OnClickListener onClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
if (imageview.isEnabled()){
//I have wrapped all code inside onClick() in this if condition
//Your onClick() code will only execute if the imageview is enabled
//Now we can use setEnabled() instead of setClickable() everywhere
}}
};
Inside onCreate(), you can do setEnabled(false) which will be equivalent to setClickable(false).
We are able to use setEnabled() as tag because it's state remains uneffected on invocation of click (unlike setClickable() whose state changes).
Like Other friends said, setOnClickListener will override the flag to true. So the Workaround is to setOnTouchEvent return true whenever you want to disable clicks and set it to retrun false when you want to enable click events. This is because onTouchEvent is called before every clickListener you define for a view, so returning true will say to all listeners that :
"Ok, I received this event here, nobody else can receive it".
So your solution may be something like this:
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
YourWrapper wrapper = null;
HashMap<String, Object> cTa= new HashMap<String, Object>();
cTa= d.getPosition(position)
Button mButton = (Button)convertView.findViewById(R.id.mBtn);
if (row == null)
{
row = inflater.inflate(R.layout.layout, parent, false);
wrapper = new YourWrapper (row);
row.setTag(wrapper);
}
else
wrapper = (YourWrapper) row.getTag();
if(success)
{
// section-1
mButton.setOnTouchListener((v, event) -> false);
}
else{
// section-2
mButton.setOnTouchListener((v, event) -> true);
}
wrapper.getButton().setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
//operation
}
});
return row;
}
Set the click listener to null
someView.setOnClickListener(null)
As #Jan notes, the setOnClickListener enables the click listener automatically. Therefore, a null click listener can be set to disable future clicks. After setting the view to a null click listener, there are no adverse effects to future clicks on that view.
I wanted to do it on Spinner, and only this one worked for me:
spinner.setOnTouchListener { v, event ->
return#setOnTouchListener true
}
I just checked setClickable(true) and setClickable(false) on Android 4.1.1 and it seems to be working now.

android imageview.setBackgroundResource() doesn't work

I have an imageview that should be changed on click
public class Settings extends Activity implements OnClickListener
{
private ImageView im1;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
im1 = (ImageView) findViewById( R.id.imageView1 );
im1.setOnClickListener(this);
}
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
if (v == im1 )
{
Log.d("test", "hey!");
v.setBackgroundResource(R.drawable.img1);
}
}
}
when clicked the method runs and prints out "hey!" but the image won't change?
EDIT: forgot to mention that imageview contains another image provided by xml layout file
By convention, you should be using setImageResource(R.drawable.img1); (or setImageDrawable(getResources().getDrawable(R.drawable.img1));) instead of setBackgroundResource(R.drawable.img1);.
ImageView i = (ImageView) findViewById( R.id.imageView1 );
i.setImageResource(R.id.logo);
or
i.setBackgroundResource(R.drawable.icon);
UPDATE
Now, you can use like below,
imageView.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.img1));
Try This In API 25
imgSchedule.setImageDrawable(getResources().getDrawable(R.drawable.circle_image_selected));
Its no convention that setimageResource() should be used.
Both the APIs can be used.
Also, in your case, it just looks like the case of out of sync resources.

Categories

Resources