Java tutorial
/* * Copyright 2011 Google Inc. * * 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.glennbech.trafikkinfo.fragments; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.Vibrator; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.glennbech.trafikkinfo.activities.*; import com.norsktrafikkinfo.R; public class DashboardFragment extends Fragment { private Vibrator myVib; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //To change body of overridden methods use File | Settings | File Templates. myVib = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View root = inflater.inflate(R.layout.fragment_dashboard, container, false); root.findViewById(R.id.home_btn_near).setOnClickListener(new DashBoardButtonListener(MainActivity.class)); root.findViewById(R.id.home_btn_poi).setOnClickListener(new DashBoardButtonListener(POIActivity.class)); root.findViewById(R.id.home_btn_counties) .setOnClickListener(new DashBoardButtonListener(CountyListActivity.class)); root.findViewById(R.id.home_btn_mountain_pass) .setOnClickListener(new DashBoardButtonListener(MountainPassActivity.class)); root.findViewById(R.id.home_btn_config) .setOnClickListener(new DashBoardButtonListener(ConfigActivity.class)); return root; } class DashBoardButtonListener implements View.OnClickListener { private Class targetActivity; DashBoardButtonListener(Class targetActivity) { this.targetActivity = targetActivity; } public void onClick(View view) { vibrate(); Intent i = new Intent().setClass(DashboardFragment.this.getActivity(), targetActivity); startActivity(i); } private void vibrate() { myVib.vibrate(50); } } }