Created
June 16, 2017 14:49
-
-
Save Palatis/84a168c07beb7f4412cc9a4404309f3f to your computer and use it in GitHub Desktop.
CustomToolbar with custom Title/Subtitle and NavIcon views
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<declare-styleable name="CustomToolbar"> | |
<attr name="titleView" format="reference"/> | |
<attr name="subtitleView" format="reference"/> | |
<attr name="navigationView" format="reference"/> | |
</declare-styleable> | |
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomToolbar extends android.support.v7.widget.Toolbar { | |
private static final String TAG = "CustomToolbar"; | |
public CustomToolbar(Context context) { | |
this(context, null); | |
} | |
public CustomToolbar(Context context, @Nullable AttributeSet attrs) { | |
this(context, attrs, 0); | |
} | |
public CustomToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
final TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomToolbar, defStyleAttr, 0); | |
try { | |
mTitleViewId = a.getResourceId(R.styleable.CustomToolbar_titleView, 0); | |
mSubtitleViewId = a.getResourceId(R.styleable.CustomToolbar_subtitleView, 0); | |
mNavigationViewId = a.getResourceId(R.styleable.CustomToolbar_navigationView, 0); | |
} finally { | |
a.recycle(); | |
} | |
} | |
@IdRes | |
private int mTitleViewId; | |
@IdRes | |
private int mSubtitleViewId; | |
@IdRes | |
private int mNavigationViewId; | |
private Drawable mNavigationIcon; | |
@Override | |
public void setTitle(CharSequence title) { | |
final TextView titleView = (TextView) findViewById(mTitleViewId); | |
if (titleView != null) { | |
titleView.setText(title); | |
titleView.setVisibility(TextUtils.isEmpty(title) ? View.GONE : View.VISIBLE); | |
} else | |
super.setTitle(title); | |
} | |
@Override | |
public void setSubtitle(CharSequence subtitle) { | |
final TextView subtitleView = (TextView) findViewById(mSubtitleViewId); | |
if (subtitleView != null) { | |
subtitleView.setText(subtitle); | |
subtitleView.setVisibility(TextUtils.isEmpty(subtitle) ? View.GONE : View.VISIBLE); | |
} else | |
super.setSubtitle(subtitle); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public void setNavigationIcon(@Nullable Drawable icon) { | |
final TextView navView = (TextView) findViewById(mNavigationViewId); | |
if (navView != null) { | |
mNavigationIcon = icon; | |
// navView.setCompoundDrawables(mNavigationIcon, null, null, null); | |
navView.setVisibility(mNavigationIcon != null ? View.VISIBLE : View.GONE); | |
} else | |
super.setNavigationIcon(icon); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
@Nullable | |
public Drawable getNavigationIcon() { | |
final TextView navView = (TextView) findViewById(mNavigationViewId); | |
if (navView != null) { | |
if (navView.getVisibility() == View.GONE) | |
return null; | |
return mNavigationIcon; | |
} | |
return super.getNavigationIcon(); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
@Nullable | |
public CharSequence getNavigationContentDescription() { | |
final View navView = findViewById(mNavigationViewId); | |
if (navView != null) { | |
if (navView.getVisibility() == View.GONE) | |
return null; | |
return navView.getContentDescription(); | |
} | |
return super.getNavigationContentDescription(); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public void setNavigationOnClickListener(OnClickListener listener) { | |
final View navView = findViewById(mNavigationViewId); | |
if (navView != null) { | |
navView.setOnClickListener(listener); | |
} else | |
super.setNavigationOnClickListener(listener); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment