package com.jproject.mysturdy;

import androidx.appcompat.app.AppCompatActivity;

import android.app.LauncherActivity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.lang.reflect.Array;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
   
ArrayList<String> Items = new ArrayList<String>();
   
ArrayAdapter<String> Adapter;
   
ListView list;

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

       
Items.add("Frist");
       
Items.add("Second");
       
Items.add("Third");
       
Items.add("Fourth");
       
Items.add("Fifth");

       
Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, Items);

       
list = (ListView) findViewById(R.id.list);
       
list.setAdapter(Adapter);
       
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    }

   
public void mOnclick(View v) {
       
EditText ed = (EditText)findViewById(R.id.newitem);
       
int pos;
       
String text;
       
SparseBooleanArray sb;

       
switch(v.getId()){
           
case R.id.add:
               
text = ed.getText().toString();
               
if (text.length() != 0){
                   
Items.add(text);
                   
ed.setText("");
                    
Adapter.notifyDataSetChanged();
                }
               
break;
               
case R.id.change:
                   
sb = list.getCheckedItemPositions();
                   
text = ed.getText().toString();
                   
pos = list.getCheckedItemPosition();
                   
if (sb.size() != 0){
                       
for (int i = list.getCount()-1;i>=0;i--){
                           
if(sb.get(i)){
                               
Items.remove(i);
                                
Items.add(i,text);
                            }
                        }

                       
list.clearChoices();
                       
ed.setText("");
                       
Adapter.notifyDataSetChanged();
                    }
                    
break;

           
case R.id.delete:

               
pos = list.getCheckedItemPosition();
               
sb = list.getCheckedItemPositions();

               
if (sb.size() != 0){
                   
for (int i = list.getCount()-1;i>=0;i--){
                       
if(sb.get(i)){
                           
Items.remove(i);
                        }
                    }

                   
list.clearChoices();
                   
Adapter.notifyDataSetChanged();
                }
                
break;
        }
    }
}

 

<?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:orientation="vertical"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
tools:context=".MainActivity">

    <
EditText
       
android:id="@+id/newitem"
       
android:layout_width="wrap_content"
       
android:layout_height="120px"
       
/>
    <
LinearLayout
       
android:orientation="horizontal"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content">
        <
Button
           
android:id="@+id/add"
           
android:layout_width="8px"
           
android:layout_weight="3"
           
android:layout_height="wrap_content"
           
android:text="add"
           
android:onClick="mOnclick"
           
/>
        <
Button
           
android:id="@+id/change"
           
android:layout_width="8px"
           
android:layout_weight="3"
           
android:layout_height="wrap_content"
           
android:text="change"
           
android:onClick="mOnclick"
       
/>
        <
Button
            
android:id="@+id/delete"
           
android:layout_width="8px"
           
android:layout_weight="3"
           
android:layout_height="wrap_content"
           
android:text="delete"
           
android:onClick="mOnclick"
           
/>
    </
LinearLayout>
    <
ListView
       
android:id="@+id/list"
       
android:layout_width="match_parent"
       
android:layout_height="500px"
       
/>
</
LinearLayout>

 

 

'IT 통신 수학 과학 > 자바 안드로이드' 카테고리의 다른 글

안드로이드 kotlin view binding  (0) 2021.10.18
[Android/Kotlin] DataBinding(데이터바인딩)  (0) 2021.03.20
Android Studio : WebView 구현 오류 문제.  (0) 2021.02.22
안드로이드 파일 입출력 및 삭제  (0) 2020.12.16
AdapterView2  (0) 2020.11.01
ArrayAdapter  (0) 2020.10.28
Android popmenu  (0) 2020.09.08
컨텍스트 메뉴  (0) 2020.09.06

<?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:orientation="vertical"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
tools:context=".MainActivity">

    <
EditText
       
android:id="@+id/newitem"
       
android:layout_width="wrap_content"
       
android:layout_height="120px"
       
/>
    <
LinearLayout
       
android:orientation="horizontal"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content">
        <
Button
           
android:id="@+id/add"
           
android:layout_width="8px"
            
android:layout_weight="3"
           
android:layout_height="wrap_content"
           
android:text="add"
           
android:onClick="mOnclick"
           
/>
        <
Button
           
android:id="@+id/change"
           
android:layout_width="8px"
           
android:layout_weight="3"
           
android:layout_height="wrap_content"
           
android:text="change"
           
android:onClick="mOnclick"
       
/>
        <
Button
           
android:id="@+id/delete"
           
android:layout_width="8px"
           
android:layout_weight="3"
           
android:layout_height="wrap_content"
           
android:text="delete"
           
android:onClick="mOnclick"
           
/>
    </
LinearLayout>
    <
ListView
       
android:id="@+id/list"
       
android:layout_width="match_parent"
       
android:layout_height="500px"
       
/>
</
LinearLayout>

 

 

package com.jproject.mysturdy;

import androidx.appcompat.app.AppCompatActivity;

import android.app.LauncherActivity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.lang.reflect.Array;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
   
String[] arGeneral = {"김유신","이순신","강감찬","을지문덕"};
   
ArrayList<String> Items = new ArrayList<String>();
   
ArrayAdapter<String> Adapter;
   
ListView list;

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

       
Items.add("Frist");
       
Items.add("Second");
       
Items.add("Third");

       
Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, Items);

       
list = (ListView) findViewById(R.id.list);
       
list.setAdapter(Adapter);
       
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    }

   
public void mOnclick(View v) {
       
EditText ed = (EditText)findViewById(R.id.newitem);
       
int pos;
       
String text;
       
switch(v.getId()){
           
case R.id.add:
               
text = ed.getText().toString();
               
if (text.length() != 0){
                   
Items.add(text);
                   
ed.setText("");
                   
Adapter.notifyDataSetChanged();
                }
               
break;
               
case R.id.change:
                   
text = ed.getText().toString();
                   
pos = list.getCheckedItemPosition();
                   
if (text.length() != 0|pos != ListView.INVALID_POSITION){
                       
Items.remove(pos);
                       
Items.add(pos,text);
                       
ed.setText("");
                       
Adapter.notifyDataSetChanged();
                    }
                    
break;

           
case R.id.delete:

               
pos = list.getCheckedItemPosition();
               
if (pos != ListView.INVALID_POSITION){
                   
Items.remove(pos);
                   
list.clearChoices();
                   
Adapter.notifyDataSetChanged();
                }
               
break;
        }
    }
}

 

 

 

---------------------------------------- 

       MainActivity.java

 ----------------------------------------

package com.jproject.mysturdy;

import androidx.appcompat.app.AppCompatActivity;

import android.app.LauncherActivity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.lang.reflect.Array;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
   
String[] arGeneral = {"김유신","이순신","강감찬","을지문덕"};

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

       
ArrayAdapter<String> Adapter;
       
Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arGeneral);

       
ListView list = (ListView) findViewById(R.id.mylist);
       
list.setAdapter(Adapter);
       
list.setOnItemClickListener(nItemClickListener);
    }

       
AdapterView.OnItemClickListener nItemClickListener = new AdapterView.OnItemClickListener() {
           
@Override
           
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                
String mes;
               
mes = "Select  Item = " + arGeneral[position];
               
Toast.makeText(getApplicationContext(), mes,Toast.LENGTH_SHORT).show();

            }
        };


}

 

------------------------------------- 

         activity.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:orientation="vertical"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
tools:context=".MainActivity">
    <
EditText
       
android:id="@+id/edit1"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"


       
/>
    <
ListView
        
android:id="@+id/mylist"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
/>
</
LinearLayout>

 

 

 

 

 

 

================Res/menu/popupmenutestmenu.xml=======

<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:id="@+id/popup_red"
    android:title="Back-Red"/>
  <item android:id="@+id/popup_green"
    android:title="Back-green"/>
  <item android:id="@+id/popup_blue"
    android:title="Back-blue"/>
  <item android:id="@+id/popup_text"
    android:title="TextColor">
    <menu>
      <item android:id="@+id/popup_black"
         android:title="BACK-black"/>
      <item android:id="@+id/popup_white"
         android:title="BACK-white"/>
      <item android:id="@+id/popup_gray"
         android:title="BACK-gray"/>
    </menu>
  </item>
</menu>

--------------------Activity_main.xml---------------

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:gravity="center"
  tools:context=".MainActivity">

  <Button
    android:id="@+id/btn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button"
    android:onClick="mOnClick"
  />
  <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editext"
    android:text="EditText"
  />
  <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/Image1"
    android:src="@drawable/woman3"
  />
</androidx.appcompat.widget.LinearLayoutCompat>

---------------Main_Activity.java-----------------

package com.jproject.learn1;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatImageView;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import java.text.AttributedCharacterIterator;

public class MainActivity extends AppCompatActivity {
Button mBtn;
EditText mEdit;
ImageView mImage;

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

mBtn = (Button)findViewById(R.id.btn);
registerForContextMenu(mBtn);

mEdit = (EditText)findViewById(R.id.editext);
registerForContextMenu(mEdit);

mImage = (ImageView) findViewById(R.id.Image1);
registerForContextMenu(mImage);
}

public void mOnClick(View v){
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
Menu menu = popup.getMenu();
inflater.inflate(R.menu.popupmenutestmenu, menu);
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.popup_red:
mBtn.setBackgroundColor(Color.RED);
break;
case R.id.popup_green:
mBtn.setBackgroundColor(Color.GREEN);
break;
case R.id.popup_blue:
mBtn.setBackgroundColor(Color.BLUE);
break;
case R.id.popup_black:
mBtn.setBackgroundColor(Color.BLACK);
break;
case R.id.popup_white:
mBtn.setBackgroundColor(Color.WHITE);
break;
case R.id.popup_gray:
mBtn.setBackgroundColor(Color.GRAY);
break;
}
return false;
}
});
popup.show();
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v == mBtn) {
menu.setHeaderTitle("Button Menu");
menu.add(0,1,0,"red");
menu.add(0,2,0,"green");
menu.add(0,3,0,"blue");
}

if (v==mEdit){
menu.add(0,4,0,"번역");
menu.add(0,5,0,"작곡");
}
if (v==mImage){
menu.setHeaderTitle("Image Menu");
menu.add(0,100,0,"회전");
menu.add(0,200,0,"이동");
}
}

@Override
public boolean onContextItemSelected(MenuItem item) {
super.onContextItemSelected(item);
switch (item.getItemId())
{
case 1:
mBtn.setTextColor(Color.RED);
return true;
case 2:
mBtn.setTextColor(Color.GREEN);
return true;
case 3:
mBtn.setTextColor(Color.BLUE);
return true;
case 4:
Toast.makeText(this,"번역",Toast.LENGTH_SHORT).show();
return true;
case 5:
Toast.makeText(this,"작곡",Toast.LENGTH_SHORT).show();
return true;
case 100:
Toast.makeText(this,"회전",Toast.LENGTH_SHORT).show();
return true;
case 200:
Toast.makeText(this,"이동",Toast.LENGTH_SHORT).show();
return true;
}
return true;
}
}

 

'IT 통신 수학 과학 > 자바 안드로이드' 카테고리의 다른 글

안드로이드 파일 입출력 및 삭제  (0) 2020.12.16
AdapterView 다중선택  (0) 2020.11.02
AdapterView2  (0) 2020.11.01
ArrayAdapter  (0) 2020.10.28
컨텍스트 메뉴  (0) 2020.09.06
menu기능//item.setIcon(R.drawable.ic_launcher)오류  (0) 2020.09.02
타이머 메시지 핸들러  (0) 2020.08.29
버튼 리스너 구현  (0) 2020.08.23

클릭을 길게하여 나타나는 메뉴를 보인다.  컴퓨터의 우클릭과 비슷한 기능을 한다.

@drawable에 삽입하는 그림의 해상도가 지나치게 클 경우 프로그램이 죽는 현상이 발생한다.

 

 

------------------ xml파일 내용 ----------------------------

<?xml version="1.0" encoding="utf-8"?>
  <androidx.appcompat.widget.LinearLayoutCompat 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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context=".MainActivity">

    <Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Button1"
    />
    <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editext"
      android:text="EditText"
     />
    <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/Image1"
      android:src="@drawable/woman3"
    />
 </androidx.appcompat.widget.LinearLayoutCompat>

 

---------------------------- mainActivity.java-----------------------------------

public class MainActivity extends AppCompatActivity {
  Button mBtn;
  EditText mEdit;
  ImageView mImage;

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

    mBtn = (Button)findViewById(R.id.button1);
    registerForContextMenu(mBtn);

    mEdit = (EditText)findViewById(R.id.editext);
    registerForContextMenu(mEdit);

    mImage = (ImageView) findViewById(R.id.Image1);
    registerForContextMenu(mImage);
  }

  @Override
  public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    if (v == mBtn) {
      menu.setHeaderTitle("Button Menu");
      menu.add(0,1,0,"red");
      menu.add(0,2,0,"green");
      menu.add(0,3,0,"blue");
    }

    if (v==mEdit){
      menu.add(0,4,0,"번역");
      menu.add(0,5,0,"작곡");
    }
    if (v==mImage){
      menu.setHeaderTitle("Image Menu");
      menu.add(0,100,0,"회전");
      menu.add(0,200,0,"이동");
    }
  }

  @Override
  public boolean onContextItemSelected(MenuItem item) {
    super.onContextItemSelected(item);
    switch (item.getItemId())
    {
      case 1:
        mBtn.setTextColor(Color.RED);
        return true;
      case 2:
        mBtn.setTextColor(Color.GREEN);
        return true;
      case 3:
        mBtn.setTextColor(Color.BLUE);
        return true;
      case 4:
        Toast.makeText(this,"번역",Toast.LENGTH_SHORT).show();
        return true;
      case 5:
        Toast.makeText(this,"작곡",Toast.LENGTH_SHORT).show();
        return true;
      case 100:
        Toast.makeText(this,"회전",Toast.LENGTH_SHORT).show();
        return true;
      case 200:
        Toast.makeText(this,"이동",Toast.LENGTH_SHORT).show();
        return true;
    }
    return true;
  }
}

 

 

'IT 통신 수학 과학 > 자바 안드로이드' 카테고리의 다른 글

AdapterView 다중선택  (0) 2020.11.02
AdapterView2  (0) 2020.11.01
ArrayAdapter  (0) 2020.10.28
Android popmenu  (0) 2020.09.08
menu기능//item.setIcon(R.drawable.ic_launcher)오류  (0) 2020.09.02
타이머 메시지 핸들러  (0) 2020.08.29
버튼 리스너 구현  (0) 2020.08.23
터치 입력시험  (0) 2020.08.19

R.drawable.ic_launcher 문제를 해결하려면 drawables를 사용하는 대신에 mipmaps를 사용한다.

public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView MyText = new TextView(this);
    MyText.setText("메뉴키를 누르세요");
    setContentView(MyText);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    MenuItem item = menu.add(0,1,0,"짜장");
    item.setIcon(R.mipmap.ic_launcher);
    item.setAlphabeticShortcut('a');

    menu.add(0,2,0,"짬뽕").setIcon(R.mipmap.ic_launcher);
    SubMenu etc = menu.addSubMenu("기타");
    etc.add(0,3,0,"우동");
    etc.add(0,4,0,"만두");
    return true;
  }


  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);
    switch(item.getItemId()){
      case 1 :
      case 2 :
      case 3 :
      case 4 :
        Toast.makeText(this,"메뉴선택",Toast.LENGTH_SHORT).show();
        return false;
    }
    return false;
  }
}

 

 

=======================================================================

xml 메뉴파일을 이용한 메뉴구현..

안드로이드 스트디오 1.1 이후로는 메뉴에서 drawable을 사용하지않고 mipmaps폴더의 lanuncher를 사용합니다.

핸드폰의 해상도별로 아이콘을 다르게 서비스해줄 수있기 때문에 mipmaps를 사용한다고 합니다.

 

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/jjajang"
  android:title="짜장"
  android:icon="@mipmap/ic_launcher"
  />
  <item android:id="@+id/jjambbong"
    android:title="짬뽕"
    android:icon="@mipmap/ic_launcher"
  />
  <item android:id="@+id/submenu"
    android:title="기타">
    <menu>
      <item android:id="@+id/udong"
        android:title="우동"
      />
     <item android:id="@+id/mando"
       android:title="만두"
     />
    </menu>
  </item>
</menu>

 

===================================

public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView MyText = new TextView(this);
    MyText.setText("메뉴키를 누르세요");
    setContentView(MyText);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.optionmenu, menu);

    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);
    switch(item.getItemId()){
      case R.id.jjajang :
      case R.id.jjambbong :
      case R.id.udong :
      case R.id.mando :
        Toast.makeText(this,"메뉴선택",Toast.LENGTH_SHORT).show();
        return false;
    } 
    return false;
  }
}

========================================

menu xml에서 onclick 속성을 사용하는 방법,

-----------------------------------------------------

----  onclickmenu.xml -----

<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:id="@+id/about"
    android:title="프로그램 소개"
    android:onClick="mOnClick"
  />
  <item android:id="@+id/help"
    android:title="도움말"
    android:onClick="mOnClick"
  />
</menu>

------------------------------------------------------------------

public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView MyText = new TextView(this);
    MyText.setText("메뉴키를 누르세요");
    setContentView(MyText);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.onclickmenu, menu);

    return true;
  }

  public void mOnClick(MenuItem item){
    switch (item.getItemId()){
      case R.id.about:
      case R.id.help:
      Toast.makeText(this,"onClick",Toast.LENGTH_SHORT).show();
      break;
    }
  }
}

 

==========================================================================Goodbye launcher drawables, hello mipmaps!

(UPDATE 18th FEB:  Android Studio 1.1 has been released!)

The upcoming release of Android Studio 1.1 will largely be a bug fixing release. There is one change however that might catch your eye when creating a new project. Projects now create launcher icons in the mipmap resource folders instead of the drawable folders. I am sure we will hear a lot more about this with release of Android Studio 1.1 coming very soon.

Why use mipmaps for your launcher icons?

Using mipmaps for your launcher icon is described as best practice by the Android team. The advantage you get is that you can keep resources in the mipmap folders for all device densities and then strip out other resources from the drawable folders that are not relevant to the specific users device density.

Here is an example, say a user has a device that is classified as xxhdpi. The drawable resources in your apk for all other densities, such as xxxhdpi, are not required and can be stripped out. The launcher icon is different to other resources as it is possible that a higher resolution icon is displayed in the users launcher. If that higher resolution image has been stripped from your drawables folder, then a lower density icon will programmatically be up-scaled. This may cause an unattractive blurry icon.

You may not be producing multiple apk files with stripped out resources to minimize apk size yet. Therefore making the change to mipmaps for your launcher icon will currently not make any difference. That does not mean you should not do it. The best reason for using mipmaps folder is that the Android team want you to and describe it as best practice. They know what new features are in the pipeline for Android and who knows, but maybe they are planning to introduce a new feature where unused resources are automatically stripped for the user when they download apps.

History of launcher mipmaps in Android

The mipmaps resource folders in Android started appearing long before they were introduced to the wider Android development community to use, which started in Android Jelly Bean 4.3. Very little official information came from the Android team about why to use them. Around the time of the release of 4.3, Android framework developer @hackbod announced:

…if you are building different versions of your app for different densities, you should know about the “mipmap” resource directory.  This is exactly like “drawable” resources, except it does not participate in density stripping when creating the different apk targets.

 

REPORT THIS AD

Since that time if you have taken a close look at the apps produced out of Google you will notice that some apps were doing exactly that.  Shipping only the required density but including all densities in the mipmap folders for the launcher icons.

As time went on still there was not much noise out of the Android team about using launcher mipmaps, that was until Lollipop hit AOSP and the new Nexus devices hit the market.  The Android Developers blog posted about preparing your apps for the new Nexus 6 and 9.  This detailed a compelling reason you should use mipmap folders for your app icon and announced the shift of best practice to using mipmaps:

…Provide at least an xxxhdpi app icon because devices can display large app icons on the launcher. It’s best practice to place your app icons in mipmap- folders (not the drawable- folders) because they are used at resolutions different from the device’s current density. For example, an xxxhdpi app icon can be used on the launcher for an xxhdpi device.

Interestingly there is still no update in the Official Android documentation and it still mentions drawable folders being the place for your launcher icons.  I am sure that will change in the coming months as the update to Android Studio is the next step in the progression of the change.

Update May 2015: The Android developer documentation has now been updated to document the purpose of mipmaps.

Creating a new project using launcher mipmaps

Update to Android Studio 1.1, any new project will use the new structure when it is created.

Updating an existing project to use launcher mipmaps

If you have used the excellent tool Android Asset Studio to generate your launcher icons recently you will notice the mipmap change is now reflected in the structure of assets generated. Even if you normally generate your icons through another process this is a great place to start as it shows the format and structure you should be using.

Your assets need to go into a structure much like your previous launcher drawables.

res/ mipmap-mdpi/ic_launcher.png (48x48 pixels) mipmap-hdpi/ic_launcher.png (72x72) mipmap-xhdpi/ic_launcher.png (96x96) mipmap-xxhdpi/ic_launcher.png (144x144) mipmap-xxxhdpi/ic_launcher.png (192x192)

You will also need to change your AndroidManifest.xml to use mipmaps for the launcher.

android:icon="@mipmap/ic_launcher"

'IT 통신 수학 과학 > 자바 안드로이드' 카테고리의 다른 글

AdapterView2  (0) 2020.11.01
ArrayAdapter  (0) 2020.10.28
Android popmenu  (0) 2020.09.08
컨텍스트 메뉴  (0) 2020.09.06
타이머 메시지 핸들러  (0) 2020.08.29
버튼 리스너 구현  (0) 2020.08.23
터치 입력시험  (0) 2020.08.19
콜백메서드 재정의 / 리스너 인터페이스 구현  (0) 2020.08.17

+ Recent posts