Search code examples
androidtitlebar

Custom Title bar in Android


I have create a custom title bar, It contains a TextView and label. i have added this to an activity as follows

 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.homepage);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
            R.layout.windowtitle);

But now i want to display current time in the textview inside the titlebar.

How can this be done?


Solution

  • you can get text view like this.create a mytitle.xml layout with textview and then add this code it will work..

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    
    setContentView(R.layout.main);
    
      if ( customTitleSupported ) {
          getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
      }
    
      final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
      if ( myTitleText != null ) {         
          myTitleText.setText(" TITLE  ");     
      }
    }