Android custom action bar - android

I have a problem that is I add the custom action bar in app, but the action bar view has a gap(the red circle position, in the link's picture).
MainActivity code:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_layout);
getSupportActionBar().setCustomView(R.layout.actionbar_layout);
getSupportActionBar().setDisplayShowCustomEnabled(true);
}
}
actionbar_layout code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0146A3">
<TextView
android:id="#+id/abr_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:gravity="center"
android:singleLine="true"
android:textColor="#FFFFFF"
android:textSize="26sp"
android:text="Title"
tools:text="Title"/>
</RelativeLayout>
But not use android support library, this problem does not occur.("MainActivity extands ActionBarActivity" change to "MainActivity extands Activity", and "getSupportActionBar() change to getActionBar()")

set Actionbar background using style.xml
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionBarItemBackground">#drawable/bg_actionbar_selected</item>
</style>
<style name="action_bar" parent="#android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#drawable/yourBackgound or color</item>
</style>

Let me suggest another way to do that:
Step 1:
Hide your actionbar.
Use this theme for the activity.
android:theme="#android:style/Theme.NoTitleBar"
step2:
Use a custom relative layout as actionbar
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="#009ACD">
</RelativeLayout>

Related

How to change actionbar title and subtitle font size in android?

This is what I try but nothing happen
<style name="MyCustomTheme" parent="Theme.Freshchat.Light.DarkActionBar">
<item name="titleTextStyle">#style/Style1</item>
</style>
<style name="Style1" parent="#android:style/Widget.TextView">
<item name="android:textSize">12sp</item>
</style>
For more context, I am using Freshchat chat widget and want to change the Title and Subtitle font size. Thanks!
Try this your can you Toolbar
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/myToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:titleTextAppearance="#style/ToolbarTitleTheme"
app:subtitleTextAppearance="#style/ToolbarSubtitleTheme">
</android.support.v7.widget.Toolbar>
than create Style in Style.xml
<style name="ToolbarTitleTheme" parent="#style/TextAppearance.Widget.AppCompat.Toolbar.Title">
<item name="android:textSize">20dp</item>
<<item name="android:textColor">#FFAA12</item>
<!-- include other attributes you want to change: textColor, textStyle, etc -->
</style>
<style name="ToolbarSubtitleTheme" parent="#style/TextAppearance.Widget.AppCompat.Toolbar.Subtitle">
<item name="android:textSize">14dp</item>
<item name="android:textColor">#FFAA12</item>
</style>
1>Create an XML file with name titleview.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textSize="20sp"
android:textColor="#android:color/white"
android:maxLines="1"
android:ellipsize="end"
android:text="" />
</RelativeLayout>
2>In OnCreate method:
if(getSupportActionBar() != null) {
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
LayoutInflater inflator = LayoutInflater.from(this);
View v = inflator.inflate(R.layout.titleview, null);
//if you need to customize anything else about the text, do it here.
//I'm using a custom TextView with a custom font in my layout xml so all I need to do is set title
((TextView) v.findViewById(R.id.title)).setText(this.getTitle());
((TextView) v.findViewById(R.id.title)).setTextSize(20);
//assign the view to the actionbar
this.getSupportActionBar().setCustomView(v);
}
3>In Manifest file to your Activity Add this line
android:label="#string/room_title"
The simple way will be to create a custom_action_bar.xml layout for your action bar as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/action_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="You title"
android:textColor="#ff0"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="#+id/action_bar_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your subtitle"
android:textColor="#ff0"
android:textSize="12sp" />
</LinearLayout>
Then you have to set the layout of you custom action bar as follows:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.custom_action_bar);
}
Using this method you will be able to easily customize your action bar.
You can use SpannableString as follows:
SpannableString s = new SpannableString(getResources().getString(R.string.manage_prescription));
s.setSpan(new TypefaceSpan(YourActivity.this, Constants.TOOLBAR_TITLE_FONT), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
And user this SpannableString object to set the title to your action bar.

How to set actionbar transparent background

I use below code to show an actionbar on the activity. But the background of the action bar is not transparent. I have searched some articles about how to set the actionbar background transparent but it doesn't seem to work for me. I have attached all the layout and theme code as below. Please help to check what I am missing here.
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scanner_activity);
Toolbar toolBar = (Toolbar)findViewById(R.id.scanner_toolbar);
toolBar.setTitle("Title");
setSupportActionBar(toolBar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Below is the activity layout xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/scanner_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="#android:color/transparent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:theme="#style/AppScannerToolBarTheme"
/>
<com.journeyapps.barcodescanner.DecoratedBarcodeView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#+id/scanner_toolbar"
android:layout_alignParentBottom="true"
android:id="#+id/zxing_barcode_scanner"
app:zxing_use_texture_view="true"/>
</RelativeLayout>
Below is the AppScannerToolBarTheme for the toolbar:
<style name="AppScannerToolBarTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#android:color/transparent</item>
<item name="windowActionBarOverlay">true</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:backgroundStacked">#android:color/transparent</item>
<item name="android:background">#android:color/transparent</item>
</style>
EDIT:
I have added below code but it still doesn't work:
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
toolBar.setAlpha(0.5f);
toolBar.setBackground(new ColorDrawable(Color.TRANSPARENT));
Below image is what I want to achieve:
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000")));
OR
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Hope this will work for you thanks .
Toolbar is a view, so you could use alpha method from view class.
toolbar.setAlpha(0.5f);

how to delete the toolbar in android design

The red frame should not have in my code, but after I updated my Android Studio, it is showed in all of my APPS. please click and see the picture.
How to delete the red frame from my code? And it could not find in its design/text. The blue frame's begin code is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/activity_login_user_email_edt"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:paddingLeft="35dp"
android:autoText="true"
android:hint="e-mail" />
The Answer Skynet posted is correct,but this is the programatical way
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Full Screen and remove toolbar
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
That is the theme your activity is extending from. You change it in the styles.xml. Then you need to update this setting in the Manifest.
You need to add this to styles.xml:
<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="#style/Theme.AppCompat.Light">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
Then in your AndroidManifest.xml for this Activity:
<activity
android:name=".YourActivity"
android:label="#string/title_activity_main"
android:screenOrientation="landscape"
android:theme="#style/Theme.AppCompat.Light.NoActionBar.FullScreen">
</activity>
The above will work if you are extending from AppCompatActivity which should look like:
public class YourActivity extends AppCompatActivity{}

Toolbar and ActionBar not shown in activity

I use Material design firstly in my apps and my problem is when I create activity there is no action bar or toolbar but I have Material Navigation drawer why it not implemented automatically like in older versions
Layout of activity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView4" />
</LinearLayout>
Activity:
package fragments;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
public class Noname1 extends ActionBarActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
}
`
My theme:
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/nliveo_green_colorPrimary</item>
<item name="colorPrimaryDark">#color/nliveo_green_colorPrimaryDark</item>
<item name="colorAccent">#color/nliveo_green_colorPrimary</item>
<item name="android:textColorPrimary">#color/myTextPrimaryColor</item>
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
<item name="android:windowBackground">#color/myWindowBackground</item>
</style>
Make a custom_toolbar.xml file like this:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#color/nliveo_green_colorPrimary"
android:layout_height="?android:attr/actionBarSize" />
Include it in your layout file wherever you want like:
<include android:id="#+id/toolBar" layout="#layout/custom_toolbar"/>
in Your onCreate Method:
public class Noname1 extends ActionBarActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
Toolbar tb = (Toolbar)findViewById(R.id.toolBar);
setSupportActionBar(tb);
}
}
And keep your manifest file:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/nliveo_green_colorPrimary</item>
<item name="colorPrimaryDark">#color/nliveo_green_colorPrimaryDark</item>
<item name="colorAccent">#color/nliveo_green_colorPrimary</item>
<item name="android:textColorPrimary">#color/myTextPrimaryColor</item>
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
<item name="android:windowBackground">#color/myWindowBackground</item>
</style>
Hope it helps!!!
You should include toolbar in your layout. Something like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView4" />
</LinearLayout>
Hope it helps you!!
Write getSupportActionBar().setDisplayHomeAsUpEnabled(true); if you are using support library and if not then remove Support just write getActionBar().setDisplayHomeAsUpEnabled(true);

Android: custom title style

I have a listactivity with iconified text which I'm giving a custom title. Here's my custom_style.xml:
<resources>
<style name="CustomWindowTitleBackground">
<item name="android:background">#222222</item>
</style>
<style name="CustomTheme" parent="android:Theme">
<item name="android:windowTitleSize">50dip</item>
<item name="android:windowTitleBackgroundStyle">#style/CustomWindowTitleBackground</item>
</style>
</resources>
Here's the layout for the title, window_title.xml:
<?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="50dip"
android:gravity="center_vertical"
android:paddingLeft="10dip"
android:background="#222222">
<ImageView
android:id="#+id/header"
android:src="#drawable/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
I set the new title style in onCreate:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
// if it wasn't a listactivity, here I'd use
// setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
browseToRoot();
}
My problem is that the styled title bar appears at the correct size and with the correct background colour (taken from custom_style.xml) but eveything in window_title.xml is ignored. The rest of the listadapter still works fine. In a very similar question here: Custom title with image it says setFeatureInt must come before super.onCreate but either way my result is the same.
I've tried replacing the imageview with a nice simple textview, but the entire window_title.xml seems to be ignored. Any ideas where I'm going wrong? Many thanks, Baz.

Categories

Resources