When you press the button, you can run the code below.

 

startActivity(new Intent(Intent.ACTION_VIEW)
    .setData(Uri.parse("https://your_youtube_url_here")) // edit this url
    .setPackage("com.google.android.youtube"));	// do not edit

 

Edit the url in the second line

The setPackage on the third line doesn't need to be modified.

It is simpler when you have a lot of image buttons, and you don't want to write xml-s for every button.

Java Version:

public static void buttonEffect(View button){
    button.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    v.getBackground().setColorFilter(0xe0f47521,PorterDuff.Mode.SRC_ATOP);
                    v.invalidate();
                    break;
                }
                case MotionEvent.ACTION_UP: {
                    v.getBackground().clearColorFilter();
                    v.invalidate();
                    break;
                }
            }
            return false;
        }
    });
}
Kotlin Version:
fun buttonEffect(button: View) {
    button.setOnTouchListener { v, event ->
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                v.background.setColorFilter(-0x1f0b8adf, PorterDuff.Mode.SRC_ATOP)
                v.invalidate()
            }
            MotionEvent.ACTION_UP -> {
                v.background.clearColorFilter()
                v.invalidate()
            }
        }
        false
    }
}

안드로이드 LinearLayout 오른쪽 정렬하기, 왼쪽 정렬하기

layout_gravity="left" 왼쪽정렬

layout_gravity ="right" 오른쪽 정렬

다음 예제를 따라해보시면 됩니다. 

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
/>

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:src="@drawable/ic_menu_image" />

 

 

안드로이드 사운드 재생 방법입니다.

1. SoundPool
2. MediaPlayer

차이점이라면

SoundPool  알림사운드,게임효과등 짧은 사운드클립에 적합하고
MediaPlayer  노래와 같이 더  사운드파일을 재생할 때 적합합니다.

 

 

MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_dingdong); mediaPlayer.start();

MediaPlayer 는 사운드 외에 동영상도 재생합니다.

 

상황에 맞게 SoundPool 과 MediaPlayer 를 사용하시면 됩니다.

 

안드로이드 button.setBackground

setBackgroundDrawable(ContextCompat.getDrawable(Context, R.drawable.Drawable파일));

예제

  button.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.btn_blue));


 

안드로이드 화면 가로 세로 고정하기

가로 landscape

세로 portrait

 

두가지 방법이 있습니다. 

첫번째 방법 Android Manifest 파일에서 

<activity android:name="com.example.appname.ActivityName"

            android:theme="@style/AppTheme"

            android:screenOrientation="portrait" //화면을 portrait(세로) 화면으로 고정하고 싶은 경우

            android:screenOrientation="landscape"> //화면을 landscape(가로) 화면으로 고정하고 싶은 경우

        </activity>

 

두번째 방법 액티비티 onCreate()메소드에서

@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
         
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        // 화면을 portrait(세로) 화면으로 고정하고 싶은 경우
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        // 화면을 landscape(가로) 화면으로 고정하고 싶은 경우
         
        setContentView(R.layout.main);
        // setContentView()가 호출되기 전에 setRequestedOrientation()이 호출되어야 함
    }





+ Recent posts