3

I am trying to add two AndroidFragmentApplication in the single Activity. But only the last added fragment actually renders on the screen. Other fragment does not show anything. What do i need to do to make both views draw?

I was first trying to initialize views using AndroidApplication class. But that caused the app to crash. then, I added multiple fragments extending AndroidFragmentApplication. Now, the app does not crash, but only last added fragment draws on the screen.

This is where i am adding the fragments-

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">
    <fragment
        android:layout_width="80dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:id="@+id/alpha_fragment"
        android:name="com.sample.libgdx_final.AlphaFragment" />

    <fragment
        android:id="@+id/coin_fragment"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:name="com.sample.libgdx_final.CoinFragment"
        />
</FrameLayout>

This is my Fragment and ApplicationListener-

public class CoinFragment extends AndroidFragmentApplication implements AndroidFragmentApplication.Callbacks{
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return initializeForView(new CoinTranslateLibgdx());
    }
}

CoinTranslateLibgdx.java

class CoinTranslateLibgdx extends ApplicationAdapter {
    private SpriteBatch batch;
    private Texture texture;
    private Sprite sprite;
    private float deltaTime = 0f;
    private boolean increasing = true;

    @Override
    public void create() {
        batch = new SpriteBatch();
        texture = new Texture(Gdx.files.internal("token.png"));
        sprite = new Sprite(texture);
        sprite.setCenterX(Gdx.graphics.getWidth() / 2f);
        sprite.setCenterY(Gdx.graphics.getHeight());
    }
    @Override
    public void render() {
        Gdx.gl20.glClearColor(0, 0, 0, 0);
        batch.begin();
        if (deltaTime <= 1 && increasing) {
            deltaTime += Gdx.graphics.getDeltaTime() * 1f;
        } else if (deltaTime >= 0.09) {
            increasing = false;
            deltaTime -= Gdx.graphics.getDeltaTime() * 1f;
        } else {
            increasing = true;
        }
        batch.setColor(1, 1, 1, deltaTime);
        batch.draw(sprite, 0, 0);
        batch.end();
    }
    @Override
    public void dispose() {
        batch.dispose();
        texture.dispose();
    }
}

0 Answers0