Reputation: 23344
My parent Linearlayout1
contains two child Frame Layouts: FrameLayout1
and FrameLayout2
.
The FrameLayout1
is on top of FrameLayout2
but only covers the half of FrameLayout2
.
I replaced FrameLayout1
with some fragment1
and also replaced FrameLayout2
with some fragment2
.
Now, when I click on FrameLayout2
, the FrameLayout1
should get Invisible. But this is not happening.
I tried the following to do so:
userDetails.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.userDetails, container, false);
topLayout = (LinearLayout) getActivity().findViewById(R.id.FrameLayout1);
bottomLayout = (LinearLayout) getActivity().findViewById(R.id.FrameLayout2);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
topLayout.setVisibility(View.GONE);
}
});
}
I also found that onClick listener of view is not getting called on its click.
UPDATE:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/FrameLayout1"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/FrameLayout2"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</FrameLayout>
Upvotes: 0
Views: 547
Reputation: 3776
I think that you have to add to your FrameLayout2 clickable. Try adding
android:clickable="true"
Then in your code setOnClickListener in your FrameLayout2. Btw I think you should cast your topLayout and your bottomLayout as FrameLayout
instead of LinearLayout
.
Note that your framelayouts should be final in order to be accessed by the listener class.
Hope it helps :)
Upvotes: 0
Reputation: 16463
You've set OnClickListener to whole View instead of bottomLayout. and I think you can't cast FrameLayout to LinearLayout. The following code works fine here.
final FrameLayout topLayout = (FrameLayout) findViewById(R.id.FrameLayout1);
final FrameLayout bottomLayout = (FrameLayout) findViewById(R.id.FrameLayout2);
bottomLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
topLayout.setVisibility(View.GONE);
}
});
Upvotes: 2