2014年7月8日火曜日

Androidアプリのパッケージ名を変更する方法

目次へ



プロジェクト作成時にcom.example.xxという名前のパッケージ名をつけておくと、禁止された名前のため、このアプリを公開できません。
そこで、すでに作成してしまったパッケージ名を変更する方法を書いておきます(ECLIPSEでの方法)
  1. srcフォルダ内のパッケージ名変更
  2. AndroidManifest.xmlを変更する
の2つです。


■■■■src内のパッケージ名変更■■■■

ECLIPSEのパッケージエクスプローラで、プロジェクトのsrcフォルダの中のパッケージ名を右クリックし、 [リファクタリング]-[名前変更]を順にクリックし、com.example.xx以外の名前を決めます。
これでsrc内のパッケージ名は変更できます。

■■■■AndroidManifest.xmlの変更■■■■

次はgenフォルダの中のパッケージ名を変更します。
これは、AndroidManifest.xml内で指定することにより、変更できます。
manifestタグの中のpackage=の値を上で変更したsrcのパッケージ名と同じものにして保存します


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.xx"  ←この名前を変更する
    android:versionCode="1"
    android:versionName="1.0" >


この2つの操作でsrcとgenの中のパッケージ名が変更できます。
genの中には、もう一つ、android.support.xxxというパッケージがありますがそれはそのままでOKです。



これでapkファイルを作成し、アプリを公開すれば、パッケージ名でおこられることはありません。
アプリ公開の方法はこちら(Androidアプリを公開する)です。


にほんブログ村 IT技術ブログ IT技術メモへ
にほんブログ村

2014年7月5日土曜日

AndroidのLayout  RelativeLayout

目次へ



RelativeLayoutでは、その中に置く部品をParentあるいは、他の部品との位置でしめすことにより、レイアウトします。


■■■■ RelativeLayoutの例 ■■■■



次のような画面は下のようなレイアウトファイルを使いました。


レイアウトファイル

<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="ボタン1" />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/button1"
        android:text="ボタン2" />
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="ボタン3" />
    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="ボタン4" />
    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/button1"
        android:layout_alignParentTop="true"
        android:text="ボタン5" />
    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/button2"
        android:layout_below="@id/button2"
        android:text="ボタン6" />

</RelativeLayout>




Relative Layoutの属性まとめ
親コンテナを基準にする方法と、他の部品を基準にする方法があります。
属性意味
layout_alignParentTop true 親コンテナの上端に配置
layout_alignParentBottom true 親コンテナの下端に配置
layout_alignParentLeft true 親コンテナの左端に配置
layout_alignParentRight true 親コンテナの右端に配置
layout_centerHorizontal true 親コンテナの水平方向の中央に配置
layout_centerVertical true 親コンテナの垂直方向の中央に配置
layout_centerParent true 親コンテナの水平垂直の中心に配置
layout_below "@id/button1" button1というIDのコンポーネントの下に配置
layout_above "@id/button1" button1というIDのコンポーネントの上に配置
layout_toLeftOf "@id/button1" button1というIDのコンポーネントの左に配置
layout_toRightOf "@id/button1" button1というIDのコンポーネントの右に配置



にほんブログ村 IT技術ブログ IT技術メモへ
にほんブログ村

2014年6月5日木曜日

AndroidのEditTextを使う

目次へ



Androidで文字を入力してもらうには、EditTextを使います。
  • EditTextのプロパティ(hint、inputType)
  • 入力された文字列の取り出し


■■■■いろいろなプロパティ■■■■

EditTextでは、入力文字列をチェックして、たとえば、数字しか入力できないようなEditTextを作ることができます。
そんな時に使うのがinputTypeというものです。

また、EditTextに何も入力されていない時に、表示しておく文字列をヒントといいます。
ヒントとして使う文字列はres/values/strings.xmlに登録しておきます。



文字列をリソースとして登録
<string name="prompt">ここに〇〇を入力</string>


リソースのレイアウトres/layout/activity_main.xmlでinputType、hintを設定する。
<EditText
     android:id="@+id/edit1"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:inputType="textMultiLine"
     android:hint="@string/prompt"  />


android:inputTypeについて

android:inputTypeを指定するとたとえば、数字しか入力できないなどを指定することができます。
リソースで登録しておく場合とプログラムで指定する場合があると思いますが、 プログラムで指定するには、setInputTypeメソッドを使います。
EditText edit = (EditText)findViewById(R.id.edit1);
edit.setInputType
    (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL);


よく使いそうな、入力チェックについて、リソースで指定する場合の値とプログラムで指定する場合の引数を次にまとめてみました。
入力値 android:inputTypeで指定するもの EditText#setInputTypeメソッドの引数
改行なしテキスト text InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL
改行含むテキスト textMultiLine InputType. TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE
パスワード入力 textPassword InputType. TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD
符号なし整数 number InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL
符号付き整数 numberSigned InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED
小数点含めた数字 numberDecimal InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL


android:hintについて

android:hintでの指定をプログラムで行うにはsetHintメソッドを使います。
EditText edit = (EditText)findViewById(R.id.edit1);
edit.setHint(R.string.prompt);



■■■■入力された値を取り出す■■■■

プログラムで入力されたテキストを取得するには、次のように、getText().toString()で取得します。

EditText edit = (EditText)findViewById(R.id.edit1);
String str = edit.getText().toString();



にほんブログ村 IT技術ブログ IT技術メモへ
にほんブログ村

2014年5月29日木曜日

AndroidのSpinnerを使う

目次へ



次の3点について、書きます。
  • Spinnerの表示項目をリソースファイルだけで設定する方法
  • 表示項目をプログラムで動的に作成する方法
  • 選択された時のリスナ


■■■■リソースだけで表示項目を設定する方法■■■■

まずプロンプト(項目の上に表示する文字列)と選択項目とをres/values/strings.xmlにstringとstring-arrayとして登録しておきます。
ただ、なぜか、プロンプトがどうしても表示されませんでした。
その理由がわかったら、ここに追加します。


<string name="prompt">項目を選択してください</string>
<string-array name="kinds">
    <item >項目1</item>
    <item >項目2</item>
    <item >項目3</item>
</string-array>



次にres/layout/activity_main.xmlにSpinnerを登録します。


<Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:entries="@array/kinds"    ← string-arrayのname
    android:prompt="@string/prompt" /> ← stringのname



■■■■表示項目をプログラムで動的に作成し、項目が選択された時Toast表示■■■■

リソースのレイアウト(res/layout/activity_main.xml)ではentriesとpromptはなしでSpinnerを登録します。


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


ActivityのonCreateでは、
  • ArrayAdapterを使って選択項目を登録し、
  • プロンプトを登録し、
  • 項目が選択されたらそれを表示します


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

 strs = getResources().getStringArray(R.array.kinds);

 //--------------------------スピナー取り出し
 spinner = (Spinner)findViewById(R.id.spinner1);

 //--------------------------スピナー用アダプタ
 ArrayAdapter adapter = new ArrayAdapter
   (this,android.R.layout.simple_spinner_item, strs);
 spinner.setAdapter(adapter);


 //-------------------------- Spinnerのレイアウトとプロンプト
 adapter.setDropDownViewResource
          (android.R.layout.simple_spinner_dropdown_item);
 spinner.setPromptId(R.string.prompt);

 //-------------------------- Spinnerが選択された時の処理
 spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
  public void onItemSelected(AdapterView parent, 
                View view, int position, long id) {
   String item = (String) spinner.getSelectedItem();
   Toast.makeText(MainActivity.this, 
                                      item, Toast.LENGTH_LONG).show();
  }
  public void onNothingSelected(AdapterView parent) {
  }
 });
}



にほんブログ村 IT技術ブログ IT技術メモへ
にほんブログ村

2014年5月27日火曜日

Androidエミュレータで日本語入力する方法

目次へ



エミュレータを使いEditTextに何か入力しようとするとデフォルトでは日本語が入力できません。
日本語が使えるようにするには次のような設定が必要です。


■■■■エミュレータで日本語を使うための設定■■■■

エミュレータのMENUボタン(下の画像の〇の部分)をクリックするとメニューが表示されるので、その中のSystem settingsをクリックする。


次に表示されたリストの中からLanguage&inputをクリック


次のリストからDefaultをクリックし、表示されたメニューからJapaneseIMEを選択する。


この後、EditTextに文字を入力しようとすると下のように日本語が入力できるようになる。





にほんブログ村 IT技術ブログ IT技術メモへ
にほんブログ村