1

I am new to java and I am developing a simple mediaplayer: I have problems with two image buttons: when i run the app the relative images are not displayed. This is my code:

JAVA:

package lukes.mediaplayer;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.app.Activity;

public class alpha extends Activity implements View.OnClickListener {
    SeekBar seekBar_alpha;
    ImageButton btPlay_alpha;
    ImageButton btStop_alpha;
    MediaPlayer mp;
    Handler seekHandler = new Handler();
    ImageView display;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alpha);

        display = (ImageView) findViewById(R.id.imagetop);
        display.setOnClickListener(this);

        getInit();
        seekUpdation();
    }

    public void getInit() {
        seekBar_alpha = (SeekBar) findViewById(R.id.seekBar_alpha);
        btPlay_alpha = (ImageButton) findViewById(R.id.btPlay_alpha);
        btStop_alpha = (ImageButton) findViewById(R.id.btStop_alpha);
        btPlay_alpha.setOnClickListener(this);
        btStop_alpha.setOnClickListener(this);
        mp = MediaPlayer.create(this, R.raw.alpha_audio);
        seekBar_alpha.setMax(mp.getDuration());
    }

    Runnable run = new Runnable() {

        @Override
        public void run() {
            seekUpdation();
        }
    };

    public void seekUpdation() {
        seekBar_alpha.setProgress(mp.getCurrentPosition());
        seekHandler.postDelayed(run, 1000);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.imagetop:
                Intent intent = new Intent (alpha.this, alpha_img.class);
                startActivity(intent);

            case R.id.btPlay_alpha:
                mp.start();
                break;

            case R.id.btStop_alpha:
                mp.pause();
        }
    }

    @Override
    protected void onPause(){
        super.onPause();
        if (mp!=null && mp.isPlaying()){
            mp.pause();
        }
    }

}

The "btPlay_alpha" and "btStop_alpha" image buttons does not display their image. Can you help me to fix that? Thank you!


Hey guys, I add the XML code:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:fitsSystemWindows="true"
    tools:context=".alpha">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/main.appbar"
        android:layout_width="match_parent"
        android:layout_height="350dp"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/main.collapsing"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:id="@+id/main.toolbar"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

            <ImageButton
                android:id="@+id/imagetop"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/black"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:src="@drawable/alpha"
                app:layout_collapseMode="parallax" />

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="16dp"
                app:cardElevation="5dp"
                app:cardUseCompatPadding="true">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/textView3"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:fontFamily="sans-serif-condensed"
                        android:lineSpacingExtra="8dp"
                        android:padding="16dp"
                        android:text="@string/alpha_title"
                        android:textColor="@color/colorPrimary"
                        android:textSize="24sp"
                        android:textStyle="bold" />

                    <RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:paddingLeft="16dp"
                        android:paddingRight="16dp">


                        <ImageButton
                            android:id="@+id/btStop_alpha"
                            android:layout_width="60dp"
                            android:layout_height="55dp"
                            android:layout_alignParentLeft="true"
                            android:layout_alignParentStart="true"
                            android:layout_alignParentTop="true"
                            android:backgroundTint="@color/lightRed"
                            android:elevation="6dp"
                            android:padding="16dp"
                            app:srcCompat="@android:drawable/ic_media_pause" />

                        <ImageButton
                            android:id="@+id/btPlay_alpha"
                            android:layout_width="60dp"
                            android:layout_height="55dp"
                            android:layout_alignParentTop="true"
                            android:layout_gravity="center"
                            android:layout_toEndOf="@+id/btStop_alpha"
                            android:layout_toRightOf="@+id/btStop_alpha"
                            android:backgroundTint="@color/lightRed"
                            android:elevation="6dp"
                            app:srcCompat="@android:drawable/ic_media_play" />

                        <SeekBar
                            android:id="@+id/seekBar_alpha"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_centerVertical="true"
                            android:layout_toEndOf="@+id/btPlay_alpha"
                            android:layout_toRightOf="@+id/btPlay_alpha"
                            android:max="100" />

                    </RelativeLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <TextView
                            android:id="@+id/textView"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:fontFamily="serif"
                            android:lineSpacingExtra="8dp"
                            android:padding="16dp"
                            android:text="@string/alpha_text"
                            android:textSize="18sp" />

                    </LinearLayout>
                </LinearLayout>
            </android.support.v7.widget.CardView>
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

Thank you for your support!

2
  • put your xml file Commented Nov 26, 2017 at 0:12
  • You are not setting your images against these Variables anywhere in the code. Show us the XML .... Commented Nov 26, 2017 at 0:18

1 Answer 1

2

I see youre extending Activity Instead of AppCompatActivity. In your xml, you are setting attribute srcCompat; make sure As of Android Support Library 23.3.0, support vector drawables can only be loaded via app:srcCompat .

you need to add vectorDrawables.useSupportLibrary = true to your build.gradle file

// Gradle Plugin 2.0+  
android {  
 defaultConfig {  
   vectorDrawables.useSupportLibrary = true  
  }  
}  
Sign up to request clarification or add additional context in comments.

2 Comments

Hey! You are right! Now it works perfectly. I thank you all. Have a nice day!
I guess "Android Support Library 23.3.0" is only available in Android Studio because on my Eclipse+ADT setup, "Android SDK Manager" would only show "Android Support Library 22.1.1". Could stackoverflow.com/a/40358345/2597758 be related?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.