2

How to access an attribute in the activity xml? I want to use it in fragment.

for example : this is my activity xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    xmlns:fab="http://schemas.android.com/apk/res-auto">

<androidx.constraintlayout.widget.ConstraintLayout
    fab:layout_constraintTop_toTopOf="parent"
    fab:layout_constraintLeft_toLeftOf="parent"
    fab:layout_constraintRight_toRightOf="parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/layTitle"
    android:layoutDirection="rtl"
    android:background="@color/colorPrimary"
    android:padding="@dimen/paddingTitle"
    android:elevation="@dimen/elevationActivityTitle"
    android:layout_alignParentTop="true">
    <ImageView
        fab:layout_constraintLeft_toLeftOf="parent"
        fab:layout_constraintTop_toTopOf="parent"
        fab:layout_constraintBottom_toBottomOf="parent"
        android:layout_width="@dimen/imgActivityTitle"
        android:layout_height="@dimen/imgActivityTitle"
        android:id="@+id/btn_back"
        android:src="@drawable/ic_back"
        android:elevation="5dp"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"/>
    

</androidx.constraintlayout.widget.ConstraintLayout>
<FrameLayout
    android:id="@+id/frag"
    android:layout_width="0dp"
    android:layout_height="0dp"
    fab:layout_constraintBottom_toBottomOf="parent"
    fab:layout_constraintTop_toBottomOf="@+id/layTitle"
    fab:layout_constraintLeft_toLeftOf="parent"
    fab:layout_constraintRight_toRightOf="parent"
    />
</androidx.constraintlayout.widget.ConstraintLayout>

I want findViewByid ImageView in fragment When I was searching for solution I got the following one:

TextView btn_back= (TextView) getView().findViewById(R.id.btn_back)

But, unfortunately it was not working for me.

Pouria Hemi
  • 706
  • 5
  • 15
  • 30

5 Answers5

1

Use either viewModel or interfaces to talk between activity and fragment. don't try to send the button to fragment but trigger it from there. That will prevent any leaks or null pointers.

Arutha
  • 494
  • 5
  • 14
1

Ok. You have some options.

Firstly, you can access these variables to directly in fragment like that.

MainActivity.java

TextView backButton;

backButton = findViewById(R.id.btn_back);

Fragment.java

((MainActivity) getActivity()).backButton

You can get current activity with getActivity() but Java compiler cannot understand which activity so have to cast to MainActivity.

Secondly, you can pass your activity instance to fragment. You can use constructors and newInstance() function. You can read that post Best practice for instantiating a new Android Fragment

In my opinion, I prefer second option because you guarantee these variables that include activity class. Some devices occur errors at first option. But second option uses more ram because you are passing your variables to another class.

The choice is yours

Süleyman Bilgin
  • 172
  • 1
  • 11
1

You can use the interface to do it :

Create an interface names IBtnClick with one void method: "void onBtnClicked();"

use interface in activity like this:

IBtnClick iBtnClick= (IBtnClick)getActivity();

here getAtivity is our context, if it doesn't work use another scope context.

and use your method in button click listener like this

iBtnClick.onBtnClicked();

Now in your fragment you have to implements interface and

you can add any action you want in Overrided method.

Hamed Karami
  • 405
  • 1
  • 6
  • 28
0

Try This code

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.recharge_plane_fragment, container, false);

  
    alertdialog_Listview=view.findViewById(R.id.alertdialog_Listview);

  

    return view;
}
Sandeep Pareek
  • 1,636
  • 19
  • 21
0

You can declare your TextView btn_back as static in activity

public static TextView btn_back;
btn_back = (TextView) findViewById(R.id.btn_back);

and get it in fragment

TextView btn_back = ((MainActivity)getActivity()).btn_back;
  • 1
    Making views static causes memory leaks. Also, what is the point of using a fragment if it only works in 1 specific Activity? – Elletlar Nov 11 '20 at 12:05