How to use Custom Font with Youtube Player? - java

I have successfully integrated YouTube Player in my app but I am not able to use a custom font to other UI elements. On other Activities, the font is working.
public class PlayerActivity extends YouTubeBaseActivity implements CustomAdapter.Listner {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
}
}

I found the solution.
By using YoutubePlayerSupportFragment instead of YoutubePlayerView I can simply use AppCompatActivity and hence use Custom fonts in XML.

Related

Is there a way I can use two classes or more for one Activity in android studio?

Is there a way I can use two classes or more for one Activity in android studio?
I used this Test code but this App crashes:
Note : This is for learning purposes so that it can be used to split up huge classes into sub classes
//Main Activity Class
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Test ob=new Test ();
ob.test();
}
}
// Test Class
public class Test extends MainActivity {
public void test()
{
TextView t=findViewById(R.id.h);
t.setText("Miaooo");
}
}
What you can do is to pass the activity with as the parameter of constructor and use that reference to call "findViewbyId()"
public class Test{
public void test(Activity activity)
{
TextView t=activity.findViewById(R.id.h);
t.setText("Miaooo");
}
while in your main activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Test ob=new Test (this);
ob.test();
}
If you want to your "Test" class to be extended by an activity than you should use activity creation wizard to setup all the stuff in manifest and .xml. It would be quite long process to do it manually.
When you write class Test extends MainActivity it means that Test should be activity too and you have to write setContentView(R.layout.your_layout); to set the view layer for your activity. Also, Test should be registered as Activity in your manifest file. Anyway if you want to change the text of the TextView why you want to create a new activity?
place your code in the activity main.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
test();
}
public void test()
{
TextView t=findViewById(R.id.h);
t.setText("Miaooo");
}

Is it possible to include viewBinding in every project automatically?

It's quite long to change the code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
to:
public class MainActivity extends AppCompatActivity {
ActivityMainBinding mainBinding;
#Override
protected void onCreate(Bundle savedInstanceState) {
mainBinding = ActivityMainBinding.inflate(getLayoutInflater());
super.onCreate(savedInstanceState);
setContentView(mainBinding.getRoot());
}
}
every time I create a new project or an activity. Is it possible to automate this process?
You can not automate the process since we have to provide the layoutId ourself. Activity is not gonna bind to a layout automatically. What you can do is Create a BaseActivity and inherit it from all your Activites. Below is a template with binding .
public abstract class BaseActivity<B extends ViewDataBinding> extends AppCompatActivity {
protected abstract int getContentViewId();
protected B binding;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, getContentViewId())
}
}
class MainActivity extends BaseActivity<ActivityMainBinding> {
#Override
public int getContentViewId() {
return R.layout.activity_main;
}
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// now you can directly access binding here
}
}
This is just for Binding you can Also add some reusable method in BaseActivity and use them in Any Activity without writing them again. and super.onCreate(savedInstanceState) should be first line for call .
No, I think it's not possible but you can make Live Template to get it frequently.
Or Simple that you can make a copy of that folder as a template. Whenever you need to create a new project it will be easy to copy/paste and just change the name of the project.
Thank you.

How to correct add a listener to a Fragment

I got two Fragments (basic-setup) with a FragmentPagerAdapter for use in a TabLayout.
tabLayout.setupWithViewPager(myViewPagerWithAdapter);
My Adapter takes an array of Fragments and titles as argument (also the FragmentManger for the super call).
MyViewPagerAdapter adapter = new MyViewPagerAdapter(getSupportFragmentManager() ,fragments, titles);
My Fragments (indeed 2) got a WebView in their xml file. To use the WebViews outside the fragments (it's kinda odd to code the control of the WebViews inside fragments, because the MainActivity is mainly involved with my user interaction). To know when my WebViews in the fragments were inflated, I added a simple interface, called OnFragmentCreatedListener, which is triggered in public void onActivityCreated(Bundle savedInstanceState) of the fragments. I add the listener through following way:
private OnFragmentCreatedListener listener;
...
public void addOnFragmentCreatedListener(OnFragmentCreatedListener listener) {
this.listener = listener;
}
In my MainActivity I add this listener like this:
public class MainActivity extends AppCompatActivity implements OnFragmentCreatedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
...
MyFragment fragment = MyFragment.newInstance();
...
fragment.addOnFragmentCreatedListener(this);
...
}
#Override
public void onFragmentCreatedListener() {
// Do my stuff
}
Now when the screen rotates and the activity is restarted, the listener in the fragment stays null (wasn't initialized). Now I don't know what to do, because I don't understand this behavior. The code should do the same like in the first protected void onCreate(Bundle savedInstanceState) of the MainActivity. It's adding the listener but it stays null (like wtf?).
Thank you for spending your time!

Extending a second GameView in Android

I'm making an application on Android which is to contain 2 games. I've done this but I've come across a problem that the two buttons that are supposed to extend to different GameViews are extending to the same GameViews. The classes both have different names and I've tried changing some of the content to specifically refer to the BallGameView class however it causes the program not to compile. I've had a look around to see if there's anything on extending to 2 separate GameViews in a single application but not came across anything so far.
EDIT: For clarity, the problem is that the buttons that should open different games are opening the same game. The game is compiling but not as I want it to. I'll post the code that's referring to the different games below.
public class BallGameActivity extends Activity {
GameView GV;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ballgame);
GV = new GameView(this);
setContentView(GV);
To refer to Ball Game.
public class BallSplash extends Activity implements View.OnClickListener {
Button playBallButton;
Button guideButton;
Intent ballIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ball_splash);
playBallButton = (Button)findViewById(R.id.startBallGame);
guideButton = (Button)findViewById(R.id.guideButton);
playBallButton.setOnClickListener(this);
guideButton.setOnClickListener(this);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.startBallGame:
ballIntent = new Intent(this,BallGameActivity.class);
startActivity(ballIntent);
break;
case R.id.guideButton:
ballIntent = new Intent(this,Guide.class);
startActivity(ballIntent);
break;
}
}
To refer to Sprite Game
public class Splash extends Activity implements View.OnClickListener {
Button playButton;
Button instructionButton;
Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
playButton = (Button)findViewById(R.id.toPlay);
instructionButton = (Button)findViewById(R.id.toInstructions);
playButton.setOnClickListener(this);
instructionButton.setOnClickListener(this);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.toPlay:
intent = new Intent(this,GameActivity.class);
startActivity(intent);
break;
case R.id.toInstructions:
intent = new Intent(this,InstructionActivity.class);
startActivity(intent);
break;
}
}
Maybe the problem exists because you call setContentView() twice. You should remove the wrong one.
The correct way to do it is to call that class that you want to use as the view
BallGameView GV = new BallGameView(this);
setContentView(GV);
in your current code you are instantiating GameView but that isn't what you want for this Activity. You need to instantiate BallGameView GV instead. Since you are using
GV = new GameView(this);
setContentView();
in both Activities, you are seeing the same thing no matter which Activity you start. So the problem was never with starting an activity or extending anything.
Also, you can remove
setContentView(R.layout.activity_ballgame);
That line will set the Activity to use that layout. But since you are calling setContentView() again and using the class which extends SurfaceView then it will set the Activity to use that as the layout instead, making the above line useless.

App crashes when setting layout background

I'm trying to set a drawable resource as background for my main relative layout via java, but whenever I do it, my app crashes.
Here is the part of the code which doesn't work fine:
public class GameActivity extends Activity {
RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout = (RelativeLayout) findViewById(R.layout.activity_game);
setContentView(R.layout.activity_game);
layout.setBackgroundResource(R.drawable.image);
}
}
Any idea?
Thanks in advance
Call findViewById() only after setContentView() and supply an identifier in R.id (and not R.layout) so that it can return something other than null.
Remember,you are trying to find a view by id but what you are doing is actually trying to inflate a layout the wrong way.Change the R.layout.activity_game to R.id.activity_game and make sure the relative layout is givent the
android:id = "#+id/activity_game"
The full code should be
public class GameActivity extends Activity {
RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
layout = (RelativeLayout) findViewById(R.id.activity_game);
layout.setBackgroundResource(R.drawable.image);
}
}
Hope it helps.Happy coding.

Categories