Android Fragment SQLite 1
This tutorial is adapted from http://androidopentutorials.com/android-sqlite-example/.
1) Startup
Follow Startup Project Tutorial or Download Zip here.
2) Disable Floating Action Button
The startup project contains Floating Action Button which will not be used in this tutorial.
Disable the button in activity_main.xml and MainActivity.java
File Name: Res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context=".MainActivity"> <android.support.design.widget.AppBarLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_main" /> <!-- <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" android:src="@android:drawable/ic_dialog_email" /> --> </android.support.design.widget.CoordinatorLayout> |
File Name: MainActivity.java
package com.notarazi.myviewpagertablayout1; import android.os.Bundle; import android.support.design.widget.TabLayout; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); // Get the ViewPager and set it's PagerAdapter so that it can display items ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager); viewPager.setAdapter(new MainFragmentPagerAdapter(getSupportFragmentManager(), MainActivity.this)); // Give the TabLayout the ViewPager TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs); tabLayout.setupWithViewPager(viewPager); //FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); //fab.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View view) { // Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) // .setAction("Action", null).show(); // } //}); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, 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(); //noinspection SimplifiableIfStatement if (id == R.id.action_add) { return true; } return super.onOptionsItemSelected(item); } } |
3) Edit Resource Files
File Name: Res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <color name="transparent">#00000000</color> <color name="list_item_bg">#ffffff</color> <color name="view_divider_color">#BABABA</color> </resources> |
File Name: Res/values/strings.xml
<resources> <string name="app_name">AndroidSQLite</string> <string name="add_emp">Add Employee</string> <string name="update_emp">Update Employee</string> <string name="name">Employee Name</string> <string name="dob">Employee DOB</string> <string name="salary">Employee Salary</string> <string name="update">Update</string> <string name="add">Add</string> <string name="reset">Reset</string> <string name="cancel">Cancel</string> </resources> |
File Name: Res/menu/menu_main.xml
Take note that the statement to display menu item as action button uses the tag word “app:” instead of “android:”. Refer the orange highlighted color text below.
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> <item android:id="@+id/action_add" android:icon="@android:drawable/ic_menu_add" android:orderInCategory="100" app:showAsAction="always" android:title="@string/add_emp" tools:ignore="AppCompatResource" /> </menu> |
File Name: Res/layout/activity_main.xml
No changes were made. |
File Name: Res/layout/fragment_page1.xml
(This file follows fragment_add_emp.xml file of the original tutorial)
(Replace the whole content)
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" > <EditText android:id="@+id/etxt_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:hint="@string/name" android:nextFocusDown="@+id/etxt_dob" android:singleLine="true" /> <EditText android:id="@+id/etxt_dob" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/etxt_name" android:hint="@string/dob" android:nextFocusDown="@+id/etxt_salary" android:singleLine="true" /> <EditText android:id="@+id/etxt_salary" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/etxt_dob" android:hint="@string/salary" android:inputType="numberDecimal" android:singleLine="true" /> <LinearLayout android:id="@+id/layout_submit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/etxt_salary" android:layout_margin="5dp" android:orientation="horizontal" android:weightSum="2" > <Button android:id="@+id/button_add" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/add" /> <Button android:id="@+id/button_reset" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/reset" /> </LinearLayout> </RelativeLayout> </ScrollView> |
File Name: Res/layout/fragment_page2.xml
(This file follows fragment_emp_file.xml file of the original tutorial)
(Replace the whole content)
<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" android:background="#EDEDED" > <ListView android:id="@+id/list_emp" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@color/transparent" android:dividerHeight="10dp" android:drawSelectorOnTop="true" android:footerDividersEnabled="false" android:padding="10dp" android:scrollbarStyle="outsideOverlay" /> </RelativeLayout> |
File Name: Res/layout/list_item.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/list_item_bg" android:descendantFocusability="blocksDescendants" > <RelativeLayout android:id="@+id/layout_item" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/txt_emp_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="6dp" /> <TextView android:id="@+id/txt_emp_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/txt_emp_id" android:padding="6dp" /> <TextView android:id="@+id/txt_emp_dob" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/txt_emp_name" android:padding="6dp" /> <TextView android:id="@+id/txt_emp_salary" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/txt_emp_dob" android:padding="6dp" /> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_below="@+id/layout_item" android:background="@color/view_divider_color" /> </RelativeLayout> |
4) Run
DOWNLOAD : AndroidFragmentSQLite1.zip