Java tutorial
/* * Copyright 2012 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.vurf.vurfx.view; import android.app.ActionBar; import android.app.FragmentTransaction; import android.graphics.Color; import android.graphics.PorterDuff.Mode; import android.graphics.drawable.LayerDrawable; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.RatingBar; import android.widget.TextView; import com.vurf.vurfx.R; public class Place_template extends FragmentActivity implements ActionBar.TabListener { /** * The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the * three primary sections of the app. We use a {@link android.support.v4.app.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}. */ AppSectionsPagerAdapter mAppSectionsPagerAdapter; /** * The {@link ViewPager} that will display the three primary sections of the app, one at a * time. */ private ViewPager mViewPager; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_place_template); // Create the adapter that will return a fragment for each of the three primary sections // of the app. mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager()); // Set up the action bar. final ActionBar actionBar = getActionBar(); final String TitleNames[] = new String[] { "About", "Main", "Photos" }; // Specify that the Home/Up button should not be enabled, since there is no hierarchical // parent. actionBar.setHomeButtonEnabled(false); // Specify that we will be displaying tabs in the action bar. actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); // Set up the ViewPager, attaching the adapter and setting up a listener for when the // user swipes between sections. mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setAdapter(mAppSectionsPagerAdapter); mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { // When swiping between different app sections, select the corresponding tab. // We can also use ActionBar.Tab#select() to do this if we have a reference to the // Tab. actionBar.setSelectedNavigationItem(position); } }); // TODO swipy tabs //PagerTitleStrip mPagerTitleStrip = (PagerTitleStrip) findViewById(R.id.pager_title_strip); // For each of the sections in the app, add a tab to the action bar. for (int i = 0; i < mAppSectionsPagerAdapter.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 // listener for when this tab is selected. actionBar.addTab(actionBar.newTab().setText(TitleNames[i]).setTabListener(this)); } actionBar.hide(); //mViewPager.setCurrentItem(tab.getPosition()) mViewPager.setCurrentItem(1); } @Override public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } @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 onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } /** * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary * sections of the app. */ public static class AppSectionsPagerAdapter extends FragmentPagerAdapter { public AppSectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int i) { switch (i) { case 0: // The first section of the app is the most interesting -- it offers // a launchpad into the other demonstrations in this example application. return new AboutFragment(); case 1: return new MainFragment(); case 2: return new AboutFragment(); default: return new MainFragment(); } } @Override public int getCount() { return 3; } @Override public CharSequence getPageTitle(int position) { switch (position) { case 0: return "About"; case 1: return "Main"; case 2: return "Photos"; default: return "Title"; } } } /** * A fragment that launches other parts of the demo application. */ public static class MainFragment extends Fragment { private ImageView imageview_cover; private ImageView imageview_displaypicture; private ImageView imageview_map; private ImageView imageview_call; private TextView Title; private TextView Description; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_place_template_main, container, false); RatingBar ratingBar = (RatingBar) rootView.findViewById(R.id.rating); LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable(); //get default rating from server ratingBar.setRating(1); stars.getDrawable(2).setColorFilter(Color.parseColor("#FFE7711B"), Mode.SRC_ATOP); imageview_cover = (ImageView) rootView.findViewById(R.id.imageview_cover); imageview_displaypicture = (ImageView) rootView.findViewById(R.id.imageview_displaypicture); // imageview_map = (ImageView) rootView.findViewById(R.id.imageview_map); imageview_call = (ImageView) rootView.findViewById(R.id.imageview_calllocation); Title = (TextView) rootView.findViewById(R.id.textview_displayname); Description = (TextView) rootView.findViewById(R.id.textview_displayaddress); Title.setText("Zakir Tikka"); Description.setText("Finest Pakistani cuisine.\nLUMS,\nSector X, DHA, Lahore\nPh. 090078601"); // imageview_cover.setImageDrawable(getResources().getDrawable(R.drawable.tikka)); // imageview_displaypicture.setImageDrawable(getResources().getDrawable(R.drawable.marker192)); //imageview_map.setImageDrawable(getResources().getDrawable(R.drawable.button2)); //imageview_call.setImageDrawable(getResources().getDrawable(R.drawable.button1)); // Demonstration of a collection-browsing activity. /* rootView.findViewById(R.id.demo_collection_button) .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(getActivity(), MainActivity.class); startActivity(intent); } }); // Demonstration of navigating to external activities. rootView.findViewById(R.id.demo_external_activity) .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Create an intent that asks the user to pick a photo, but using // FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET, ensures that relaunching // the application from the device home screen does not return // to the external activity. Intent externalActivityIntent = new Intent(Intent.ACTION_DIAL); externalActivityIntent.setType("image/*"); externalActivityIntent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); startActivity(externalActivityIntent); } }); */ return rootView; } } /** * A dummy fragment representing a section of the app, but that simply displays dummy text. */ public static class AboutFragment extends Fragment { public static final String ARG_SECTION_NUMBER = "section_number"; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_place_template_about, container, false); return rootView; } } }