How to set holoeverywhere slider menu width (addonSlider)? - android

I'm having a bit of a problem setting the addonSlider() (aka Sliding menu) width for menu/shadow.
I looked inside the AddonSlider class and SliderMenu class and didn't find anything that refers to setting the width of menu/shadow.
The only thing regarding that i found was this:
private void attach(View view, int gravity) {
if (view == null) {
return;
}
final ViewGroup.LayoutParams initialParams = view.getLayoutParams();
DrawerLayout.LayoutParams params;
if (initialParams instanceof DrawerLayout.LayoutParams) {
params = (LayoutParams) initialParams;
} else if (initialParams != null) {
params = new LayoutParams(initialParams);
} else {
params = new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.MATCH_PARENT);
}
params.gravity = gravity;
view.setLayoutParams(params);
ViewParent parent = view.getParent();
if (parent != null) {
((ViewGroup) parent).removeView(view);
}
requestDrawerLayout();
mDrawerLayout.addView(view, gravity == Gravity.NO_GRAVITY ? 0 : -1, params);
}
but messing a bit with this settings got me nowhere

So finally i got what i wanted. It passed me, but all i had to do is override the:
<dimen name="menu_width">200dp</dimen>. attribute
Thanx to Prototik for that

Related

I'm using material Design Fragment tabs layout

Currently my problem is how to increase the Images width and height?
Below I mentioned the Image for tab layout fragment
Please anyone help me.
Try this.
1.TabLayout 's width
public void setIndicator (TabLayout tabs,int leftDip,int rightDip){
Class<?> tabLayout = tabs.getClass();
Field tabStrip = null;
try {
tabStrip = tabLayout.getDeclaredField("mTabStrip");
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
tabStrip.setAccessible(true);
LinearLayout llTab = null;
try {
llTab = (LinearLayout) tabStrip.get(tabs);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());
for (int i = 0; i < llTab.getChildCount(); i++) {
View child = llTab.getChildAt(i);
child.setPadding(0, 0, 0, 0);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
params.leftMargin = left;
params.rightMargin = right;
child.setLayoutParams(params);
child.invalidate();
}
}
And then
tab.post(new Runnable() {
#Override
public void run() {
setIndicator(tab,60,60);
}
});
My modification w/o reflection (custom view should be set!).
for (int i = 0; i < tabs.getTabCount(); i++) {
TabLayout.Tab tab = tabs.getTabAt(i);
if (tab != null) {
View customView = tab.getCustomView();
if (customView != null) {
View targetViewToApplyMargin = (View) customView.getParent();
ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) targetViewToApplyMargin.getLayoutParams();
layoutParams.rightMargin = totalTabMargin;
targetViewToApplyMargin.setLayoutParams(layoutParams);
}
}
}
2.TabLayout 's height
app:tabIndicatorHeight="10dp"
And java for setting height
mTabLayout.setSelectedTabIndicatorHeight(10);
According to my answer

Get the sum of horizontal margins/paddings

I have a nested LinearLayout (or other type of layout), and I would just like to sum up all the horizontal values of margins/paddings of this view and its parent and potentially grandparents and so on.
I just need the clean width of this Layout which will be window width minus all horizontal margins/paddings.
I've started like that:
private int sumMargins(View view) {
LayoutParams params = (LayoutParams) view.getLayoutParams();
int margins = params.leftMargin + params.rightMargin + view.getPaddingStart() + view.getPaddingEnd();
ViewParent parent = view.getParent();
while (parent != null) {
// here I need to recursively gather all nested parents margins/paddings
// (add them to the `margins` variable)
}
return margins;
}
ViewGroup.MarginLayoutParams will give you access to the margins of a view. The following code will follow the hierarchy of views up to the top-level DecorView and sum left/right margins and padding.
private int sumMargins(View view) {
ViewGroup.MarginLayoutParams marginParams;
int margins = 0;
View decorView = getWindow().getDecorView();
while (view != null && view != decorView) {
marginParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
if (marginParams != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
margins += marginParams.getMarginStart() + marginParams.getMarginEnd() +
view.getPaddingStart() + view.getPaddingEnd();
} else {
margins += marginParams.leftMargin + marginParams.rightMargin +
view.getPaddingLeft() + view.getPaddingRight();
}
}
view = (View) view.getParent();
}
return margins;
}

Creating gridlayout not working in android

In my android app, I want to make a gridlayout of a 5x5 grid. No spacing in between any rows/columns. The grid should match the parent width/height with a 10dp margins. Also all the cells should have equal width and height (i.e. all widths the same, and all heights the same even if width not equals height).
This is what I have so far but nothing shows
public class ColorGrid {
private Context context;
private RelativeLayout container = null;
private GridLayout gridlayout = null;
public ColorGrid(RelativeLayout container) {
this.context = container.getContext();
this.container = container;
this.gridlayout = createGrid();
for(int i=0; i<25; i+=1) {
addCell();
}
container.addView(gridlayout);
}
private GridLayout createGrid() {
GridLayout gridview = new GridLayout(context);
gridview.setColumnCount(5);
gridview.setRowCount(5);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
params.setMargins(10, 10, 10, 10);
gridview.setLayoutParams(params);
return gridview;
}
private void addCell() {
View cell = new View(context);
cell.setBackgroundColor(Color.parseColor("#ffffff"));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, 0, 1.0f);
cell.setLayoutParams(params);
gridlayout.addView(cell);
}
public void dismiss() {
if (container != null && gridlayout != null) {
container.removeView(gridlayout);
}
}
}
Does anyone know whats wrong?
Thanks

Android customlayout children not gettting measured height and width

I have implemented and activity (called HomeScreenActivity) that consists of two fragments. One of them displays a list of items, and the other displays the details of the selected item. The details fragment (ResumeFragment in my code) consists of a series of components, which can be configured in height and width by the user by specifying it as a number of cells in a configurations file. These components are added to a custom layout called ComponentsLayout.
When opening the HomeScreenActivity i display each fragment in the onCreate method, and they are shown as intended.
But when i click and item in the items list fragment, and i replace the resumeFragment with a new instance of the same class, nothing is displayed.
I have spend a lot of time debugging and i see that the resumeFragment, does fill out the height and width it is given, but the componentsLayout only has a height of 1. This i have found out is because the height and width of the components contained in the componentsLayout are not getting set, even though i set their layoutParams in the componentsLayout onMeasure method.
Below is the code of the HomeScreenActivity, the ResumeFragment and the ComponentsLayout:
public class HomeScreenActivity extends FragmentActivity implements OnItemSelectedListener {
.
.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_screen);
.
.
//Check if device is in landscape orientation
if(width > height && isTablet) {
//The patient context fragment must maintain the same width as when in portrait orientation.
findViewById(R.id.patient_resume_fragment_container).setLayoutParams(new LinearLayout.LayoutParams(height, height));
findViewById(R.id.screen_pager).setLayoutParams(new LinearLayout.LayoutParams(width-height, height));
fragmentManager = getSupportFragmentManager();
patientResumeFragment = new ResumeFragment();
testFragment = new NysomTestFragment();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.patient_resume_fragment_container, patientResumeFragment);
fragmentTransaction.commit();
}
}
.
.
#Override
public void onItemSelected(Item item) {
itemResumeFragment.getView().findViewById(R.id.componentsContainer);
itemResumeFragment = new ResumeFragment(item);
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.item_resume_fragment_container, itemResumeFragment);
fragmentTransaction.commit();
}
The ResumeFragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.resume_fragment,container, false);
ComponentsLayout componentsContainer = (ComponentsLayout) fragmentView.findViewById(R.id.componentsContainer);
placeComponents(componentsContainer);
return fragmentView;
}
private void placeComponents(ComponentsLayout layout) {
List<ComponentConfig> configs = new ArrayList<ComponentConfig>(testMobile.getModel().getComponentConfiguration());
int viewId = 1;
for (ComponentConfig currConfig : configs) {
ComponentsLayout.LayoutParams params = new ComponentsLayout.LayoutParams(0, 0);
params.setWidthInCells(currConfig.getWidth());
params.setHeightInCells(currConfig.getHeight());
if (viewId == 1) {
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
} else {
if (currConfig.getPositionX() > 0) {
// looking for rightOf viewId
params.addRule(RelativeLayout.RIGHT_OF, getRightOfComponentId(configs, currConfig, viewId - 1));
}
if (currConfig.getPositionY() > 0) {
// looking for below viewId
params.addRule(RelativeLayout.BELOW, getBelowComponentId(configs, currConfig, viewId - 1));
}
}
try {
// this code will be changed in story 9
View view = currConfig.getClassType().newInstance().getView(getActivity());
view.setId(viewId);
view.setBackgroundColor(colors[(viewId - 1) % 6]);
layout.addView(view, params);
} catch (Exception e) {
// we should never get to this point as configuration is validated
Log.e(TAG, "getView: unable to create component for position " + viewId, e);
throw new RuntimeException(e);
}
viewId++;
}
}
And the ComponentLayout
public class ComponentsLayout extends RelativeLayout {
public ComponentsLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int horizontalCellsNum = getResources().getInteger(R.integer.resume_horizontal_cells_num);
int cellHeight = (int) (getResources().getDimension(R.dimen.resume_cell_height));
int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
LayoutParams params = (LayoutParams) child.getLayoutParams();
params.width = parentWidth * params.getWidthInCells() / horizontalCellsNum;
params.height = cellHeight * params.getHeightInCells();
child.setLayoutParams(params);
}
}
And once again, this works when called from the HomeScreenActivity onCreate method, but not when done from the onItemSelected method.
Can anyone help??
I have created a work around by adding all layout params to the components before onMeasure is called on the componentLayout. It was not the solution i was looking for, but it was quite simple, which is possibly why i overlooked it.

Add footer to Android TouchListView

Hi I'm using the TouchListView control from here: https://github.com/commonsguy/cwac-touchlist
and I've added some buttons to add to the list in the footer:
mFooter = getLayoutInflater().inflate(R.layout.edit_homepage_footer_layout, null);
mListView = (TouchListView) findViewById(R.id.sectionList);
mListView.addFooterView(mFooter);
It all seems to be working fine until I drag an item in the list, at which point the footer collapses (to the height of one list item I think) obscuring the buttons I have added.
Can anyone suggest a fix/workaround for this?
I actually worked this out shortly after asking it (always the way...)
The issue is in the doExpansion() and unExpandViews() methods which were modifying every item in the list including the footer. To fix it I created a method to check whether we are dealing with a draggable item or the footer:
private boolean isDraggableItem(View view) {
View dragger = view.findViewById(grabberId);
return dragger != null;
}
And then modified the methods mentioned as follows:
private void unExpandViews(boolean deletion) {
for (int i = 0; ; i++) {
View v = getChildAt(i);
if (v == null) {
if (deletion) {
// HACK force update of mItemCount
int position = getFirstVisiblePosition();
int y = getChildAt(0).getTop();
setAdapter(getAdapter());
setSelectionFromTop(position, y);
// end hack
}
layoutChildren(); // force children to be recreated where needed
v = getChildAt(i);
if (v == null) {
break;
}
}
if (isDraggableItem(v)) { //check this view isn't the footer
ViewGroup.LayoutParams params = v.getLayoutParams();
params.height = mItemHeightNormal;
v.setLayoutParams(params);
v.setVisibility(View.VISIBLE);
}
}
}
private void doExpansion() {
Log.d(logTag, "Doing expansion");
int childnum = mDragPos - getFirstVisiblePosition();
if (mDragPos > mFirstDragPos) {
childnum++;
}
View first = getChildAt(mFirstDragPos - getFirstVisiblePosition());
for (int i = 0; ; i++) {
View vv = getChildAt(i);
if (vv == null) {
break;
}
int height = mItemHeightNormal;
int visibility = View.VISIBLE;
if (vv.equals(first)) {
// processing the item that is being dragged
if (mDragPos == mFirstDragPos) {
// hovering over the original location
visibility = View.INVISIBLE;
} else {
// not hovering over it
height = 1;
}
} else if (i == childnum) {
if (mDragPos < getCount() - 1) {
height = mItemHeightExpanded;
}
}
if (isDraggableItem(vv)) { //check this view isn't the footer
ViewGroup.LayoutParams params = vv.getLayoutParams();
params.height = height;
vv.setLayoutParams(params);
vv.setVisibility(visibility);
}
}
}
Would be worth updating the github project to include this I think.

Categories

Resources