Android Resize selected item in GridView - android

I have a grid view displaying a number of ImageView with different background colors. Is it possible to scale just the selected item without affecting the placement of the other items in the GridView? Something like below?
I have tried setting the layout params on the underlying view that is returned when an item is clicked, but that seems to be chopping off part of the resized item and it not centering the selected item.
int newDimension = Utility.dpToPx(100);
float measuredWidth = view.getMeasuredWidth();
float measuredHeight = view.getMeasuredHeight();
GridView.LayoutParams layoutParams = (GridView.LayoutParams) view.getLayoutParams();
view.setPivotX(newDimension / 2);
view.setPivotY(newDimension / 2);
layoutParams.width = newDimension;
layoutParams.height = newDimension;
view.setLayoutParams(layoutParams);

close try calling, gridView.getItemAtPosition(position) and it will return your view do your operation then

Related

How to scale the view to fit the content size in Android animation?

I am using CardView as a item of Recycler Adapter.
At first, all items will not show the whole content, (I cut it and put the '... ...' instead)
but after click it, the clicked item will scale its height to fit the content height. ( Here I set the text to the whole content and hope the card can scale to fit it )
looks like:
I know how to animate the height to the specific target height..
but in this case, I don't know how to measure the height needed to show the whole content, each item should have different target height.
How can I do it?
What you can do is ask the View to measure itself giving no constraint on its height. Please note the call to view.getWidth(), you can do that only after the View had been laid out, since you're calling it in onClick() it should be fine.
int widthSpec = View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
view.measure(widthSpec, heightSpec);
int targetHeight = view.getMeasuredHeight();
Assuming that your View is a TextView with these attributes set:
android:layout_height="wrap_content"
android:maxLines="1"
android:ellipsize="end"
the full example would be
// this is the height measured with maxLines 1 and height
// to wrap_content
final int startHeight = view.getHeight();
// you want to measure the TextView with all text lines
view.setMaxLines(Integer.MAX_VALUE);
int widthSpec = View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
view.measure(widthSpec, heightSpec);
// final height of the TextView
int targetHeight = view.getMeasuredHeight();
// this is the value that will be animated from 0% to 100%
final int heightSpan = targetHeight-startHeight;
// remove that wrap_content and set the starting point
view.getLayoutParams().height = startHeight;
view.setLayoutParams(view.getLayoutParams());
Animation animation = new Animation(){
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
view.getLayoutParams().height = (int) (startHeight + heightSpan*interpolatedTime);
view.setLayoutParams(view.getLayoutParams());
}
};
animation.setDuration(1000);
view.startAnimation(animation);
you can do it very simple.
when set a text to TextView for the first time, write this:
int minLineNumber = 2;
textView.setMaxLines(minLineNumber); // 2 is your number of line for the first time
and when click on that:
textView.setMaxLines(Integer.MAX_VALUE); // set the height like wrap_content
if you want the animation, you have sth like this:
ObjectAnimator animation = ObjectAnimator.ofInt(
textView,
"maxLines",
textView.getLineCount());
int duration = (textView.getLineCount() - minLineNumber) * 50;
animation.setDuration(duration);
animation.start();
if the text in your TextView to too much, it's better to use a fixed duration, not depend on height.

Android calculate height of child View When overflow

I have nested LinearLayouts where Parent height is 90 but the child view may have different heights according to the text set inside it and may overflow its parent. I Want to animate Parent to final Height of child but both methods of getHeight() on child and getMeasuredHeight() on parent, return 90 not 400.
parent.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
targetHeight = parent.getMeasuredHeight();
//returns 90
and also
View child=((ViewGroup) parent).getChildAt(0);
int targetHeight=child.getHeight();
//returns 90
Try this code. Hope it'll work.
ViewGroup.LayoutParams params = yourView.getLayoutParams();
int height = params.height;
LayoutParams paramsChild = child.getLayoutParams();
LayoutParams paramsParent = parent.getLayoutParams();
paramsParent.height = paramsChild.height;
parent.setLayoutParams(paramsParent);

Changing RelativeLayout - the height changes but not width

I have a relative layout that will expand / collapse - so I increase/decrease the height when a user taps on it.
I'm using this code
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
params.height = intialExpandedHeight; // or initialHeight - based on the expand or collapse effect
params.width = screenWidht + 150; // or a screenWidht value when collapsed
view.setLayoutParams(params);
view.requestLayout();
The height of the layout changes and it works the way I want it to - the only problem is that the width is not changing. (it's the same every time)
Could you please tell me what seems to be the problem with my code? Why does the height changes but not width?

Android how to programmatically create scrollview and add programmatically created views into it

Alright I'm trying to build an activity that has a horizontal scrollview, that the user can swipe through, to view different "pages". My train of thought is these "pages" will be views. The following is a mockup of my idea (to mess around to see if it works)
I've experimented with this as follows:
My content view is set to the the scrollview. (unsure if this is an incorrect approach)
I create the scrollview, and place a view into it as follows:
private void setupScrollView()
{
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics ();
display.getMetrics(outMetrics);
float density = getResources().getDisplayMetrics().density;
float dpHeight = outMetrics.heightPixels / density;
float dpWidth = outMetrics.widthPixels / density;
int width = (int)MeasureUtils.convertDpToPixel(dpWidth, getApplicationContext());
int height = (int)MeasureUtils.convertDpToPixel(dpHeight, getApplicationContext());
_scrollView = new HorizontalScrollView(getApplicationContext());
_scrollView.setBackgroundColor(Color.CYAN);
_scrollView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Log.i("DEBUG", "Screen dp width = " + dpWidth + " screen dp height = " + dpHeight);
TextView view = new TextView(getApplicationContext());
view.setBackgroundColor(Color.RED);
view.setText("TEST");
view.setX(0); // Start at the left of the scrollview.
view.setWidth(width); // Size it so that it fills to the right of the scrollview.
TextView view2 = new TextView(getApplicationContext());
view2.setBackgroundColor(Color.GREEN);
view2.setText("TEST2");
view2.setX(width); // Start the second "page/view" offscreen to the right where i can scroll to it
view.setWidth(width); // Fill the screen width
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setBackgroundColor(Color.MAGENTA);
layout.addView(view);
layout.addView(view2);
_scrollView.addView(layout);
}
The idea above is that I will see a view, that takes up the screen, representing a page. This view should be "RED" in color. I can then scroll horizontally to the right and see the second view (view2) representing the next page. This view should be "GREEN" in color. This does not happen. I end up seeing what looks like 1/3rd or 1/2 of my screen being view1, the linearlayout taking up almost the whole screen (a bit of a gap to the right edge where the CYAN from the scrollview bleeds through).
Am I approaching this the wrong way, and/or is it possible to make this work the way I'm going at it?
You probably do not want to use a horizontalscroll view to create "pages".
Try looking at PageViewer
This automatically builds in all the sywpe and inflating logic for you.
Basically you will get a call to inflate a certain page. There you can then create your view (dynamically if you wish) and then just return the root to be rendered.
Alright I've figured out what I was doing wrong, and it turned out to be something very small...
The complete code is here:
private void setupScrollView()
{
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics ();
display.getMetrics(outMetrics);
float density = getResources().getDisplayMetrics().density;
float dpHeight = outMetrics.heightPixels / density;
float dpWidth = outMetrics.widthPixels / density;
int width = (int)MeasureUtils.convertDpToPixel(dpWidth, getApplicationContext());
int height = (int)MeasureUtils.convertDpToPixel(dpHeight, getApplicationContext());
_scrollView = new HorizontalScrollView(getApplicationContext());
_scrollView.setBackgroundColor(Color.CYAN);
_scrollView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Log.i("DEBUG", "Screen dp width = " + dpWidth + " screen dp height = " + dpHeight);
TextView view = new TextView(getApplicationContext());
view.setBackgroundColor(Color.RED);
view.setText("TEST");
view.setX(0);
view.setWidth(width);
view.setHeight(height - 50);
TextView view2 = new TextView(getApplicationContext());
view2.setBackgroundColor(Color.GREEN);
view2.setText("TEST2");
view2.setX(0);
view2.setWidth(width);
view2.setHeight(height - 50);
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setBackgroundColor(Color.MAGENTA);
layout.addView(view);
layout.addView(view2);
_scrollView.addView(layout);
}
This creates a horizontal scrollview programmatically, as I had, but the problem was that I was setting the second view to be "width" away, when it should be set to "0"as can be seen by:
view2.setX(0);
With that, I get 2 "views" that resemble pages in my scrollview that I can swipe through. Each taking up the whole page.
Hate having the code close and it being a simple fix that I missed :|
Hope this helps anyone else that tries to do it this way. I'm going to look into the PageViewer as Frank suggested.

Android - Dynamic textViews - setMargins not applying?

I'm trying to make a dynamic grid layout, it being API 10+ is the part that's been making it slow going. I tried to make it wrap automatically.. but in the end found it easier just to try to force it into a grid pattern using coordinates. This script was working by itself when I did the positioning at time of creation, but now I am trying to loop through each item as a sort. So if one item is deleted, they all float back into a grid without a hole in the middle.
Problem is, it seems the layout parameters are only applying to the last object.
Here's some base variables and onCreate setup:
int screenWidth;
int screenHeight;
int distStep = 130;
int leftPad = 20;
int numCols;
int baseID = 0;
android.util.DisplayMetrics metrics = this.getResources().getDisplayMetrics();
screenWidth = metrics.widthPixels;
screenHeight = metrics.heightPixels;
numCols = (int) (screenWidth - leftPad) / distStep;
int scrRemain = screenWidth - ((numCols * distStep) + leftPad);
distStep += (int) scrRemain / numCols;
Then on to the main function for adding:
public void addObjToLayout() {
RelativeLayout relLay = (RelativeLayout) this.findViewById(R.id.mainWindow);
for(int i = 1; i <= currQuantity; i++){
TextView tv=new TextView(this);
tv.setTextSize(40);
tv.setId(baseID + i);
tv.setPadding(24, 4, 24, 4);
tv.setBackgroundColor(0x110000FF);
tv.setText(String.valueOf(baseID + i)); //Val for debugging
tv.setTextColor(0xFFFFFFFF);
relLay.addView(tv);
}
baseID += currQuantity;
sortLayout();
}
Then the sorting:
public void sortLayout() {
int leftNum = 20;
int topNum = 0;
for(int i = 1; i <= baseID; i++){
TextView tv= (TextView) this.findViewById(baseID);
MarginLayoutParams mp = new MarginLayoutParams(tv.getLayoutParams());
mp.setMargins(leftNum, topNum, 0, 0);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(mp);
tv.setLayoutParams(lp);
leftNum += distStep;
if(leftNum >= distStep * numCols){
leftNum = leftPad;
topNum += distStep;
}
}
}
What I am getting is all the textViews pile up in the top left corner, except the last one which is positioned exactly where it should be. So it seems in my head, the params object isn't applying until the loop ends or something.. but logically I don't see why.
As I said, this worked when I set the params at the get go, problem is mass updating them all at once. I am pretty new to android, so I hope I'm not just doing something stupid.
Thanks for your time
Margin means it will set a gap between the previous view and current view.
When you add view1, view2 and view3 to grid layout and if you remove view2 at some point of time, then the margin for view3 is set according to view1. So, it won't leave empty space in place of view2. Instead of removing view2 at run time, set the background for view2 as null and set the text as empty as below.
textView.setBackground(null);
textView.setText("");
So that the view is still available but looks as deleted.
Started looking into GridView using an extended baseAdapter. Looks promising:
For more (see #2):
http://www.mkyong.com/android/android-gridview-example/

Categories

Resources