I put a font in the assets folder I created and have implemented the following code in my MainActivity:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
// Code to change Welcome text to 'BankGothic Bold' font
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "BankGothic Bold.ttf");
TextView myTextview = (TextView)findViewById(R.id.WelcomeTextView);
myTextview.setTypeface(myTypeface);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
// Code to display Home Fragment on App Launch Page
HomeFragment homeFragment = new HomeFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(
R.id.relativelayout_for_fragment,
homeFragment,
homeFragment.getTag()
).commit();
}
I have created the ID in the XML file too so that is fine.
If I include this code my app crashes on launch:
// Code to change Welcome text to 'BankGothic Bold' font
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "BankGothic Bold.ttf");
TextView myTextview = (TextView)findViewById(R.id.WelcomeTextView);
myTextview.setTypeface(myTypeface);
If I remove it my app works fine with the standard font. Does anyone know why?
Thanks for your time.
logcat
error when you run app and it crashes? – Akram