Currently, I need to access parts of the ActionBar in order to attach contextual help bubbles in appropriate locations. That seems fairly simple for Menu Items as menuItem.getActionView is available.
However, I'm not sure how to get similar access to the navigation list in the ActionBar. Instead of including items in the menu xml file, the navigation options and listeners are added like this:
getActivity().getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getActivity().getActionBar().setListNavigationCallbacks(mNavigationListAdapter, this);
With Menu Items, the following works:
MenuItem menuItem = menu.findItem(R.id.my_nifty_menu_item);
View viewToAttachHelpBubbleTo = menuItem.getActionView();
How can I get a similar view reference to the navigation menu used in the ActionBar?
It is possible to store the view in the Adapter and then pull it from there.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Stuff happens...
mLastParentView = parent;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
// Stuff happens...
mLastParentView = parent;
}
public View getLastParentView() {
return mLastParentView;
}
and then after
getActivity().getActionBar().setListNavigationCallbacks(mNavigationListAdapter, this);
it is possible to just request the view for the navigation list from the adapter with getLastParentView()
Related
In my application i need to show a list of objects. In the first row of listview contains single object, but the second row should contain two objects from list. A sample image for my requirement is attached here please go through this. if any idea please share here. Thanks .
In your BaseAdapter do this.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if(position%2 != 0) {
view = layoutInflater.inflate(R.layout.view_1, null, true);
// Do action for view 1
}
else{
view = layoutInflater.inflate(R.layout.view_2, null, true);
// Do action for view 2
}
return view;
}
I need to achieve a navigation menu such as this:
I don't want to use Navigation drawer since I am already using it for other purposes.
Let me explain a little bit the situation:
I have a navigation drawer that shows a listview with some options. When user clicks on an option, navigation drawer disappear and in may content view should show some buttons with the look & feel as the image above.
When a button is pressed, a new view (corresponding to button selection) should slide in from the right.
How can I achieve this? I have not found a good resource for this.
Thanks
Jaime
I have done it finally by using a ListView.
This is the code,and I can assure it is not confusing, and in fact, this is done in every android app:
public void DrawAreas() {
final ListView areaView = (ListView) _activity.findViewById(R.id.area_list);
ArrayAdapter<TdcArea> areas = new ArrayAdapter<TdcArea>(_activity.getApplicationContext(),
android.R.layout.simple_list_item_1,
_system.getAreas()) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text = (TextView) view.findViewById(android.R.id.text1);
text.setTextColor(Color.BLACK);
Drawable icon = ContextCompat.getDrawable(_activity.getApplicationContext(), R.drawable.chevron_red);
text.setCompoundDrawablesWithIntrinsicBounds(null, null, icon, null);
return view;
}
};
areaView.setAdapter(areas);
areaView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TdcArea area = (TdcArea) areaView.getItemAtPosition(position);
Intent intent = new Intent(_activity.getApplicationContext(), FormActivity.class);
intent.putExtra("AREA", area);
_activity.startActivity(intent);
}
});
I want to hide the group expanded/collapsed indicator in n expandable list view in all of my list elements and instead show an image in place of the indicator. I've made the indicator color transparent using this:
android:groupIndicator="#android:color/transparent
But that won't let me place the image where the indicator used to be so I need a new way to do it.
Implement your own BaseExpandableListAdapter:
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
...
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View rowView = inflater.inflate(R.layout.list_group_item, null);
return rowView;
}
...
}
list_group_item should contain the desired group indicator image. And don't forget to disable native indication:
expandableListView.setGroupIndicator(null);
you need set android:groupIndicator="#null"
I have several spinners that I have created a custom ArrayAdapter for so I can change the drop down menu look. I want to manipulate the view depending on what spinner the dropdown belongs to. I thought I would be able to do something like parent.getTag() but it is returning null.
The custom array adapter looks like:
class BackgroundColorAdapter extends ArrayAdapter<String> {
BackgroundColorAdapter() {
super(SettingsActivity.this, R.layout.settings_spinner_item, R.id.item_text, textColors);
}
public View getDropDownView (int position, View convertView, ViewGroup parent){
View row=super.getView(position, convertView, parent);
if(parent.getTag().equals("background"){
//Do custom stuff here
}
return(row);
}
}
and I'm setting the tag:
settingsSpinner.setTag("bg_color_spinner");
settingsSpinner.setAdapter(new BackgroundColorAdapter());
I think I'm confused how the view hierarchy works but it seems logical that the parent of the spinner drop down would be the spinner. Anyone know how I can find out what spinner the drop down belongs to in getDropDownView?
edit: made the settingsSpinner a single spinner instead of an array of spinners to make it less confusing
Eventually got this to work, here is the code for example that changes the text font for each item in the drop down.
class TextSizeAdapter extends ArrayAdapter<String> {
TextSizeAdapter() {
super(SettingsActivity.this, R.layout.settings_spinner_item, R.id.item_text, textSizes);
}
public View getDropDownView (int position, View convertView, ViewGroup parent){
View row=super.getView(position, convertView, parent);
TextView text = (TextView)row.findViewById(R.id.item_text);
text.setTextSize(TypedValue.COMPLEX_UNIT_PX,appState.FONTSIZES[position]);
RadioButton radio = (RadioButton)row.findViewById(R.id.item_radio);
if(settingsSpinners[2].getSelectedItemPosition() == position){
radio.setChecked(true);
}else{
radio.setChecked(false);
}
return(row);
}
}
I'm unfamiliar with getDropDownView(), and don't know why you use it. Documentation for getDropDownView() states the following about the parent:
parent the parent that this view will eventually be attached to
This doesn't sound like the 'parent' you are looking for...
Since the 'parent' in the getView() call is indeed a Spinner, you could use that to store an instance variable of the parent like below:
public Spinner mParent = null;
public View getView (int position, View convertView, ViewGroup parent)
{
this.mParent = parent;
return super.getView(position, convertView, parent);
}
public View getDropDownView(int position, View convertView, ViewGroup parent)
{ // Your code here -> but use 'mParent'
}
I haven't tried, but maybe it's a workaround to get what you need. Please let me know if you found the solution.
How do I do a custom theme android spinner dialog?
This blog has an example that probably covers your question:
http://www.mokasocial.com/2011/03/easily-create-a-default-custom-styled-spinner-android/
Edit:
These blog posts has a pretty similar answer:
http://karanbalkar.com/2013/07/tutorial-39-create-custom-spinner-in-android/
http://stephenpengilley.blogspot.no/2013/01/android-custom-spinner-tutorial.html
The key to customizing an Android Spinner is to create a custom resource for the spinner rows, and then passing that row layout into a custom adapter. In the custom adapter, one can override the following two functions:
#Override
public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
// Custom view generation
}
#Override
public View getView(int pos, View cnvtView, ViewGroup prnt) {
// Custom view generation
}
To determine how the Android Spinner will look to the user.