I was able to manipulate some text of an existing TextView in a Fragment. However I was not able to programatically add a new ProgressBar
to the existing layout.
In the Fragment class:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_completed_office_hours, container, false);
LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.linearLayoutCompletedOfficeHours);
progressBar = new ProgressBar(this.getContext());
progressBar.setMax(daysInTotal);
progressBar.setProgress(daysCompleted);
linearLayout.addView(progressBar);
TextView textView = (TextView) view.findViewById(R.id.completedXOfYDays);
textView.setText(daysCompleted + " / " + daysInTotal);
return view;
}
The xml:
<FrameLayout 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"
tools:context=".fragment.CompletedOfficeHoursFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/activity_horizontal_margin"
android:orientation="horizontal"
android:id="@+id/linearLayoutCompletedOfficeHours">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/completedXOfYDays" />
</LinearLayout>
</FrameLayout>
When executing it I get 19 / 299
as text, but without any ProgressBar
. What am I doing wrong=
You have specified "linearLayoutCompletedOfficeHours" as a Linear Layout with
android:orientation="horizontal"
and given textviewandroid:layout_width="match_parent"
.Making this will make textview to take entire space and progressbar is created and shown in the screen. Instead change textView width toandroid:layout_width="wrap_content"
and the progress will be visible.It is not showing because you did not specify its child's layout_param and therefore will result to parent not displaying it.
You need to specify the layout params of the child view you want to attach.