how to update layout in a tabs in android Tabs with fragment , i have more than two tabs with different layout
I have first tab with xml layout as shown below (fragment_main.xml) :
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PosMainActivity$PlaceholderFragment">
<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="@+id/constraintLayout"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp"
tools:ignore="MissingConstraints">
<include
android:id="@+id/header_view_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/header_layout" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="110dp"
android:orientation="vertical">
<TextView
android:id="@+id/receipt_text_area"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/receipt_text_area"
android:textAlignment="center"
android:gravity="bottom"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="110dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:id="@+id/shortKeyButton1"
android:text=""
style="@style/EnableShortcutButton"/>
<Button
android:id="@+id/shortKeyButton2"
android:text=""
style="@style/EnableShortcutButton"/>
<Button
android:id="@+id/shortKeyButton3"
android:text=""
style="@style/EnableShortcutButton"/>
<Button
android:id="@+id/shortKeyButton4"
android:text=""
style="@style/EnableShortcutButton"/>
<Button
android:id="@+id/shortKeyButton5"
android:text=""
style="@style/EnableShortcutButton"/>
<Button
android:id="@+id/shortKeyButton6"
android:text=""
style="@style/EnableShortcutButton"/>
<Button
android:id="@+id/shortKeyButton7"
android:text=""
style="@style/EnableShortcutButton"/>
<Button
android:id="@+id/shortKeyButton8"
android:text=""
style="@style/EnableShortcutButton"/>
<Button
android:id="@+id/shortKeyButton9"
android:text=""
style="@style/EnableShortcutButton"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:id="@+id/shortKeyButton10"
android:text=""
style="@style/EnableShortcutButton"/>
<Button
android:id="@+id/shortKeyButton11"
android:text=""
style="@style/EnableShortcutButton"/>
<android.support.design.widget.TextInputLayout
android:layout_width="200dp"
android:layout_height="50dp"
android:background="@drawable/edit_box"
android:layout_marginRight="2dp">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/shortKeyButton12"
android:text=""
style="@style/EnableShortcutButton"/>
<Button
android:id="@+id/shortKeyButtonDot"
android:text="."
style="@style/EnableShortcutButton"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="220dp"
android:orientation="vertical"
android:background="@color/dynakeyBackGround">
<TextView
android:id="@+id/footerTextView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="@string/result_text"
android:textAlignment="center"
android:background="@drawable/result_box"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/dynakey1"
android:text=""
style="@style/LeftDisableDynakeyButton"/>
<Button
android:id="@+id/dynakey2"
android:text=""
style="@style/RightDisableDynakeyButton"/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<RelativeLayout
android:id="@+id/relativeLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/dynakey3"
android:text=""
style="@style/LeftDisableDynakeyButton"/>
<Button
android:id="@+id/dynakey4"
android:text=""
style="@style/RightDisableDynakeyButton"/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<RelativeLayout
android:id="@+id/relativeLayout3"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/dynakey5"
android:text=""
style="@style/LeftDisableDynakeyButton"/>
<Button
android:id="@+id/dynakey6"
android:text=""
style="@style/RightDisableDynakeyButton"/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<RelativeLayout
android:id="@+id/relativeLayout4"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/dynakey7"
android:text=""
style="@style/LeftDisableDynakeyButton"/>
<Button
android:id="@+id/dynakey8"
android:text=""
style="@style/RightDisableDynakeyButton"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:background="@color/dynakeyBackGround">
<TextView
android:id="@+id/txtPosNo"
android:text="@string/pos_no"
style="@style/MposDetailsView"/>
<TextView
android:id="@+id/txtSeqNo"
android:text="@string/seq_no"
style="@style/MposDetailsView"/>
<TextView
android:id="@+id/txtvosk"
android:text="@string/osk"
style="@style/MposDetailsView"/>
<TextView
android:id="@+id/txtLogical_date"
android:text="@string/logical_date"
style="@style/MposDetailsView"/>
<TextView
android:id="@+id/txtVersionNo"
android:text="@string/version_no"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:textStyle="bold"
android:textSize="20dp"
android:textAlignment="center"
android:background="#f48fb1"/>
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Another tab with layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PosMainActivity$PlaceholderFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/dynakeyBackGround">
<include
android:id="@+id/header_view_keyboard"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/header_layout" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:id="@+id/PrevMenuButton"
android:text="Previous Menu"
style="@style/LeftKeyboardButton"/>
<Button
android:id="@+id/MainMenuButton"
android:text="Main Menu"
style="@style/RightKeyboardButton"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:id="@+id/ClearButton"
android:text="Clear"
style="@style/LeftKeyboardButton"/>
<Button
android:id="@+id/FlightDetailsButton"
android:text="Flight Details"
style="@style/RightKeyboardButton"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:id="@+id/LagButton"
android:text="Lag"
style="@style/LeftKeyboardButton"/>
<Button
android:id="@+id/ChipButton"
android:text="Chip"
style="@style/RightKeyboardButton"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:id="@+id/RunApplicationButton"
android:text="Run Application"
style="@style/LeftKeyboardButton"/>
<Button
android:id="@+id/TotalButton"
android:text="Total"
style="@style/RightKeyboardButton"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:id="@+id/Backspace"
android:text="Backspace"
style="@style/LeftKeyboardButton"/>
<Button
android:id="@+id/SubTotalButton"
android:text="Sub Total"
style="@style/RightKeyboardButton"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:id="@+id/ShowDiscount"
android:text="Show Discount"
style="@style/LeftKeyboardButton"/>
<Button
android:id="@+id/PLUButton"
android:text="PLU"
style="@style/RightKeyboardButton"/>
</RelativeLayout>
<EditText
android:id="@+id/DataEntryEditText"
android:layout_width="match_parent"
android:layout_height="40dp"
android:ems="10"
android:inputType="text"
android:text=""
android:background="@drawable/edit_box"
android:layout_marginTop="5dp"/>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text=""
android:layout_marginTop="5dp"
android:background="@drawable/result_box"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:background="@color/dynakeyBackGround">
<TextView
android:id="@+id/tvPosNo"
android:text="@string/pos_no"
style="@style/MposDetailsView"/>
<TextView
android:id="@+id/tvSeqNo"
android:text="@string/seq_no"
style="@style/MposDetailsView"/>
<TextView
android:id="@+id/tvosk"
android:text="@string/osk"
style="@style/MposDetailsView"/>
<TextView
android:id="@+id/tvLogical_date"
android:text="@string/logical_date"
style="@style/MposDetailsView"/>
<TextView
android:id="@+id/tvVersionNo"
android:text="@string/version_no"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:textStyle="bold"
android:textSize="20dp"
android:textAlignment="center"
android:background="#f48fb1"/>
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
In both above layout I have included header_layout.xml which is having common TextView for both of my layout ie for fragment_main.xml and fragment_keyboard.xml
<?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="fill_parent"
>
<TextView
android:id="@+id/headerTextView"
style="@style/TitleText"
android:text="" />
</RelativeLayout>
I need to update the TextView after parsing the values from a json file , my problem is when I change the tab the header_layout.xml does not gets updated ,So how to update this ?Or initially itself I will update both the tab layouts if so how?
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public void onViewCreated(View v, Bundle savedInstanceState) {
super.onViewCreated(v, savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
//Switch to different layout accordingly
switch (getArguments().getInt(ARG_SECTION_NUMBER))
{
case 1: {
rootView = inflater.inflate(R.layout.fragment_main, container, false);
break;
}
case 2: {
rootView = inflater.inflate(R.layout.fragment_receipt, container, false);
break;
}
case 3: {
rootView = inflater.inflate(R.layout.fragment_totals, container, false);
break;
}
case 4: {
rootView = inflater.inflate(R.layout.fragment_keyboard, container, false);
break;
}
case 5: {
rootView = inflater.inflate(R.layout.fragment_keylock, container, false);
break;
}
}
return rootView;
}
}
In the parseJsonResponse() method, I sets the value for the objects in the layout
String UIFooter = ResponseHeaderObject.getString("FOOTER");
//Set Header Display
headerTextView =(TextView) findViewById(R.id.headerTextView);
headerTextView.setText(UIHeader);
来源:https://stackoverflow.com/questions/52927163/how-to-update-layout-all-layouts-in-different-tabs-in-tabs-in-android-tabs-wit