switching between layouts using setvisibility on android not working - android

I'm using a screen that I want to switch between layouts on the click of a button. I want both layouts to occupy the full height of the screen, when the button on the first layout is pressed, i want this layout to disappear and the other layout to take it's place, and then same on the next layout.
<LinearLayout
android:id="#+id/layoutFirst"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
..... views
</LinearLayout>
<LinearLayout
android:id="#+id/layoutSecond"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
..... views
</LinearLayout>
On each layout I have a button. I want this button to switch so the other layout occupies the full screen, but nothing happens. The code in the on-click event for the first screen is the following
LinearLayout layoutFirst = (LinearLayout) this.findViewById(R.id.layoutFirst);
layoutFirst.setVisibility(View.GONE);
LinearLayout layoutSecond = (LinearLayout) this.findViewById(R.id.layoutSecond);
layoutSecond.setVisibility(View.VISIBLE);
Just in case the problem was to do with the fill_parent, I also tried this with the height being set to wrap_content. Initially I can see both layouts on the screen, when I press the button, the layout still does nothing.
Can anyone tell me if I am doing something wrong or if there's a way to solve this issue. Any help would be greatly appreciated it

Put the two LinearLayouts inside a FrameLayout:
<FrameLayout
android:id="#+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/layoutFirst"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:visibility="gone">
</LinearLayout>
<LinearLayout
android:id="#+id/layoutSecond"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
</FrameLayout>
Give the LinearLayouts different ids!

Is your parent layout a "RelativeLayout" ? => not necessary here.
Try also the solution proposed in this quite similar thread how to hide linearlayout from java code?
Update:
/*global or add final keyword if not global in order to use inside listener */
LinearLayout layoutFirst;
LinearLayout layoutSecond;
/*end global*/
/*in onCreate(Bundle) method*/
layoutFirst = (LinearLayout) this.findViewById(R.id.layoutFirst);
layoutSecond = (LinearLayout) this.findViewById(R.id.layoutSecond);
/*and then in your listener, alternatively*/
layoutFirst.setVisibility(View.GONE);
layoutSecond.setVisibility(View.VISIBLE);

Related

scrollview doesn't scroll setting height programmatically

I would start with my goal. What I want to achieve is to have layout which is 3 times bigger then real screen of phone.
Right now I try to test my solution, but I have problem with scrolling.
This is how look my layout:
<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="400dp"
tools:context=".Zo"
android:id="#+id/zo_root"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="9">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:background="#color/Red"
></FrameLayout >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:background="#color/Yellow"
></FrameLayout >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:background="#color/Blue"
></FrameLayout >
</LinearLayout>
</ScrollView>
My code behind:
#Override
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zo);
setParameterForScroolView();
}
private void setParameterForScroolView()
{
ParameterKeeper parameterKeeper = new ParameterKeeper(this);
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.zo_root);
linearLayout.getLayoutParams().height = parameterKeeper.getHeightOfScreen() *3;
}
The code behind works well, cause my whole screen is red, but I can't scroll it at all.
There was answer which said, that I need to remove from scrollView android:fillViewport="true". After that my LinearLayout diseapers like in this question.
I added LinearLayout as root (source) but it not helped me at all.
Have you got any idea to solve it?
I try also set height 1600dp instead 400dp for Root LinearLayout in xml to be sure, that it's not problem with recalculating view. It's not working too.
You are changing the size of the wrong layout. you suppose to modify the height of the first layout inside the scroll view.
Some way scroll doesn't work if the child elements have layout_weight and I change programmatically value of height in parent's view.
In my case the only way to make it works was to set height equal to size of screen in every of frame layout in code of activity. This way I could reach my goal which was to have layout height == 3 x real screen size

Checking child is going out of bound in LinearLayout

I am dynamically adding children in LinearLayout horizontally. I am adding child on button click. But after adding some children, say 4, others children go out of screen as these are being added horizontally. How can I check that child is going beyond screen in LinearLayout, and I need to create new layout? Or what can I do with the LinearLayout so that when it goes out of screen, it wraps itself?
Any help? Thanks in advance.
Just put your LinearLayout inside HorizontalScrollView, it will do all the necessary work for you:
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
</HorizontalScrollView>
EDIT if you want your views to be wrapped inside the parent view, please take a look here Line-breaking widget layout for Android

Activity does not scroll when rotating screen

when I turn the phone and the screen is rotating I cannot see the whole screen anymore. This is ok but I cannot scroll! Do I have to set some flag or property for scrolling? This is part of my xml...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="fill_parent" android:layout_height="fill_parent" >
<TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="*">
...
</TableLayout>
<TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
...
</TableLayout>
</LinearLayout>
Thanks!
You have to put your complete layout inside a ScrollView in order for it to scroll.
It scrolls if your layout height is more than the screen height, which happens generally in landscape mode.
In your case put the LinearLayout inside a ScrollView, since ScrollView can only have 1 child.
You must know that in order to use an scrollView , you must put only one component inside (maybe a linearLayout) .Maybe that's your problem if you are trying to put in more components at that level.

Reserve a "slot" in a layout XML file for a dynamically created button?

In my app, I have one (and only one) UI element which isn't referenced in the XML layout file.
That element is a button, instantiated and returned at run-time by a 3rd party library (i.e. I don't have control over that).
My problem is that I would like some of the elements (TextViews) in the XML layout file to be placed relative to that button, using RelativeLayout.
Is it possible to "reserve an empty slot" in the XML layout file for that button such that I can do something like the following?
<TextView android:id="#+id/tv_text_under_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btn_dynamically_created_button"
android:text="" />
Alternatively, if I were to set the layout at run-time using RelativeLayout.LayoutParams.addRule(), what would be the ID of that dynamically created button, if it has no reference at all in the XML layout file?
For example, in the following call:
layoutParams.addRule(RelativeLayout.BELOW, R.id.btn_dynamically_created_button);
What would I put instead of R.id.btn_dynamically_created_button?
Update: Thanks to the answer below, I created a place holder like this:
<LinearLayout android:id="#+id/btn_dynamically_created_button"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</LinearLayout>
The challenge now is: How to associate the returned object from getDynamicallyCreatedButton() (returned object is subclass of LinearLayout, not Button), with R.id.btn_dynamically_created_button?
EDIT: This thread seem to address a similar issue, but I am not sure that I understand the solution offered.
I'd suggest:
Put a LinearLayout with width/height set to wrap-content, horizontal orientation and zero padding as the placeholder.
Orient all the other things to that LinearLayout.
When its time to put the button, simply stick it into the LinearLayout.
See if that works for you.
EDIT: attempt at a short example:
The layout (suitably shortened): you can place other components relative to the LinearLayout with id LinearLayout01.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_marginTop="2sp" android:layout_marginBottom="2sp" android:layout_height="wrap_content">
<LinearLayout android:id="#+id/LinearLayout01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:gravity="right" style="#style/SimpleButtonBar" android:layout_below="#+id/rootlayout" android:layout_alignParentBottom="true">
</LinearLayout>
<ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentTop="true" android:layout_above="#+id/LinearLayout01" android:fillViewport="true">
<RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="#+id/detaillayout">
</RelativeLayout>
</ScrollView>
</RelativeLayout>
The code (for example, this would go in onCreate): fetch your button (you need to make sure it has the right Context, but I figure you're doing that alright), fetch the LinearLayout, create a layout parameters object and stick your button into the LinearLayout.
Button b = getButton(); // retrieve your button somehow
LinearLayout l = (LinearLayout)findViewById(R.id.LinearLayout01);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
l.addView(b, lp);

Nested Linear layout only shows first view after being set from gone to visible in Android

I am developing an Android app but I'm still pretty new. I want to have a button, and when you push that button, a few TextViews and Buttons will appear. So I have a main linear layout, and then another linear layout nested inside containing the things I want hidden. I have the nested linear layout set to android:visibility="gone".
The problem I am having is that it only shows the first item inside the hidden linear layout instead of all of them. The way I try to make it appear is
vgAddView = (ViewGroup)findViewById(R.id.add_details);
btnAche.setOnClickListener(new OnClickListener(){
public void onClick(View v){
vgAddView.setVisibility(0);
}
});
My XML file is this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:text="#string/but_stomach_ache"
android:id="#+id/but_stomach_ache"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button
android:text="#string/but_food"
android:id="#+id/but_food"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<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/add_details"
android:visibility="gone">
<TextView
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/when_happen">
</TextView>
<Button
android:text="#string/happen_now"
android:id="#+id/happen_now"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
</LinearLayout>
Your TextView in the LinearLayout is set to android:layout_width="fill_parent" and android:layout_height="fill_parent", which means it will take up the entire space of the LinearLayout, leaving no room for the Button. If you use the hierarchyviewer tool that shipped with the SDK, you can see that when you look at the activity.
You need to set the height of the TextView to be wrap_content or otherwise have it leave room for the Button.
After you are setting its visibility to true set like below:
vgAddView.setVisibility(View.VISIBLE); - to show it
vgAddView.setVisibility(View.GONE); - to hide it
You first TextView has a layout_width="fill_parent".
It's mean,that you TextView don't leave space for Button.
You may try to set layout_weight attribute or set layout_width to wrap_content

Categories

Resources