Imagebutton inside a fragment clicked to open a new fragment

Course Queries Syllabus Queries 3 years ago

4.91K 2 0 0 0

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 years ago


I am creating an application which consists of a viewpager with different fragments.And these fragments contains different imagebuttons.I want to open fragments when each of these image button is clicked.But i don't know how to do it.I am new to fragments and i don't know much about them.I am trying to make this code since 2 days.Please if you can help me out.So i will be able to complete my project app.
My layout for NoticeBoard.java is as below: This contains2 imagebuttons and a listview.

 xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
     xmlns : android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="match_parent" 
        android:id="@+id/relative">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:background="#AAAAAA" 

            android:id="@+id/line">

            <ImageButton
                android:id="@+id/ptu"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dp"
                android:background="#CCCCCC"
                android:clickable="true"
               android:onClick="OnClick"
                android:contentDescription="@string/mko"
                android:paddingBottom="10dp"
                android:paddingLeft="9.9dp"
                android:paddingRight="20dp"
                android:paddingTop="10dp"
                android:src="@drawable/img" />



            <ImageButton
                android:id="@+id/ku"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="1dp"
                android:background="#CCCCCC"
                android:clickable="true"
                android:contentDescription="@string/mn"
                android:paddingBottom="10dp"
                android:paddingLeft="15dp"
                android:paddingRight="15dp"
                android:paddingTop="10dp"
                android:src="@drawable/image" />

        LinearLayout>

        <ListView
            android:id="@+id/nb"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:layout_above
                                                
0 views
0 shares

profilepic.png
manpreet 3 years ago


You have a Main fragment manager Activity name fragment activity. this is the main fragment activity manager. you have to set all fragment using by this activity.

i1 = (ImageButton ) rootView
                            .findViewById(R.id.ptu);
                                 i2 = (ImageButton ) rootView
                            .findViewById(R.id.cu);
                    i3 = (ImageButton ) rootView
                            .findViewById(R.id.ku);
                    i4 = (ImageButton ) rootView
                            .findViewById(R.id.hptu);

                    i1.setOnClickListener(new OnClickListener(){

    public void onClick(View v) {

                          fragmentActiivty.setFragment();
}

Notes: :SetFragment function is made in fragment-activity class

public static void set_view_FAQ() {
        FragmentManager fm = act.getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        Fragment fragment = new yourFragnmentName();
        ft.add(R.id.activity_main_content_fragment, fragment);
        ft.commit();

    }

Note: new yourFragmentNAme means yous fragment class likes Ku,ptu,etc


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Similar Forum