Is it possbile to call android spinner from a click of ImageButton? - android

I have an image button which says Select city I want to call an android spinner if anyone clicks on that image. Is it possible??

I would recommend you check out the App/Alert Dialogs section of the API Demos application. I think what you are looking for is a dialog with a single choice list.
Or take a look at this:
http://developer.android.com/guide/topics/ui/dialogs.html#AddingAList

This is entirely possible in several ways. One way is to have a spinner whose visibility is GONE. Clicking on the image makes the spinners visibility VISIBLE.
Another choice is to create a dialog that has the spinner and use the selection to do whatever it is you want to do.
There are other options, but that should get you started.

ImageView iv = (ImageView) findViewById(R.id.iv);
iv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mySpinner.performClick();
}
});

Related

Which one is better, setting OnClickListener on a Button or View?

I want to set OnClickListener on a Button. Should I cast it to the Button or let it in View? You can see from the code below, the former cast the View into Button while the later let the widget in a form of View. Is the later better because it doesn't need to do the casting or is it just do the same?
It's not that I don't understand how to set click listener on a button. I just want to know it in term of performance and best practice point of view.
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
}
}
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(listener);
or
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
}
}
View button = findViewById(R.id.button);
button.setOnClickListener(listener);
The Same, You only save the cast (Performance).
if you dont need the View object I would prefer
findViewById(R.id.button).setOnClickListener(listener);
I believe there's no difference in performance. What changes is your code readability so, if you're setting a clickListener for a button and you also use the button instance later in the code you might want to use "button".setOnClickListener.
Since, most of the times you don't need to keep a reference of the clicked view, i'd suggest to use something like ButterKnife
Performance wise there is almost no difference. For readabillity I would suggest using Button and not View.
I am pretty sure the difference in performance is minimal or simply there is no difference.
However, there is a difference between View and Button.
To shorten the answer and be as specific as possible ,I will tell you that if you want the button to be as "Standard" (Android standard at least) as possible, you should use the Button ,but if you want to sort of create a Custom Button, than you should go ahead and use the View.
To make it a bit more clear ,the Buttom is actually a View ,so when you use a Button, you actually use a View that is customized for the purpose of being a button and acting like one.
If you need more details, ask up,I will try to help or link you some documentation.

Select from dynamically created imageviews

I have generated dynamically ImageView's in LinearLayout - let's say 6, with backgrounds and images, so they are looking like an icons.
Now I would like to select one and based on this which icon is selected proceed with other things.
I know that I can set onClickListener for dynamically create ImageView's.
but question is, how to select one? with jQUery I would add some class after tapping on an icon, in Android ? I really do know, tried something with setTag() but, well, did not happened.
Assuming that somehow I know which icon was tapped, then how can I loop through all dynamic generated ImageView's to get selected one?
Lets say in a loop you are creating the ImageView's and adding that to the LinearLayout. Assign a onClickListener to all the ImageViews. Just like the following code.
yourImageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ImageView selectedImageView = (ImageView) v;
// selectedImageView is the imageView which you have selected
}
});
So when you tap on a imageView, its onClick function will be called. Parameter passed to the onClick function will be the imageView which you have selected. Just typeCast the View 'v' to ImageView and use in your application.

how to change Button background in Custom adapter on Its onClick?

I am trying to change UI of button in custom adapter click, I am able to do functional thing on particular button click event. But when I tried to change UI of any Button, it reflects to last added Button.
I tried with setTag() option too.
use V.setBackgroundResource(R.drawable.drawableName);
it worked for me, where the drawable name is the background resource in your drawable folder.
you can set the background of button inside onClick event like this :
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundDrawable(drawable);
}
});
I found answer by myself.
When any Inflator and Holder used in Custom adapter, you need to create new Object for every single row (data), so you can access any item belongs to their family.

Swapping image on a screen in android

Im working on a school project and it's my first time on android development. I've searched the whole web but couldn't find any answers to my problem. I have a list of images and i would like to put them on one screen and the user would be able to navigate through these images without changing the whole screen. Also Im trying to put an AutoComplete search box to call any image from the list on the same screen. I tried to use viewFlipper method but did not work out. Is there any other way to do it?
You could add an onClick listener to the ImageView. Something like this:
ImageView imgFavorite = (ImageView) findViewById(R.id.favorite_icon);
imgFavorite.setClickable(true);
imgFavorite.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Change the image here.
}
});
Source: Android ImageView's onClickListener does not work

How can i display a listview by clicking on a button?

I've some buttons on main.xml layout. When i click on a button it should display listview. How can i implement this? Where can i find a best example for this?
You will want to tie your button click to your View. In this case a list. The below code should be something similar to what you have in your class.
Button Button1 = (Button) findViewById(R.id.MyView1);
MyStuff.setOnClickListener(new Button.OnClickListener() {
public void onClick(View z) {
//You will also have to understand intents
Intent myIntent = new Intent(z.getContext(), MyStuff.class);
startActivityForResult(myIntent, 0);
}
});
Assuming your XML etc are set up properly this should get you on the right track.
Show some of your code and people will be able to exactly pinpoint your problem.
You can check out this SO post which kinda covers the same thing and may get you started.
The accepted answer should tie up any loose ends.
Button Click to List View
Can you give some more details? Do you want to have the listview popup as a dialog box or a new activity? If it's a new activity you do what harper89 suggested above.
If it is a popup dialog, instead of creating a new intent in the onClick(View view) you would have to inflate the listview layout from another layout xml file, create the adapter, then set the listview to that adapter.
The ListViewis a bit tricky, since you need to add an Adapter to it, that takes the items that should be displayed.
A good starter for ListViews is the developer resource for this topic.

Categories

Resources