Reputation: 1
I'm new to Android developing. Can someone explain how to add action button into this class if it is possible?
public class HomeFragment extends Fragment {
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
HomeViewModel homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
final TextView textView = root.findViewById(R.id.text_home);
homeViewModel.getText().observe(this, new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText(s);
}
});
return root;
}
and xml file
<TextView
android:id="@+id/text_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Upvotes: 0
Views: 89
Reputation: 497
Once you want to add onCreateOptionsMenu, you should press CTRL + O to open the dialog window, then type in onCreateOptionsMenu and choose it.
Let the Android Studio creates the code in a related and necessary place. (Source: https://www.youtube.com/watch?v=oh4YOj9VkVE )
So, you may write the true code, on the wrong paranthesis.
Upvotes: 0
Reputation: 11
If you want to add an action button over the toolbar, you have to:
Create the menu and the related item inside the menu folder:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_add"
android:icon="@android:drawable/ic_menu_add"
android:title="Add"
app:showAsAction="ifRoom|collapseActionView"/>
</menu>
NOTE: In this example, I added a button to add an element;
You have to inflate the menu inside the MainActivity class:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
getMenuInflater().inflate(R.menu.add_menu, menu);
return true;
}
Then you can add action over the button
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.action_add){
//Do something
}
return super.onOptionsItemSelected(item);
}
I Hope this will help you!
Upvotes: 0
Reputation: 81
implement mymenu.xml in this way
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/mybutton"
android:title="Add"
app:showAsAction="always"
android:icon="@drawable/mybuttonicon"
/>
</menu>
Upvotes: 1
Reputation: 81
If you want to customize U.I of your Action Bar, then you can use Toolbar as Action Bar. Here are the steps to do the same:
// create an action bar button
@Override public boolean on CreateOptionsMenu(Menu menu) {
// R.menu.mymenu is a reference to an xml file named mymenu.xml which should be inside your res/menu directory.
// If you don't have res/menu, just create a directory named "menu" inside res
getMenuInflater().inflate(R.menu.mymenu, menu);
return super.onCreateOptionsMenu(menu);
}
Upvotes: 1
Reputation: 725
override this method in your activity
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
for this you have to create a menu named "home" in your menu folder (if you don't see a menu folder please create one inside res)
Upvotes: 0