SoFunction
Updated on 2025-04-04

Adding the return button function in the Android title bar

The return button in the title bar is used more often in actual use. Today I will talk about my experience in project development. Without further ado, I will just use the source code directly. The source code is the most powerful.

1. Write custom classes

public class CustomTitle {  
  private static Activity mActivity;  
  public static void getCustomTitle(Activity activity, String title) { 
   mActivity = activity; 
    (Window.FEATURE_CUSTOM_TITLE); 
   (.custom_title); 
    ().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, 
        .custom_title);     
    TextView textView = (TextView) (.head_center_text);  
    (title);  
   Button titleBackBtn = (Button) ();  
    (new OnClickListener() {  
      public void onClick(View v) {  
       ("Title back","key down"); 
         
        (); 
      }  
    });  
  } 
} 

2. xml resources, define custom_title in layout

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:andro 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" >   
  <Button  
    android:  
    android:layout_width="50dp"    
   android:layout_height="wrap_content"   
    android:gravity="center"  
   android:layout_alignParentRight="true"  
    android:background="@android:drawable/ic_menu_revert"/>      
  <TextView  
    android:  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:layout_centerInParent="true"  
   android:text=""   
    android:textSize="25sp" 
    android:textColor="#FFFFFF"  
    />  
</RelativeLayout> 

3. Call in the activity that needs to be called

public class InformationActivity extends Activity{ 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub     
(savedInstanceState);          
   (this, "personal information"); 
    setContentView(); 
  ....................... 
  } 
} 

4. Add style definition in res/values/

 <style name="MyCustomTheme" parent="android:Theme">     
     <item name="android:windowTitleBackgroundStyle">@style/TitleBarBackground</item>  
     <item name="android:windowTitleSize">50dp</item>  
  </style> 

5. Add support to InformationActivity in

     android:name="" 
      android:theme="@style/MyCustomTheme" 
      android:screenOrientation="landscape" /> 

OK, just complete the above steps.

The above is the addition of the return button function in the Android title bar introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message. The editor will reply to everyone in time!