Android

Bottom Navigation View

          dependencies { implementation ‘com.google.android.material:material:1.1.0’ }     drawable 디렉토리에 Vector Asset 5개를 추가한다. res/menu 폴더를 만들고 bottom_menu.xml 파일을 만든다. <menu xmlns:android=”http://schemas.android.com/apk/res/android”> <item android:id=”@+id/action_airplane” android:enabled=”true” android:icon=”@drawable/ic_baseline_flight_24″ android:title=”비행기”/> <item android:id=”@+id/action_airport” android:enabled=”true” android:icon=”@drawable/ic_baseline_filter_drama_24″ android:title=”버스”/> <item android:id=”@+id/action_bt” android:enabled=”true” android:icon=”@drawable/ic_baseline_emoji_events_24″ android:title=”블루투스”/> <item android:id=”@+id/action_call” android:enabled=”true” android:icon=”@drawable/ic_baseline_drive_eta_24″ android:title=”전화”/> <item android:id=”@+id/action_run” android:enabled=”true” android:icon=”@drawable/ic_baseline_comment_24″ android:title=”사람”/> </menu>       frag1.xml 부터 frag5.xml 까지 5개를 만든다.   <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”match_parent” android:layout_height=”match_parent”> <TextView android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”1번화면 입니다.” android:textSize=”40sp”/> </LinearLayout>       activity_main.xml   <?xml version=”1.0″ encoding=”utf-8″?> <androidx.constraintlayout.widget.ConstraintLayout 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” tools:context=”.MainActivity”> <com.google.android.material.bottomnavigation.BottomNavigationView android:id=”@+id/bottomNavi”…

Read More
Android

구글 맵

  Google Developers Console 사이트에서 프로젝트를 만들고 API 키를 가져온다. https://console.developers.google.com/apis/dashboard     윈도우에서 SHA-1 인증서 지문 얻기 “C:\Program Files\Android\Android Studio\jre\bin\keytool” -list -v -keystore “%USERPROFILE%\.android\debug.keystore” -alias androiddebugkey -storepass android -keypass android     AndroidManifest.xml 에 메타데이터 추가 <application <meta-data android:name=”com.google.android.geo.API_KEY” android:value=”API 키 값” /> </application>     dependencies { implementation ‘com.google.android.gms:play-services-maps:17.0.0’ implementation ‘com.google.android.gms:play-services-location:17.0.0’ }       public class MainActivity extends AppCompatActivity implements OnMapReadyCallback { FragmentManager fragmentManager; SupportMapFragment mapFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fragmentManager = getSupportFragmentManager(); mapFragment = (SupportMapFragment) fragmentManager.findFragmentById(R.id.googleMap); mapFragment.getMapAsync(this); } @Override public void onMapReady(GoogleMap…

Read More
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