Android

ConstraintLayout

        Build a Responsive UI with ConstraintLayout  – Android Develpoers https://developer.android.com/training/constraint-layout   Constraint Layout – Part1. 만능 레이아웃 https://www.charlezz.com/?p=669   Constraint Layout – Part2. 뷰의 배치를 돕는 가상 오브젝트 https://www.charlezz.com/?p=691   안드로이드 앱 만들기 #33 Constraint Layout https://www.youtube.com/watch?v=2zBgR3toNwI    

Read More
Android

뒤로가기 두번 눌러 앱 종료

    public class MainActivity extends AppCompatActivity { private long backBtnTime = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public void onBackPressed() { long curTime = System.currentTimeMillis(); long gapTime = curTime – backBtnTime; if(0 <= gapTime && 2000 >= gapTime) { super.onBackPressed(); } else { backBtnTime = curTime; Toast.makeText(this, “한번 더 누르면 종료됩니다.”,Toast.LENGTH_SHORT).show(); } } }     https://www.youtube.com/watch?v=3jiQGrjOgMM   https://github.com/hongdroid94/23_BackButton  

Read More
Android

Spinner

    MainActivity.java   private Spinner spinner; private TextView tv_result; spinner = (Spinner)findViewById(R.id.spinner); tv_result = (TextView)findViewById(R.id.tv_result); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { tv_result.setText(parent.getItemAtPosition(position).toString()); } @Override public void onNothingSelected(AdapterView<?> adapterView) { } });     values/arrays.xml     <resources> <string-array name=”string_array”> <item>월요일</item> <item>화요일</item> <item>수요일</item> <item>목요일</item> </string-array> </resources>       activity_main.xml     <Spinner android:layout_width=”150dp” android:layout_height=”40dp” android:id=”@+id/spinner” android:entries=”@array/string_array” /> <TextView android:id=”@+id/tv_result” android:layout_width=”wrap_content” android:layout_height=”wrap_content” />       arrays.xml   <?xml version=”1.0″ encoding=”utf-8″?> <resources> <string-array name=”string_array”> <item>월요일</item> <item>화요일</item> <item>@string/wednesday</item> <item>@string/thursday</item> </string-array> <integer-array name=”integer_array”>…

Read More
Android

MP3 음악 재생

    MediaPlayer mediaPlayer;     플레이 버튼 눌렀을 때   mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.music); mediaPlayer.start();     정지 버튼 눌렀을 때   if(mediaPlayer.isPlaying()) { mediaPlayer.stop(); mediaPlayer.reset();     onDestroy() 일 때   if(mediaPlayer != null) { mediaPlayer.release(); mediaPlayer = null; }       안드로이드 앱 만들기 #22 (음악재생 MP3) – 홍드로이드 https://www.youtube.com/watch?v=-jTbUeTSAYU    

Read More
Android

FCM 푸시 알림

    manifest 에 서비스 등록 <service android:name=”.MyFireBaseMessagingService”> <intent-filter> <action android:name=”com.google.firebase.MESSAGING_EVENT” /> </intent-filter> </service>   인터넷 권한 허용 <uses–permission android:name=“android.permission.INTERNET”></uses–permission>     MyFireBaseMessagingService.java   public class MyFireBaseMessagingService extends FirebaseMessagingService { @Override public void onNewToken(String token) { Log.d(“FCM Log”, “Refreshed token: ” + token); } @Override public void onMessageReceived(RemoteMessage remoteMessage) { if (remoteMessage.getNotification() != null) { Log.d(“FCM Log”, “알림 메시지: ” + remoteMessage.getNotification().getBody()); String messageBody = remoteMessage.getNotification().getBody(); String messageTitle = remoteMessage.getNotification().getTitle(); Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); String channelId = “Channel ID”; Uri…

Read More
Android

Fragment

  Fragment 간 데이터 전송 (setArguments() 와 getArguments() 이용)   MainActivity.java (onCreate) FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); Fragment1 fragment1 = new Fragment1(); transaction.replace(R.id.frameLayout, fragment1); transaction.commit(); // 저장   activity_main.xml <ConstraintLayout> <FrameLayout> // id : frameLayout </ConstraintLayout>   fragment_1.xml, fragment_2.xml textview 와 button 한개씩 만든다.   Fragment1.java, Fragment2.java (onCreateView)   View view = inflater.inflate(R.layout.frag1, container, false); TextView textView = view.findViewById(R.id.textView); Button button = view.findViewById(R.id.button); if (getArguments() != null) { String result = getArguments().getString(“fragSend”); textView.setText(result); } button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Bundle bundle = new Bundle(); bundle.putString(“fragSend”,”프레그먼트 데이터 전달…

Read More
Android

      public class MainActivity extends AppCompatActivity { Button btn_start, btn_stop; Thread thread; boolean isThread = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 스레드 시작 btn_start = (Button)findViewById(R.id.btn_start); btn_start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { isThread = true; thread = new Thread() { public void run() { while(isThread) { try { sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } handler.sendEmptyMessage(0); } } }; thread.start(); } }); // 스레드 종료 btn_stop = (Button)findViewById(R.id.btn_stop); btn_stop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { isThread = false;…

Read More
Android

Navigation Menu Custom

    activity_main.xml   <androidx.drawerlayout.widget.DrawerLayout 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:id=”@+id/drawer_layout” android:layout_width=”match_parent” android:layout_height=”match_parent” tools:context=”.MainActivity”> <LinearLayout android:layout_width=”match_parent” android:layout_height=”match_parent”> <Button android:id=”@+id/btn_open” android:layout_width=”match_parent” android:layout_height=”wrap_content” android:text=”Open”/> </LinearLayout> <include layout=”@layout/activity_drawer” /> </androidx.drawerlayout.widget.DrawerLayout>       activity_drawer.xml   <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”240dp” android:layout_height=”match_parent” android:layout_gravity=”start” android:background=”#a8a7ff” android:id=”@+id/drawer” android:orientation=”vertical”> <Button android:id=”@+id/btn_close” android:layout_width=”match_parent” android:layout_height=”wrap_content” android:layout_margin=”10dp” android:text=”Close”/> </LinearLayout>       public class MainActivity extends AppCompatActivity { private DrawerLayout drawerLayout; private View drawerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout); drawerView = (View)findViewById(R.id.drawer); Button btn_open = (Button)findViewById(R.id.btn_open); btn_open.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { drawerLayout.openDrawer(drawerView); } });…

Read More
Android

Dialogs (AlertDialog, DialogFragment)

  # Dialogs (android developers) https://developer.android.com/guide/topics/ui/dialogs#java   # AlertDialog (멈춤보단 천천히라도) https://webnautes.tistory.com/1094   예 아니오 다이얼로그 리스트 다이얼로그 입력 창 예제 다중 선택 다이얼로그 로그인 창 다이얼로그 DialogFragment (Activity에서 호출하는 경우) DialogFragment (Fragment 에서 호출하는 경우)         예 아니오 다이얼로그   AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this); dialog.setTitle(“타이틀”); dialog.setMessage(“콘텐트”); dialog.setPositiveButton(“예”, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(),”예를 선택했습니다.”,Toast.LENGTH_LONG).show(); } }); dialog.setNegativeButton(“아니오”, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(),”아니오를 선택했습니다.”,Toast.LENGTH_LONG).show(); } }); dialog.show();   <?xml version=”1.0″ encoding=”utf-8″?> <RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent”…

Read More
Android

SharedPreferences

  Shared 라는 xml 파일에 저장 (onDestroy에서 사용) SharedPreferences pref = getSharedPreferences(“Shared”, Activity.MODE_PRIVATE); SharedPreferences.Editor editor = pref.edit(); editor.putString(“myText”, value); editor.commit();   불러오기 (OnCreate 에서 사용) SharedPreferences pref = getSharedPreferences(“Shared”, Activity.MODE_PRIVATE); String myText = pref.getGetstring(“myText”, 0);   값 삭제 editor.remove(“myText”);  // myText 라는 키 값 삭제 editor.clear();  // 모두 삭제   // Fragment 는 onCreateView, onDestroyView   [안드로이드] SharedPreference 사용 방법 https://swalloow.tistory.com/59   안드로이드 SharedPreferences 사용법 http://zeany.net/24   홍드로이드 유튜브 (SharedPreferences) https://youtu.be/-2QPmS4OWos   홍드로이드 github https://github.com/hongdroid94  

Read More