Action bar icon size in Android 4.2 - android

Has the action bar icon size changed in Android 4.2 ?
I've had a 120x48px HDPI icon that was rendered perfectly in Android 4.1 and below. It still is.
However, on any 4.2 device, it is squelched to fit as 48x48px from what I can see. Or something like that; it's definitely a square.
Any ideas ? Thanks !

This is not ideal, but it appears you can get around this limitation by using an custom action view.
Something like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/log_in"
android:title="Login"
android:showAsAction="always"
android:actionLayout="#layout/log_in_button"/>
</menu>
Where #layout/log_in_button points to a layout file containing an ImageButton with your icon set as the src.
You will have to bind the click listener in the OnCreateOptionsMenu method. There is a halfway good example here: http://developer.android.com/guide/topics/ui/actionbar.html#ActionView.
I only just learned to use this method, so I don't know yet if there are any major downfalls besides the increased complexity.

So, I found an answer, it's kinda hacky but works (TM):
The general idea is to listen for the layout changes and apply new bounds to the drawables. This could look like this:
public static void updateActionBar(final Activity activity) {
if (Build.VERSION.SDK_INT >= 17) {
try {
final View content = activity.findViewById(android.R.id.content);
if (content instanceof FrameLayout) {
final FrameLayout contentFrameLayout = (FrameLayout) content;
final ViewParent parent = contentFrameLayout.getParent();
if (parent instanceof LinearLayout) {
final LinearLayout parentLinearLayout = (LinearLayout) parent;
final Class<?> actionBarContainerClass = Class.forName("com.android.internal.widget.ActionBarContainer");
final Class<?> actionBarViewClass = Class.forName("com.android.internal.widget.ActionBarView");
final Class<?> actionMenuViewClass = Class.forName("com.android.internal.view.menu.ActionMenuView");
final Class<?> actionMenuItemViewClass = Class.forName("com.android.internal.view.menu.ActionMenuItemView");
for (int i = 0, childCount = parentLinearLayout.getChildCount(); i < childCount; i++) {
final View parentLinearLayoutChild = parentLinearLayout.getChildAt(i);
handleParentLinearLayoutChild(actionBarContainerClass, actionBarViewClass, actionMenuViewClass, actionMenuItemViewClass, parentLinearLayoutChild);
}
}
}
} catch (Exception e) {
// Handle or ignore
}
}
}
private static void handleParentLinearLayoutChild(final Class<?> actionBarContainerClass, final Class<?> actionBarViewClass, final Class<?> actionMenuViewClass, final Class<?> actionMenuItemViewClass, final View parentLinearLayoutChild) {
if (parentLinearLayoutChild instanceof FrameLayout && parentLinearLayoutChild.getClass().equals(actionBarContainerClass)) {
final FrameLayout actionBarContainer = (FrameLayout) parentLinearLayoutChild;
for (int i = 0, actionBarContainerChildCount = actionBarContainer.getChildCount(); i < actionBarContainerChildCount; i++) {
final View actionBarContainerChild = actionBarContainer.getChildAt(i);
handleActionBarContainerChild(actionBarViewClass, actionMenuViewClass, actionMenuItemViewClass, actionBarContainerChild);
}
}
}
private static void handleActionBarContainerChild(final Class<?> actionBarViewClass, final Class<?> actionMenuViewClass, final Class<?> actionMenuItemViewClass, final View actionBarContainerChild) {
if (actionBarContainerChild instanceof ViewGroup && actionBarContainerChild.getClass().equals(actionBarViewClass)) {
final ViewGroup actionBarView = (ViewGroup) actionBarContainerChild;
actionBarView.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() {
#Override
public void onChildViewAdded(final View parent, final View child) {
handleActionBarViewChild(child, actionMenuViewClass, actionMenuItemViewClass);
}
#Override
public void onChildViewRemoved(final View parent, final View child) {
}
});
for (int i = 0, actionBarViewCount = actionBarView.getChildCount(); i < actionBarViewCount; i++) {
handleActionBarViewChild(actionBarView.getChildAt(i3), actionMenuViewClass, actionMenuItemViewClass);
}
}
}
private static void handleActionBarViewChild(final View child, final Class<?> actionMenuViewClass, final Class<?> actionMenuItemViewClass) {
try {
if (child instanceof LinearLayout && child.getClass().equals(actionMenuViewClass)) {
final LinearLayout actionMenuView = (LinearLayout) child;
actionMenuView.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() {
#Override
public void onChildViewAdded(final View parent, final View child) {
handleActionMenuViewChild(child, actionMenuItemViewClass);
}
#Override
public void onChildViewRemoved(final View parent, final View child) {
}
});
for (int i = 0, actionMenuViewCount = actionMenuView.getChildCount(); i < actionMenuViewCount; i++) {
handleActionMenuViewChild(actionMenuView.getChildAt(i), actionMenuItemViewClass);
}
}
} catch (Exception e) {
// Handle or ignore
}
}
private static void handleActionMenuViewChild(final View child, final Class<?> actionMenuItemViewClass) {
try {
if (child instanceof TextView && child.getClass().equals(actionMenuItemViewClass)) {
final TextView menuViewChild = (TextView) child;
final Drawable[] compoundDrawables = menuViewChild.getCompoundDrawables();
final Drawable leftDrawable = compoundDrawables[0];
final int intrinsicWidth = leftDrawable.getIntrinsicWidth();
final int intrinsicHeight = leftDrawable.getIntrinsicHeight();
leftDrawable.setBounds(0, 0, intrinsicWidth , intrinsicHeight );
menuViewChild.setCompoundDrawables(leftDrawable, null, null, null);
menuViewChild.setPadding(menuViewChild.getPaddingLeft(), 0, menuViewChild.getPaddingRight(), 0);
menuViewChild.invalidate();
menuViewChild.requestLayout();
}
} catch (Exception e) {
// Handle or ignore
}
}
You then have to call updateActionBar in every Activity (I suggest making an abstract base activity from which you extend) in the following callbacks:
onCreate
onMenuOpened (I found that it would improve performance and reduce flickering (size changes of the drawables) if you call this delayed (e.g. 200ms))
onPrepareOptionsMenu (I found that it would improve performance and reduce flickering (size changes of the drawables) if you call this delayed (e.g. 200ms))
This works for me on Nexus 7 and Nexus 10 with Android 4.2. You can expect it to fail with future updates but at least for now it seems to work.

Related

How to add views to Fragment B that are not involved in a shared element transition from Fragment A?

So I have a recycler view item that looks like this:
Transition View Start
And I want to end up with this:
Transition View End
The problem I keep getting is an index out-of-bounds exception. So the start has 6 views that transition and all have the appropriate transition name based on a unique id.
The end view has all 6 but 2 more, the small water and thermometer images. Those two have no transition names. Yet they keep getting added to a list that stores the transition views. The following code is in DefaultSpecialEffectsController.java - line 701
void captureTransitioningViews(ArrayList<View> transitioningViews, View view) {
if (view instanceof ViewGroup) {
if (!transitioningViews.contains(view)
&& ViewCompat.getTransitionName(view) != null) {
transitioningViews.add(view);
}
ViewGroup viewGroup = (ViewGroup) view;
int count = viewGroup.getChildCount();
for (int i = 0; i < count; i++) {
View child = viewGroup.getChildAt(i);
if (child.getVisibility() == View.VISIBLE) {
captureTransitioningViews(transitioningViews, child);
}
}
} else {
if (!transitioningViews.contains(view)) {
transitioningViews.add(view);
}
}
}
And the index out of bounds occurs here FragmentTranstionImpl.java - line 176
void setNameOverridesReordered(final View sceneRoot,
final ArrayList<View> sharedElementsOut, final ArrayList<View> sharedElementsIn,
final ArrayList<String> inNames, final Map<String, String> nameOverrides) {
final int numSharedElements = sharedElementsIn.size();
final ArrayList<String> outNames = new ArrayList<>();
for (int i = 0; i < numSharedElements; i++) {
final View view = sharedElementsOut.get(i);
final String name = ViewCompat.getTransitionName(view);
outNames.add(name);
if (name == null) {
continue;
}
ViewCompat.setTransitionName(view, null);
final String inName = nameOverrides.get(name);
for (int j = 0; j < numSharedElements; j++) {
if (inName.equals(inNames.get(j))) {
ViewCompat.setTransitionName(sharedElementsIn.get(j), name);
break;
}
}
}
Is it possible to add the two small icons and in general any view, without them being in the starting transition view in recycler view in Fragment A?
This is a bug in Fragments , specifically fixed in Fragment 1.3.5. You'll need to upgrade to that version.
implementation "androidx.fragment:fragment:1.3.5"

Android - Disable parent view click events when popup window is viewable

I've spent a large amount of time trying to solve this issue both by myself and searching through here to find a solution, none of which have worked for me.
My current scenario is when a popup window appears I want to disable all clickable events on the foreground view which is underneath the popup window.
if (Shared.InspectionData.JobViewModel.RAMS_Id == null || Shared.InspectionData.JobViewModel.RAMS_Id.equals("")) {
// Disable foreground view here
LoadRAMSPopup();
}
private void LoadRAMSPopup() {
mainLayout.getForeground().setAlpha(150);
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
final View ramsView = layoutInflater.inflate(R.layout.popup_rams, null);
final PopupWindow popupRAMS = new PopupWindow(
ramsView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
if (Build.VERSION.SDK_INT >= 21) {
popupRAMS.setElevation(5.0f);
}
findViewById(R.id.mainLayout).post(new Runnable() {
#Override
public void run() {
popupRAMS.showAtLocation(findViewById(R.id.mainLayout), Gravity.CENTER, 0, 0);
popupRAMS.setOutsideTouchable(false);
popupRAMS.update();
Button btnGenerate = (Button) ramsView.findViewById(R.id.btnGenerate);
btnGenerate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), CreateRAMSActivity.class);
startActivity(intent);
popupRAMS.dismiss();
mainLayout.getForeground().setAlpha(0);
}
});
}
});
}
Hitchhiking off of Akshay Mukadam Disabling all child views inside the layout. I've tweaked it slightly to include enabling my views.
public static void disableEnableViews(ViewGroup layout) {
layout.setEnabled(false);
for (int i = 0; i < layout.getChildCount(); i++) {
View child = layout.getChildAt(i);
if (child instanceof ViewGroup) {
disableEnableViews((ViewGroup) child);
} else {
if(child.isEnabled()){
child.setEnabled(false);
} else {
child.setEnabled(true);
}
}
}
}
Simply give your top view an id, reference it, and then put into the method.

ViewPager like of thing in BrowseFragment in AndroidTV

I am new in Android TV. Please have a look to the below attached image.
on the top of this picture there is a crousel and all the red marked area are the images.
i want to make this screen in Android TV. i have use BrowseFragment for this. I have use two Presenter one for First Row and another for other row.
The code which i have used :
HomeFragment.java
private CustomListRowPresenter mListRowPresenter;
private void loadRows() {
List<Movie> list = MovieList.setupMovies();
mListRowPresenter = new CustomListRowPresenter(getActivity(),this);
mRowsAdapter = new ArrayObjectAdapter(mListRowPresenter);
CardPresenter cardPresenter = new CardPresenter(getActivity());
mNumberOfRows = NUM_ROWS;
HeaderItem gridHeader = new HeaderItem(0, "");
GridItemPresenter mGridPresenter = new GridItemPresenter();
ArrayObjectAdapter gridRowAdapter = new ArrayObjectAdapter(mGridPresenter);
gridRowAdapter.add(getResources().getString(R.string.grid_view));
gridRowAdapter.add(getString(R.string.error_fragment));
gridRowAdapter.add(getResources().getString(R.string.personal_settings));
mRowsAdapter.add(new ListRow(gridHeader, gridRowAdapter));
int i;
for (i = 1; i < NUM_ROWS; i++) {
if (i != 0) {
Collections.shuffle(list);
}
ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(cardPresenter);
for (int j = 1; j < NUM_COLS; j++) {
listRowAdapter.add(list.get(j % 5));
}
HeaderItem header = new HeaderItem(i, MovieList.MOVIE_CATEGORY[i]);
mRowsAdapter.add(new ListRow(header, listRowAdapter));
}
setAdapter(mRowsAdapter);
}
CardPresenter.java
public class CardPresenter extends Presenter {
private static final String TAG = "CardPresenter";
private static int CARD_WIDTH = 360;
private static int CARD_HEIGHT = 160;
private static int sSelectedBackgroundColor;
private static int sDefaultBackgroundColor;
private Drawable mDefaultCardImage;
private Activity mActivity;
public CardPresenter(Activity activity){
mActivity = activity;
}
public CardPresenter(){
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
Log.d(TAG, "onCreateViewHolder");
sDefaultBackgroundColor = parent.getResources().getColor(R.color.default_background);
sSelectedBackgroundColor = parent.getResources().getColor(R.color.selected_background);
mDefaultCardImage = parent.getResources().getDrawable(R.drawable.movie);
/*
ImageCardView cardView = new ImageCardView(parent.getContext()) {
#Override
public void setSelected(boolean selected) {
updateCardBackgroundColor(this, selected);
super.setSelected(selected);
}
};
cardView.setFocusable(true);
cardView.setFocusableInTouchMode(true);
cardView
.setBackgroundResource(R.drawable.gridview_cell_border);
updateCardBackgroundColor(cardView, false);*/
View view = mActivity.getLayoutInflater().inflate(R.layout.gridview_cell_rows, parent,
false);
LinearLayout linearLayout = (LinearLayout)view.findViewById(R.id.container_grid_view_item);
linearLayout.setBackgroundResource(R.drawable.gridview_cell_border);
view.setFocusable(true);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
private static void updateCardBackgroundColor(ImageCardView view, boolean selected) {
int color = selected ? sSelectedBackgroundColor : sDefaultBackgroundColor;
// Both background colors should be set because the view's background is temporarily visible
// during animations.
view.setBackgroundColor(color);
view.findViewById(R.id.info_field).setBackgroundColor(color);
}
#Override
public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
Movie movie = (Movie) item;
View cardView = (View) viewHolder.view;
ImageView imageView = (ImageView)cardView.findViewById(R.id.imageView);
LinearLayout linearLayout = (LinearLayout)cardView.findViewById(R.id.container_grid_view_item);
linearLayout.setBackgroundResource(R.drawable.gridview_cell_border);
Log.d(TAG, "onBindViewHolder");
if (movie.getCardImageUrl() != null) {
/* cardView.setTitleText(movie.getTitle());
cardView.setContentText(movie.getStudio());
cardView.setMainImageDimensions(CARD_WIDTH, CARD_HEIGHT);*/
Glide.with(viewHolder.view.getContext())
.load(movie.getCardImageUrl())
.centerCrop()
.error(mDefaultCardImage)
.into(imageView);
}
}
#Override
public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) {
Log.d(TAG, "onUnbindViewHolder");
View cardView = (View) viewHolder.view;
// Remove references to images so that the garbage collector can free up memory
}
So for first row i have used CardPresenter and for other row i have used GridPresenter.
My Requirment is that this viewpager type of thing should be auto rotate in every 5 second.
So Please help me how can i design this fragment.
As discussed in Creating a Catalog Browser, if you wish to customize the header views between fragments, use a Presenter and implement the abstract methods to create, bind, and unbind the view holder. In the BrowseFragment implementation that displays the catalog browser, use the setHeaderPresenterSelector() method to set the presenter for the row header or setOnItemViewSelectedListener() method to set an item selection listener, as shown in the following example.
setHeaderPresenterSelector(new PresenterSelector() {
#Override
public Presenter getPresenter(Object o) {
return new IconHeaderItemPresenter();
}
});
Then, set time duration of each page presenter using Timer as shown in this GitHub post.
For sample code implementation, this tutorial might also help.

Gingerbread listview item animation issue

I've looked for this issue but nothing I've found matches my situation.
My problem occurs onAndroid 2.3.x (on 4.x it works perfectly)
I have an application with a custom list view. I initialize my listview as follows
ListAdapter mListAdapter = new ListAdapter(getApplicationContext(), this, ...);
lvSelector = (ListView) findViewById(R.id.listView1);
lvSelector.setAdapter(mListAdapter);
My ListAdapter is as follows:
public class ListAdapter extends BaseAdapter {
static class Holder {
LinearLayout layoutRoot, layoutColor;
TextView hText;
Animation anim = AnimationUtils.loadAnimation(mActivity, R.anim.anim_list_item);
public Holder() {
layoutRoot = new LinearLayout(mContext);
layoutColor = new LinearLayout(mContext);
hText = new TextView(mContext);
}
public Holder(Holder holder) {
this.layoutRoot = holder.layoutRoot;
this.layoutColor = holder.layoutColor;
this.hText = holder.hText;
}
}
int mSwap1, mSwap2;
Animation mAnimation;
public ListAdapter(Context _context, Activity _activity, FileHandler _fileHandler, String _strSchemaName, List<String> _list, List<String> _solution) {
mContext = _context;
mActivity = _activity;
mAnimation = AnimationUtils.loadAnimation(mActivity, R.anim.anim_list_item);
mAnimation.reset();
mSwap1 = mSwap2 = -1;
/* ... */
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final int fPosition = position;
View row = convertView;
Holder lHolder = null;
if (row==null) {
LayoutInflater inflater = mActivity.getLayoutInflater();
row = inflater.inflate(R.layout.layout_schema_element, parent, false);
lHolder = new Holder();
lHolder.layoutRoot = (LinearLayout) row.findViewById(R.id.elementLayoutRoot);
lHolder.layoutColor = (LinearLayout) row.findViewById(R.id.elementLayoutColor);
lHolder.hText = (TextView) row.findViewById(R.id.textViewWord);
row.setTag(lHolder);
}
else {
lHolder = (Holder)row.getTag();
}
row.setOnClickListener(null);
if (position==0 || position==mDataList.size()-1) {
lHolder.layoutColor.setBackgroundResource(R.drawable.bg_elem_fixed);
lHolder.layoutColor.setOnClickListener(null);
}
else {
lHolder.layoutColor.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveElement(fPosition);
}
});
}
lHolder.hText.setText(mDataList.get(position));
lHolder.layoutRoot.setBackgroundResource(0);
mHolder.set(position, lHolder);
return row;
}
}
protected void moveElement(int _element) {
if (mDataList.get(_element).equals(mSolution.get(_element)))
return;
if (mSwap1==-1)
{
System.out.println("setting swap1=" + _element);
mHolder.get(_element).layoutRoot.setBackgroundResource(R.drawable.bg_elem_selected_lite);
mSwap1 = _element;
}
else
{
if (mSwap2==-1)
{
System.out.println("setting swap2=" + _element);
mHolder.get(_element).layoutRoot.setBackgroundResource(R.drawable.bg_elem_selected_lite);
mSwap2 = _element;
}
}
if (mSwap1!=-1)
{
System.out.println("running animation on mSwap1=" + mSwap1);
mHolder.get(mSwap1).layoutRoot.clearAnimation();
mHolder.get(mSwap1).layoutRoot.startAnimation(mAnimation);
}
/***** THIS IS WHAT DOES NOT WORK *****/
if (mSwap2!=-1)
{
System.out.println("running animation on mSwap2=" + mSwap2);
mHolder.get(mSwap2).layoutRoot.clearAnimation();
mHolder.get(mSwap2).layoutRoot.startAnimation(mAnimation);
}
if (mSwap1!=-1 && mSwap2!=-1)
{
mHolder.get(mSwap1).layoutRoot.setBackgroundColor(0);
mHolder.get(mSwap2).layoutRoot.setBackgroundColor(0);
if (mSwap1==mSwap2)
{
mSwap1 = mSwap2 = -1;
return;
}
Collections.swap(mDataList, mSwap1, mSwap2);
Collections.swap(mHolder, mSwap1, mSwap2);
Collections.swap(dataObjs, mSwap1, mSwap2);
mSwap1 = mSwap2 = -1;
notifyDataSetChanged();
}
}
}
Everything works fine when I perform Collections.swap(list, mSwap1, mSwap2), elements are correctly swapped.
First animation (mSwap1) is run fine; my problem is that when second animation is run (mSwap2), it is executed on another element in screen even if mSwap2 is right (e.g.: mSwap1=1 -> second element in list is animated, mSwap2=2 -> n-1 element and n-2 element in list are animated where n is the number of visible elemnts).
I've solved my problem replacing animation calls
mHolder.get(idx).layoutRoot.clearAnimation();
mHolder.get(idx).layoutRoot.startAnimation(mAnimation);
with the following method
private void animateItem(int _index, Animation _animation) {
if (
_index<mLvSelector.getFirstVisiblePosition() // selected item is above first visible element
|| _index>mLvSelector.getLastVisiblePosition() // selected item is below last visible element
)
// element is invisible -> no animation
return;
int newIndex = _index;
if (
android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH // before android 4.0
&& mSwap2>=0 // second selection
&& mSwap1!=mSwap2 // second selection differs from first selection
)
newIndex = mLvSelector.getFirstVisiblePosition() + (mLvSelector.getLastVisiblePosition() - _index);
mHolder.get(newIndex).layoutRoot.clearAnimation();
mHolder.get(newIndex).layoutRoot.startAnimation(_animation);
}
Adding Animation argument to the method allows to differentiate animation between elements (mSwap1 and mSwap2).

Unable to have a change in the LayoutParams of Android Gallery item be reflected in the view

I'm using the Android Gallery in SDK 2.3.3. I need the margin to be wider for the selected item. I keep a reference of the "last" selected item so when a new selection is made, I can modify the margin in the layout of the newly selected item as well as the previously selected item. I have tried invalidating the view items concerned but can't seem to trigger the gallery items to be "refreshed". Any help on how to make the change in margins affect the gallery would be greatly appreciated.
I'm happy to clarify any point, if necessary.
Thanks,
Yuan
private class MySelectListener implements AdapterView.OnItemSelectedListener {
private static final int unselected_margin_left = 1;
private static final int unselected_margin_right = 1;
private static final int selected_margin_left = 15;
private static final int selected_margin_right = 15;
private LinearLayout currWrapper = null;
private LinearLayout lastWrapper = null;
private Context ctx;
public MySelectListener(Context c) {
ctx = c;
}
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
// This LinearLayout is a wrapper for the gallery items
currWrapper = (LinearLayout)(v.findViewById(R.id.unconf_order_item_layout_wrapper));
// Reset the margins of the view that was selected
try {
if (lastWrapper != null) {
setMargins(lastWrapper, false);
}
} catch (Exception clear) {
}
// Set the "wider" margins for the selected item
try {
setMargins(currWrapper, true);
} catch (Exception animate) {
}
lastWrapper = currWrapper;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
private void setMargins(LinearLayout view, boolean selected) {
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)view.getLayoutParams();
if (selected) {
Toast.makeText(ctx, "setting selected margins", Toast.LENGTH_SHORT).show();
layoutParams.setMargins(selected_margin_left, 0, selected_margin_right, 0);
} else {
Toast.makeText(ctx, "setting unselected margins", Toast.LENGTH_SHORT).show();
layoutParams.setMargins(unselected_margin_left, 0, unselected_margin_right, 0);
}
}
}

Categories

Resources