Androidアプリ開発でフッター広告を固定しながら画面遷移する方法

このQ&Aのポイント
  • Androidアプリ開発において、フッター広告を固定して画面の遷移を行いたい場合の方法を解説します。
  • フッター広告のみを固定し、画面遷移時には広告以外の範囲だけにonCreateを適応させる方法について説明します。
  • フッター広告を固定する方法や画面遷移時の処理についての詳細な手順を解説します。
回答を見る
  • ベストアンサー

androidアプリ開発の画面遷移について

androidアプリ開発でフッター広告だけを固定して、画面の遷移をしたいのですがやり方がわかりません。 フッター広告は固定で、画面を遷移するたびにフッター広告以外はoncreateで処理を行いたいです。 感じとしては、 final LinearLayout linearLayout = (LinearLayout)findViewById(広告以外の範囲id); getLayoutInflater().inflate(レイアウトid, linearLayout); が近かったのですが、これではただのxmlの切り替えでした。 やりたいのは、切り替え時に「広告範囲以外の範囲だけに」oncreateを適応させたいことです。 説明が下手ですみませんが、どうか宜しくお願いします。

質問者が選んだベストアンサー

  • ベストアンサー
  • anmochi
  • ベストアンサー率65% (1332/2045)
回答No.1

> 「広告範囲以外の範囲だけに」oncreateを適応させたいことです。 うーん、無理。Activityでは。 Activityの中に広告とアプリ内容があるんだから。 Activityの機能であるonCreateを中身の一部だけでやるという方法は無理でごんす。 で、回答としてはFragmentという機能を使うといいでしょう。 Activityの中に広告エリアは適当に、それ以外の領域をFragmentLayoutにして、そこにFragmentをアタッチするとFragment#onAttachの後にFragment#onCreateが呼ばれるのです。 Activityでしたい事を全部Fragmentに変えて、Fragment#onCreateをActivity#onCreateと同じ感覚で使うといいんじゃないでしょうか。 画面を分割して個別に有効にしたり無効にしたりできる。そう、Fragmentならね。

aoki_reika
質問者

お礼

Fragmentの使い方を調べながらやったら何とか思い通りの動きができました。 まだ、使い方があやふやですが頑張ってみたいと思います。 これからは、Fragmentを考慮した実装を心がけたいと思います。 本当に有難うございましたm(_ _)m

関連するQ&A

  • androidアプリ開発について

    androidアプリ開発初心者です。 やりたいことは、 スタートボタンを押して、自分のお気に入りのページに飛び、(例えばオークションなど)サイトを表示させ、何分間ごとに更新できるループ処理?などができればいいなと考えています。 スタートボタンを押してサイトを表示するまでは何とか作れたのですが、それ以降をどのように作ったらいいのか分かりません。 初心者的な質問で申し訳ないのですが、どのように書いたらよいのか教えてください。 コードはこのようになっています。 <<main.xml>> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="192dp" android:layout_height="wrap_content" android:text="スタート" /> </LinearLayout> <<MainActivity.java>> import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { Button btn =null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn=(Button)findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v){ Uri uri =Uri.parse("http://"); Intent intent=new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(uri); startActivity(intent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } 長文で申し訳ありません。どうぞ宜しくお願い致します。

  • Androidの画面表示のタイミングについて

    androidで、画面のタッチイベントを感知し、画像やボタンを表示させたいと考えています。 その際、ボタンの表示を遅らせるため、sleep()を入れて、表示タイミングをずらそうとしましたが、うまくいかず、ボタンが遅れて表示されるのではなく、画面全体がsleep()で指定した時間分遅れて表示されます。 ファンクションの終了時に画面の表示がまとめて行われるようですが、これはJAVAの仕様なのでしょうか? //タッチイベント public boolean onTouchEvent(MotionEvent event){ //背景画像の表示 final LinearLayout layout= (LinearLayout)findViewById(R.id.Layout); layout.setBackgroundResource(R.drawable.gazou1); //ボタンの表示を遅らせるためスリープ try{ Thread.sleep(3000); }catch (InterruptedException e) { System.out.println(e); } //ボタンの表示 Button a = (Button)findViewById(R.id.button_id); a.setVisibility(View.VISIBLE); return true; } ようろしくお願いいたします。

    • ベストアンサー
    • Java
  • 【Android開発】ボタンのテキスト表示

    Android初心者です。現在画面遷移先の画面に配列で設定した質問文、選択肢を表示するアプリを制作しています。質問文はTextView、選択肢は2つのButtonにテキストとして表示する。画面遷移した後にsetQuestion()、setChoiceを呼び出したいんですが実機で試したところ、画面遷移した後に「問題が発生しため、(アプリ名)を終了します」とポップアップが出て、アプリが落ちます。 この2つのメソッドをonCreateに入れるのは間違なのでしょうか?どうすればうまくいきますか?初心者なのでうまく書けてないかも知れません。 よろしくお願いします。 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sub); setChoice(); setQuestion();  } int number = 0; //質問番号 //選択肢 Button button1; Button button2; Button button3; Button button4; //質問文を配列に格納 static String question[] = { "質問文1", "質問文2", }; //選択肢 static String choice[][] = { {"1番", "2番", "3番", "4番"}, {"1番", "2番", "3番", "4番"}, }; //問題の回答 static String answer[] = { "2番", "4番" }; //出力 //質問文を表示 public void setQuestion() { String setQuestion = question[number]; // TextView question = (TextView) findViewById(R.id.Question); question.setText(setQuestion); } //選択肢の表示 public void setChoice() { TextView button1 = (Button) findViewById(R.id.button1); TextView button2 = (Button) findViewById(R.id.button2); button1.setText(choice[number][0]); button2.setText(choice[number][1]); } xml <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Question" android:id="@+id/Question" android:textSize="40dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="81dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button1" android:textSize="30dp" android:onClick="choiceClick" android:layout_above="@+id/button2" android:layout_centerHorizontal="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button2" android:textSize="30dp" android:onClick="choiceClick" android:layout_alignParentBottom="true" android:layout_alignLeft="@+id/button1" android:layout_alignStart="@+id/button1" />

  • Androidで画面遷移ができません.

    初心者ながら,Android向けにアプリを開発しています. ページXXXXXでボタンを表示させ,ページYYYYYで画像が表示されるようにしたいのですが,ページXXXXXで表示されたボタンを押すとエラーが出て強制終了となってしまいます. 画面遷移されるページ(YYYYY)には,適当な位置にタッチさせるとタッチした位置に移動する画像についてプログラミングしてあります.ページYYYYYは個別で実行すると正常に起動します. どこに原因があるのか教えてください.よろしくお願いします. ページXXXXXのソースは以下のとおりです. 【ソース】 import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.content.Intent; public class XXXXX extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button b1 = (Button) findViewById(R.id.button1); b1.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Intent intent = new Intent(XXXXX.this, YYYYY.class); startActivity(intent); } }); } }

    • ベストアンサー
    • Java
  • Android開発(Listview)について

    Androidアプリ開発における listviewについて質問させてください。 以下で、東京都,神奈川県,千葉県,埼玉県,茨城県,栃木県,群馬県と表示されています。 東京をクリックしたら、新宿、上野、秋葉原 千葉をクリックしたら、千葉、市川、船橋 といったように、さらに地域を絞っていきたいと考えています。 どのような方法で、実現できるか アドバイスして頂けると助かります。 ■searchActivity.java import android.app.Activity; import android.os.Bundle; import android.widget.*; public class searchActivity extends Activity { private ListView list; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); String[] arr = {"東京都","神奈川県","千葉県","埼玉県","茨城県","栃木県","群馬県"}; // コンポーネントの設定 list = (ListView)this.findViewById(R.id.list); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list, arr); list.setAdapter(adapter); } } ■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="fill_parent" android:layout_height="fill_parent" android:textSize="20sp" android:background="#ffffff" > <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout> ■list.xml <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textSize="20sp" android:paddingTop="50.0px" android:paddingLeft="20.0px" android:background="#ffffff" android:textColor="#000000" /> 以上、宜しくお願いいたします。

  • すいません、Android初心者です

    最近EclipseでAndroidの勉強を始めたものです。 早速ですが、以下のソースを実行すると、何故か「[アプリ名](パッケージ名)が予期せず停止 しました。やり直してください」と出てきます・・・コメントの部分をコメントアウトするとうまく動作 するのですが、何故でしょうか?なお、ターゲットは2.3.3のものを利用しています。 /* +++ Javaソース +++ */ import android.app.Activity; import android.os.Bundle; import android.widget.Button; public class Android_Test01Activity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Button button1 = (Button) findViewById(R.id.button1); //button1.setText("button1"); setContentView(R.layout.main); } } /* +++ XMLソース +++ */ <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout>

    • ベストアンサー
    • Java
  • 【android開発】ヘッダ、フッタ固定方法

    Androidアプリ開発を始めたばかりの初心者です。 どう進めていくべきか悩んでるので、 相談させてください。 ヘッダ、フッタ固定で登録画面、更新画面、参照画面を作ったのですが、 登録、更新、参照とそれぞれActivity、XMLを作成した為、 実機で動かすと、画面遷移時にヘッダ、フッタが再表示されます。 遷移時もヘッダが再表示されず、 ボディ部のみ切り替えるようなやり方をする為には どう実現すればよいのでしょうか? 想像するには、 Activity、XMLを1つのみ作成し、 Activityで画面ごとにレイアウトを変更すればいいのかな・・・と考えているのですが、 良い案あれば教えてください。 宜しくお願いします。

  • Android開発についての初心者質問です。

    Android開発についての質問です。初心者でAndroidアプリを作っています。 初心者でAndroidアプリを作っています。 XMLからの実装をしたいのですが、 やり方がよく分かりません。 どなたかご教授頂けないでしょうか? よろしくお願いいたします。 やりたいことは、imageButtonをクリックしたら imageViewの画像部分が変わる。 imageButton1をクリック → imageView1が表示 imageButton2をクリック → imageView2が表示 デフォルトでは、imageView1が表示している状態 buttonのリスナーを登録するのが関の山で、ボタンクリック時の imageView表示切替処理等ちんぷんかんぷんです。 XMLはこういった感じで書いてます。 よろしくお願いいたします。 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <FrameLayout android:id="@+id/frameLayout1" android:layout_height="wrap_content" android:foregroundGravity="center" android:layout_width="match_parent" > <ImageView android:layout_height="wrap_content" android:src="@drawable/icon" android:id="@+id/imageView1" android:layout_gravity="center" android:layout_width="match_parent" > </ImageView> </FrameLayout> <LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_width="match_parent" > <ImageButton android:id="@+id/imageButton1" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_width="wrap_content" > </ImageButton> <ImageButton android:id="@+id/imageButton2" android:layout_width="wrap_content" android:src="@drawable/icon" android:layout_height="wrap_content" > </ImageButton> <ImageButton android:id="@+id/imageButton3" android:layout_width="wrap_content" android:src="@drawable/icon" android:layout_height="wrap_content" > </ImageButton> </LinearLayout> </LinearLayout>

  • Android開発でお聞きしたいことがあります。

    プリファレンスを使用してActivity間でデータの受け渡しをしたいのですが、 layoutのxmlファイルで定義したid( EditText→@+id/a ) を取得して(EditText edit =(EditText)findViewByid(R.id.a); ) editをプリファレンスに書き込みしたいです。 プリファレンスを定義した後、 editor.putString("data1",edit); でエラーが出てしまいます。 私では editにid aを取得し、String型としてプリファレンスに書き込みたいのですが、 間違いなどありましたらご指摘、お願いします。 質問させていただいて申し訳ないのですが、詳しい修正方法もできたらお願いします。

  • androidアプリ開発 -間違い探しアプリ-

    androidの間違い探しアプリを作ろうと思っています。 イメージ的には背景用のImageViewに画像を表示して、 間違っている箇所にカラのImageViewを載せ、 ImageViewがクリックされたら正解というような仕組みにしようと思っています。 そこで、下記コードを書いています。 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_vertical" > <ImageView android:id="@+id/mainImage" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/stage1" ></ImageView> <ImageView android:id="@+id/onImage" android:layout_width="40dip" android:layout_height="40dip" android:layout_marginLeft="240dp" android:layout_marginTop="180dp" android:onClick="seikai" /> </RelativeLayout> が、解像度や画面サイズによってカラのImageViewの場所が変わってしまい、うまくいきません。 resフォルダ内に 「layout-land-hdpi」 「layout-land-ldpi」 「layout-land-mdpi」 「layout-land-xhdpi」 フォルダを作り、解像度ごとに位置の微調整は行なっていますが、 画面サイズが異なるとやはりずれてしまいます。 解像度、画面サイズにかかわらず位置を固定する方法はないのでしょうか。 ない場合はやはり画面サイズと解像度すべての条件のxmlを作成する必要がありますか。 また、そもそも 「イメージ的には背景用のImageViewに画像を表示して、間違っている箇所にカラのImageViewを載せる」やり方が間違っているのでしょうか。

専門家に質問してみよう