Child Layout visibility gone on another child layout click event - android

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>

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);
}
});

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 :)

Related

OnClick Listener is not triggered inside a fragment with View Pager

I'd like to be able to set a View.OnclickListener to an ImageView inside a page (fragment) of my ViewPager, I don't want to click on the whole page of my ViewPager, I just want to be able to click on a specifig ImageView. I followed these questions (onClick not triggered on LinearLayout with child, How to set OnClickListener in ViewPager
) but they did not help me. This is my actual code:
MyPageFragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:focusableInTouchMode="true"
android:id="#+id/container_linear"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop" />
</LinearLayout>
</LinearLayout>
MyActivity.xml
<android.support.v4.view.ViewPager
android:id="#+id/product_view_pager"
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:gravity="center"
android:overScrollMode="never" />
MyFragment.class (where I'm binding the views using Butterknife)
#BindView(R.id.container_linear)
LinearLayout mContainer;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View mRootView = inflater.inflate(R.layout.my_layout, container, false);
ButterKnife.bind(this, mRootView);
mContainer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Test", "Clicked!");
}
});
}
Thanks for your support.
One thing to consider would be moving your OnClickListener to the parent LinearLayout you're setting as clickable and seeing if that helps.
Another thing is adding android:focusableInTouchMode="true" to the view you're attaching your OnClickListener to.
Other than that, you might want to share the code for registering the OnClickListener so we can investigate other possible errors.
If I understand correctly you want to click on the image view insideMyPageFragment.xml. This is simple but you need to give ID to the ImageView and refer it in your onCreateView method like you do for LinearLayout and then use onClickListener on that image.
So with recent conversation it seems that in linear layout you need to remove line clickable="false"
Basically that has blocked all the clicks on its children, thus that linear layout as well as the imageview clicks are not working for you. See this code I removed that line. Hope this works
Follow this code.
MyPageFragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:duplicateParentState="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:focusableInTouchMode="true"
android:id="#+id/container_linear"
android:gravity="center"
android:orientation="vertical">
<--you need to give ID to the view to be able to
perform events on them specifically.-->
<ImageView
android:id="#+id/my_awesome_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop" />
</LinearLayout>
Now in your MyFragment.class
#BindView(R.id.container_linear)
LinearLayout mContainer;
//declare with butterknife
#BindView(R.id.my_awesome_image)
ImageView myAwesomeImage;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View mRootView = inflater.inflate(R.layout.my_layout, container, false);
ButterKnife.bind(this, mRootView);
//you set click listener previously on entire linear layout instead of imageview
//that's why it was not reflecting on imageview click.
myAwesomeImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG , "Awesome Imageview clicked!");
}
});
}
I solved this issue using the following chunk of code:
#Override
public void onViewCreated(View view, Bundle savedInstanceState){
view.setOnClickListener(new OnClickListener(){
public void onClick(View v){
/* Do as you please here. */
}
});
}
The reference for this answer can be viewed here: How to detect click on ImageView in ViewPager's PagerAdapter (android)?

Fragment is not fully covering parent activity

There is Button in Activity when we click on Button fragment's view overlaps Activity's view .
When Button is clicked it is forwarded to Fragment. Problem is it overlaps with Button.
In MainActivity i have created Button and set listener on that when user click on Button it is forwarded to fragment file which contain only textview.
MainActivity.java:
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
fragment fr = new fragment();
getSupportFragmentManager().beginTransaction().add(R.id.parent_frame, fr).commit();
}
});
}
}
fragment.java
public class fragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
return view;
}
}
fragment.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/detailsText"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text="Default Text ggggggg"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30dip" />
</RelativeLayout>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press to update"
/>
<fragment
class="com.example.demo3.fragment"
android:id="#+id/parent_frame"
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
Why aren't you using the Activity merely as a container, and use Fragments for ALL ui elements, including the button you are clicking to change between them?
Something like this: Programatically switching between Fragments
this is unique thought and i appreciate it.if you want to do this just follow below steps:
1) in your activity main take framelayout with height width system fit(matchparent) and id is parent_frame
2) take button inside it and when you click on it set button visible to gone
3) then you can add fragment to this framlayout
Make sure in your activity_main must contain framlayout with matchparent

GraphView in Fragment

I want to display a graph in a fragment, but I didn't found many information about how to use GraphView in general. Can someone please help me to display a simple graph?
I started with this: http://android-graphview.org/#doc_createsimplegraph but don't get it working in a fragment.
What I did was to create a fragment_graph.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="250dip"
android:id="#+id/graph1"
android:background="#102f65" />
</LinearLayout>
and add the relevant code in the Fragment class:
public class GraphFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_graph, container, false);
// add the data here if necessary
// ...
LinearLayout layout = (LinearLayout) view.findViewById(R.id.graph1);
layout.addView(graphView);
return view;
}
// ...
}
Then you can use the fragment "as is".
Most credit actually goes to vogella.
define a empty LinearLayout with fixed width and height in your layout.xml file and then add the graphview view to it.
Layout
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="250dip"
android:id="#+id/graph1" />
Java
LinearLayout layout = (LinearLayout) findViewById(R.id.graph1);
layout.addView(graphView);
Just for the record:
You cannot use it in connection with ScrollViews. If a ScrollView is wrapping your LinearLayout the lib doesn't show anything.

Android - ScrollView doesnt work on Relative Layout

I put a scrollview around a Relative Layout that I am adding to dynamically. If I set the height of the Relative Layout to a fixed amount that goes off of the page the scrollview will work.
Example:
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<RelativeLayout
android:id="#+id/llcustomrow"
android:layout_width="match_parent"
android:layout_height="800dp" >
</RelativeLayout>
</ScrollView>
But I need the RelativeLayout to be able to hold however many items I add to it dynamically o I set The relativelayout height to wrap_content. But once I add enough items to the relative layout so that they are going off the screen the Scrollview doesnt register
Below is how I am dynamically adding to the relative layout
LinearLayout mLinearLayout;
RelativeLayout rlcopy;
RelativeLayout[] rArray = new RelativeLayout[20];
int counter = 0;
RelativeLayout llcustomrow;
RelativeLayout.LayoutParams paramsleft;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mLinearLayout = (LinearLayout) inflater.inflate(
R.layout.customworkout, container, false);
paramsleft = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
llcustomrow = (RelativeLayout)mLinearLayout.findViewById(R.id.llcustomrow);
for(int i = 0;i<=rArray.length-1;i++){
rArray[i] = (RelativeLayout)View.inflate(getActivity(), R.layout.addworkoutlayout, null);
paramsleft.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
paramsleft.setMargins(10, 0, 0, 0);
rArray[i].setLayoutParams(paramsleft);
}
Button bAdd = (Button) mLinearLayout.findViewById(R.id.bAddExcercise);
bAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
rArray[counter].setY(rArray[counter-1].getY() + (rArray[counter-1].getHeight() +25));
llcustomrow.addView(rArray[counter]);
counter++;
}
});
return mLinearLayout;
}
Thanks
Why are you using RelativeLayout? I'd advise switching to LinearLayout.
TRY THIS it will work for me
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/outer_ll"
android:layout_width="match_parent"
android:layout_height="900dp"
android:background="#E7D687"
>
</RelativeLayout>
</ScrollView>
change the layout_height of your RelativeLayout to match_parent.
it will solve the problem.

How to use the xml setting in a view of a activity?

I want to show two views in one activity. If I clicked on button in the first view I want to see the second and other way round.
The views should not have the same size as the screen so I want e.g. to center it, like you see in first.xml.
But if I add the views with
addContentView(mFirstView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
the views are not centered. They are shown at top left.
How can I use the xml settings to e.g. center it?
first.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:background="#drawable/background"
android:layout_gravity="center"
android:minWidth="100dp"
android:minHeight="100dp"
android:paddingBottom="5dp"
>
<LinearLayout android:id="#+id/head"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton android:id="#+id/first_button"
android:src="#drawable/show_second"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null" />
</LinearLayout>
second.xml same as first.xml but with
<ImageButton android:id="#+id/second_button"
android:src="#drawable/show_first"
... />
ShowMe.java
public class ShowMe extends Activity {
View mFirstView = null;
View mSecondView = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initFirstLayout();
initSecondLayout();
showFirst();
}
private void initFirstLayout() {
LayoutInflater inflater = getLayoutInflater();
mFirstView = inflater.inflate(R.layout.first, null);
getWindow().addContentView(mFirstView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
ImageButton firstButton = (ImageButton)mMaxiView.findViewById(R.id.first_button);
firstButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ShowMe.this.showSecond();
}
});
}
private void initSecondLayout() {
// like initMaxiLayout()
}
private void showFirst() {
mSecondView.setVisibility(View.INVISIBLE);
mFirstView.setVisibility(View.VISIBLE);
}
private void showSecond() {
mFirstView.setVisibility(View.INVISIBLE);
mSecondView.setVisibility(View.VISIBLE);
}}
Hope someone can help.
Thanks
Why don't you use setContentView(R.layout.yourlayout)? I believe the new LayoutParams you're passing in addContentView() are overriding those you defined in xml.
Moreover, ViewGroup.LayoutParams lacks the layout gravity setting, so you would have to use the right one for the layout you're going to add the view to (I suspect it's a FrameLayout, you can check with Hierarchy Viewer). This is also a general rule to follow. When using methods that take layout resources as arguments this is automatic (they might ask for the intended parent).
With this consideration in mind, you could set your layout params with:
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(/* wrap wrap */);
lp.setGravity(Gravity.CENTER);
addContentView(mYourView, lp);
But I would recommend setContentView() if you have no particular needs.
EDIT
I mean that you create a layout like:
~~~/res/layout/main.xml~~~
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="....."
android:id="#+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
then in your onCreate() or init...Layout():
setContentView(R.layout.main);
FrameLayout mainLayout = (FrameLayout)findViewById(R.id.mainLayout);
// this version of inflate() will automatically attach the view to the
// specified viewgroup.
mFirstView = inflater.inflate(R.layout.first, mainLayout, true);
this will keep the layout params from xml, because it knows what kind it needs. See reference.

Categories

Resources