Android Custom Spinner with Image selection - java

I am working on a custom spinner, I already understand how to use an ArrayAdapter in order to customise the layout of the custom popup.
The issue I am having however is that I do not want to show the default spinner on my UI instead I want to show an image, then this image will changed based on the item that has been selected, the image would look something like below:
Further to this, is there a way that I can use wrap_content, but to wrap it based on this image size rather than the default spinner size?

If I understood correctly, you want one layout for the value shown in the Spinner proper (layoutA), and a different one (layoutB) for the spinner option views in the popup window?
If that's the case, then it's very simple: in the adapter's constructor, pass layoutA. And then call setDropDownViewResource() with layoutB.
Alternatively, if you have a custom adapter class, you can achieve the same result by overriding getView() and getDropDownView() respectively.
Setting the spinner's height to WRAP_CONTENT should work in this scenario (although I haven't tested that particular bit).

Well its very easy to implement either you use switch case or if else both will work fine. I have implement this one using if else condition by attaching image source to each argument that I have declared.
Here is example of code.
spinner = (Spinner) findViewById(R.id.spinner1);
imageview = (ImageView) findViewById(R.id.imageView1);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
//spinner.
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if(arg2==0)
{
imageview.setImageResource(R.drawable.apple);
}
else if(arg2==1)
{
imageview.setImageResource(R.drawable.microsoft);
}
else
{
imageview.setImageResource(R.drawable.google);
}
Source - http://kamleshnishad.com/android-studio-spinner-example-onclick-change-image/

Related

How to maintain state of filtered CursorAdapter through orientation changes?

I have an application that helps users organize prescriptions. I have one listview that shows medications and I use an EditText to allow the user to filter the list. The problem I'm having is that the CursorLoader is replaced each time the orientation changes.
From what I understand, the LoaderManager.initLoader() function should be called in onActivityCreated(). In my particular case, I don't have a fragment so I put the initLoader() call inside onPostCreate():
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
getSupportLoaderManager().initLoader(MEDICATION_LOADER, null, this);
}
And here is the filter I'm using:
// Set text filter
mMedication.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mMedicationAdapter.getFilter().filter(s);
}
});
I removed the empty beforeTextChanged and afterTextChanged methods to shorten this.
So, what appears to be happening (from using the debugger) is that each time I change the device orientation, initLoader() is called again and the entire list of medications are displayed, not just the filtered ones. Is there a way to implement onSaveInstanceState() to store the current filtered state of the adapter?
Or am I using the text filter wrong? Should I pass the charSequence as a Bundle argument to the loader, or create another loader that handles the text as an argument?
A solution is you can keep the current activity when orientation changes, Just set the Activity to handle config changes in AndroidManifest.xml:
Android config changes
e.g.
<activity
android:name="com.test.MyActivity"
android:configChanges="screenSize|orientation"/>
If your edittext was inflated through a layout file and it has an id; the framework should be automatically save its text.
You could try checking right before you attach your textwatcher, if the edittext has any text inside, then filter your list and then attach your textwatcher.

Android: Adding accessibility to a custom view

I'm attempting to implement accessibility on a few custom views for an Android app.
I've condensed what is done in the Google Authenticator app with no luck:
public class CardView extends RelativeLayout {
// ...
#Override
public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
event.setClassName(this.getClass().getName());
event.setPackageName(this.getContext().getPackageName());
event.getText().add("Card Test");
return true;
}
}
All TalkBack reports back is "Double-tap to select" when it's inside a ListView or ViewPager.
Does ViewPager override accessibility events?
What do I need to do in order to have TalkBack say "Card Test" inside ViewPagers and ListViews like I expect it to?
For current versions of Android, you need to set the content description of the view.
myView.setContentDescription("Card Test");
ListView and associated classes expect you to use the onItemSelectedListener instead of assigning an onClickListener to each View (and rightfully so).
If incorporating alanv's suggestion, try to convince android system to read out the content description
by either
If(accessibilityModeIsEnabled())//custom method that checks context.getSystemService(Context.ACCESSIBILITY_SERVICE).isEnabled()
myView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
or AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED.
or requestFocus
Above should done when myView is visible. May be during onMesasure when width and height are both positive
If list view is still unable to do so, then try doing the above tricks on the first element of list view. Accessibility in Android varies devices to device and not one strategy fits all

Add onLongClickListener to tab on TabHost android

On an app that I'm working on, I need a context menu to show up if a user performs a longClick on a tab, which would allow them to close the tab. I can't seem to find a way to add a listener to a tab though. I either need each tab to have its own listener or the listener needs to be able to tell which tab had the longClick performed on it, as it won't always be the active tab.
Any ideas?
I appreciate that an answer has been accepted but if you want to utilise the built-in ContextMenu capabilities rather than set onLongClickListeners on the TabWidget itself, you can do this as follows...
Example, my current TabActivity adds tabs in a for loop and to register each for context menu I do the following.
for (int tabNumber = 1; tabNumber < 8; tabNumber++) {
...
spec = tabHost.newTabSpec(tag).setIndicator(indicator).setContent(intent);
tabHost.addTab(spec);
View v = tabWidget.getChildAt(tabNumber - 1);
registerForContextMenu(v);
...
}
Then in my Activity I simply override onCreateContextMenu(...) and onContextItemSelected (MenuItem item)
#Override
public void onCreateContextMenu (ContextMenu menu, View v, ContextMenuInfo menuInfo) {
...
// The parameter v is the actual tab view and not the TabWidget
// this makes it easy to get the indicator text or its tag in order
// to easily identify which tab was long-clicked and build the menu
...
}
#Override
public boolean onContextItemSelected (MenuItem item) {
...
// Process selected item here
...
}
There's no need to set an OnLongClickListener on any views explicitly as that is done by the call to registerForContextMenu(...). Also, the ContextMenu creation and selection handling is all handled for you by the ContextMenu methods exposed by Activity.
Unless you need to handle all of this stuff yourself (for a custom context menu layout for example) it seems easier to just use what's buit-in to Activity.
A TabWidget is a View like any other; you should be able to register an OnLongClickListener with it via myTabWidget.setOnClickListener and use the View argument of OnLongClickListener.onLongClick(View v) to determine which tab was clicked.
When you use a TabSpec to register the indicator for each tab with your TabHost, if the resource you pass in has an associated ID, you should be able to use that ID to look up the tab itself. This may mean you could have to start using Views or layouts as your TabSpec.setContent or TabSpec.setIndicator arguments (if you aren't already) so you can look them up by ID later.

Java android put a variable to find a spinner id

I have a xml layout where is my spinner with id "test"
How can I put my variable "IdSpinner" into R object.
I want to make a common method to create a lot of spinners ,where argument is a spinner id.
listLanguage('test');
public void listLanguage(String test) {
final Spinner spinnerLanguage = (Spinner) findViewById(R.id....);
}
You can't do it in this way (if I understood correctly)- I mean put idSpinner in R.id..., use another approach - just create dynamically Spinner object, in this case you can create as much Spinners as you want.

How to create above respective Views into my android application?

I'm creating an application similar to the link given here, where it shows three images transition from one state to another when click.
1) Stage 1: When a series of listview of video files are stored in a video directory. How do i create this particular view into the ListView?
2) Stage 2: When a video file is being click it does not immediately play the video instead it shows a dialog box showing the detail of the file.
3) Stage 3: The user could either Exit, Select Play video or show roadmap details...
Could someone help me i'm kinna new in android/java here, i'm totally lost on how to start creating the above views like how do i populate the Listview with existing video files found in my video directory?
You realize that your basically asking someone to make the application for you? In any case, I shall try to give you some help to get started.
Stage 1: Do you know how to create a ListView? Here's an example:
listView = (ListView) findViewById(R.id.list_view);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, elements); //elements is a List<String>
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//What to do when an item is clicked
}
});
You can customize the look of the list items by creating a XML-file containing a single TextView and then use that when creating the ArrayAdapter (like R.layout.list_item).
If you want a completely custom View, like in the app in the link, you can create your own adapter and then implementing the View getView(int position, View convertView, ViewGroup parent) function, where you return the View you want to be displayed.
Example:
View row = convertView;
if (row == null) {
LayoutInflater mInflater = LayoutInflater.from(getContext());
row = mInflater.inflate(R.layout.bookings_list_item, parent, false);
}
return row;
You can create the elements list by counting the files in the video directory. I don't really know how to do this, but it shouldn't be too hard. Perhaps someone else can provide you with an answer if you don't find out yourself.
Stage 2: Implement this in the OnItemClickListener in the example above by showing a dialog.
Stage 3: Implement what the Dialog will do when it's buttons are pressed. Exit: dismiss popup. Play video: Launch an intent to a video player. I'm not sure about how to show roadmap details, but you can always use Google Maps in your app (there's a sample here).
Now, I hope you can get something useful from this. I hope I have provided you with enough details to be able to get started with researching and a little coding. :)

Categories

Resources