Can I change the width of a view inside a viewpager? - android

I'm currently using a Viewpager with 3 fragment. I would like to know if I can change the width of the first fragment.
In fact , I try this do to something like the category menu in the android play store when you watching it with tablet, it looks like a fragment but its not taking all the space when swiping to it so you still see the fragment next to it (i don't know if i'm clear). I was wondering if I could simply do that by changing the width of the first fragment from MATCH_PARENT to a fixed value, i don't even know if the result of this could be nice.
i tried setLayoutParams() , set it in the xml , nothing seems to work. the fragment still match parent.

With ViewPager, it is not supported. You have to write your own ViewGroup class, or you might try something as i answered in this question.

Actually, you might want to try setting a negative margin on the pages in the ViewPager, using setPageMargin(int). See also this answer I provided not too long ago on a similar question:
Can I set viewpager's item width?

Related

How do I make UI elements fill the entire Android Screen

I am trying to add a bar similar to the "Today" bar here, but I cannot figure out how to force the width to match the screen. I am just using a simple textview (if there is a better way.. that's awesome, please tell it to me, but i cant find anything for it)
Here is what is happening with match_parent or fill_parent I recolored the textview to show the margins (that I dont want)
match_parent should fill the entire screen.
Check your parent activity layout or your fragment layout and you should find your margins there.
BTW, if you have a list of items like the one in the picture and you want it to have sections (like 'today', 'yesterday', etc) you should probably use recyclerview with headers instead of text view. Check this library for this, it's pretty easy:
https://github.com/cymcsg/UltimateRecyclerView

Layouting android fragments programmatically

I am new to android development and currently struggling with fragments and layouts (Xamarin.Android for what it's worth).
I am adding fragments to my View programatically in the OnCreate method of my main view. One of the fragments holds the controls for navigation and implements a fly-over Idiom. Now my problem is, that I haven't found a way to make the width of the navigation fragment, say, half of the width of the parent control. In every override I have been able to access the layout has not been performed and hence the width of the fragments and the main view are both 0. Setting the size of the fragments decleratively in the layout files did not work either (neither for the translate properties which I'll nees to access in the same way to hide the fragment initially). Xamarin/VS complains that setting the size or translate properties to a string (i.e. parent_width or the like) was not valid.
Every tutorial on fragments I've read omits this point.
If I am understanding your problem correctly you are having an issue with layout sizing of fragments added dynamically.
It may be helpful if you were able to paste your code outlining how you are adding your fragments and what override methods you attempted to use. However, here is a best attempt to assist without seeing any code.
There are two approaches to sizing dynamically added elements that I have used in the past consistently. One is to use a base container for your layout of LinearLayout and specify weights for your fragments dynamically. This would allow you to specify that your fragments should consume portions of the layout easily. For instance if you have fragment A and B and you wish for fragment A to consume 25% of your layout space - you can set its weight to 1 and Fragment B's weight to 4. You can find support for dynamically setting weight of an element (such as your fragment) here: How to set layout_weight attribute dynamically from code?
Another option would be to use the an OnGlobalLayoutListener that will "fire" when the control is laid out. This will ensure when you try to measure the layout to determine its width/height that it has already laid out and consumed the proper space for measurement and you will not get back 0. There are a number of articles outlining how to write up the code for this listener scattered throughout the web. I It may also be useful to subclass out some of the listener functionality for easy reuse if you find you have a need often to know when the control has laid out. Here is a link to the Android documentation on the subject http://developer.android.com/reference/android/view/ViewTreeObserver.OnGlobalLayoutListener.html.
Hope it helps.

gridview cells in android?

While back I asked a question on adding clients to a listview, but I am wanting to transition it to gridview similar to this picture. They use textview and tablelayout, but it doesn't have to be that whatever is easier. http://sdroid.blogspot.com/2011/01/fixed-header-in-tablelayout.html would using textview/tablelayout, or is there better way of doing that?
I attempted to do it but it overlapped my header.. I forgot to save the code to show to see what I did wrong.
If I understand your question, you would like a fixed header displayed at the top of your screen. Below that, you would like a (scrollable?) gridview. Is this correct?
If so, than this is easy to accomplish. Use the FragmentActivity class (instead of the Activity class). Implement two fragments on your main page. An upper fragment will contain the header. The lower fragment will contain your gridview. They can both be displayed at the same time.

Replacing views dynamically without changing activity

I would like to know how to go about doing this small problem that I am encountering while making a video player app.
On clicking the first control(the rectangular icon) in the above image the following view must be displayed instead of it which I am quite unsure as to how to do it. Here is what it is replaced by
Also please note, by any chance the activity should not be changed. I have been able to design the views individually but having problem changing them at runtime when user clicks. Could someone go about explaining as to how it can be done or provide some suitable links to achieve my goal. Thanks.
For something as simple as this you can just change the visibility of the views.
view.setVisibility(View.INVISIBLE)
Or the more effective:
view.setVisibility(View.GONE)
Do that on the views you want gone, I suggest a wrapper class. It's either this or changing the contentView as describded below.
this.setContentView(R.layout.newLayoutToUse);
However, I have a feeling there is a better way to do what you want. It's overkill to load a complete new layout if you just want to change the image of some buttons or imageviews.
This might be a stupid solution, 'cause i'm terribly tired right now, but why not use the bringToFront() method on the View that you want to display in the front? Display them both in front of each other, maybe in a RelativeLayout, and then swap between them as you wish.
They are small objects, so don't consume memory. I don't see why this shouldn't work.
OR
Place them above one another, so they overlap and then make the above view visible/invisible depending on which one you need to display.
OR
just remembered I read somewhere that you can scroll through a ScrollView automatically from code. So display both Views in a ScrollView in succession and when pressing the button or whatever, you scroll down to make the next menu visible. When pres back, you scroll up to make the previous thing available. Should work, and might also make a nice animation between changing of the menus.

TextView's text doesn't change?

i'm facing a problem that i never came accross before:
well i successfully applyed the 3D transition between a listview and someother layout container some info about the selected item. well the probelm is: some of the textviews are displayed correctly while others display nothing, even the imageview is shown as expected...
any suggestions?
thanks in advance..
I guess you had same collision in android:name tags. Acting wierd could happen by many reasons, but one of the most liklly to happen is when you have some collision in names, I mean more views defined with same name
#+id/something definition and #id/something reference is okey
but having #+id/something twice sometimes could be a problem.
This kind of references is usually used in relative layaout when you say I want this view to be to the left (or above, or below) some other view. Then you must give a reference to other view...
I fixed this: well i don't know the exact reason but it solved the problem
i changed the textview id in the layout and it worked fine. Weird why it didn't work until this..if any one can explain it would be great^^
I have 2 text view with the same id, one of them is inside the parent layout and the other one was in the viewpager which is child of the parent layout. I changed the latter one is and the problem is solved.

Categories

Resources