Why I create toolbar I do not have three dots who will appear. How can I do ?
public class Main extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
}
And XML :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="woowin.woowin.Main">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#393939"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
/>
</RelativeLayout>
And can you explain me, how add options in this three dots after that ?
thank you
you forgot to add your menu in your activity use below code to add menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_menu, menu);
return true;
}
You need to add at least one menu item either via XML or in java code using add into your menu to see the menu options
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0,0,0,"title");
return true;
}
Related
Suppose I have two buttons, one is an action button which is in the action bar(#+id/button1). And another is common button in the layout(#+id/button2).
How can I set the button1 disabled when i click the button2?
findViewById(button1) seems not work. it will return null.
this is my menu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/button1"
android:title="submit"
android:showAsAction="always" />
</menu>
and this is my mainacticity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.button1 ) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
button1 = (Button) findViewById(R.id.button1);/*which return null*/
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
button1.setEnabled(false);/*what i failed to do*/
}
});
}
1.Create a variable MenuItem in your Activity class
MenuItem menuItem;
2. Find your variable in onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
menuItem = menu.findItem(R.id.item_circuit);
return super.onCreateOptionsMenu(menu);
}
Disable the item in your buttonClick method
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
menuItem.setEnabled(false);
}
});
Try this
public void button1click(View v)
{
Button button2=(Button)getActionBar().getCustomView().findViewById(R.id.button2);
button2.setClickable(false);
button2.setEnabled(false);
}
You have to inflate the layout/views before using findViewById.
Like that:
private Toolbar toolbar;
// Set a Toolbar to replace the ActionBar.
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
cartLayout = (RelativeLayout) toolbar.findViewById(R.id.rl_cart);
Your toolbar layout would be like this:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right">
<RelativeLayout
android:id="#+id/rl_cart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:padding="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:src="#drawable/ic_cart" />
</RelativeLayout>
</RelativeLayout>
You can find more answer here:
How can I implement custom Action Bar with custom buttons in Android?
How to set two custom actionbar button in left and right in android?
I found this post How to add a switch to android action bar? and this works for me. But I can't get its event. I'm using appcompat, and I used app namespace for actionLayout and showAsAction, but I'm not able to handle its click on onOptionsItemSelected method?. My app is crashing on start up. how to handle it or what is wrong with this code?
here is my code
menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<item
android:id="#+id/myswitch"
android:title=""
app:showAsAction="always"
app:actionLayout="#layout/switch_layout"/>
switch_layout.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="match_parent"
android:orientation="horizontal" >
<Switch
android:id="#+id/switchForActionBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true" />
</RelativeLayout>
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.your_menu, menu);
MenuItem menuItem= menu.findItem(R.id.myswitch);
Switch switcha = (Switch)menuItem.findViewById(R.id.switchForActionBar);
switcha.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do anything here on check changed
}
});
return super.onCreateOptionsMenu(menu);
}
and
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.myswitch) {
//do something
}
return super.onOptionsItemSelected(item);
}
Any help is highly appreciated. Thanks in advance.
I found my error. I have to use
View view = MenuItemCompat.getActionView(menuItem);
extra line in onCreateOptionsMenu to find out the switch button
. Here is my full method
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem menuItem = menu.findItem(R.id.myswitch);
View view = MenuItemCompat.getActionView(menuItem);
Switch switcha = (Switch) view.findViewById(R.id.switchForActionBar);
switcha.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do anything here on check changed
}
});
return super.onCreateOptionsMenu(menu);
}
Now my mainActivity.xml file like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/tool_bar" />
<FrameLayout android:id="#+id/tab_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<include layout="#layout/bottom_bar" />
</LinearLayout>
This page has four tabs on the bottom,each tab is associated with a different fragment which will be added to android:id="#+id/tab_content".
The menu_main.xml like this
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_calendar"
android:icon="#drawable/ic_title_date"
android:orderInCategory="100"
android:title="#string/calendar"
app:showAsAction="always" />
<item
android:id="#+id/action_notification"
android:icon="#drawable/ic_title_notification"
android:orderInCategory="200"
android:title="#string/notification"
app:showAsAction="always" />
</menu>
I have these code in my MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
return super.onOptionsItemSelected(item);
}
Fragment2 and Fragment3 have these code:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
if (id == R.id.action_calendar)
{
showDatePicker();
return true;
}
return super.onOptionsItemSelected(item);
}
android:id="#+id/action_calendar" menuItem is a datePicker,and i want to use this menuItem to be filter to load data for fragment2 and fragment3.
Here is my question,when I navigate to the fragment3 and click the calendar menu and then change to fragment2,click the calendar,the system call onOptionsItemSelected method in fragment 3 ,not in fragment2 ?
I want calendar menu response to different fragment separately .
I want to change dynamically the aspect of my actionbar. Applying the custom view layout i've created before, but still not working. This is my code:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar mActionBar = getSupportActionBar();
mActionBar.setCustomView(R.layout.activity_main);
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
ActionBar mActionBar = getSupportActionBar();
Toast.makeText(this,item.toString(),Toast.LENGTH_LONG).show();
if(item.toString() == "search"){
mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
mActionBar.setCustomView(R.layout.search);
}
return super.onOptionsItemSelected(item);
}
Custom layout xml
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
xmlns:android="schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText android:id="#+id/etSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
</LinearLayout>
consider trying item.toString().equals("search")
I want to have a toolbar-like menu at the bottom of an activity, and I'm using ActionBarSherlock in my app, and I found the "Static attachment"-demo that adds a "toolbar" at the bottom...
So I've tried to implement this like below:
public class ReadMailInbox extends Activity implements OnCreateOptionsMenuListener
{
ActionBarSherlock mSherlock = ActionBarSherlock.wrap(this);
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mSherlock.setContentView(R.layout.readmessage_layout);
}
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
return mSherlock.dispatchCreateOptionsMenu(menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Refresh")
.setIcon(android.R.drawable.ic_menu_rotate)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
}
But it appears at the top of the view instead of at the bottom...
Additionally, I would like to use an xml layout for the menu, instead of adding each menu button, since I want to use this for several activities...
So how I can get it to show at the bottom instead???
Use splitActionBarWhenNarrow option in AndroidManifest.xml:
<activity android:name=".YourActivity" android:uiOptions="splitActionBarWhenNarrow" />
Override onCreateOptionsMenu as following:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
final MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.activity_home, menu);
return super.onCreateOptionsMenu(menu);
}