Here is source code of the Program to to Change the Displayed Text alongside the Application Icon at Runtime in Android using Java. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.
MainActivity.java
package com.example.change_text_displayed_action_bar; import android.annotation.SuppressLint; import android.app.ActionBar; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.Button; @SuppressLint("NewApi") public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button change = (Button)findViewById(R.id.button1); change.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub ActionBar actionBar = getActionBar(); actionBar.setSubtitle("Changed text"); actionBar.setTitle("And the magic happens!!!!"); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
Sanfoundry Global Education & Learning Series – 100+ Java Android Tutorials.
If you wish to look at all Tutorials, go to Java Android Tutorials.
Next Steps:
- Get Free Certificate of Merit in Java Programming
- Participate in Java Programming Certification Contest
- Become a Top Ranker in Java Programming
- Take Java Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Apply for Java Internship
- Apply for Information Technology Internship
- Practice BCA MCQs
- Buy Java Books
- Buy Programming Books