Pages

Friday, March 29, 2013

Add home button in title bar

1.http://pastebin.com/GiyLvmsj
2.http://www.londatiga.net/it/how-to-create-custom-window-title-in-android/




In values create a file called custom_title.xml

<resources>
<style name="CustomWindowTitleBackground">
<item name="android:background">#323331</item>
    </style>
    
    <style name="CustomTheme" parent="android:Theme">
    <item name="android:windowTitleSize">35dip</item>
        <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
    </style>
</resources>

In laylout create a file called window_title


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="35dip"
android:gravity="center_vertical"
android:paddingLeft="5dip"
android:background="#323331">

<ImageView
android:id="@+id/header"
android:src="@drawable/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right|center_vertical"
android:paddingRight="5dip">


 
   <ImageButton
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>

</LinearLayout>





Create a class 

public class CustomWindows extends Activity {

protected ImageButton icon;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
       
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
       
        setContentView(R.layout.m);
       
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
       
       
        icon  = (ImageButton) findViewById(R.id.icon);
        icon.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent goHome = new Intent(Intent.ACTION_MAIN);
       goHome.setClass(CustomWindows.this, MainActivity.class);
       goHome.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
       startActivity(goHome);
       finish();
}
});
}
}




and to use it...extends CustomWindows

No comments:

Post a Comment