I'm trying to add two decorations to a ViewPager. One, PageIndicator, is a custom decoration I made, and the other is a standard PagerTitleStrip.
Adding these as the ViewPager's children in the XML doesn't do anything, and at least the PageIndicator's constructor doesn't even get called. So, I'm currently using this bit of code:
mPageIndicator = new PageIndicator(this);
PagerTitleStrip strip=new PagerTitleStrip(this);
float d=getResources().getDisplayMetrics().density;
ViewPager.LayoutParams params = new ViewPager.LayoutParams();
params.isDecor = true;
params.gravity = Gravity.BOTTOM;
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
mPageIndicator.setLayoutParams(params);
ViewPager.LayoutParams params2=new ViewPager.LayoutParams();
params2.isDecor = true;
params2.gravity = Gravity.BOTTOM;
params2.height= (int) (20*d);
strip.setLayoutParams(params2);
mViewPager.addView(strip);
mViewPager.addView(mPageIndicator);
This gets executed during my activity's onStart, because I'm showing a splash screen with the same activity and calling setContentView in onStart.
This makes both decorations visible at the bottom of the view, but the title strip doesn't show any text before the view is swiped once, and swiping back shows the first page title. This behavior repeats whenever mViewPager's setAdapter is called. I have tried calling setCurrentItem twice when the adapter is set, but that doesn't seem to help.
How do I work around this?
As CommonsWare pointed out, this is a bug with appcompat-v4 23.0.1. I managed to solve this by returning to support appcompat-v7 and appcompat-v4 version 22.2.0.
This answer to a duplicate question offers an alternative solution.
Related
I'm dynamically injecting a content view into the CoordinatorLayout and would now like to apply a layout_below property to the injected view so that it isn't hidden behind the AppBar.
Is there any way to do this at runtime from code instead of xml properties of the annotation?
Taking one step back and building the entire view in plain xml, I realized that layout_below is not the property I needed for my use case: placing the content view below the app bar. I did not make this clear in my question though, as I assumed layout_below would be the proper option for that.
In fact, to insert a non scrolling view into the CoordinatorLayout it should first be wrapped with a android.support.v4.widget.NestedScrollView. Then, to avoid its content being hidden behind the app bar, it is necessary to update its behavior to android.support.design.widget.AppBarLayout.ScrollingViewBehavior. Otherwise a default behavior is used which will hide behind the app bar.
val viewToInsert = getLayoutInflater.inflate( id, coordinatorWrapper, false )
val p = viewToInsert.getLayoutParams.asInstanceOf[Coordinator.LayoutParams]
p.setBehavior( new ScrollingViewBehavior )
coordinatorWrapper.addView( viewToInsert, 1, p )
You could try setting an anchor.
ContentView view = getContentView(); //your view
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) view.getLayoutParams();
params.setAnchorId(R.id.app_bar_layout);
params.anchorGravity = Gravity.BOTTOM; //we will anchor to the bottom line of the appbar
params.gravity = Gravity.BOTTOM; //we want to be BELOW that line
view.setLayoutParams(params);
I'm using AndroidResideMenu (https://github.com/SpecialCyCi/AndroidResideMenu) and I've found a problem related with the view size.
The library works by inflating a FrameLayout, removing the current view from the DecorView and adding it to the new FrameLayout (wich also contains scrollviews for adding menu items, and a shadow view).
The old view mantains its size and position, but the new FrameLayout seems to be in fullscreen mode and is hidden under the status bar when the menu is open, as I describe here (https://github.com/SpecialCyCi/AndroidResideMenu/issues/33).
In api14+ devices the problem can be solved by adding a call to setFitsSystemWindow(true) in the ResideMenu, but I don't know what to do in older devices.
Ideas?
Thanks!
Well, I've found the solution.
Just implement the method fitSystemWindows(Rect insets), making what is said in the documentation of this method (which I misunderstood until now):
The default implementation of this method simply applies the content insets to the view's padding, consuming that content (modifying the insets to be 0), and returning true. This behavior is off by default, but can be enabled through setFitsSystemWindows(boolean).
So, in the ResideMenu class:
#Override
protected boolean fitSystemWindows(Rect insets) {
setPadding(paddingLeft + insets.left, paddingTop + insets.top, ...);
insets.left = insets.top = insets.right = insets.bottom = 0;
return true;
}
Hope this helps!
I want to display two screen (One main and another at bottom). Following is my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seekbar);
showBottomScreen();
}
private void showBottomScreen() {
setContentView(R.layout.bottom_layout);
LinearLayout layout = (LinearLayout)findViewById(R.id.bottom_layout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.leftMargin = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics());
params.rightMargin = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics());
params.gravity = Gravity.CENTER;
layout.addView(new bottomProvider(this).generateInLineView(),params);
}
Problem is that now i am able to see bottom screen not main screen. Please give some suggestion?
What is happening now:
Using setContentView() twice only sets the content of the activity to whichever is set the last.
So:
setContentView(R.layout.seekbar); and after that
setContentView(R.layout.bottom_layout);
Just sets the bottom_layout.xml as the content view of your activity.
What you want:
There are alternatives to achieve what you want, depending on your requirement.
A simple way of doing it:
You can try including all elements of your screen as one, in either seekbar.xml or bottom_layout.xml
If working your way with various layouts - relative, linear, frame and a scrollview.
Or
Go for the multi-pane UI and related development by using Fragments in Android app,
for reference:
1. Multi-pane development in Android with Fragments
2. Android Fragments Example
3. Imp read:How to use Fragments? and its Life cycle
I think what you're looking for are fragments.
I wish to create a activity that look like a popup window
That is with transparent background for activity als i wish to show it in custom position on scree that is on right corner of device screen
what i did was inside onCreate of pop over like activty
Display display = getWindow().getWindowManager().getDefaultDisplay();
WindowManager.LayoutParams params = getWindow().getAttributes();
// params.x = -20;
params.height = (display.getHeight()) / 2;
params.width = (display.getWidth()) / 2;
// params.y = -10;
params.gravity = Gravity.RIGHT;
getWindow().setAttributes(params);
in maifest
<activity android:name=".DialogAct"
android:theme="#android:style/Theme.Holo.Light.NoActionBar"></activity>
This is what my launcher activity looks like
So when i click on search on action bar i'm sending intent to my new activity .I want to make it look like pop just below the search icon
This what i obtained is
You can see search of previous activity is clicked and new activity is loaded(blue portion).
As you can see the new activity moves to centre of screen . but i want it just below the action bar icon.
i tried params.gravity = Gravity.TOP | Gravity.RIGHT;. then i got this
I want to place it just below the action bar of previous activity . I tried many ways to achieve it but failed. so can anyone suggest a methode
In your manifest you wrote
<activity android:name=".DialogAct"
android:theme="#android:style/Theme.Holo.Light.NoActionBar">
you should remove the NoActionBar part
<activity android:name=".DialogAct"
android:theme="#android:style/Theme.Holo.Light">
I can think of two things that may help. First, you could try changing the Theme in your manifest to android:theme="#android:style/Theme.Dialog and this should give you the wanted look.
Edit
You can hide the title bar with
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
just make sure to call it before calling setContentView()
Also, if it doesn't need to be a separate Activity you could use PopupWindow. With this you can use isAboveAnchor and use the ActionBar as the anchor and place it below or use one of the other associated methods. I don't know if either of these will fulfill what you need but they may work for you.
#edwin What you can do is after making
params.gravity = Gravity.TOP | Gravity.RIGHT;
you just provide some margin from top, like this:
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);
yourCustomPopOveractivity.setLayoutParams(params);
and i think after doing this you can get your needed position and everything you just did is right.
Please try then tell me
Try Gravity to Center rather than Top Right..
I'm doing this in Monodroid(C#) but I'm sure most of you understand anyway. I need to move a certain textview in intervalls. The first aligning to its parent left works fine. Then i wait 4 seconds and want it to align right of the parent, which gives no effect at all. It stays left. How come?
Example (C#)
RelativeLayout.LayoutParams newParams = (RelativeLayout.LayoutParams)textView.LayoutParameters;
newParams.AddRule(LayoutRules.AlignParentLeft);
textView.LayoutParameters = newParams;
System.Threading.Thread.Sleep(4000);
RelativeLayout.LayoutParams newParams2 =(RelativeLayout.LayoutParams)textView.LayoutParameters;
newParams2.AddRule(LayoutRules.AlignParentRight);
textView.LayoutParameters = newParams2;
Really odd since it works flawless the 1st time..
after setting new layout parameters, you need to call requestLayout() on your view to take effects.
requestLayout()
Call this when something has changed which has invalidated the layout
of this view. This will schedule a layout pass of the view tree.
so you should call:
textViewToAnimate.requestLayout();