Practical Problem_Fragment_Android
Practical Problem_Fragment_Android
1. Project Structure
2. MainActivity
java
Copy code
package com.example.notesapp;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@Override
public void onNoteSelected(String note) {
// Replace NoteListFragment with NoteDetailFragment and pass selected
note
NoteDetailFragment detailFragment =
NoteDetailFragment.newInstance(note);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fragment_container, detailFragment);
transaction.addToBackStack(null); // Add to back stack to handle back
navigation
transaction.commit();
}
}
3. NoteListFragment
java
Copy code
package com.example.notesapp;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.fragment.app.Fragment;
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof NoteSelectedListener) {
listener = (NoteSelectedListener) context;
} else {
throw new ClassCastException(context.toString() + " must implement
NoteSelectedListener");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_note_list, container,
false);
ListView listView = view.findViewById(R.id.note_list);
String[] notes = {"Note 1: Meeting at 3 PM", "Note 2: Buy groceries",
"Note 3: Call John"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(),
android.R.layout.simple_list_item_1, notes);
listView.setAdapter(adapter);
return view;
}
}
xml
Copy code
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/note_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
4. NoteDetailFragment
java
Copy code
package com.example.notesapp;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_note_detail, container,
false);
return view;
}
}
xml
Copy code
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/note_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:padding="16dp" />
</LinearLayout>