* MainActivity.java

public class MainActivity extends AppCompatActivity {

    EditText editText;
    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = findViewById(R.id.editText);
        webView = findViewById(R.id.webView);

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(request.getUrl().toString());
                return true;
            }
        });

        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = editText.getText().toString().trim();

                if (url != null && !url.isEmpty()) {
                    webView.loadUrl(url);
                } else {
                    Toast.makeText(getApplicationContext(), "Url을 입력하세요.", Toast.LENGTH_LONG).show();
                }
            }
        });

    }

}

* activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="http://m.naver.com" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="열기" />

    <!-- widget -->
    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

* AndroidMenifest.xml

<uses-permission android:name="android.permission.INTERNET" />

<application android:usesCleartextTraffic="true">

* app

minSdkVersion 21

'android' 카테고리의 다른 글

키패드 닫기  (0) 2020.02.03
SeekBar 사용법  (0) 2020.02.03
Spinner 사용법  (0) 2020.02.03
RecyclerView 리스트(문자목록, 통화목록 ..)  (0) 2020.02.03
레이아웃 사용자정의 사용  (0) 2020.02.03
블로그 이미지

디츠

“말은 쉽지, 코드를 보여줘.” “Talk is cheap. Show me the code.” – 리누스 토르발스(Linus Torvalds)

,

Spinner 사용법

android 2020. 2. 3. 22:30

* MainActivity.java

public class MainActivity extends AppCompatActivity {

    TextView textView;

    String[] items = { "mike", "angel", "crow", "test", "sky" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.textView);

        Spinner spinner = findViewById(R.id.spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                textView.setText(items[position]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                textView.setText("");
            }
        });
    }

}

* activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="선택된 아이템"
        android:textSize="24sp" />

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

'android' 카테고리의 다른 글

SeekBar 사용법  (0) 2020.02.03
입력된 url을 WebView로 열기  (0) 2020.02.03
RecyclerView 리스트(문자목록, 통화목록 ..)  (0) 2020.02.03
레이아웃 사용자정의 사용  (0) 2020.02.03
버튼 사용자정의 사용  (0) 2020.02.03
블로그 이미지

디츠

“말은 쉽지, 코드를 보여줘.” “Talk is cheap. Show me the code.” – 리누스 토르발스(Linus Torvalds)

,

* activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

* MainActivity.java

public class MainActivity extends AppCompatActivity {

    private DbHelper dbHelper = new DbHelper(this);
    private LibraryHelper libraryHelper = new LibraryHelper();

    private TextView subject;

    private DrawerLayout drawerLayout;
    private MainFragment mainFragment = new MainFragment(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        
        ArrayList<Memory> memoryList = dbHelper.memoryList();
        
        MemoryAdapter memoryAdapter = new MemoryAdapter(memoryList);
        recyclerView.setAdapter(memoryAdapter);
	}
    
}

* recycler_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/url"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />
    
</LinearLayout>

* MemoryAdapter.java

public class MemoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static class MemoryViewHolder extends RecyclerView.ViewHolder {

        TextView title;
        TextView url;

        MemoryViewHolder(View view) {
            super(view);

            title = view.findViewById(R.id.title);
            url = view.findViewById(R.id.url);
        }

    }

    private ArrayList<Memory> memoryList;

    public MemoryAdapter(ArrayList<Memory> memoryList) {
        this.memoryList = memoryList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);

        return new MemoryViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        MemoryViewHolder memoryViewHolder = (MemoryViewHolder) holder;

        memoryViewHolder.title.setText(memoryList.get(position).getTitle());
        memoryViewHolder.url.setText(memoryList.get(position).getUrl());
    }

    @Override
    public int getItemCount() {
        return memoryList.size();
    }

}

'android' 카테고리의 다른 글

입력된 url을 WebView로 열기  (0) 2020.02.03
Spinner 사용법  (0) 2020.02.03
레이아웃 사용자정의 사용  (0) 2020.02.03
버튼 사용자정의 사용  (0) 2020.02.03
NicePatch(png 이미지 확대시)  (0) 2020.02.03
블로그 이미지

디츠

“말은 쉽지, 코드를 보여줘.” “Talk is cheap. Show me the code.” – 리누스 토르발스(Linus Torvalds)

,
<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="이미지 변경" />

    <com.example.layout.Layout1
        android:id="@+id/layout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    </com.example.layout.Layout1>

</LinearLayout>

* Layout1.java

public class Layout1 extends LinearLayout {

    ImageView imageView;
    TextView textView;
    TextView textView2;

    public Layout1(Context context) {
        super(context);

        init(context);
    }

    public Layout1(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        init(context);
    }

    public void init(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.layout1, this, true);

        imageView = findViewById(R.id.imageView);
        textView = findViewById(R.id.textView);
        textView2 = findViewById(R.id.textView2);
    }

    public void setImage(int resId) {
        imageView.setImageResource(resId);
    }

    public void setName(String name) {
        textView.setText(name);
    }

    public void setMobile(String mobile) {
        textView2.setText(mobile);
    }

}

* Layout1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <!-- CardView는 Palette에서 라이브러리 다운로드 -->
    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="#FFFFFF"
        app:cardCornerRadius="10dp"
        app:cardElevation="5dp"
        app:cardUseCompatPadding="true">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="80dp"
            android:layout_height="80dp"
            app:srcCompat="@mipmap/ic_launcher" />

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

            <TextView
                android:id="@+id/textView"
                android:textSize="30dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:text="이름" />

            <TextView
                android:id="@+id/textView2"
                android:textSize="30dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:text="전화번호" />

        </LinearLayout>

    </androidx.cardview.widget.CardView>

</LinearLayout>

* MainActivity.java

public class MainActivity extends AppCompatActivity {

    Layout1 layout1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        layout1 = findViewById(R.id.layout1);
        layout1.setImage(R.drawable.ic_launcher_background);
        layout1.setName("홍길동");
        layout1.setMobile("010-1000-1000");

        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                layout1.setImage(R.drawable.ic_launcher_foreground);
            }
        });
    }
}

* activity_main.xml

 

'android' 카테고리의 다른 글

Spinner 사용법  (0) 2020.02.03
RecyclerView 리스트(문자목록, 통화목록 ..)  (0) 2020.02.03
버튼 사용자정의 사용  (0) 2020.02.03
NicePatch(png 이미지 확대시)  (0) 2020.02.03
권한 요청  (0) 2020.02.03
블로그 이미지

디츠

“말은 쉽지, 코드를 보여줘.” “Talk is cheap. Show me the code.” – 리누스 토르발스(Linus Torvalds)

,

* 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.example.view.MyButton
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="확인"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

* MyButton.java

public class MyButton extends AppCompatButton {

    public MyButton(Context context) {
        super(context);

        init(context);
    }

    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs);

        init(context);
    }

    public void init(Context context) {
        setBackgroundColor(Color.CYAN);
        setTextColor(Color.BLACK);

        float textSize = getResources().getDimension(R.dimen.text_size); // pixel이 기본값이라 sp로 변환
        setTextSize(textSize);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Log.d("MyButton", "onDraw 호출됨");
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d("MyButton", "onTouchEvent 호출됨");

        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                setBackgroundColor(Color.BLUE);
                setTextColor(Color.RED);
                break;
            case MotionEvent.ACTION_OUTSIDE:
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                setBackgroundColor(Color.CYAN);
                setTextSize(Color.BLACK);
                break;
        }

        invalidate();

        return true;
    }

}

* dimens.xml(res > values)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="text_size">30sp</dimen>
</resources>

'android' 카테고리의 다른 글

RecyclerView 리스트(문자목록, 통화목록 ..)  (0) 2020.02.03
레이아웃 사용자정의 사용  (0) 2020.02.03
NicePatch(png 이미지 확대시)  (0) 2020.02.03
권한 요청  (0) 2020.02.03
SMS Receiver  (0) 2020.02.03
블로그 이미지

디츠

“말은 쉽지, 코드를 보여줘.” “Talk is cheap. Show me the code.” – 리누스 토르발스(Linus Torvalds)

,

* Drawable 리소스 이미지에 png 파일을 이미지 확대시 깨짐을 방지하려면 이미지 파일 선택후 우클릭하여 "Create 9-Patch file"을 선택하면 "파일명.9.확장자"로 파일 생성 가능

'android' 카테고리의 다른 글

레이아웃 사용자정의 사용  (0) 2020.02.03
버튼 사용자정의 사용  (0) 2020.02.03
권한 요청  (0) 2020.02.03
SMS Receiver  (0) 2020.02.03
Service 사용법  (0) 2020.02.02
블로그 이미지

디츠

“말은 쉽지, 코드를 보여줘.” “Talk is cheap. Show me the code.” – 리누스 토르발스(Linus Torvalds)

,