Ask Your Question
0

In Android Studio's Main Activity, what are the steps to create a TextView element using a menu xml file?

asked 2021-12-21 11:00:00 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-01-03 02:00:00 +0000

pufferfish gravatar image
  1. Create a new menu xml file in the res/menu folder.
  2. Add an item to the menu with the attribute android:id="@+id/textviewmenu_item" and android:title="TextView".
  3. In the Main Activity's onCreateOptionsMenu() method, inflate the menu xml file and return true.
  4. In the Main Activity's onOptionsItemSelected() method, check if the selected menu item is the textviewmenu_item.
  5. If it is, create a new TextView element using the findViewById() method with the id R.id.text_view, and set its text to "Hello World".
  6. Finally, add the TextView element to the Main Activity's layout using the addView() method of the parent container.
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-12-21 11:00:00 +0000

Seen: 13 times

Last updated: Jan 03 '22