Reputation: 33
How can we set title text padding without making custom ActionBar?
Here is good answer about ActionBar`s logo padding in xml: Padding between ActionBar's home icon and title
So I want to do something like this for title.
Here is my styles.xml code:
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:background">@android:color/transparent</item>
<item name="android:actionBarSize">80dp</item>
<item name="android:actionBarStyle">@style/MyActionBar</item>
<item name="android:windowContentOverlay">@null</item>
</style>
<style name="MyActionBar" parent="android:Widget.Holo.ActionBar" >
<item name="android:logo">@drawable/action_bar_logo_padding</item>
<item name="android:background">@drawable/action_bar</item>
<item name="android:titleTextStyle">@style/MyActionBarTitleText</item>
</style>
<style name="MyActionBarTitleText"
parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">@color/green</item>
<item name="android:paddingTop">25dp</item>
<item name="android:textSize">30sp</item>
</style>
android:paddingTop not working (same goes for android:top and android:topOffset)
Upvotes: 2
Views: 4644
Reputation: 5034
This is for API 21!
After messing around with this for several days, I gave up on XML (and the documentation) and did the Java thing.
Get the action bar.
Loop through its children.
When the title view is found, do things to it!
final View actionBarView = this.findViewById(R.id.action_bar);
final ViewGroup actionBarViewGroup = (ViewGroup) actionBarView;
for (int i = 0; i < actionBarViewGroup.getChildCount(); i++) {
final View actionBarChildView = actionBarViewGroup.getChildAt(i);
if (actionBarChildView instanceof AppCompatTextView) {
final AppCompatTextView titleText = (AppCompatTextView) actionBarChildView;
// 50 pixels will be 25dp assuming your screen density is 2:1
titleText.setPadding(0, 50, 0, 0);
titleText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
titleText.setTextColor(0xff00ff00);
break;
}
}
More info can be found here.
Upvotes: 0
Reputation: 33
The reason I didn`t want to create custom actionBar is that actionBar in my application contain only basic things like logo and title. Plus I already found how to control logo. But in the end, as Souto suggested, I created my own custom view:
java code (public class with static method):
import android.app.ActionBar;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
public class MyActionBar {
static void CreateActionBar(ActionBar ab, Activity ac, String str) {
ab.setDisplayShowHomeEnabled(false);
ab.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(ac);
View mCustomView = mInflater.inflate(R.layout.action_bar, null);
TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text);
mTitleTextView.setText(str);
ab.setCustomView(mCustomView);
ab.setBackgroundDrawable(ac.getResources().getDrawable(R.drawable.action_bar));
ab.setDisplayShowCustomEnabled(true);
}
}
action_bar.xml relative layout:
<?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="80dp"
android:background="@android:color/transparent" >
<ImageView
android:id="@+id/image"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:src="@drawable/logo" />
<TextView
android:id="@+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image"
android:layout_marginLeft="10dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="9dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/green"
android:textStyle="normal"
android:fontFamily="sans-serif-light"
android:textSize="34sp" />
</RelativeLayout>
styles.xml:
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:background">@android:color/transparent</item>
<item name="android:actionBarSize">80dp</item>
<item name="android:actionBarStyle">@style/MyActionBar</item>
<item name="android:windowContentOverlay">@null</item>
</style>
<style name="MyActionBar" parent="android:Widget.Holo.ActionBar" >
<item name="android:displayOptions"></item>
<item name="android:background">@drawable/action_bar</item>
</style>
and in each activity:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar mActionBar = getActionBar();
MyActionBar.CreateActionBar(mActionBar, this, "My title");
......................................
Upvotes: 1
Reputation: 771
You problem will not resolve so easy with Styles... The best solution is create a CustomView to you actionBar and use it! Sometimes styles padding in actionbar(title) doesn't work on some devices.
Example: -> http://javatechig.com/android/actionbar-with-custom-view-example-in-android
Good Luck!
Upvotes: 1