Reputation: 573
I am trying to add ActionBar to my android activity, but app automatically closes due to some code error. So what is the proper code to display ActionBar in the Activity?
I tried this code in onCreate method but its not working
ActionBar actionBar;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timetable);
actionBar = getActionBar();
actionBar.setTitle("MY TITLE");
actionBar.show();
}
Upvotes: 0
Views: 12197
Reputation: 847
Following code:
public class Test extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
context = this.getApplicationContext();
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
ActionBar actionBar = getActionBar();
// show the action bar title
actionBar.setDisplayShowTitleEnabled(true);
// adding style and colour to title
SpannableString s = new SpannableString("Field");
s.setSpan(new ForegroundColorSpan(Color.parseColor("#ffffff")), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
actionBar.setTitle(s);
}
Upvotes: 0
Reputation: 6828
Try this,
ActionBar
use Toolbar
.AppCompatActivity
instead of ActionBarActivity
.getSupportActionBar()
instead of getActionBar()
.Toolbar
is shown by default, so you dont nedd to call the show()
method.Upvotes: 1