I have a simple app that displays text.
The app starts with a main screen with a few options (ex. an info button that leads to info about the app, a browse button th
Extend you activity as AppCompatActivity
and then use action bar as:-
getSupportActionBar().hide(); // for hiding
getSupportActionBar().show(); // for showing
In onCreate method, after setContentView, use this simply to show or hide for each class.
getSupportActionBar().hide();
getSupportActionBar().show();
If you want to set title,
getSupportActionBar().setTitle("This is ToolBar");
If you are trying to show/hide the ActionBar of an INTENT with AppCompatActivity and you get the following issues:
Follow these steps to access to the same relevant thread into the intent:
// 1) Calling the intent in the "main activity"
Intent intent = new Intent(context, YourClass.class);
startActivity(intent);
// 2) Get the context in your "Intent activity"
public class YourClass extends AppCompatActivity {
YourClass mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
mContext = this;
}
// 3) Hide/Show Action bar from a method
public void yourMethod(YourClass mContext){
mContext.runOnUiThread(new Runnable() {
@Override
public void run() {
mContext.getSupportActionBar().hide();
}
});
}
<style name="Theme.AppCompat.NoActionBar" parent="Theme.AppCompat.Light">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
remove android in item windowActionbar.just like follow:
<style name="Theme.AppCompat.NoActionBar" parent="Theme.AppCompat.Light">
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
I hope it can help you.
AppCompatActivity has its own embedded toolbar. You dont need to use extra toolbar definition in your main xml.
just call getSupportActionBar().show()
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().show();
}
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
TextView text1=(TextView)findViewById(R.id.textView);
text1.setText("I changed the text!");
}
return super.onOptionsItemSelected(item);
}
Dismiss ToolBar for all activities with AppTheme
:
styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"><!-- .NoActionBar -->