Переглянути джерело

Initial commit for FragmentsDynamicLayout project.

Cristian Tanas 11 роки тому
батько
коміт
dc737d286f

+ 27 - 0
FragmentsDynamicLayout/AndroidManifest.xml

@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="org.uab.android.ui.fragments.dynamiclayout"
+    android:versionCode="1"
+    android:versionName="1.0" >
+
+    <uses-sdk
+        android:minSdkVersion="14"
+        android:targetSdkVersion="19" />
+
+    <application
+        android:allowBackup="true"
+        android:icon="@drawable/ic_launcher"
+        android:label="@string/app_name"
+        android:theme="@style/AppTheme" >
+        <activity
+            android:name=".MainActivity"
+            android:label="@string/app_name" >
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+    </application>
+
+</manifest>

BIN
FragmentsDynamicLayout/ic_launcher-web.png


BIN
FragmentsDynamicLayout/res/drawable-hdpi/ic_launcher.png


BIN
FragmentsDynamicLayout/res/drawable-mdpi/ic_launcher.png


BIN
FragmentsDynamicLayout/res/drawable-xhdpi/ic_launcher.png


BIN
FragmentsDynamicLayout/res/drawable-xxhdpi/ic_launcher.png


+ 14 - 0
FragmentsDynamicLayout/res/layout/activity_main.xml

@@ -0,0 +1,14 @@
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    tools:context="${relativePackage}.${activityClass}" >
+
+    <FrameLayout 
+        android:id="@+id/fragment_container"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent">
+        
+    </FrameLayout>
+
+</RelativeLayout>

+ 176 - 0
FragmentsDynamicLayout/res/layout/fragment_form.xml

@@ -0,0 +1,176 @@
+<?xml version="1.0" encoding="utf-8"?>
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent" >
+    
+    <TextView
+        android:id="@+id/sep1"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_alignParentLeft="true"
+        android:layout_marginTop="5dp"
+        android:text="@string/class_sep"
+        style="?android:attr/listSeparatorTextViewStyle" />
+
+    <TextView
+        android:id="@+id/classLabel"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_below="@+id/sep1"
+        android:layout_alignParentLeft="true"
+        android:layout_marginTop="15dp"
+        android:layout_marginLeft="10dp"
+        android:text="@string/class_name" />
+
+    <AutoCompleteTextView
+        android:id="@+id/classAutoComplete"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_alignBaseline="@+id/classLabel"
+        android:layout_alignBottom="@+id/classLabel"
+        android:layout_marginLeft="16dp"
+        android:layout_toRightOf="@+id/classLabel"
+        android:ems="10" >
+        
+        <requestFocus />
+ 	</AutoCompleteTextView>
+
+    <TextView
+        android:id="@+id/creditLabel"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_alignLeft="@+id/classLabel"
+        android:layout_below="@+id/classLabel"
+        android:layout_marginTop="30dp"
+        android:text="@string/credits" />
+
+    <EditText
+        android:id="@+id/creditsEditText"
+        android:layout_width="50dp"
+        android:layout_height="wrap_content"
+        android:layout_alignBottom="@+id/creditLabel"
+        android:layout_toRightOf="@+id/creditLabel"
+        android:layout_marginLeft="16dp"
+        android:ems="10"
+        android:inputType="numberDecimal" >
+    </EditText>
+
+    <TextView
+        android:id="@+id/sep2"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_alignParentLeft="true"
+        android:layout_below="@+id/creditLabel"
+        android:layout_marginTop="25dp"
+        android:text="@string/timetable_sep"
+        style="?android:attr/listSeparatorTextViewStyle" />
+
+    <TextView
+        android:id="@+id/dayOfWeekLabel"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_alignLeft="@+id/classLabel"
+        android:layout_below="@+id/sep2"
+        android:layout_marginTop="15dp"
+        android:text="@string/day_of_week" />
+
+    <CheckBox
+        android:id="@+id/checkBoxMon"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_alignParentLeft="true"
+        android:layout_below="@+id/dayOfWeekLabel"
+        android:layout_marginTop="10dp"
+        android:text="@string/monday" />
+    
+    <CheckBox
+        android:id="@+id/checkBoxTue"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_alignBaseline="@+id/checkBoxMon"
+        android:layout_alignBottom="@+id/checkBoxMon"
+        android:layout_toRightOf="@+id/checkBoxMon"
+        android:text="@string/tuesday" />
+
+    <CheckBox
+        android:id="@+id/checkBoxWed"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_alignBaseline="@+id/checkBoxTue"
+        android:layout_alignBottom="@+id/checkBoxTue"
+        android:layout_toRightOf="@+id/checkBoxTue"
+        android:text="@string/wednesday" />
+
+    <CheckBox
+        android:id="@+id/checkBoxThu"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_alignBaseline="@+id/checkBoxWed"
+        android:layout_alignBottom="@+id/checkBoxWed"
+        android:layout_toRightOf="@+id/checkBoxWed"
+        android:text="@string/thursday" />
+
+    <CheckBox
+        android:id="@+id/checkBoxFri"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_alignBaseline="@+id/checkBoxThu"
+        android:layout_alignBottom="@+id/checkBoxThu"
+        android:layout_toRightOf="@+id/checkBoxThu"
+        android:text="@string/friday" />
+
+    <TextView
+        android:id="@+id/hourLabel"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_alignLeft="@+id/classLabel"
+        android:layout_below="@+id/checkBoxMon"
+        android:layout_marginTop="20dp"
+        android:text="@string/hour" />
+
+    <TextView
+        android:id="@+id/defaultHourLabel"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_alignBaseline="@+id/hourLabel"
+        android:layout_alignBottom="@+id/hourLabel"
+        android:layout_toRightOf="@+id/hourLabel"
+        android:layout_marginLeft="15dp"
+        android:text="@string/default_hour_label" />
+
+    <Button
+        android:id="@+id/setHourButton"
+        style="?android:attr/buttonStyleSmall"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_alignBaseline="@+id/defaultHourLabel"
+        android:layout_alignBottom="@+id/defaultHourLabel"
+        android:layout_toRightOf="@+id/defaultHourLabel"
+        android:layout_marginLeft="10dp"
+        android:text="@string/sethour" />
+
+    <Button
+        android:id="@+id/saveButton"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_alignParentBottom="true"
+        android:layout_alignParentLeft="true"
+        android:text="@string/submit" />
+
+    <Button
+        android:id="@+id/clearButton"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_alignParentBottom="true"
+        android:layout_toRightOf="@+id/saveButton"
+        android:text="@string/clear" />
+
+    <Button
+        android:id="@+id/cancelButton"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_alignParentBottom="true"
+        android:layout_toRightOf="@+id/clearButton"
+        android:text="@string/cancel" />
+
+</RelativeLayout>

+ 8 - 0
FragmentsDynamicLayout/res/layout/list_item.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<TextView xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:padding="5dp" >
+    
+
+</TextView>

+ 11 - 0
FragmentsDynamicLayout/res/values-v11/styles.xml

@@ -0,0 +1,11 @@
+<resources>
+
+    <!--
+        Base application theme for API 11+. This theme completely replaces
+        AppBaseTheme from res/values/styles.xml on API 11+ devices.
+    -->
+    <style name="AppBaseTheme" parent="android:Theme.Holo.Light">
+        <!-- API 11 theme customizations can go here. -->
+    </style>
+
+</resources>

+ 12 - 0
FragmentsDynamicLayout/res/values-v14/styles.xml

@@ -0,0 +1,12 @@
+<resources>
+
+    <!--
+        Base application theme for API 14+. This theme completely replaces
+        AppBaseTheme from BOTH res/values/styles.xml and
+        res/values-v11/styles.xml on API 14+ devices.
+    -->
+    <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
+        <!-- API 14 theme customizations can go here. -->
+    </style>
+
+</resources>

+ 12 - 0
FragmentsDynamicLayout/res/values/arrays.xml

@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+    
+    <string-array name="classes">
+        <item>Desarrollo de Apps Móviles</item>
+        <item>Desarrollo de Apps en plataforma Android</item>
+        <item>Desarrollo de Apps en plataforma iOS</item>
+        <item>Desarrollo de Apps en plataforma Windows Phone</item>
+        <item>Desarrollo de Apps Multiplataforma</item>
+    </string-array>
+    
+</resources>

+ 23 - 0
FragmentsDynamicLayout/res/values/strings.xml

@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+
+    <string name="app_name">FragmentsDynamicLayout</string>
+    <string name="hello_world">Hello world!</string>
+    <string name="class_name">Nombre asignatura</string>
+    <string name="credits">Créditos</string>
+    <string name="class_sep">Datos asignatura</string>
+    <string name="timetable_sep">Horario</string>
+    <string name="day_of_week">Día de la semana</string>
+    <string name="monday">Lun</string>
+    <string name="tuesday">Mar</string>
+    <string name="wednesday">Mie</string>
+    <string name="thursday">Jue</string>
+    <string name="friday">Vie</string>
+    <string name="hour">Hora:</string>
+    <string name="default_hour_label">00:00</string>
+    <string name="sethour">Establecer</string>
+    <string name="submit">Guardar</string>
+    <string name="clear">Limpiar</string>
+    <string name="cancel">Cancelar</string>
+
+</resources>

+ 20 - 0
FragmentsDynamicLayout/res/values/styles.xml

@@ -0,0 +1,20 @@
+<resources>
+
+    <!--
+        Base application theme, dependent on API level. This theme is replaced
+        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
+    -->
+    <style name="AppBaseTheme" parent="android:Theme.Light">
+        <!--
+            Theme customizations available in newer API levels can go in
+            res/values-vXX/styles.xml, while customizations related to
+            backward-compatibility can go here.
+        -->
+    </style>
+
+    <!-- Application theme. -->
+    <style name="AppTheme" parent="AppBaseTheme">
+        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
+    </style>
+
+</resources>

+ 86 - 0
FragmentsDynamicLayout/src/org/uab/android/ui/fragments/dynamiclayout/FormFragment.java

@@ -0,0 +1,86 @@
+package org.uab.android.ui.fragments.dynamiclayout;
+
+import android.app.Activity;
+import android.app.Fragment;
+import android.os.Bundle;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.view.ViewGroup;
+import android.widget.ArrayAdapter;
+import android.widget.AutoCompleteTextView;
+import android.widget.Button;
+import android.widget.TextView;
+
+public class FormFragment extends Fragment {
+	
+	OnSetHourButtonClickListener 	setHourListener;
+	Activity						parentActivity;
+	
+	AutoCompleteTextView	classesAutoCompleteTextView;
+	TextView				defaultHourTextView;
+	Button					setHourButton;
+	
+	@Override
+	public void onAttach(Activity activity) {
+		super.onAttach(activity);
+		
+		// Enforces that the Activity implements the OnSetHourButtonClickListener
+		try {
+			setHourListener = (OnSetHourButtonClickListener) activity;
+		} catch (ClassCastException e) {
+			throw new ClassCastException(activity.toString() + " must implement OnSetHourButtonClickListener.");
+		}
+	}
+
+	@Override
+	public View onCreateView(LayoutInflater inflater, ViewGroup container,
+			Bundle savedInstanceState) {
+		
+		// Inflate the Fragment's layout from XML
+		View layoutView = inflater.inflate(R.layout.fragment_form, container, false);
+		return layoutView;
+	}
+	
+	@Override
+	public void onActivityCreated(Bundle savedInstanceState) {
+		super.onActivityCreated(savedInstanceState);
+		
+		// Get a reference to the parent Activity
+		parentActivity = getActivity();
+		
+		// Get a reference to the UI element
+		classesAutoCompleteTextView = (AutoCompleteTextView) 
+				parentActivity.findViewById(R.id.classAutoComplete);
+
+		// Set an ArrayAdapter containing pre-define classes names
+		ArrayAdapter<CharSequence> classesAdapter = ArrayAdapter.
+				createFromResource(parentActivity, R.array.classes, R.layout.list_item);
+		classesAutoCompleteTextView.setAdapter(classesAdapter);
+		
+		setHourButton = (Button) parentActivity.findViewById(R.id.setHourButton);
+		
+		// Set an OnClickListener for the set hour Button
+		setHourButton.setOnClickListener(new OnClickListener() {
+			
+			@Override
+			public void onClick(View v) {
+				
+				setHourListener.onSetHourClicked(v);
+			}
+		});
+		
+		defaultHourTextView = (TextView) parentActivity.findViewById(R.id.defaultHourLabel);
+	}
+	
+	// The Activity will call this function in order to display the new time set
+	public void updateHourLabel(int hourOfDay, int minute) {
+		
+		// Set the hour and minute on the corresponding TextView
+		defaultHourTextView.setText(hourOfDay + ":" + minute);
+	}
+	
+	public interface OnSetHourButtonClickListener {
+		void onSetHourClicked(View v);
+	}
+}

+ 52 - 0
FragmentsDynamicLayout/src/org/uab/android/ui/fragments/dynamiclayout/MainActivity.java

@@ -0,0 +1,52 @@
+package org.uab.android.ui.fragments.dynamiclayout;
+
+import android.app.Activity;
+import android.app.DialogFragment;
+import android.app.FragmentManager;
+import android.app.FragmentTransaction;
+import android.os.Bundle;
+import android.view.View;
+
+public class MainActivity extends Activity implements 
+				FormFragment.OnSetHourButtonClickListener,
+				TimePickerFragment.OnTimeSetByUserListener {
+	
+	static final String TIME_PICKER_FRAGMENT_TAG = "TIME_PICKER_FRAGMENT";
+	
+	FragmentManager		fragmentManager;
+	FormFragment		formFragment = new FormFragment();
+
+	@Override
+	protected void onCreate(Bundle savedInstanceState) {
+		super.onCreate(savedInstanceState);
+		setContentView(R.layout.activity_main);
+		
+		// Get a reference to the FragmentManager
+		fragmentManager = getFragmentManager();
+		
+		// Start a new FragmentTransaction
+		FragmentTransaction transaction = fragmentManager.beginTransaction();
+		
+		// Add the FormFragment to the layout
+		transaction.add(R.id.fragment_container, formFragment);
+		
+		// Commit the FragmentTransaction
+		transaction.commit();
+	}
+
+	@Override
+	public void onTimeSet(int hourOfDay, int minute) {
+		
+		// The user has set the time using the TimePickerDialog
+		// Update the time label within the Fragment's layout
+		formFragment.updateHourLabel(hourOfDay, minute);
+	}
+
+	@Override
+	public void onSetHourClicked(View v) {
+		
+		// When user click on the set hour Button create a new TimePickerFragment
+		DialogFragment timePickerFragment = new TimePickerFragment();
+		timePickerFragment.show(getFragmentManager(), TIME_PICKER_FRAGMENT_TAG);
+	}
+}

+ 54 - 0
FragmentsDynamicLayout/src/org/uab/android/ui/fragments/dynamiclayout/TimePickerFragment.java

@@ -0,0 +1,54 @@
+package org.uab.android.ui.fragments.dynamiclayout;
+
+import java.util.Calendar;
+
+import android.app.Dialog;
+import android.app.DialogFragment;
+import android.app.TimePickerDialog;
+import android.os.Bundle;
+import android.text.format.DateFormat;
+import android.widget.TimePicker;
+
+public class TimePickerFragment extends DialogFragment implements 
+				TimePickerDialog.OnTimeSetListener {
+	
+	OnTimeSetByUserListener onTimeSetListener;
+	
+	@Override
+	public Dialog onCreateDialog(Bundle savedInstanceState) {
+		
+		// Get an instance of a Calendar to set the current time
+		Calendar c = Calendar.getInstance();
+		int hourOfDay = c.get(Calendar.HOUR_OF_DAY);
+		int minute = c.get(Calendar.MINUTE);
+		
+		// Create a new dialog that displays a TimePicker
+		TimePickerDialog timePickerDialog = new TimePickerDialog(
+				getActivity(),
+				this,
+				hourOfDay,
+				minute,
+				DateFormat.is24HourFormat(getActivity()));
+		return timePickerDialog;
+	}
+
+	// This function is invoked when the users set the hour and minute within the TimePicker
+	@Override
+	public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
+		
+		try {
+			
+			// Forward the event to the Activity containing the DialogFragment
+			onTimeSetListener = (OnTimeSetByUserListener) getActivity();
+			onTimeSetListener.onTimeSet(hourOfDay, minute);
+			
+		} catch (ClassCastException e) {
+			throw new ClassCastException(getActivity().toString() + " must implement OnTimeSetByUserListener.");
+		}
+	}
+	
+	// Interface to notify the Activity the user has set the time through the TimePickerDialog
+	public interface OnTimeSetByUserListener {
+		void onTimeSet(int hourOfDay, int minute);
+	}
+}