I need to manage a specific time slot as shown in the picture.
I need that when I click on a specific TextView, it opens a new fragment sending me which element was clicked on. That is the layout file
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:id="@ id/tableLayoutWeek"
>
<!--riga dei giorni-->
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blueBlack"
android:padding="20dp"
>
<TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="lun"
android:textAllCaps="true"
android:textColor="#FFFFFF" />
<TextView
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="mar"
android:textAllCaps="true"
android:textColor="#FFFFFF" />
<TextView
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="mer"
android:textAllCaps="true"
android:textColor="#FFFFFF" />
<TextView
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="gio"
android:textAllCaps="true"
android:textColor="#FFFFFF" />
<TextView
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="ven"
android:textAllCaps="true"
android:textColor="#FFFFFF" />
</TableRow>
<!--riga delle 16-->
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/yellowRow"
android:padding="20dp"
android:id="@ id/row16"
>
<TextView
android:id="@ id/lun16"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="16:00"
android:textStyle="bold"
android:textAllCaps="true"
android:textColor="@color/black"
android:layout_margin="5dp"
/>
<TextView
android:id="@ id/mar16"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="16:00"
android:textAllCaps="true"
android:textColor="@color/black"
android:layout_margin="5dp"/>
<TextView
android:tag="mer16"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="16:00"
android:textAllCaps="true"
android:textColor="@color/black"
android:layout_margin="5dp"/>
<TextView
android:tag="gio16"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="16:00"
android:textAllCaps="true"
android:textColor="@color/black"
android:layout_margin="5dp"/>
<TextView
android:tag="ven16"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="16:00"
android:textAllCaps="true"
android:textColor="@color/black"
android:layout_margin="5dp"/>
</TableRow>
<!--riga delle 17-->
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/yellowRow"
android:padding="20dp"
>
<TextView
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="17:00"
android:textAllCaps="true"
android:textColor="@color/black"
android:layout_margin="5dp"/>
<TextView
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="17:00"
android:textAllCaps="true"
android:textColor="@color/black"
android:layout_margin="5dp"/>
<TextView
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="17:00"
android:textAllCaps="true"
android:textColor="@color/black"
android:layout_margin="5dp"/>
<TextView
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="17:00"
android:textAllCaps="true"
android:textColor="@color/black"
android:layout_margin="5dp"/>
<TextView
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="17:00"
android:textAllCaps="true"
android:textColor="@color/black"
android:layout_margin="5dp"/>
</TableRow>
<!--riga delle 18-->
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/yellowRow"
android:padding="20dp"
>
<TextView
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="18:00"
android:textAllCaps="true"
android:textColor="@color/black"
android:layout_margin="5dp"/>
<TextView
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="18:00"
android:textAllCaps="true"
android:textColor="@color/black"
android:layout_margin="5dp"/>
<TextView
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="18:00"
android:textAllCaps="true"
android:textColor="@color/black"
android:layout_margin="5dp"/>
<TextView
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="18:00"
android:textAllCaps="true"
android:textColor="@color/black"
android:layout_margin="5dp"/>
<TextView
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="18:00"
android:textAllCaps="true"
android:textColor="@color/black"
android:layout_margin="5dp"/>
</TableRow>
<!--riga delle 19-->
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/yellowRow"
android:padding="20dp"
>
<TextView
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="19:00"
android:textAllCaps="true"
android:textColor="@color/black"
android:layout_margin="5dp"/>
<TextView
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="19:00"
android:textAllCaps="true"
android:textColor="@color/black"
android:layout_margin="5dp"/>
<TextView
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="19:00"
android:textAllCaps="true"
android:textColor="@color/black"
android:layout_margin="5dp"/>
<TextView
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="19:00"
android:textAllCaps="true"
android:textColor="@color/black"
android:layout_margin="5dp"/>
<TextView
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="19:00"
android:textAllCaps="true"
android:textColor="@color/black"
android:layout_margin="5dp"
/>
</TableRow>
</TableLayout>
I tried this way, but I always get the row id and not the child id.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = FragmentWeekBinding.inflate(inflater, container, false);
View root = binding.getRoot();
binding.row16.setClickable(true);
binding.row16.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int id = view.getId();
Toast.makeText(getContext(), "pos:" id, Toast.LENGTH_SHORT).show();
}
});
return root;
}
I also tried tag but it still doesn't work, does anyone have any solution to recommend? I would be very grateful
CodePudding user response:
Set an onClickListener on every TextView.
Example: https://www.folkstalk.com/tech/onclicklistener-and-table-layout-with-example/
It's done on a Button. Change to a TextView.
CodePudding user response:
// The row the view is in
TableRow row = (TableRow) view.getParent();
// It's index
int index = tableLayout.indexOfChild(row);