Android

Check Box

    chk_red.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { } }); chk_blue.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(((CompoundButton) view).isChecked()){ Log.d(TAG, “Checked”); } else { Log.d(TAG, “Un-Checked”); } } }); https://stackoverflow.com/questions/8386832/android-checkbox-listener         <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=”.CheckboxActivity”> <TextView android:id=”@+id/tv_result” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:layout_marginTop=”32dp” android:text=”결과 텍스트” android:textSize=”36sp” app:layout_constraintLeft_toLeftOf=”parent” app:layout_constraintRight_toRightOf=”parent” app:layout_constraintTop_toBottomOf=”@+id/btn_result” /> <CheckBox android:id=”@+id/chk_red” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:layout_marginTop=”100dp” android:text=”빨강” app:layout_constraintEnd_toEndOf=”parent” app:layout_constraintStart_toStartOf=”parent” app:layout_constraintTop_toTopOf=”parent” /> <CheckBox android:id=”@+id/chk_blue” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”파랑” app:layout_constraintEnd_toEndOf=”parent” app:layout_constraintHorizontal_bias=”0.498″ app:layout_constraintStart_toStartOf=”parent” app:layout_constraintTop_toBottomOf=”@+id/chk_red” /> <CheckBox android:id=”@+id/chk_green” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”초록” app:layout_constraintEnd_toEndOf=”parent” app:layout_constraintHorizontal_bias=”0.498″ app:layout_constraintStart_toStartOf=”parent” app:layout_constraintTop_toBottomOf=”@+id/chk_blue” />…

Read More
Android

Radio Button

          activity_main.xml   <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”> <RadioGroup android:id=”@+id/rg_gender” android:layout_width=”wrap_content” android:layout_height=”wrap_content” app:layout_constraintBottom_toBottomOf=”parent” app:layout_constraintEnd_toEndOf=”parent” app:layout_constraintStart_toStartOf=”parent” app:layout_constraintTop_toTopOf=”parent” > <RadioButton android:id=”@+id/rb_man” android:layout_width=”match_parent” android:layout_height=”wrap_content” android:text=”남자” /> <RadioButton android:id=”@+id/rb_woman” android:layout_width=”match_parent” android:layout_height=”wrap_content” android:text=”여자” /> </RadioGroup> <Button android:id=”@+id/btn_result” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:layout_marginTop=”50dp” android:text=”결과 버튼” app:layout_constraintEnd_toEndOf=”parent” app:layout_constraintStart_toStartOf=”parent” app:layout_constraintTop_toBottomOf=”@+id/rg_gender” /> </androidx.constraintlayout.widget.ConstraintLayout>       MainActivity.java   public class MainActivity extends AppCompatActivity { private RadioGroup rg_gender; private RadioButton rb_man, rb_woman; private Button btn_result; private String str_result; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rg_gender = findViewById(R.id.rg_gender); // 라디오 버튼들을 담고있는 그룹 rb_man =…

Read More
Android

ViewPager

    Migrate from ViewPager to ViewPager2 https://developer.android.com/training/animation/vp2-migration   ViewPager2 https://developer.android.com/reference/androidx/viewpager2/widget/ViewPager2   FragmentStateAdapter https://developer.android.com/reference/androidx/viewpager2/adapter/FragmentStateAdapter     Frag1.java, Frag2.java, Frag3.java   public class Frag1 extends Fragment { private View view; public static Fragment newInstance() { return new Frag1(); } @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment1, container, false); } }         ViewPagerAdapter.java   public class ViewPagerAdapter extends FragmentPagerAdapter { public ViewPagerAdapter(@NonNull FragmentManager fm, int behavior) { super(fm, behavior); } @NonNull @Override public Fragment getItem(int position) { switch (position) {…

Read More
Android

WebView

  <uses-permission android:name=“android.permission.INTERNET“/>   // https 가 아닌 http 는 보안 문제로 접근이 되지 않는다. 그걸 풀어준다. android:usesCleartextTraffic=”true” (AndroidManifest.xml -> application)     <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”match_parent” android:orientation=”vertical” android:layout_height=”match_parent”> <WebView android:id=”@+id/webView” android:layout_width=”match_parent” android:layout_height=”match_parent” /> </LinearLayout>     public class SettingsActivity extends AppCompatActivity { private WebView webView; private String url = “https://www.naver.com”; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.settings_activity); webView = (WebView) findViewById(R.id.webView); webView.getSettings().setJavaScriptEnabled(true); // 자바스크립트 기본값은 false webView.loadUrl(url); webView.setWebChromeClient(new WebChromeClient()); webView.setWebViewClient(new WebViewClientClass()); // setWebViewClient 가 주어지지 않으면 시스템 브라우저로 보여짐 } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if…

Read More
Android

VideoView

    <uses-permission android:name=”android.permission.INTERNET” /> android:screenOrientation=”landscape” android:theme=”@style/Theme.AppCompat.NoActionBar”     <RelativeLayout 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”> <VideoView android:id=”@+id/videoView” android:layout_alignParentTop=”true” android:layout_alignParentBottom=”true” android:layout_alignParentLeft=”true” android:layout_alignParentRight=”true” android:layout_width=”fill_parent” android:layout_height=”fill_parent”> </VideoView> </RelativeLayout>         public class MainActivity extends AppCompatActivity { private VideoView videoView; private MediaController mediaController; private String videoURL = “영상 주소”; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); videoView = findViewById(R.id.videoView); mediaController = new MediaController(this); mediaController.setAnchorView(videoView); Uri uri = Uri.parse(videoURL); videoView.setMediaController(mediaController); videoView.setVideoURI(uri); videoView.start(); } }           안드로이드 앱 만들기 #32 동영상 풀화면 재생 (VideoView) https://www.youtube.com/watch?v=OBY1jDXF8QE  

Read More
Android

Volley 라이브러리

  implementation ‘com.android.volley:volley:1.1.1’     <uses-permission android:name=”android.permission.INTERNET”/>       public class RegisterRequest extends StringRequest { final static private String URL = “Register.php”; private Map<String, String> map; public RegisterRequest(String user, String age, Response.Listener listener) { super(Method.POST, URL, listener, null); map = new HashMap<>(); map.put(“user”,user); map.put(“age”, age); } @Override protected Map<String, String> getParams() throws AuthFailureError { return map; }     Response.Listener<String> responseListener = new Response.Listener<String>() { @Override public void onResponse(String response) { try { JSONObject jsonObject = new JSONObject(response); boolean success = jsonObject.getBoolean(“success”); } catch (JSONException e) { e.printStackTrace(); }…

Read More
Android

BroadcastReceiver

  와이파이 상태 체크       NetworkReceiver.java   public class NetworkReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // 네트워크 상태 값 받아오기 if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) { NetworkInfo info = (NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); NetworkInfo.DetailedState state = info.getDetailedState(); if (state == NetworkInfo.DetailedState.CONNECTED) { // 네트워크 연결 상태이면… MainActivity.tv_state.setText(“네트워크 연결 완료”); } else if (state == NetworkInfo.DetailedState.DISCONNECTED) { // 네트워크 연결 해제이면.. MainActivity.tv_state.setText(“네트워크 연결 해제”); } } } }     MainActivity.java   public class MainActivity extends AppCompatActivity { public static TextView tv_state; private NetworkReceiver receiver; @Override protected void…

Read More
Android

Selector

  셀렉터를 이용해서 버튼을 눌렀을 때 버튼의 백그라운드를 바꿀 수 있다.     1번 버튼 (버튼의 백그라운드 색상) android:background=”@drawable/selector_button”   2번 버튼 (버튼의 백그라운드 이미지) android:background=”@drawable/selector_button_img”     drawable/selector_button.xml   <selector xmlns:android=”http://schemas.android.com/apk/res/android”> <item android:state_pressed=”true”> <shape> <solid android:color=”#8A9AF8″/> <corners android:radius=”10dp”/> </shape> </item> <item android:state_pressed=”false”> <shape> <solid android:color=”#536DFE”/> <corners android:radius=”10dp”/> </shape> </item> </selector>       drawable/selector_button_img.xml   <selector xmlns:android=”http://schemas.android.com/apk/res/android”> <item android:state_pressed=”true” android:drawable=”@drawable/ic_baseline_comment_24″ /> <item android:state_pressed=”false” android:drawable=”@drawable/ic_baseline_emoji_events_24″ /> </selector>       안드로이드 앱 만들기 #27 Selector https://www.youtube.com/watch?v=9E0WwR_6P9w   홍드로이드 깃헙 https://github.com/hongdroid94/27_Selector  

Read More
Android

StartActivityForResult

      SubActivity.java   public class SubActivity extends AppCompatActivity { private EditText et_comeback; private Button btn_close; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sub); et_comeback = findViewById(R.id.et_comeback); btn_close = findViewById(R.id.btn_close); btn_close.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(); intent.putExtra(“comeback”, et_comeback.getText().toString()); // 입력폼에 적은 값 담아주기 setResult(RESULT_OK, intent); // 결과 값 설정 finish(); // 현재 액티비티 종료 } }); } }       <LinearLayout 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=”.SubActivity”> <EditText android:id=”@+id/et_comeback” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:hint=”Main으로 보낼 값 입력해주세요.”/> <Button android:id=”@+id/btn_close” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”종료”/>…

Read More