-
Fragment transfers data to activity -
Activity transfers data to Fragment -
Communication mode between fragments
I Fragment transfers data to activity
//Define Interface public interface DataTransferInterface { void onDataReceived(String data); } //Fragment Implementation public class MyFragment extends Fragment { private DataTransferInterface dataTransferInterface; @Override public void onAttach(@NonNull Context context) { super.onAttach(context); if (context instanceof DataTransferInterface) { dataTransferInterface = (DataTransferInterface) context; } else { throw new RuntimeException("Activity must implement DataTransferInterface"); } } public void sendData(String data) { dataTransferInterface.onDataReceived(data); } } //Activity Implementation Interface public class MainActivity extends AppCompatActivity implements DataTransferInterface { @Override public void onDataReceived(String data) { //Processing data transferred from Fragment } }
public class SharedViewModel extends ViewModel { private MutableLiveData<String> mMessage = new MutableLiveData<>(); public LiveData<String> getMessage() { return mMessage; } public void setMessage(String message) { mMessage.setValue(message); } }
fragment_share_data.xml
<? xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android " android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <EditText android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter some text" /> <Button android:id="@+id/button_share" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Share Data" /> </LinearLayout> ShareDataFragment.java
public class ShareDataFragment extends Fragment { private SharedViewModel viewModel; private EditText editText; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_share_data, container, false); } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); viewModel = new ViewModelProvider(requireActivity()).get(SharedViewModel.class); editText = view.findViewById(R.id.edit_text); view.findViewById(R.id.button_share).setOnClickListener(v -> { viewModel.setMessage(editText.getText().toString()); }); } }
activity_main.xml
<? xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android " android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" /> </LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity { private TextView textView; private SharedViewModel viewModel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.text_view); viewModel = new ViewModelProvider(this).get(SharedViewModel.class); viewModel.getMessage().observe(this, message -> { textView.setText(message); }); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .replace(R.id.fragment_container, new ShareDataFragment()) .commit(); } } }
dependencies { implementation 'org.greenrobot:eventbus:3.3.1' }
public class MessageEvent { public final String message; public MessageEvent(String message) { this.message = message; } }
fragment_share_data.xml
<? xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android " android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <EditText android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter some text" /> <Button android:id="@+id/button_share" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Share Data" /> </LinearLayout>
ShareDataFragment.java
public class ShareDataFragment extends Fragment { private EditText editText; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_share_data, container, false); } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); editText = view.findViewById(R.id.edit_text); view.findViewById(R.id.button_share).setOnClickListener(v -> { String message = editText.getText().toString(); EventBus.getDefault().post(new MessageEvent(message)); }); } @Override public void onStart() { super.onStart(); EventBus.getDefault().register(this); } @Override public void onStop() { super.onStop(); EventBus.getDefault().unregister(this); } }
activity_main.xml
<? xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android " android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" /> </LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.text_view); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .replace(R.id.fragment_container, new ShareDataFragment()) .commit(); } } @Override protected void onStart() { super.onStart(); EventBus.getDefault().register(this); } @Override protected void onStop() { super.onStop(); EventBus.getDefault().unregister(this); } @Subscribe(threadMode = ThreadMode.MAIN) public void onMessageEvent(MessageEvent event) { textView.setText(event.message); } }
II Activity transfers data to Fragment
//Create Bundle and add data Bundle bundle = new Bundle(); bundle.putString("key", "value"); //Instantiate Fragment and set arguments MyFragment fragment = new MyFragment(); fragment.setArguments(bundle); //Get data in Fragment String value = getArguments().getString("key");
import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Suppose this is the data we want to pass to Fragment String dataToSend = "Hello, Fragment! "; //Get FragmentManager instance FragmentManager fragmentManager = getSupportFragmentManager(); //Start Fragment Transaction FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); //Check whether the ContentFragment has been added. If not, add it if (fragmentManager.findFragmentByTag("ContentFragment") == null) { ContentFragment contentFragment = new ContentFragment(); //Pass data to Fragment through Bundle Bundle args = new Bundle(); args.putString("data", dataToSend); contentFragment.setArguments(args); //Add Fragment to Transaction fragmentTransaction.add(R.id.fragment_container, contentFragment, "ContentFragment"); } //Commit Transaction fragmentTransaction.commit(); } }
<FrameLayout xmlns:android=" http://schemas.android.com/apk/res/android " android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent"/>
import android.os.Bundle; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class ContentFragment extends Fragment { public ContentFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_content, container, false); //Get data from Bundle Bundle args = getArguments(); if (args != null) { String data = args.getString("data"); //Display the passed data on the fragment UI TextView textView = view.findViewById(R.id.textview_data); textView.setText(data); } return view; } }
<LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android " android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <TextView android:id="@+id/textview_data" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" /> <!-- Other layout contents --> </LinearLayout>
import androidx.lifecycle.ViewModel; public class SharedViewModel extends ViewModel { private String data; public void setData(String data) { this.data = data; } public String getData() { return data; } }
import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; import androidx.lifecycle.ViewModelProvider; import android.os.Bundle; public class MainActivity extends AppCompatActivity { private SharedViewModel sharedViewModel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sharedViewModel = new ViewModelProvider(this).get(SharedViewModel.class); //Suppose this is the data we want to pass to Fragment sharedViewModel.setData("Hello, Fragment! "); //Get FragmentManager instance FragmentManager fragmentManager = getSupportFragmentManager(); //Start Fragment Transaction FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); //Check whether the ContentFragment has been added. If not, add it if (fragmentManager.findFragmentByTag("ContentFragment") == null) { ContentFragment contentFragment = new ContentFragment(); fragmentTransaction.add(R.id.fragment_container, contentFragment, "ContentFragment"); } //Commit Transaction fragmentTransaction.commit(); } }
import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class ContentFragment extends Fragment { private SharedViewModel sharedViewModel; public ContentFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_content, container, false); sharedViewModel = new ViewModelProvider(this).get(SharedViewModel.class); //Display the data in ViewModel on the UI of Fragment TextView textView = view.findViewById(R.id.textview_data); textView.setText(sharedViewModel.getData()); return view; } }
<FrameLayout xmlns:android=" http://schemas.android.com/apk/res/android " android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent"/>
<LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android " android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <TextView android:id="@+id/textview_data" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" /> <!-- Other layout contents --> </LinearLayout>
III Communication between Fragments
import androidx.lifecycle.ViewModel; public class SharedViewModel extends ViewModel { private String data = ""; public void setData(String data) { this.data = data; } public String getData() { return data; } }
import android.os.Bundle; import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; import androidx.navigation.Navigation; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; public class FragmentA extends Fragment { private SharedViewModel sharedViewModel; public FragmentA() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_a, container, false); sharedViewModel = new ViewModelProvider(this).get(SharedViewModel.class); EditText editText = view.findViewById(R.id.editTextData); Button button = view.findViewById(R.id.buttonSendData); button.setOnClickListener(new View. OnClickListener() { @Override public void onClick(View v) { String data = editText.getText().toString(); sharedViewModel.setData(data); //You can navigate FragmentB here Navigation.findNavController(v).navigate(R.id.action_fragmentA_to_fragmentB); } }); return view; } }
<LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android " android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <EditText android:id="@+id/editTextData" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter data here"/> <Button android:id="@+id/buttonSendData" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send Data"/> </LinearLayout>
import android.os.Bundle; import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class FragmentB extends Fragment { private SharedViewModel sharedViewModel; public FragmentB() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_b, container, false); sharedViewModel = new ViewModelProvider(this).get(SharedViewModel.class); TextView textView = view.findViewById(R.id.textViewData); textView.setText(sharedViewModel.getData()); return view; } }
<LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android " android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <TextView android:id="@+id/textViewData" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" /> <!-- Other layout contents --> </LinearLayout>
dependencies { implementation 'org.greenrobot:eventbus:3.2.0' }
import org.greenrobot.eventbus.EventBus; public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); EventBus.builder().addIndex(OptInIndex.class).installDefaultEventBus(); } }
import org.greenrobot.eventbus.Subscribe; public class DataEvent { public final String data; public DataEvent(String data) { this.data = data; } }
import android.os.Bundle; import androidx.fragment.app.Fragment; import androidx.navigation.Navigation; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import org.greenrobot.eventbus.EventBus; public class FragmentA extends Fragment { private EventBus eventBus; public FragmentA() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_a, container, false); eventBus = EventBus.getDefault(); Button button = view.findViewById(R.id.buttonSendData); button.setOnClickListener(new View. OnClickListener() { @Override public void onClick(View v) { //Publish Events eventBus.post(new DataEvent("Data from FragmentA")); //Navigate to FragmentB, if necessary Navigation.findNavController(v).navigate(R.id.action_fragmentA_to_fragmentB); } }); return view; } @Override public void onStart() { super.onStart(); eventBus.register(this); } @Override public void onStop() public void onStop() { super.onStop(); eventBus.unregister(this); } }
import android.os.Bundle; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; public class FragmentB extends Fragment { private EventBus eventBus; private TextView textView; public FragmentB() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_b, container, false); textView = view.findViewById(R.id.textViewData); eventBus = EventBus.getDefault(); return view; } @Override public void onStart() { super.onStart(); eventBus.register(this); } @Override public void onStop() { super.onStop(); eventBus.unregister(this); } @Subscribe public void onEvent(DataEvent event) { //Update UI textView.setText(event.data); } }
<!-- fragment_a.xml --> <Button android:id="@+id/buttonSendData" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send Data"/> <!-- fragment_b.xml --> <TextView android:id="@+id/textViewData" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp"/>
import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; import android.os.Bundle; import android.view.View; import android.widget.EditText; public class MainActivity extends AppCompatActivity implements FragmentA.OnDataPassListener { public interface OnDataPassListener { void onDataPass(String data); } private String receivedData = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) { FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction.add(R.id.fragment_container, new FragmentA(), "FragmentA"); fragmentTransaction.commit(); } } public void receiveData(String data) { receivedData = data; updateFragmentB(); } private void updateFragmentB() { Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container); if (currentFragment instanceof FragmentB) { FragmentB fragmentB = (FragmentB) currentFragment; fragmentB.updateData(receivedData); } } }
import android.os.Bundle; import androidx.fragment.app.Fragment; import androidx.appcompat.widget.AppCompatButton; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; public class FragmentA extends Fragment { private OnDataPassListener onDataPassListener; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_a, container, false); final EditText editText = view.findViewById(R.id.editTextData); AppCompatButton button = view.findViewById(R.id.buttonSendData); button.setOnClickListener(new View. OnClickListener() { @Override public void onClick(View v) { String data = editText.getText().toString(); if (onDataPassListener != null) { onDataPassListener.onDataPass(data); } } }); return view; } @Override public void onAttach(Context context) { super.onAttach(context); if (context instanceof MainActivity. OnDataPassListener) { onDataPassListener = (MainActivity. OnDataPassListener) context; } else { throw new RuntimeException(context.toString() + " must implement OnDataPassListener"); } } @Override public void onDetach() { super.onDetach(); onDataPassListener = null; } }
import android.os.Bundle; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class FragmentB extends Fragment { private TextView textView; private MainActivity mainActivity; public FragmentB() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_b, container, false); textView = view.findViewById(R.id.textViewData); return view; } public void updateData(String data) { textView.setText(data); } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mainActivity = (MainActivity) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement MainActivity"); } } @Override public void onDetach() { super.onDetach(); mainActivity = null; } }
<FrameLayout xmlns:android=" http://schemas.android.com/apk/res/android " android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent"/>
<LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android " android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <EditText android:id="@+id/editTextData" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter data here"/> <Button android:id="@+id/buttonSendData" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send Data"/> </LinearLayout>
<LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android " android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <TextView android:id="@+id/textViewData" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" /> <!-- Other layout contents --> </LinearLayout>
import android.os.Bundle; import androidx.fragment.app.Fragment; import androidx.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class FragmentB extends Fragment { private OnFragmentBActionListener listener; //Define an interface for callback public interface OnFragmentBActionListener { void onFragmentBAction(String data); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); //Set callback interface listener.onFragmentBAction("Initial data"); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_b, container, false); TextView textView = view.findViewById(R.id.textViewData); textView.setText("Waiting for data from Fragment A"); return view; } //Set the implementer of the callback interface public void setOnFragmentBActionListener(OnFragmentBActionListener listener) { this.listener = listener; } }
import android.os.Bundle; import androidx.fragment.app.Fragment; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; public class FragmentA extends Fragment implements FragmentB.OnFragmentBActionListener { private EditText editText; private Button button; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_a, container, false); editText = view.findViewById(R.id.editTextData); button = view.findViewById(R.id.buttonSendData); button.setOnClickListener(new View. OnClickListener() { @Override public void onClick(View v) { String data = editText.getText().toString(); //Call the FragmentB method to pass data onFragmentBAction(data); } }); return view; } @Override public void onFragmentBAction(String data) { //Implement the callback method of FragmentB here //Since FragmentA implements the interface of FragmentB, this method can be called directly here //But we need to hold the reference of FragmentB to pass data } }
import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); FragmentA fragmentA = new FragmentA(); fragmentTransaction.add(android. R.id.content, fragmentA); fragmentTransaction.commit(); //Assume FragmentB has been instantiated and added to Activity FragmentB fragmentB = (FragmentB) fragmentManager.findFragmentById(R.id.fragment_b); if (fragmentB != null) { //Set the callback interface of FragmentB fragmentB.setOnFragmentBActionListener(fragmentA); } } }
<!-- fragment_a.xml --> <LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android " android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <EditText android:id="@+id/editTextData" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter data here"/> <Button android:id="@+id/buttonSendData" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send Data"/> </LinearLayout> <!-- fragment_b.xml --> <LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android " android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <TextView android:id="@+id/textViewData" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" /> <!-- Other layout contents --> </LinearLayout>