Tuesday, November 29, 2022

How to change title of Activity in Android ?

When you are using the fragments and activities together in an application, it takes the title for the fragments automatically as the names of the fragments. But for the activities, it takes the app name itself as the title of the activity, So how to change the title of the activity.

 There are two ways to change the title of the activity. 

1. You can do it programmatically through activity by setting the title as below:

public class QuestionActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question);
setTitle("Questions ?");

}
}

Before:                                       After: 




2. You can do it through androidmanifest.xml by setting the label there.  Open Androidmanifest.xml, in the activity you can add the label as the string or from the string file below to display the title:


<activity
android:name=".QuestionActivity"
android:label="@string/Questions"
android:exported="false" />

Here is the title shown:




That's it. Happy coding


No comments: