-2

This is representation of my app layout:

layout

Where should I create onClickListeners for included layouts? I tried inside the fragment but I could not get through with findViewById. So I tried from Main Activity but I'm not sure how to get to included layouts from there.

I also tried this inside the fragment:

public class MainMenu extends Fragment implements View.OnClickListener{

View button_call;

@Override
public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedinstanceState) {
    View myView = inflater.inflate(R.layout.fragment_main_menu, container, false);
    button_call = myView.findViewById(R.id.btn_call);
    button_call.setOnClickListener(this);
    return myView;
}

@Override
public void onClick(View v) {
    // implements your things
}


public MainMenu() {

}

}

But then Fragment seems to be empty

Fragment XML:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:id="@+id/mainmenu_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingRight="60dp">

<include android:id="@+id/btn_call"
    layout="@layout/call_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="30dp"
    android:paddingBottom="30dp"
    android:layout_marginTop="20dp"
    android:layout_marginBottom="5dp"/>

<include android:id="@+id/button2"
    layout="@layout/message_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="30dp"
    android:paddingBottom="30dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp" />

<include android:id="@+id/button3"
    layout="@layout/navigate_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="30dp"
    android:paddingBottom="30dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"/>

<include android:id="@+id/button4"
    layout="@layout/remind_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="30dp"
    android:paddingBottom="30dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"/>


</LinearLayout>

</ScrollView>
Charuක
  • 12,953
  • 5
  • 50
  • 88
Somachr
  • 464
  • 1
  • 8
  • 21

2 Answers2

2

Steps

  1. Create a xml file you want to inclue (eg. web.xml)
  2. use include tag in fragment's xml and connect your layout you need to include using layout="@layout/
  3. Initialize include tag inside fragment (when you do this make sure about the root tag of your web.xml)
  4. Once initializing include tag is done access views inside the layout that included (web.xml) using include tag

Example

Here in my fragment I have an include tag in it's XML. It connects to my web.xml and ..web.xml's parent tag/ root tag is a FrameLayout ..

Now see how I initialize my include tag..(If you do not use the right root tag to initialize include,it will crash with a null pointer)

I have an id called g_betta in my web.xml( XML layout that I included in my fragment) see how I access that view..

public class FragmentAbout extends Fragment  {

private RelativeLayout relativeLayoutConnectedInsideIncludeTag;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.my_fragment,container,false);


        FrameLayout view  =(FrameLayout) rootView.findViewById(R.id.include_tag);
        relativeLayoutConnectedInsideIncludeTag = (RelativeLayout) view.findViewById(R.id.g_betta);

        relativeLayoutConnectedInsideIncludeTag.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "Yes I Found you", Toast.LENGTH_SHORT).show();
            }
        });
        return rootView;
    }


}

my fragment xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:id="@+id/mainmenu_layout"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="vertical">

    <include
        android:id="@+id/include_tag"
        layout="@layout/web"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>


</ScrollView>

my include--> web.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/maroon"
    android:gravity="center_horizontal">


    <ScrollView

        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:id="@+id/g_betta"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="HELLO"
                android:textColor="#FFF"
                android:textSize="20dp" />

            <RelativeLayout
                android:id="@+id/lin"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center">

                <Button
                    android:id="@+id/button"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"

                    android:layout_alignParentBottom="true"
                    android:layout_marginBottom="16dp"
                    android:background="@color/colorPrimaryDark"
                    android:gravity="bottom"
                    android:paddingLeft="24dp"
                    android:paddingRight="24dp" />
            </RelativeLayout>

            <ImageView
                android:id="@+id/imageone"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:layout_below="@+id/lin"
                android:layout_margin="16dp"
                android:background="@drawable/girl"
                android:scaleType="fitXY" />

            <ImageView
                android:id="@+id/imagetwo"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:layout_below="@+id/imageone"
                android:layout_margin="16dp"
                android:background="@drawable/amanda"
                android:scaleType="fitXY" />


        </RelativeLayout>

    </ScrollView>

</FrameLayout>
Charuක
  • 12,953
  • 5
  • 50
  • 88
2

Do like this:

button_call = myView.findViewById(R.id.btn_call);
button_call.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                           // write your code here...
                    });
Arun kumar
  • 1,894
  • 3
  • 22
  • 28