I've been trying to figure out how to place a container that fills the entire screen vertically inside a ScrollView that has content below.
Something like this:
[ BOX THAT FILLS ENTIRE SCREEN ]
[ Some other stuff / requires scroll to view ]
This is the content I would like below the box:
http://myupload.dk/handleupload/ce34836ostt
I've tried everything - it seems like i can't position stuff absolute to the container that fills the entire screen. I can make a box that fills the entire screen, but everything i adds below it squeezes the box together.
It works when I add a size to the box, like this:
<com.github.ksoichiro.android.observablescrollview.ObservableScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top"
android:minWidth="25px"
android:minHeight="0px"
android:paddingTop="0dp"
android:fillViewport="true"
android:id="#+id/scroll">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- THIS BOX I WOULD LIKE TO FIT THE ENTIRE SCREEN -->
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="#+id/cardsPager" />
<!-- THIS CONTENT SHOULD BE BELOW THE BOX ABOVE -->
<fragment android:name="bonnier.hvadsynes.fragments.DetailsFragment"
android:id="#+id/details_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</com.github.ksoichiro.android.observablescrollview.ObservableScrollView>
Really appreciate your help!
Simon
My idea is to set the height of the screen-filling-box programmatically.
When and Where: depends on what you use e.g.in onResume there surely are better places but for now it works.
How: add an OnGlobalLayoutListener to your viewtreeobserver and then copy your scrollviews height to 'the box'.
<!--Whatever the whole things height should be, e.g. match_parent or 100dp or or or-->
<ScrollView
android:background="#0f0"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollview">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- THIS BOX I WOULD LIKE TO FIT THE ENTIRE SCREEN -->
<!-- Any height works as we overwrite it anyway-->
<View
android:id="#+id/makebig"
android:background="#f00"
android:layout_width="match_parent"
android:layout_height="300dp" />
<!-- THIS CONTENT SHOULD BE BELOW THE BOX ABOVE -->
<View
android:background="#0ff"
android:id="#+id/details_fragment"
android:layout_width="match_parent"
android:layout_height="100dp"/>
</LinearLayout>
</ScrollView>
In your Activity or wherever: e.g. in onResume()
final View makebig = findViewById(R.id.makebig);
final View scrollview = findViewById(R.id.scrollview);
final ViewTreeObserver viewTreeObserver = scrollview.getViewTreeObserver();
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
//only do this all once
scrollview.getViewTreeObserver().removeOnGlobalLayoutListener(this);
makebig.getLayoutParams().height = scrollview.getHeight();
}
});
You can do it this way:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#A5CB3A"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="#+id/layout2"
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical" >
<!-- here goes your textview -->
</LinearLayout>
</LinearLayout>
And in the onCreate method of the Activity
Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size);
int screenHeight = size.y;
LinearLayout layout = (LinearLayout)findViewById(R.id.layout1);
ViewGroup.LayoutParams params = layout.getLayoutParams();
params.height = screenHeight; //here you can substract the height of the top bar, if your app has one
layout.setLayoutParams(params);
You are just setting the height of the screen to the inner layout.
Related
I am using umano sliding up panel library. I need to set panel's height to be same as a second Textview height. Example a textview.
<?xml version="1.0" encoding="utf-8"?>
<com.sothree.slidinguppanel.SlidingUpPanelLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:sothree="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:id="#+id/testtest"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:gravity="bottom"
sothree:umanoPanelHeight="65dp"
sothree:umanoShadowHeight="4dp"
>
<TextView
android:textSize="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="main"/>
<TextView
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="second"/>
</com.sothree.slidinguppanel.SlidingUpPanelLayout>
Is there any method which it can take textview height and send it in umano panel's heigt>?
Or if i can calculate textview text size to height in dp
I want to have a dialog with buttons
whenever i click on a specific button, I want the dialog to "flip" and show another layout. A click on another button will return to the original dialog's view.
I try to use ViewFlipper as follows:
xmls:
send_feedback_dialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/feedback" />
</LinearLayout>
...
social_actions_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_activity_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent" >
<ViewFlipper
android:id="#+id/viewFlipper"
android:layout_width="300dp"
android:layout_height="407dp"/>
<RelativeLayout
android:id="#+id/main_activity_card_face"
android:layout_width="300dp"
android:layout_height="407dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#android:color/white"
android:clickable="true"
android:onClick="onCardClick"
android:padding="5dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
...
code:
final ViewFlipper viewFlipper = (ViewFlipper) mDialog
.findViewById(R.id.viewFlipper);
View feedbackview = View.inflate(mContext,
R.layout.send_feedback_dialog, viewFlipper);
// ((ViewGroup)
// feedbackview.getParent()).removeView(feedbackview);
// viewFlipper.addView(feedbackview);
// View socialActions = View.inflate(mContext,
// R.layout.social_actions_dialog, viewFlipper);
// ((ViewGroup) socialActions.getParent())
// .removeView(socialActions);
// viewFlipper.addView(socialActions);
private void flipDialog(ViewFlipper viewFlipper,
boolean isSocialActionsShown, AlphaAnimation alphaIn,
AlphaAnimation alphaOut) {
if (isSocialActionsShown) {
isSocialActionsShown = false;
viewFlipper.setInAnimation(alphaIn);
viewFlipper.setOutAnimation(alphaOut);
// Show the next Screen
viewFlipper.showNext();
} else {
isSocialActionsShown = true;
viewFlipper.setInAnimation(alphaIn);
viewFlipper.setOutAnimation(alphaOut);
viewFlipper.showPrevious();
}
}
I used to get java.lang.StackOverflowError and then i have commented some rows and now the flip is executed but nothing changes.
how can i make it properly work?
what is the right way to organize the dialog layout in different files?
i have tried to create the dialog in a different file but it requires many things and maybe it's better to use it as an inner class
I can't seem to get these image views working. I wan't to create a few imageviews dynamically but I want them to have a template or so from xml.
My loop looks as so:
LinearLayout layout = (LinearLayout) findViewById(R.id.mainLinearLayout);
for (int i=0; i<10; i++){
ImageView imgView = new ImageView(this);
imgView.setAdjustViewBounds(true);
imgView.setId(R.id.coverview1);
layout.addView(imgView);
Picasso.with(context).load("http://blah.com/image.jpg").into(imgView);
}
I'm creating a few images and just placing some temporary image in them but can't seem change the image size at all.
Here is my xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/mainLinearLayout">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/coverview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:maxWidth="20dp"
android:layout_gravity="center_horizontal" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
Taking the advice of the answer below, I added what he said but now am not able to change the scale type.
imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imgView.setAdjustViewBounds(true);
But the image is still the height of the container and not stretching.
You Need to add LayoutParams for your ChildView Like:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
yourCalculatedWidth, LayoutParams.FILL_PARENT);
params.gravity=Gravity.CENTER_HORIZONTAL;
imgView.setLayoutParams(params);
Why is there a Scroll Bar in List View even when the data in displayed in it doesn't require scrolling?
here is my xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:orientation="vertical" >
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#null"
android:dividerHeight="0dip" />
<TextView
android:id="#+id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/no_result_text"
android:visibility="gone" />
</LinearLayout>
Comparing getLastVisiblePosition() with getCount() in a Runnable, you will get to know whether your list view will fit into the screen or not. Also check if the last visible row fits entirely on the screen or not.
ListView listView;
Runnable fitsOnScreen = new Runnable() {
#Override
public void run() {
int last = listView.getLastVisiblePosition();
if(last == listView.getCount() - 1 && listView.getChildAt(last).getBottom() <= listView.getHeight()) {
listView.setScrollContainer(false);
}
else {
listView.setScrollContainer(true);
}
}
};
Finally in ListView's Handler: onCreate()
listView.post(fitsOnScreen);
You can do this by
listView.setScrollContainer(false);
for more please check
How to get a non scrollable ListView?
If you are not want to hide the scrollbar for ever. Then it will help you.
Due to android:layout_height="match_parent" on listView and TextView scrollbar appears.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:orientation="vertical" >
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" // change 1
android:divider="#null"
android:dividerHeight="0dip" />
<TextView
android:id="#+id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content" //change 2
android:gravity="center"
android:text="#string/no_result_text"
android:visibility="gone" />
</LinearLayout>
android:scrollbars="none"
Try putting the above line in your listview in xml.
I try to make a ScrollView to act like a ListView. So every row will be a TextView added dynamically. but the program crashes. I have a ScrollView inside a LinearLayout. What i do wrong? Thanks.
Here is my code
scroll = (ScrollView)findViewById(R.id.scrollView1);
layout = (LinearLayout) findViewById(R.id.LinearLayout1);
layout.setBackgroundColor(Color.TRANSPARENT);
TextView[] tx = new TextView[10];
for (int i = 0; i < 10; i++) {
tx[i] = new TextView(HelloWorldActivity.this);
tx[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
tx[i].setText("This is the textviewNo" + i);
layout.addView(tx[i]);
}
scroll.addView(layout);
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/timeflag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="50sp"
android:gravity="center"/>
<Button
android:id="#+id/pause"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/timeflag"
android:layout_marginTop="37dp"
android:text="Start" />
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/pause"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ScrollView>
</LinearLayout>
</RelativeLayout>
A scrollView Can only have one child. If you try to add more than 1 child to the scroll view then it will crash. You should have the LinearLayout inside of the scrollview and then dynamically add textViews to the linear layout.
You are trying to add LinearLayout1 as its own child's child (being your own grandfather, is a confusing notion).
Change you XML around like this:
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/pause"
>
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
You cannot add views to a scrollview. You can only add them to a viewgroup, like linear or relativelayout.
It is not exactly clear what you are trying to achieve, but what is wrong with using a listview for this? You could set a max height on the listview. Or you could try wdziemia's suggestion