XML files
Here is the activity_main.xml file. that contains two fragments fragment_list.xml and fragment_deatails.xml
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView"
android:name="com.udemy.fragmentsapp.ListFrag"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/fragment_list" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fram2"
android:name="com.udemy.fragmentsapp.DetailFrag"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
tools:layout="@layout/fragment_detail" />
Here is the fragment_list.xml file. that contain ListView
<ListView
android:id="@+id/lvList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Here is the fragment_details.xml file. that contains one TextView
<TextView
android:id="@+id/tvDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textview"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:textSize="18dp"
android:textStyle="bold"
android:textColor="@color/black"/>
Java Files
Here is the ListFrag.java file.
ItemSelected activity;
public interface ItemSelected
{
void onItemSelected(int index);
}
public ListFrag() {
// Required empty public constructor
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
activity = (ItemSelected) context;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ArrayList<String> data = new ArrayList<>();
for (int i = 1; i < 6; i++) {
data.add(i + ". This is item " + i);
}
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,data));
}
@Override
public void onListItemClick(@NonNull ListView l, @NonNull View v, int position, long id) {
super.onListItemClick(l, v, position, id);
activity.onItemSelected(position);
}
Here is the DeatailFrag.java file.
public DetailFrag(){
super(R.layout.fragment_detail);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_detail, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
}
Here is the MainActivity.java file. when I want to access the ID "tvDescription" then it's showing ERROR: @layout/activity_main does not contain a declaration with id tvDescription.
Please help me. How can I access the ID? Tell me which one is the way to find IDs.
TextView tvDescription;
ArrayList<String> description;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvDescription = findViewById(R.id.tvDescription);
for (int i = 1; i < 6; i++) {
description.add("Description for item " + i);
}
}
@Override
public void onItemSelected(int index) {
tvDescription.setText(description.get(index));
}
The problem is that your activity and fragment are different classes. And although it is obvious that they represent a single view tree, in fact, in each component class, you should refer to the elements of only its layout.
Thus you need to use
Directly in the DeatailFrag.onViewCreated after super.onViewCreated
And then, somehow pass the received value back to the activity, if necessary
Happy learning