I am doing this:
toolbar = (Toolbar) findViewById(com.sports.unity.R.id.tool_bar);
setSupportActionBar(toolbar);
setTitle("hello");
I want to set a custom font for the text here in the title "hello". How to do that?
Since android.support.v7.appcompat 24.2 Toolbar has method setTitleTextAppearance and you can set its font without external textview.
create new style in styles.xml
<style name="RobotoBoldTextAppearance">
<item name="android:fontFamily">#font/roboto_condensed_bold</item>
</style>
and use it
mToolbar.setTitleTextAppearance(this, R.style.RobotoBoldTextAppearance);
Update 2018 (kotlin version)
fun Toolbar.changeToolbarFont(){
for (i in 0 until childCount) {
val view = getChildAt(i)
if (view is TextView && view.text == title) {
view.typeface = Typeface.createFromAsset(view.context.assets, "fonts/customFont")
break
}
}
}
and use it like that toolBar.changeToolbarFont()
old-post
To use a custom title in your Toolbar all you need to do is remember is that Toolbar is just a fancy ViewGroup so you can add a custom title like so:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_top"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="#color/action_bar_bkgnd"
app:theme="#style/ToolBarTheme" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toolbar Title"
android:layout_gravity="center"
android:id="#+id/toolbar_title" />
</android.support.v7.widget.Toolbar>
This means that you can style the TextView however you would like because it's just a regular TextView. So in your activity you can access the title like so:
Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);
TextView mTitle = (TextView) toolbarTop.findViewById(R.id.toolbar_title);
And then:
Typeface khandBold = Typeface.createFromAsset(BalrogApplication.getApplication().getAssets(), "fonts/Khand-bold.ttf");
mTitle.setTypeface(khandBold);
UPDATE
dynamically version
public static void changeToolbarFont(Toolbar toolbar, Activity context) {
for (int i = 0; i < toolbar.getChildCount(); i++) {
View view = toolbar.getChildAt(i);
if (view instanceof TextView) {
TextView tv = (TextView) view;
if (tv.getText().equals(toolbar.getTitle())) {
applyFont(tv, context);
break;
}
}
}
}
public static void applyFont(TextView tv, Activity context) {
tv.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/customFont"));
}
and use it like that
changeToolbarFont(findViewById(R.id.app_bar), this);
I still wanted to use the Toolbars title methods (nor did I want to have a custom Toolbar class), so adding in the custom TextView inside of the Toolbar xml element didn't work for me. Instead, I used the following method to find the TextView:
public static void applyFontForToolbarTitle(Activity context){
Toolbar toolbar = (Toolbar) context.findViewById(R.id.app_bar);
for(int i = 0; i < toolbar.getChildCount(); i++){
View view = toolbar.getChildAt(i);
if(view instanceof TextView){
TextView tv = (TextView) view;
Typeface titleFont = Typeface.
createFromAsset(context.getAssets(), "fonts/customFont");
if(tv.getText().equals(toolbar.getTitle())){
tv.setTypeface(titleFont);
break;
}
}
}
}
You can do this using just themes:
I wrote an article outlining the full solution. Here are the basics:
1) Define a theme in styles.xml:
<style name="ToolbarTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="android:fontFamily">#font/fancy-font</item>
</style>
2) Set that theme in your Toolbars layout:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:theme="#style/ToolbarTheme"/>
This assumes that you have a .ttf file stored in res/font:
You can create fonts folder under res directory in last version Android studio 3 .After this you must be define custom style in styles, and after this , you must be use titleTextAppearance in toolbar to style you are defined.
Steps like below.
1. Create fonts directory : res > Android Resource Directory > Resource type : fonts, and click on Ok to create fonts directory(Android studio 3).
Open styles.xml and make custom style like below
<style name="**TextAppearance.TabsFont**" parent="**android:TextAppearance**">
<item name="android:fontFamily">font/rmedium</item>
<item name="android:textSize">18sp</item>
</style>
3.Now open layout and add app:titleTextAppearance="#style/TextAppearance.TabsFont" to Toolbar tag, like below.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbarMain"
app:title="#string/time_amp_date"
app:titleTextAppearance="#style/TextAppearance.TabsFont"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:theme="#style/AppTheme"
app:elevation="2dp" />
That's DONE. Now if you running app, you can see font set to your toolbar.
Placement of Fonts:
Firstly, download a .ttf file. My file is Balker.ttf
make sure that you have got an "assets" folder. If there is none, right click over app go to New>Folder>assets Folder
In assets folder, create a new folder and name it 'font'
Put your file, as I did with Balker.ttf, into the 'font' folder.
Go to the java file of the activity whose font you have to customize. I customized mainActivity here.
for(int i = 0; i < toolbar.getChildCount(); i++)
{ View view = toolbar.getChildAt(i);
if(view instanceof TextView) {
TextView textView = (TextView) view;
Typeface myCustomFont=Typeface.createFromAsset(getAssets(),"font/Balker.ttf");
textView.setTypeface(myCustomFont); }
}
Two way you can do custom font on toolbar title
Solution : 1 [ Static Method ]
1) Define a theme in styles.xml:
<style name="ToolbarTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="android:fontFamily">#font/ubuntu</item>
</style>
2) Set that theme in your Toolbar's layout:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:theme="#style/ToolbarTheme"/>
Solution 2 : [ Dynamic ]
Kotlin
fun Toolbar.titleFont(){
for (i in 0 until childCount) {
val view = getChildAt(i)
if (view is TextView && view.text == title) {
view.typeface = Typeface.createFromAsset(context.assets,"fonts/customfont.ttf")
break
}
}
}
then use it like
toolBar.titleFont()
You can use this simple method to set custom font to toolbar title.
Toolbar toolbar = (Toolbar) findViewById(com.sports.unity.R.id.tool_bar);
TextView tv = getToolBarTextView();
tv.settext("Hello");
private TextView getToolBarTextView() {
TextView titleTextView = null;
try {
Field f = mToolBar.getClass().getDeclaredField("mTitleTextView");
f.setAccessible(true);
titleTextView = (TextView) f.get(mToolBar);
Typeface font = Typeface.createFromAsset(getApplicationContext().getAssets(),"fonts/mfont.ttf");
titleTextView.setTypeface(font);
} catch (NoSuchFieldException e) {
} catch (IllegalAccessException e) {
}
return titleTextView;
}
No need to use external textview if you only want to change the font of toolbar title because android.support.v7.appcompat 24.2 Toolbar has method setTitleTextAppearance and you can set its font like below.
create a new style in styles.xml
<style name="CamptonBookTextAppearance">
<item name="android:fontFamily">#font/campton_book</item>
</style>
and use it
mToolbar.setTitleTextAppearance(this, R.style.CamptonBookTextAppearance);
This works for me
typeFace= Typeface.createFromAsset(this.getAssets(), "fonts/myfont.ttf");
((TextView)toolbar.getChildAt(1)).setTypeface(typeFace);
I searched for this question as well, and this was one of the first answers that came up. I'll add my solution since the one given here can be counted as a bit outdated (keep in mind it would work nonetheless).
Since Android now supports custom fonts, there's no reason to assign fonts in Java, it can be done while making the XML file itself.
First, in your layout file, add a custom Toolbar (you can set the text size here itself)
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_top"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="#color/primary">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/signup"
android:textSize="22sp" />
</android.support.v7.widget.Toolbar>
Then, go to the design tab of your XML file, and select the text on the toolbar.
Select the option of fontFamily and select the font you want under the given options. If it is not given, you can search for more fonts.
Search for the font you want and select it. Your font gets changed.
Your XML file now will reflect the font you added, there will be an attribute named android:fontFamily
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_top"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="#color/primary">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/roboto_slab"
android:text="#string/signup"
android:textColor="#color/secondaryBackground"
android:textSize="22sp" />
</android.support.v7.widget.Toolbar>
Hope this helped.
If anyone has an issue of getting multiple toolbar title (one default and one that you set ) for xml:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:layout_gravity="left"
android:id="#+id/toolbar_title"
android:textSize="20sp"/>
</android.support.v7.widget.Toolbar>
then do this:
getSupportActionBar().setTitle(null);//Set the default to null
typeFace= Typeface.createFromAsset(getAssets(), "fonts/Raleway-Light.ttf");
toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
toolbarTitle.setTypeface(typeFace);
Just add textapperance in your toolbar xml no need to custumization:-
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:gravity="center"
android:minHeight="?attr/actionBarSize"
**app:titleTextAppearance="#font/montserrat_regular"**// apply your font
app:titleTextColor="#android:color/white" />
I made a binding adapter for this purpose.
public class FontBindingAdapter
{
#BindingAdapter({"font"})
public static void setFont(Toolbar toolbar, String font)
{
for (int i = 0; i < toolbar.getChildCount(); i++) {
if (toolbar.getChildAt(i) instanceof AppCompatTextView) {
UIUtil.setFont(font, (AppCompatTextView) toolbar.getChildAt(i));
}
}
}
}
Then use it like:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/green"
app:font="#{`LatoRegular`}"
app:title="#string/setup_favorites"
app:titleTextColor="#color/white"/>
Thank you #gmetax, it's a good point. It's bad to access textview for each activity. We have to write code below for each activity:
TextView mTitle = (TextView) toolbarTop.findViewById(R.id.toolbar_title);
We've already bind toolbar and we've use toolbar.setTitle(). So, I extend Toolbar and override setTitle method like this:
#Override
public void setTitle(CharSequence title) {
super.setTitle("");
mTitle = (TextView) findViewById(R.id.toolbar_title);
if (mTitle != null) {
if (!TextUtils.isEmpty(title)) {
mTitle.setText(title);
}
}
}
(Of course, custom font should be setted in CustomTextView class.setTypeface)
Now, we can use just like this:
toolbar.setTitle("bla bla");
I still wanted to use the Toolbars title methods, so adding in the custom TextView inside of the Toolbar xml element didn't work for me. Instead, I used the following method to find the TextView:
public static void applyFontForToolbarTitle(Activity context){
Toolbar toolbar = (Toolbar) context.findViewById(R.id.app_bar);
for(int i = 0; i < toolbar.getChildCount(); i++){
View view = toolbar.getChildAt(i);
if(view instanceof TextView){
TextView tv = (TextView) view;
Typeface titleFont = Typeface.
createFromAsset(context.getAssets(), "fonts/customFont");
if(tv.getText().equals(context.getTitle())){
tv.setTypeface(titleFont);
break;
}
}
}
}
If you are using google fonts (ie. Open Sans) you can add TextView as a child of your Toolbar like this
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:fontFamily="#font/open_sans"/>
</android.support.v7.widget.Toolbar>
and then just set fontFamily property for your TextView in Attributes menu (More Fonts option at the end of the dropdown list)
android:fontFamily="#font/open_sans
You can Use Custom font of toolbar programmatically Like this
1) first Create sub dir fonts in asset folder
2) Add your font files in the fonts folder.
toolbar.setTitle("Welcome");
Typeface fromAsset = Typeface.createFromAsset(getAssets(),"fonts/OpenSans-Light.ttf");
((TextView)toolbar.getChildAt(1)).setTypeface(fromAsset);
1st create
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:titleTextAppearance="#style/DancingWhite"
app:popupTheme="#style/AppTheme.PopupOverlay"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toolbar Title"
android:layout_gravity="center"
android:id="#+id/toolbar_title" />
</android.support.v7.widget.Toolbar>
2nd
your activity you can access the title like so:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
TextView mTitle = (TextView)
toolbar.findViewById(R.id.toolbar_title);
Typeface font = Typeface.createFromAsset(getAssets(), "Mukta- Regular.ttf");
mTitle.setTypeface(font);
just download what font you want to show in title and move it under res->font folder and create a file over there fontfamily
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font
android:fontStyle="normal"
android:fontWeight="400"
android:font="#font/futura_book" />
<font
android:fontStyle="normal"
android:fontWeight="400"
android:font="#font/your font file name" />
</font-family>
now add fontfamily="your downloaded font family name like a.ttf" in your xml file thats it
Download the custom font and keep it in font folder inside the res folder.
As Toolbar is a viewGroup we can place the textView inside the Toolbar and use as follow
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
>
<TextView
android:id="#+id/toolbar_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Toolbar"
android:gravity="center"
android:fontFamily="#font/roboto_regular" <--- custom font
android:textColor="#color/white"
android:textStyle="bold"
android:textAppearance="#style/Base.TextAppearance.Widget.AppCompat.Toolbar.Title"
/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
This method is also used to make font BOLD.
I just add my fonts to the font folder I made in res along with the other fonts, then I made a custom XML file for the bar.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:id="#+id/action_bar_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textSize="30sp"
android:fontFamily="#font/yourfont"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/></LinearLayout>
Then, I added this to my MainActivity
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(R.layout.your_xml_file);
Both OTF and TTF works
The best answer really is the best. If you want to increase (adjust the font size) and change the colour then update your style like this:
<style name="NexaBoldToolbar">
<item name="android:fontFamily">#font/nexabold</item>
<item name="android:textSize">18sp</item>
<item name="android:textColor">#color/fullWhite</item>
</style>
Related
I want to change the size of text in Toolbar. Because in my application, Toolbar text has different sizes both for landscape and portrait mode.
Is it possible to change the text size of text in Toolbar?
Use app:titleTextAppearance:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:id="#+id/toolbar"
app:titleTextAppearance="#style/Toolbar.TitleText" />
and override the default title size in a custom style:
<style name="Toolbar.TitleText" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
<item name="android:textSize">18sp</item>
</style>
Result:
for example this your toolbar
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:gravity="center"
android:minHeight="?attr/actionBarSize"
app:theme="#style/ThemeOverlay.AppCompat.ActionBar" />
you can simple add this view this
app:titleTextAppearance="#style/yourstyle"
style.xml
<style name="yourstyle" parent="#style/Base.TextAppearance.AppCompat.Title">
<item name="android:textSize">20sp</item>
</style>
like that...
You can add a TextView to your toolbar and customize as you want:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textSize="25sp"/>
</android.support.v7.widget.Toolbar>
[Optional steps 1 & 2)
Place your font file in "Project/app/src/main/assets/fonts/" folder.
Then create a Typeface object from the font file as:
Typeface typeFace = Typeface.createFromAsset(getAssets(), "fonts/font_name.ttf");
Now, get the child item (toolbar title textview) from the existing toolbar, the default toolbar and pass the typeface object we created above.
((TextView) toolbar.getChildAt(0)).setTypeface(typeFace); // set custom typeface - font
Finally, change the text size of toolbar's title text view
((TextView) toolbar.getChildAt(0)).setTextSize(30); // set title font size
Try this in onCreate:
android.support.v7.app.ActionBar actionbar = getSupportActionBar();
TextView textview = new TextView(MainActivity.this);
RelativeLayout.LayoutParams layoutparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
textview.setLayoutParams(layoutparams);
textview.setText(getString(R.string.your_string));
textview.setTextColor(Color.WHITE);
textview.setTextSize(18);
actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionbar.setCustomView(textview);
This is taken from Android-Examples.com.
I have a bottom navigation view with 3 items. I want to only have centered text for each tab, and would therefore like to fully remove icons (not only make them transparent).
How can I remove Icons and center the titles?
This is what I have:
This is what I want:
My code: (Prefer solution in XML)
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_alignParentBottom="true">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/navigationBar"
android:background="#color/navigation"
app:theme="#style/BottomNavigationTheme"
app:menu="#menu/bottom_navigation_menu"
android:minHeight="#dimen/abc_action_bar_default_height_material">
</com.google.android.material.bottomnavigation.BottomNavigationView>
</RelativeLayout>
</merge>
bottom_navigation_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/ic_home"
android:title="#string/home">
</item>
<item
android:id="#+id/ic_today"
android:title="#string/today">
</item>
<item
android:id="#+id/ic_you"
android:title="#string/you">
</item>
</menu>
The easiest way is to just use
android:paddingBottom="16dp" //(any dp you want)
android:clipToPadding="false"
This works, for me
private int baselineHeight = 0;
private void removeIcons(BottomNavigationView view) {
BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
for (int i = 0; i < menuView.getChildCount(); i++) {
BottomNavigationItemView itemView = (BottomNavigationItemView) (menuView.getChildAt(i));
BaselineLayout baseline = (BaselineLayout) itemView.getChildAt(1);
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) baseline.getLayoutParams();
baselineHeight = baselineHeight > 0 ? baselineHeight : (menuView.getHeight() + baseline.getHeight()) / 2;
layoutParams.height = baselineHeight;
baseline.setLayoutParams(layoutParams);
}
}
just call it in onCreate() in your Activity and pass your BottomNavigationView as parameter.
If you don't want to clutter your Activities or Fragments with excess code and you want this present in your layout XML you can create a custom View that extends BottomNavigationView and call this function in onLayout() override.
Add fix height to your bottom sheet and set bottom padding. Works for me.
android:layout_height="35dp"
android:paddingBottom="20dp"
android:clipToPadding="false"
you can use this property of bottom navigationview to hide the text and it will automatically centered your icons and i think ou do not use the minHeight property sir.
app:labelVisibilityMode="unlabeled"
I restarted to develop now after years so there were many changes.
Now I'm trying to modify the AppBar (Toolbar), for the activity. (I also see the CoordinatorLayout, but i don't know what differences have with the Linear and Relative).
So in the MainActivity.class i have:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
TextView mTitle = (TextView)
toolbar.findViewById(R.id.toolbar_title);
setSupportActionBar(toolbar);
mTitle.setText(toolbar.getTitle());
getSupportActionBar().setDisplayShowTitleEnabled(false);
mTitle.setTextColor(Color.parseColor("#ff0000"));
and in activity_main.xml:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title"
android:layout_gravity="center"
android:id="#+id/toolbar_title" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
And it works fine, the color and the title (or it seems).
Then i also created two other activity (that i open from a button (i will explain one...they made similar problems):
SecondActivity.class:
public class ActivityMappa extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mappa);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarmappa);
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbarmappa_title);
setSupportActionBar(toolbar);
mTitle.setText(toolbar.getTitle());
getSupportActionBar().setDisplayShowTitleEnabled(false);
mTitle.setTextColor(Color.parseColor("#ff0000"));
toolbar.setBackgroundColor(Color.parseColor("#fafad2"));
}
and activity_second.xml:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ActivityMappa">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbarmappa"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_mappa"
android:layout_gravity="center"
android:id="#+id/toolbarmappa_title" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
</android.support.constraint.ConstraintLayout>
In this second activity, the toolbar color it's right, but the text not change when i run the app (in the android studio preview it was changed), and the text it is in the #string/title_mappa and it exists.
So why the text does not change? The code is the same.
An other thing, when i add stuff in the content_main.xml, the position start under the Toolbar, and if i set the margin/padding, it starts from it, but from the other 2 activity, when i add other stuffs (like imageview), they starts from the app start at the top, above the toolbar, why?
Thank you so much for the help.
Your issue is that you are setting the text of toolbarmappa_title to the title of toolbarmappa but the title of toolbarmappa is null therefore you are setting the text to null. You need to set the text with a predefined string or set the title of toolbarmappa before you get the title.
It would be something like this:
public class ActivityMappa extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mappa);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarmappa);
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbarmappa_title);
setSupportActionBar(toolbar);
toolbar.setTitle("insert title here");
mTitle.setText(toolbar.getTitle());
getSupportActionBar().setDisplayShowTitleEnabled(false);
mTitle.setTextColor(Color.parseColor("#ff0000"));
toolbar.setBackgroundColor(Color.parseColor("#fafad2"));
}
Also, general pratice to change the text of a toolbar is not to add a TextView. You can remove the TextView and call toolbar.setTitle("insert text here"); and that eliminates the need of the TextView.
I've been trying to set the title of my toolbar to start at the top of the toolbar, but while the I have set android:gravity="top" the text still start at the center (vertical) of the toolbar. I think this could have something to do with using a custom layout for the toolbar, but I use it because I need it, so I hope someone knows a way to set the text to start at the top.
Here is layout my toolbar is in:
<?xml version="1.0" encoding="utf-8"?>
<!--suppress AndroidDomInspection -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.percent.PercentRelativeLayout
android:id="#+id/layoutProjectOverview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar2"
android:title="#string/menuLabel1a"
app:layout_widthPercent="37%"
app:layout_heightPercent="26%"
android:gravity="top"
app:titleTextAppearance="#style/toolbarTitleText"
android:background="#drawable/toolbar_basic"
android:theme="#style/AppTheme.AppBarOverlay"
app:popupTheme="#style/AppTheme.PopupOverlay" />
<android.support.percent.PercentRelativeLayout
android:id="#+id/layoutObjectNav"
app:layout_widthPercent="63%"
app:layout_aspectRatio="400%"
android:layout_toRightOf="#id/toolbar2"
android:layout_toEndOf="#id/toolbar2"
android:background="#drawable/border_basic">
<!-- Some irrelevant xml code -->
</android.support.percent.PercentRelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/layoutObjectNav">
<!-- Some more irrelevant xml code -->
</RelativeLayout>
</android.support.percent.PercentRelativeLayout>
<!-- Some more irrelevant xml code -->
</RelativeLayout>
Here is the custom toolbar layout code:
<?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="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/action_bar_title"
android:textSize="20sp"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:gravity="top"
android:padding="5dp"
android:ellipsize="end"
android:maxLines="3"/>
</LinearLayout>
And here is the code where I set the custom layout (and some other stuff):
#Override
protected void onResume() {
super.onResume();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar2);
setSupportActionBar(toolbar);
currentProject = getIntent().getParcelableExtra("Project");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
getSupportActionBar().setTitle("");
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(R.layout.layout_toolbar);
((TextView) findViewById(R.id.action_bar_title)).setText(currentProject.getTitle());
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = (int)( size.x * 0.37);
int height = Math.round(size.x * 0.63f * 0.25f);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width,height);
toolbar.setLayoutParams(params);
} else {
getSupportActionBar().setTitle(currentProject.getTitle());
}
// Some irrelevant java code
}
Does someone know what I'm doing wrong?
EDIT:
Here is the TextAppearance Style I use for the toolbar:
<style name="toolbarTitleText">
<item name="android:textSize">20sp</item>
<item name="android:maxLines">3</item>
</style>
add a textView in your toolbar
like this:
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</android.support.v7.widget.Toolbar>
Try to do the following :
1- Disable the title by using getSupportActionBar().setDisplayShowTitleEnabled(false);
2- Add a textView under your toolbar as your toolbar title :
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"/>
</android.support.v7.widget.Toolbar>
I've made the next layout xml code which I call it layout_one -
<?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="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/layout_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:layout_marginTop="50dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="Testing 1"
android:textSize="20sp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/info_layout"
android:visibility="gone">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
As you can see there are two layout in it - one with textview and button and one with textview.
The layout with the only textview - is gone, And i want by a click on the button, from the visible layout, it will be shown.
Now at the activity i wrote the next code -
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
LinearLayout newView0 = (LinearLayout)View.inflate(this, R.layout.layout_one, null);
LinearLayout newView1 = (LinearLayout)View.inflate(this, R.layout.layout_one, null);
LinearLayout newView2 = (LinearLayout)View.inflate(this, R.layout.layout_one, null);
LinearLayout newView3 = (LinearLayout)View.inflate(this, R.layout.layout_one, null);
ll.addView(newView0);
ll.addView(newView1);
ll.addView(newView2);
ll.addView(newView3);
setContentView(sv);
newView1.findViewById(R.id.layout_one).setBackgroundColor(0xff0000ff);
TextView tv3 = (TextView) newView3.findViewById(R.id.textView1);
tv3.setText("Suprise Suprise");
infoLay = (View) findViewById(R.id.info_layout);
ll.addView(newView3);
}
Now there is something I don't understad - How could I set an on click listener to the button that will know which layout to show?
Tanks for any kind of help
Actually, there are three linear layouts, which seems redundant.
You can set the onclick listener by checking if textview of each layout is empty or not.
First off, have a look at this http://developer.android.com/training/improving-layouts/reusing-layouts.html. Use that <include> tag instead of dynamically creating all these layouts in your Activity class. Define your ScrollView, and all your LinearLayouts in your test_layout.xml file.
Then, in your Activity, all you have to do is get a reference to those views that's you've defined using findViewById.
Once you've got that working we can address your question as follows:
setContentView(R.layout.test_layout);
LinearLayout layout1 = (LinearLayout)findViewById(R.id.layout1);
LinearLayout layout2 = (LinearLayout)findViewById(R.id.layout2);
LinearLayout layout3 = (LinearLayout)findViewById(R.id.layout3);
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View view) {
((View)view.getParent().getParent()).findViewById(R.id.textView2).setVisibility(View.VISIBLE);
}
});
layout1.findViewById(R.id.button1).setOnClickListener(listener);
layout2.findViewById(R.id.button1).setOnClickListener(listener);
layout3.findViewById(R.id.button1).setOnClickListener(listener);
I'd also add that it seems like you're trying to create a list here, in which case you should use a ListView instead of many LinearLayouts.