정렬할 ArrayList 입니다.

List<VideoEntry> list = new ArrayList<>();
Collections.sort(list);

리스트에 저장된 VideoEntry 클래스 입니다.

Comparable을 상속받아 compareTo함수의 내용을 써줍니다.

public class VideoEntry implements Comparable<VideoEntry> {
    public String text;
    public String videoId;
    public String time;

    public VideoEntry(String text, String videoId, String time) {
        this.text = text;
        this.videoId = videoId;
        this.time = time;
    }

    //오름차순
    @Override
    public int compareTo(VideoEntry entry) {
        return this.time.compareTo(entry.time);
    }

    //내림차순
    @Override
    public int compareTo(VideoEntry entry) {
        return entry.time.compareTo(this.time);
    }
}

'Android' 카테고리의 다른 글

[Android] APK 만들기  (0) 2019.03.30
[Android] '돌아가기' 버튼 구현  (0) 2019.03.29
[Android] Tab 사용법  (0) 2019.03.27
[Android] RecyclerView 예제  (0) 2019.03.26
[Android] YouTube Api call Url  (0) 2019.03.22

TabHost태그 사이에 LinearLayout으로 Tab버튼과 컨텐츠를 vertical정렬한다

 

TabWidget의 Height는 반드시 wrap_content로 지정되어야 한다

<TabHost
    android:id="@+id/tabHost"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="9">

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

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <!-- 컨텐츠 삽입 -->
            
        </FrameLayout>
    </LinearLayout>
</TabHost>

 

Activity에 다음 코드를 추가한다

이 때 setIndicator("")에 들어가는 텍스트가 탭의 제목이 된다

// TabHost 참조 후 TabHost.setup() 호출. 호출하지 않으면 TabWidget이 동작 안 함.
TabHost tabHost1 = (TabHost) findViewById(R.id.tabHost1);
tabHost1.setup();

// 첫 번째 Tab. (탭 표시 텍스트:"TAB 1"), (페이지 뷰:"content1")
TabHost.TabSpec ts1 = tabHost1.newTabSpec("Tab Spec 1");
ts1.setContent(R.id.content1) ;
ts1.setIndicator("TAB 1");
tabHost1.addTab(ts1);

// 두 번째 Tab. (탭 표시 텍스트:"TAB 2"), (페이지 뷰:"content2")
TabHost.TabSpec ts2 = tabHost1.newTabSpec("Tab Spec 2");
ts2.setContent(R.id.content2);
ts2.setIndicator("TAB 2");
tabHost1.addTab(ts2);

// 세 번째 Tab. (탭 표시 텍스트:"TAB 3"), (페이지 뷰:"content3")
TabHost.TabSpec ts3 = tabHost1.newTabSpec("Tab Spec 3");
ts3.setContent(R.id.content3);
ts3.setIndicator("TAB 3");
tabHost1.addTab(ts3);

'Android' 카테고리의 다른 글

[Android] '돌아가기' 버튼 구현  (0) 2019.03.29
[Android] Comparable 정렬  (0) 2019.03.28
[Android] RecyclerView 예제  (0) 2019.03.26
[Android] YouTube Api call Url  (0) 2019.03.22
YouTube Android Player API - Download  (0) 2019.03.20

dependencies 추가


1
implementation 'com.android.support:recyclerview-v7:28.0.0'
cs


뷰 추가


1
2
3
4
5
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
cs

item 뷰 추가

1
2
3
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
cs

Activity 코드 추가


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class MainActivity extends AppCompatActivity {
 
    private RecyclerView recyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager layoutManager;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
 
        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        recyclerView.setHasFixedSize(true);
 
        // use a linear layout manager
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
 
        String[] myDataset = {"MENU 1","MENU 2","MENU 3","MENU 4"};
 
        // specify an adapter (see also next example)
        mAdapter = new MyAdapter(myDataset);
        recyclerView.setAdapter(mAdapter);
    }
}
cs


Adapter 추가


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private String[] mDataset;
 
    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class MyViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView textView;
        public MyViewHolder(TextView v) {
            super(v);
            textView = v;
        }
    }
 
    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(String[] myDataset) {
        mDataset = myDataset;
    }
 
    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                                     int viewType) {
        // create a new view
        TextView v = (TextView) LayoutInflater.from(parent.getContext())
                .inflate(R.layout.text_view, parent, false);
 
        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }
 
    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        holder.textView.setText(mDataset[position]);
 
    }
 
    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}
cs


'Android' 카테고리의 다른 글

[Android] Comparable 정렬  (0) 2019.03.28
[Android] Tab 사용법  (0) 2019.03.27
[Android] YouTube Api call Url  (0) 2019.03.22
YouTube Android Player API - Download  (0) 2019.03.20
[Android] DrawerLayout 사용하기  (0) 2019.03.19

Type : GET


Url : https://www.googleapis.com/youtube/v3/search?part=snippet&q={검색어}&key={API_KEY}&maxResults={응답 개수}&pageToken={페이지 토큰}


페이지 토큰으로 페이지를 변경할 수 있다.


https://developers.google.com/youtube/v3/docs/search/list?hl=ko


'Android' 카테고리의 다른 글

[Android] Tab 사용법  (0) 2019.03.27
[Android] RecyclerView 예제  (0) 2019.03.26
YouTube Android Player API - Download  (0) 2019.03.20
[Android] DrawerLayout 사용하기  (0) 2019.03.19
[Android] AutoCompleteTextView 사용하기  (0) 2019.03.14

안드로이드 유튜브 API 다운로드


libs 폴더에 추가하고 gradle - dependencies 추가한다

implementation files('libs/YouTubeAndroidPlayerApi.jar')


'Android' 카테고리의 다른 글

[Android] RecyclerView 예제  (0) 2019.03.26
[Android] YouTube Api call Url  (0) 2019.03.22
[Android] DrawerLayout 사용하기  (0) 2019.03.19
[Android] AutoCompleteTextView 사용하기  (0) 2019.03.14
[Android] EditText 사용하기  (0) 2019.03.13


먼저 레이아웃을 DrawerLayout으로 변경한다.


DrawerLayout 안에 메인 화면(FrameLayout)과 메뉴(ListView)를 추가한다.


activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<android.support.v4.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">
 
    <!-- 메인콘텐츠용 레이아웃 -->
 
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
 
        <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="20sp" />
    </FrameLayout>
 
    <!-- 슬라이드 메뉴용 레이아웃 -->
 
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@android:color/background_light" />
 
</android.support.v4.widget.DrawerLayout>
cs


레이아웃을 호출하기 위한 클래스 변수를 선언하고 각각 레이아웃을 호출한다.


MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
 
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;
    private TextView mTextView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        mTextView = (TextView) findViewById(R.id.text);
 
        setupNavigationDrawer();
 
        // 목록 데이터를 설정
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1);
        adapter.add("Menu1");
        adapter.add("Menu2");
        adapter.add("Menu3");
        mDrawerList.setAdapter(adapter);
 
        if (savedInstanceState == null) {
            selectItem(0);
        }
    }
cs


Drawer를 열고 닫을 때의 이벤트를 설정한다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
    private void setupNavigationDrawer() {
        // NavigationDrawer의 그림자로 설정할 Drawable을 지정
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
                GravityCompat.START);
        mDrawerList.setOnItemClickListener(this);
 
        // 드로워를 개폐 시에 이벤트를 받을 수 있게 함
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open,
                R.string.drawer_close) {
            @Override
            public void onDrawerClosed(View view) {
            }
 
            @Override
            public void onDrawerOpened(View drawerView) {
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // DrawerToggle측의 옵션 메뉴 선택을 처리할 수 있도록
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
 
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // DrawerToggle측의 옵션 메뉴를 제어할 수 있도록
        mDrawerToggle.syncState();
    }
 
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // DrawerToggle측에서 위/아래 변경을 제어할 수 있도록
        mDrawerToggle.onConfigurationChanged(newConfig);
    }
 
    @Override
    public void onItemClick(AdapterView<?> adapterView, View parent,
                            int position, long id) {
        selectItem(position);
    }
 
    private void selectItem(int position) {
        ListAdapter adapter = mDrawerList.getAdapter();
        String item = (String) adapter.getItem(position);
        mTextView.setText("선택한 항목: " + item);
        mDrawerLayout.closeDrawer(mDrawerList);
    }
cs


'Android' 카테고리의 다른 글

[Android] YouTube Api call Url  (0) 2019.03.22
YouTube Android Player API - Download  (0) 2019.03.20
[Android] AutoCompleteTextView 사용하기  (0) 2019.03.14
[Android] EditText 사용하기  (0) 2019.03.13
[Android] SeekBar 사용  (0) 2019.03.12

자동 완성 기능은 AutoCompleteTextView를 사용한다.


1
2
3
4
5
    <AutoCompleteTextView
        android:id="@+id/autocomplete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"/>
cs


AutoCompleteTextView에 자동 완성 리스트를 연결하여 주면 자동 완성이 표시된다.


1
2
3
4
    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this
                                    android.R.layout.simple_dropdown_item_1line, androids);
    textView.setAdapter(adapter);
cs


ArrayAdapter<String>을 상속받는 Adapter 클래스를 만들어서 쉽게 제어도 가능하다.


1
2
3
4
5
6
7
8
    List<String> androids = new ArrayList<>();
    androids.add("좋아하는 색은 빨간색");
    androids.add("좋아하는 색은 파란색");
    androids.add("좋아하는 색은 노란색");
 
    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
    SuggestAdapter adapter = new SuggestAdapter(this, androids);
    textView.setAdapter(adapter);
cs


SuggestAdapter.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class SuggestAdapter extends ArrayAdapter<String> implements SpinnerAdapter {
    private LayoutInflater mInflator;
    private List<String> mItems;
 
    SuggestAdapter(Context context, List<String> objects) {
        super(context, R.layout.item_suggest, objects);
        mInflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mItems = objects;
    }
 
    @Override
    public int getCount() {
        return mItems.size();
    }
 
    @Override
    public String getItem(int position) {
        return mItems.get(position);
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
 
        if (convertView == null) {
            convertView = mInflator.inflate(R.layout.item_suggest, nullfalse);
            holder = new ViewHolder();
            holder.textView = (TextView) convertView.findViewById(R.id.spinnerText);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
 
        holder.textView.setText(mItems.get(position));
 
        return convertView;
    }
 
    private class ViewHolder {
        TextView textView;
    }
}
cs


item_suggest.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <TextView
        android:id="@+id/spinnerText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/FlatDarkRed"
        android:padding="@dimen/padding_large"
        android:textColor="@color/FlatLightBlue"
        android:textSize="18sp" />
</LinearLayout>
cs


'Android' 카테고리의 다른 글

YouTube Android Player API - Download  (0) 2019.03.20
[Android] DrawerLayout 사용하기  (0) 2019.03.19
[Android] EditText 사용하기  (0) 2019.03.13
[Android] SeekBar 사용  (0) 2019.03.12
[Android] 라디오 버튼(RadioButton) 사용  (0) 2019.03.11

EditText의 입력문자는 InputType 속성을 사용하여 변경할 수 있다.


InputType에는 text, textPassword, date, datetime, phone 등 여러 type들을 제공한다.


힌트를 표시하고 싶은 경우 hint 속성을 사용한다.


1
2
3
4
5
6
    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="비밀번호를 입력하세요" />
cs


EditText의 문자 제한은 InputFilter를 사용한다.


입력을 제한하는 내용의 InputFilter를 생성하고 생성한 InputFilter는 InputFilter의 배열에 저장하여


setFilters 메소드를 사용해 적용한다.


1
2
3
4
5
6
    <EditText
        android:id="@+id/restriction"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="이메일을 입력하세요"
        android:textSize="16sp"/>
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        EditText editText = (EditText) findViewById(R.id.restriction);
 
        InputFilter inputFilter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end,
                                             Spanned dest, int dstart, int dend) {
                if (source.toString().matches("^[0-9a-zA-Z@\\.\\_\\-]+$")) {
                    return source;
                } else {
                    return "";
                }
            }
        };
 
        InputFilter lengthFilter = new InputFilter.LengthFilter(10);
 
        InputFilter[] filters = new InputFilter[] { inputFilter , lengthFilter };
        editText.setFilters(filters);
    }
cs


'Android' 카테고리의 다른 글

[Android] DrawerLayout 사용하기  (0) 2019.03.19
[Android] AutoCompleteTextView 사용하기  (0) 2019.03.14
[Android] SeekBar 사용  (0) 2019.03.12
[Android] 라디오 버튼(RadioButton) 사용  (0) 2019.03.11
[Android] CheckBox 사용  (0) 2019.03.08

+ Recent posts