2

I have image-buttons in one of the fragments of an activity and want to open corresponding fragments containing webViews on button clicks in another activity. I am a beginner so please do give appropriate codes for this.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="com.example.administrator.hiha.MainActivity"
    >
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <include
        android:id="@+id/toolbar"
        layout="@layout/tool_bar">
    </include>


        <fragment
            android:id="@+id/upperFragment"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:name="com.example.administrator.hiha.Upper_main_Fragment"
            android:layout_below="@id/toolbar"
            >
        </fragment>


        <fragment
            android:layout_below="@id/upperFragment"
            android:id="@+id/lowerFragment"
            android:layout_width="match_parent"
            android:layout_height="140dp"
            android:name="com.example.administrator.hiha.Lower_main_Fragment"
            >
        </fragment>

    <fragment
        android:layout_below="@id/lowerFragment"
        android:id="@+id/Bottom_Most_Fragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.example.administrator.hiha.BottomMostFragment"
        tools:layout="@layout/fragment_bottom_most">
    </fragment>

    <View
        android:id="@+id/hLastRow"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/Bottom_Most_Fragment"
        android:layout_width="300dp"
        android:layout_height="2dp"
        android:background="#EEEEEE"
        />

<TextView

    android:layout_alignParentBottom="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/hLastRow"
    android:layout_centerHorizontal="true"
    android:text="Designed \u0026 Developed by me © 2016."/>

</RelativeLayout>
</ScrollView>

MainActivity.java

package com.example.administrator.hiha;

import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends FragmentActivity {

//Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //toolbar=(Toolbar)findViewById(R.id.toolbar);

    }
}

fragment_lower_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.hiha.Lower_main_Fragment">


    <HorizontalScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            >
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

            <ImageButton
            android:layout_width="100dp"
            android:layout_height="80dp"
            android:src="@drawable/about_board"
            android:id="@+id/about_img_btn"
            android:onClick="onAboutBoardClick"
            />
            <TextView
                android:id="@+id/text_about_img_btn"
                android:text="About Board"
                android:layout_below="@id/about_img_btn"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:gravity="center"
                />
            <ImageButton
                android:layout_width="100dp"
                android:layout_height="80dp"
                android:src="@drawable/sarasvati"
                android:id="@+id/sarasvati_img_btn"
                android:layout_toRightOf="@id/about_img_btn"
                android:onClick="onSarasvatiClick"
                />
                <TextView
                    android:layout_toRightOf="@id/text_about_img_btn"
                    android:id="@+id/text_sarasvati_img_btn"
                    android:text="Sarasvati"
                    android:layout_below="@id/sarasvati_img_btn"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    />

            <ImageButton
                android:layout_toRightOf="@id/sarasvati_img_btn"
                android:layout_width="100dp"
                android:layout_height="80dp"
                android:src="@drawable/scientific_evidences"
                android:id="@+id/scientific_evidences_img_btn"
                android:onClick="onScientificEvidencesClick"
                />
                <TextView
                    android:layout_toRightOf="@id/text_sarasvati_img_btn"
                    android:id="@+id/text_scientific_evidences_img_btn"
                    android:text="Scientific EVidences"
                    android:layout_below="@id/scientific_evidences_img_btn"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    />

            <ImageButton
                android:layout_toRightOf="@id/digital_library_img_btn"
                android:layout_width="100dp"
                android:layout_height="80dp"
                android:src="@drawable/affltd_organization"
                android:id="@+id/affltd_oragnization_img_btn"
                android:onClick="onAffltdOrganizationClick"
                />
                <TextView
                    android:layout_toRightOf="@id/text_digital_library_img_btn"
                    android:id="@+id/text_affltd_oragnization_img_btn"
                    android:text="Affiliated Organizations"
                    android:layout_below="@id/affltd_oragnization_img_btn"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    />


            <ImageButton
                android:layout_toRightOf="@id/affltd_oragnization_img_btn"
                android:layout_width="100dp"
                android:layout_height="80dp"
                android:src="@drawable/tender"
                android:id="@+id/tender_img_btn"
                android:onClick="onTenderClick"
                />
                <TextView
                    android:layout_toRightOf="@id/text_affltd_oragnization_img_btn"
                    android:id="@+id/text_tender_img_btn"
                    android:text="Tenders"
                    android:layout_below="@id/tender_img_btn"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    />

            </RelativeLayout>
        </LinearLayout>

    </HorizontalScrollView>

</FrameLayout>

Lower_Main_Fragment.java

package com.example.administrator.hiha;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

public class Lower_main_Fragment extends Fragment {

    ImageButton about_img_btn;
    ImageButton sarasvati_img_btn;
    ImageButton scientific_evidences_img_btn;
    ImageButton digital_library_img_btn;
    ImageButton affltd_oragnization_img_btn;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView=inflater.inflate(R.layout.fragment_lower_main_,container,false);

        //cast image buttons
        about_img_btn=(ImageButton)getView().findViewById(R.id.about_img_btn);
        sarasvati_img_btn=(ImageButton)getView().findViewById(R.id.sarasvati_img_btn);
        scientific_evidences_img_btn=(ImageButton)getView().findViewById(R.id.scientific_evidences_img_btn);
        digital_library_img_btn=(ImageButton)getView().findViewById(R.id.digital_library_img_btn);
        affltd_oragnization_img_btn=(ImageButton)getView().findViewById(R.id.affltd_oragnization_img_btn);

        return rootView;

    }

    //onclick methods of image buttons
    public void onAboutBoardClick(View view){

    }
}

These are the files in which i want that if i click on about_img_btn then it should open a fragment containing a webView in another activity. The same should be repeated for all the buttons. Please define the further code that i have to use and mention the files where i have to use it. Thanks!

Guneet Kaur
  • 544
  • 1
  • 8
  • 26

1 Answers1

1

try this:

in your fragment_lower_main xml:

Give an id to the Framelayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.hiha.Lower_main_Fragment">
    <FrameLayout
         android:id="+@id/fragment_container"
         android:layout_width="match_parent"
         android:layout_height="match_parent">

         <--Rest of the code -->
    />
</LinearLayout>

Now in code open new Fragment during button click using:

  about_img_btn=(ImageButton)rootView.findViewById(R.id.about_img_btn);

 about_img_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AboutUS fragment = new AboutUS();
                Replace_fragment(fragment); //pass the fragment u want to replace
            }
        });


 public void ReplaceFragment(Fragment r_fragment) {
    FragmentManager fragmentManager = getActivity.getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.fragment_container, r_fragment).commit();
}
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62