Skip to content

Instantly share code, notes, and snippets.

@deanpanayotov
Created February 6, 2014 13:48
Show Gist options
  • Save deanpanayotov/8844483 to your computer and use it in GitHub Desktop.
Save deanpanayotov/8844483 to your computer and use it in GitHub Desktop.
A simple test using v7 appcomapt library. Tested on 2.3.3 emulator.
<?xml version="1.0" encoding="utf-8"?>
<SeekBar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/seekBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</SeekBar>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/timeDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<fragment
android:id="@+id/pickerFragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
class="com.example.testfragments.TestFragment" >
</fragment>
</LinearLayout>
package com.example.testfragments;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.SeekBar;
import android.widget.TextView;
import com.example.testfragments.TestFragment.OnNumberPickedListener;
/**
* Updates the value of the {@link TextView} according to the changes in the
* {@link SeekBar}.
*
* @author dpanayotov
*
*/
public class MainActivity extends FragmentActivity implements
OnNumberPickedListener {
TextView timeDisplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
timeDisplay = (TextView) findViewById(R.id.timeDisplay);
}
@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;
}
@Override
public void onNumberPicked(int number) {
timeDisplay.setText(Integer.toString(number));
}
}
package com.example.testfragments;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
/**
* A simple test fragment consisting of just one {@link SeekBar}
*
* @author dpanayotov
*
*/
public class TestFragment extends Fragment {
/**
* Must be implemented by the activity to which {@link TestFragment} is
* attached. This way the fragment doesn't need to know anything about the
* activity. Fragments shouldn't be communicating directly - information
* should be handled by the activity.
*/
public interface OnNumberPickedListener {
public void onNumberPicked(int number);
}
/**
* Keep a reference to the activity to which the fragment is attached
* through this field
*/
private OnNumberPickedListener listener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// set the layout
View view = inflater.inflate(R.layout.fragment, container, false);
// find the number picker and add a value change listener that will call
// the activity
SeekBar seekBar = (SeekBar) view.findViewById(R.id.seekBar);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
listener.onNumberPicked(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
return view;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// check if the activity to which the fragment attaches implements the
// required interface
if (activity instanceof OnNumberPickedListener) {
listener = (OnNumberPickedListener) activity;
} else {
throw new ClassCastException(activity.toString()
+ " must implemenet MyListFragment.OnNumberPickedListener");
}
}
@Override
public void onDetach() {
super.onDetach();
listener = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment