Autore Topic: Inserire una WebView  (Letto 704 volte)

Offline domy18

  • Nuovo arrivato
  • *
  • Post: 36
  • Respect: 0
    • Mostra profilo
    • Guadagnare con le App
  • Dispositivo Android:
    samsung s3
  • Sistema operativo:
    Android
Inserire una WebView
« il: 28 Giugno 2014, 03:31:03 CEST »
0
Ciao a tutti non riesco a inserire una WebView su questa activity
Citazione
public class MainActivity extends ActionBarActivity implements
      ActionBar.TabListener {

   /**
    * The {@link android.support.v4.view.PagerAdapter} that will provide
    * fragments for each of the sections. We use a {@link FragmentPagerAdapter}
    * derivative, which will keep every loaded fragment in memory. If this
    * becomes too memory intensive, it may be best to switch to a
    * {@link android.support.v4.app.FragmentStatePagerAdapter}.
    */
   SectionsPagerAdapter mSectionsPagerAdapter;

   /**
    * The {@link ViewPager} that will host the section contents.
    */
   ViewPager mViewPager;

   @Override
   protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);

      // Set up the action bar.
      final ActionBar actionBar = getSupportActionBar();
      actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

      // Create the adapter that will return a fragment for each of the three
      // primary sections of the activity.
      mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

      // Set up the ViewPager with the sections adapter.
      mViewPager = (ViewPager) findViewById(R.id.pager);
      mViewPager.setAdapter(mSectionsPagerAdapter);

      // When swiping between different sections, select the corresponding
      // tab. We can also use ActionBar.Tab#select() to do this if we have
      // a reference to the Tab.
      mViewPager
            .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
               @Override
               public void onPageSelected(int position) {
                  actionBar.setSelectedNavigationItem(position);
               }
            });

      // For each of the sections in the app, add a tab to the action bar.
      for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
         // Create a tab with text corresponding to the page title defined by
         // the adapter. Also specify this Activity object, which implements
         // the TabListener interface, as the callback (listener) for when
         // this tab is selected.
         actionBar.addTab(actionBar.newTab()
               .setText(mSectionsPagerAdapter.getPageTitle(i))
               .setTabListener(this));
      }
   }

   // this is called when the screen rotates.
   // (onCreate is no longer called when screen rotates due to manifest, see:
   // android:configChanges)
   @Override
   public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
   }
   
    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
        return super.onMenuOpened(featureId, menu);
    }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {

      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.about, menu);
      return true;
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
      if (id == R.id.action_about) {
            // Start about activity
            Intent myIntent = new Intent(this, AboutActivity.class);
            this.startActivity(myIntent);
         return true;
      }
      return super.onOptionsItemSelected(item);
   }

   @Override
   public void onTabSelected(ActionBar.Tab tab,
         FragmentTransaction fragmentTransaction) {
      // When the given tab is selected, switch to the corresponding page in
      // the ViewPager.
      mViewPager.setCurrentItem(tab.getPosition());
   }

   @Override
   public void onTabUnselected(ActionBar.Tab tab,
         FragmentTransaction fragmentTransaction) {
   }

   @Override
   public void onTabReselected(ActionBar.Tab tab,
         FragmentTransaction fragmentTransaction) {
   }

   /**
    * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
    * one of the sections/tabs/pages.
    */
   public class SectionsPagerAdapter extends FragmentPagerAdapter {

      public SectionsPagerAdapter(FragmentManager fm) {
         super(fm);
      }

      @Override
      public Fragment getItem(int position) {

         Fragment returnFragment;

         // getItem is called to instantiate the fragment for the given page.
         // Return a PlaceholderFragment (defined as a static inner class
         // below).
         switch (position) {
         case 0:
            returnFragment = BannerFragment.newInstance(position + 1);
            break;

         case 1:
            returnFragment = InterstitialFragment.newInstance(position + 1);
            break;

         case 2:
            returnFragment = InterstitialRewardedFragment
                  .newInstance(position + 1);
            break;
         default:
            returnFragment = null;
            break;
         }
         return returnFragment;
      }

      @Override
      public int getCount() {
         // Show 3 total pages.
         return 3;
      }

      @Override
      public CharSequence getPageTitle(int position) {
         Locale l = Locale.getDefault();
         switch (position) {
         case 0:
            return getString(R.string.title_section1).toUpperCase(l);
         case 1:
            return getString(R.string.title_section2).toUpperCase(l);
         case 2:
            return getString(R.string.title_section3).toUpperCase(l);
         }
         return null;
      }
   }

   /**
    * A placeholder fragment containing a simple view.
    */
   public static class BannerFragment extends Fragment {
      /**
       * The fragment argument representing the section number for this
       * fragment.
       */
      private static final String ARG_SECTION_NUMBER = "section_number";

      private ViewGroup mBannerContainer;
      private EbzBanner mEbzBanner;

      /**
       * Returns a new instance of this fragment for the given section number.
       */
      public static BannerFragment newInstance(int sectionNumber) {
         BannerFragment fragment = new BannerFragment();
         Bundle args = new Bundle();
         args.putInt(ARG_SECTION_NUMBER, sectionNumber);
         fragment.setArguments(args);
         return fragment;
      }

      public BannerFragment() {
      }

      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

         final View rootView = inflater.inflate(R.layout.banner_main,
               container, false);

         // Retrieve the banner container
         mBannerContainer = (RelativeLayout) rootView
               .findViewById(R.id.ad_content_view);

         final View progressIndicator = rootView
               .findViewById(R.id.loadingWheel2);
         progressIndicator.setVisibility(View.INVISIBLE);

         // Create the EbzBanner controller
         mEbzBanner = new EbzBanner(getActivity(), mBannerContainer,
               "TAG_DEMO_ANDROID");

         // Set the keywords to target more accurately
         mEbzBanner.setKeywords(new String[] { "these", "are",
               "some keywords" });
         
         // Set the listener
         mEbzBanner.setListener(new EbzBanner.EbzBannerListener() {
            @Override
            public boolean onLoadSuccess() {
               Toast.makeText(getActivity(), "banner loaded successfully",
                     Toast.LENGTH_SHORT).show();
               progressIndicator.setVisibility(View.INVISIBLE);
               return false;
            }

            @Override
            public boolean onLoadFail(EbzError e) {
               Toast.makeText(getActivity(),
                     "banner failed to load: " + e.getMessage(),
                     Toast.LENGTH_SHORT).show();
               progressIndicator.setVisibility(View.INVISIBLE);
               return false;
            }

            @Override
            public boolean onExpand() {
               Toast.makeText(getActivity(), "banner expands",
                     Toast.LENGTH_SHORT).show();
               return false;
            }

            @Override
            public boolean onClose() {
               Toast.makeText(getActivity(), "banner closes expand",
                     Toast.LENGTH_SHORT).show();
               return false;
            }

         });

         Button loadButton = (Button) rootView.findViewById(R.id.loadButton);
         loadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // Load the banner
               mEbzBanner.load();
               progressIndicator.setVisibility(View.VISIBLE);
            }
         });

         return rootView;
      }
   }

   /**
    * A placeholder fragment containing a simple view.
    */
   public static class InterstitialFragment extends Fragment {
      /**
       * The fragment argument representing the section number for this
       * fragment.
       */
      private static final String ARG_SECTION_NUMBER = "section_number";
      boolean isRewardBool = false;

      private EbzInterstitial mEbzInterstitial;

      /**
       * Returns a new instance of this fragment for the given section number.
       */
      public static InterstitialFragment newInstance(int sectionNumber) {
         InterstitialFragment fragment = new InterstitialFragment();
         Bundle args = new Bundle();
         args.putInt(ARG_SECTION_NUMBER, sectionNumber);
         fragment.setArguments(args);
         return fragment;
      }

      public InterstitialFragment() {
      }

      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
         final View rootView = inflater.inflate(R.layout.interstitial_main,
               container, false);

         // Create the EbzInterstitial controller
         mEbzInterstitial = new EbzInterstitial(getActivity(),
               "TAG_DEMO_ANDROID");

         final View progressIndicator = rootView
               .findViewById(R.id.loadingWheel);
         progressIndicator.setVisibility(View.INVISIBLE);

         final Button showButton = (Button) rootView
               .findViewById(R.id.showButton);
         showButton.setVisibility(View.INVISIBLE);
         showButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               mEbzInterstitial.show();
            }
         });

         final Button loadButton = (Button) rootView
               .findViewById(R.id.loadButton);
         loadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               showButton.setVisibility(View.INVISIBLE);
               progressIndicator.setVisibility(View.VISIBLE);

               mEbzInterstitial.load();
            }
         });

         // Set the keywords to target more accurately
         mEbzInterstitial.setKeywords(new String[] { "these", "are",
               "some keywords" });

         // Set the listener
         mEbzInterstitial
               .setListener(new EbzInterstitial.EbzInterstitialListener() {
                  @Override
                  public boolean onLoadSuccess() {
                     // Interstitial successfully loaded
                     Toast.makeText(getActivity(),
                           "Interstitial loaded successfully",
                           Toast.LENGTH_SHORT).show();
                     showButton.setVisibility(View.VISIBLE);
                     progressIndicator.setVisibility(View.INVISIBLE);
                     return false;
                  }

                  @Override
                  public boolean onLoadFail(EbzError e) {
                     Toast.makeText(
                           getActivity(),
                           "Interstitial failed to load: "
                                 + e.getMessage(),
                           Toast.LENGTH_SHORT).show();
                     progressIndicator.setVisibility(View.INVISIBLE);
                     return false;
                  }

                  @Override
                  public boolean onDisplayFullscreen() {
                     Toast.makeText(getActivity(),
                           "Interstitial takes over fullscreen",
                           Toast.LENGTH_SHORT).show();
                     return false;
                  }

                  @Override
                  public boolean onCloseFullscreen() {
                     showButton.setVisibility(View.INVISIBLE);
                     Toast.makeText(getActivity(),
                           "Interstitial dismisses fullscreen",
                           Toast.LENGTH_SHORT).show();
                     return false;
                  }

                  @Override
                  public boolean onRewardUnlocked() {
                     // used here because interstitial is not rewarded
                     return false;
                  }
               });

         return rootView;
      }
   }

   /**
    * A placeholder fragment containing a simple view.
    */
   public static class InterstitialRewardedFragment extends Fragment {
      /**
       * The fragment argument representing the section number for this
       * fragment.
       */
      private static final String ARG_SECTION_NUMBER = "section_number";
      TextView mTextView;
      int rewardQuantity = 0;

      private EbzInterstitial mEbzInterstitialRewarded;

      /**
       * Returns a new instance of this fragment for the given section number.
       */
      public static InterstitialRewardedFragment newInstance(int sectionNumber) {
         InterstitialRewardedFragment fragment = new InterstitialRewardedFragment();
         Bundle args = new Bundle();
         args.putInt(ARG_SECTION_NUMBER, sectionNumber);
         fragment.setArguments(args);
         return fragment;
      }

      public InterstitialRewardedFragment() {
      }

      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
         final View rootView = inflater.inflate(
               R.layout.interstitial_rewarded_main, container, false);

         mTextView = (TextView) rootView.findViewById(R.id.textView);
         mTextView.append(" (reward quantity : " + rewardQuantity + ")");

         // Create the EbzInterstitial controller
         mEbzInterstitialRewarded = new EbzInterstitial(getActivity(),
               "TAG_DEMO_ANDROID", true);

         final View progressIndicator = rootView
               .findViewById(R.id.loadingWheel);
         progressIndicator.setVisibility(View.INVISIBLE);

         final Button showButton = (Button) rootView
               .findViewById(R.id.showButton);
         showButton.setVisibility(View.INVISIBLE);
         showButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               mEbzInterstitialRewarded.show();
            }
         });

         final Button loadButton = (Button) rootView
               .findViewById(R.id.loadButton);
         loadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               showButton.setVisibility(View.INVISIBLE);
               progressIndicator.setVisibility(View.VISIBLE);

               mEbzInterstitialRewarded.load();
            }
         });

         // Set the keywords to target more accurately
         mEbzInterstitialRewarded.setKeywords(new String[] { "these", "are",
               "some keywords" });

         // Set the listener
         mEbzInterstitialRewarded
               .setListener(new EbzInterstitial.EbzInterstitialListener() {
                  @Override
                  public boolean onLoadSuccess() {
                     // Interstitial successfully loaded
                     Toast.makeText(getActivity(),
                           "Interstitial loaded successfully",
                           Toast.LENGTH_SHORT).show();
                     showButton.setVisibility(View.VISIBLE);
                     progressIndicator.setVisibility(View.INVISIBLE);
                     return false;
                  }

                  @Override
                  public boolean onLoadFail(EbzError e) {
                     Toast.makeText(
                           getActivity(),
                           "Interstitial failed to load: "
                                 + e.getMessage(),
                           Toast.LENGTH_SHORT).show();
                     progressIndicator.setVisibility(View.INVISIBLE);
                     return false;
                  }

                  @Override
                  public boolean onDisplayFullscreen() {
                     Toast.makeText(getActivity(),
                           "Interstitial takes over fullscreen",
                           Toast.LENGTH_SHORT).show();
                     return false;
                  }

                  @Override
                  public boolean onCloseFullscreen() {
                     showButton.setVisibility(View.INVISIBLE);
                     Toast.makeText(getActivity(),
                           "Interstitial dismisses fullscreen",
                           Toast.LENGTH_SHORT).show();
                     mTextView.setText("Reward quantity : "
                           + rewardQuantity + "");
                     return false;
                  }

                  @Override
                  public boolean onRewardUnlocked() {
                     Toast.makeText(getActivity(), "View rewarded!",
                           Toast.LENGTH_SHORT).show();
                     rewardQuantity++;
                     mTextView.setText("Reward quantity : "
                           + rewardQuantity + "");
                     return false;
                  }
               });

         return rootView;
      }
   }
}

Offline elfo83

  • Utente normale
  • ***
  • Post: 287
  • Respect: +23
    • Mostra profilo
  • Sistema operativo:
    Mac OS 10.8.2
Re:Inserire una WebView
« Risposta #1 il: 01 Luglio 2014, 11:01:32 CEST »
0
In che senso non riesci a inserirla? In primis dovresti aggiungere la WebView nel file mainactivity.xml e poi dichiararla nella classe mainactivity:

Codice (Java): [Seleziona]
WebView web = (WebView)findViewById(R.id.tuaWebView);