BABU K
BABU K

Reputation: 917

onClick event of fragmentActivity's view inside of Fragment class?

I have included a common layout inside of FragmentActivity.I want to fire onclick event of common layout ToggleButton, inside of fragment class.How can I achieve this?

ToggleButton tbMainLocation = (ToggleButton) getView().findViewById(
                R.id.tbMainLocation);

but tbMainLocation always return nullpointerexception

commonlayout:

layout_include.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="wrap_content" >

    <RelativeLayout
        android:id="@+id/rlMainLivTopBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/cyan"
        android:paddingBottom="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="10dp" >

        <TextView
            android:id="@+id/txtMainBack"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:text="&lt;"
            android:textColor="@color/white"
            android:textSize="30sp"
            android:visibility="gone" />

        <ImageView
            android:id="@+id/imgMainBack"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:background="@drawable/back"
            android:visibility="gone" />

        <AutoCompleteTextView
            android:id="@+id/actv_edt_search_tag_feed"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_marginLeft="5dp"
            android:background="@drawable/round_corner_edittext"
            android:ems="10"
            android:hint="Search by tag"
            android:singleLine="true"
            android:text=""
            android:textSize="15dp"
            android:visibility="gone" >

            <requestFocus />
        </AutoCompleteTextView>

        <TextView
            android:id="@+id/txtMainTagneme"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_centerVertical="true"
            android:text=""
            android:textColor="@color/white"
            android:visibility="gone" />

        <TextView
            android:id="@+id/txtMainExpertName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_centerVertical="true"
            android:text=""
            android:textColor="@color/white"
            android:visibility="gone" />

        <!--
         <ImageView
        android:id="@+id/img_notification"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        androImid:background="@drawable/notification" />
        -->

        <ImageView
            android:id="@+id/imgMainExpertEarnByFlight"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_centerVertical="true"
            android:layout_marginRight="5dp"
            android:background="@drawable/plane_normal"
            android:visibility="gone" />

        <!--
             <ImageView
            android:id="@+id/imgMainLocation"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginRight="5dp"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/imgMainSearch"
            android:background="@drawable/location"
            android:visibility="visible" />
        -->

        <ToggleButton
            android:id="@+id/tbMainLocation"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_centerVertical="true"
            android:layout_marginRight="5dp"
            android:background="@drawable/location_selector"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:textOff=""
            android:textOn="" />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/llExpertfragmentChooseDistance"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rl_liv_top_bar"
        android:background="@color/light_cyan"
        android:orientation="horizontal"
        android:paddingBottom="10dp"
        android:paddingTop="10dp"
        android:visibility="gone" >

        <TextView
            android:id="@+id/txt_choose_distance"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center"
            android:layout_marginRight="2dp"
            android:maxWidth="60dp"
            android:paddingLeft="10dp"
            android:text="@string/choose_distance"
            android:textSize="10dp" />

        <ImageView
            android:id="@+id/img_expert_earn_by_walk"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/walk_normal" />

        <TextView
            android:id="@+id/txt_cycle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center"
            android:layout_marginLeft="-2dp"
            android:layout_marginRight="-2dp"
            android:background="@color/cyan_line"
            android:maxHeight="6dp"
            android:minWidth="40dp" />

        <ImageView
            android:id="@+id/img_expert_earn_by_cycle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/cycle_normal" />

        <TextView
            android:id="@+id/txt_bike"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center"
            android:layout_marginLeft="-2dp"
            android:layout_marginRight="-2dp"
            android:background="@color/cyan_line"
            android:maxHeight="6dp"
            android:minWidth="40dp" />

        <ImageView
            android:id="@+id/img_expert_earn_by_bike"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/car_normal" />

        <TextView
            android:id="@+id/txt_train"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center"
            android:layout_marginLeft="-2dp"
            android:layout_marginRight="-2dp"
            android:background="@color/cyan_line"
            android:maxHeight="6dp"
            android:minWidth="40dp" />

        <ImageView
            android:id="@+id/img_expert_earn_by_flight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/plane_normal" />
    </LinearLayout>

</RelativeLayout>

fragmentactivity.xml

<com.liv.slidingmenu.layout.MainLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- This holds our menu -->

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ll_top_menu_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#6d6d6d"
        android:orientation="vertical" >

        <!-- This acts as Actionbar -->

        <LinearLayout
            android:id="@+id/ll_prof_img"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="#434242" >

            <RelativeLayout
                android:id="@+id/rr_img_detail"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <ImageView
                    android:id="@+id/img_prof"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_centerVertical="true"
                    android:layout_gravity="center"
                    android:layout_marginLeft="25dp"
                    android:layout_marginTop="20dp"
                    android:background="@drawable/profile_image" />

                <TextView
                    android:id="@+id/txt_expt_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/img_leve"
                    android:layout_marginLeft="35dp"
                    android:layout_marginTop="35dp"
                    android:layout_toRightOf="@+id/img_prof"
                    android:text=""
                    android:textColor="#ffffff" />

                <TextView
                    android:id="@+id/txt_level"
                    android:layout_width="70dp"
                    android:layout_height="30dp"
                    android:layout_below="@+id/txt_expt_name"
                    android:layout_marginLeft="35dp"
                    android:layout_marginTop="10dp"
                    android:layout_toRightOf="@+id/img_prof"
                    android:background="@drawable/level_background"
                    android:gravity="center_horizontal"
                    android:paddingTop="5dp"
                    android:text=""
                    android:textColor="#d2d2d2" />
            </RelativeLayout>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll_prof_item"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.6" >

            <ListView
                android:id="@+id/listview_menu_item"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:background="#292929"
                android:dividerHeight="0.1dip" >
            </ListView>
        </LinearLayout>
    </LinearLayout>

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

        <RelativeLayout
            android:id="@+id/ll_topbar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <include
                android:id="@+id/topbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                layout="@layout/layout_include" />
        </RelativeLayout>

        <TabHost
            android:id="@android:id/tabhost"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentBottom="true"
            android:layout_below="@+id/ll_topbar" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_marginBottom="-3dp"
                android:orientation="vertical" >

                <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_weight="0" />

                <FrameLayout
                    android:id="@+android:id/realtabcontents"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1" />
                <!-- android:id="@android:id/tabs" -->

                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:background="#f9f9f9"
                    android:orientation="horizontal" />
            </LinearLayout>
        </TabHost>
    </RelativeLayout>

</com.liv.slidingmenu.layout.MainLayout>

Upvotes: 2

Views: 690

Answers (1)

Raghunandan
Raghunandan

Reputation: 133560

To access views that belong to the activity layout you can use

 getActivity().findViewById(R.id.viewid);

Make sure the fragment is attached to the activity and getActivity() does not return null.

Quoting docs

The fragment can access the Activity instance with getActivity() and easily perform tasks such as find a view in the activity layout

http://developer.android.com/guide/components/fragments.html

Upvotes: 2

Related Questions