Thursday, 9 July 2015

Android Fragment Example

Android Fragment Example with List in One fragment and selected value displays in the Second fragment

activity_main.xml

<LinearLayout 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:orientation="horizontal"
    tools:context="com.example.fragmentproject.MainActivity">

    <fragment
        android:id="@+id/fragment"
        android:layout_width="170dp"
        android:layout_height="match_parent"
        class="com.example.fragmentproject.MenuFragment" />

    <fragment
        android:id="@+id/fragment2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.example.fragmentproject.TextFragment" />

</LinearLayout>


list_fragemnt.xml (fragment for list)

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@android:id/list" />
</LinearLayout>


text_fragment.xml (fragment to display toast)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:gravity="center"
    android:background="#5ba4e5"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40px"
        android:textColor="#ffffff"
        android:layout_gravity="center"
        android:id="@+id/AndroidOs"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textColor="#ffffff"
        android:textSize="30px"
        android:id="@+id/Version"/>

</LinearLayout>


MainActivity.java

package com.example.fragmentproject;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}


MenuFragment.java

package com.example.fragmentproject;


import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MenuFragment extends ListFragment {

    String[] AndroidOS = new String[] { "Cupcake","Donut","Eclair","Froyo","Gingerbread","Honeycomb","Ice Cream SandWich","Jelly Bean","KitKat" };

    String[] Version = new String[]{"1.5","1.6","2.0-2.1","2.2","2.3","3.0-3.2","4.0","4.1-4.3","4.4"};

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.list_fragment, container, false);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, AndroidOS);
        setListAdapter(adapter);
        return view;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        TextFragment txt = (TextFragment)getFragmentManager().findFragmentById(R.id.fragment2);
        txt.change(AndroidOS[position],"Version : "+Version[position]);
        getListView().setSelector(android.R.color.holo_blue_dark);
    }
}


TextFragment.java

package com.example.fragmentproject;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class TextFragment extends Fragment {
    TextView text,vers;

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.text_fragment, container, false);
        text= (TextView) view.findViewById(R.id.AndroidOs);
        vers= (TextView)view.findViewById(R.id.Version);
        return view;
    }
    public void change(String txt, String txt1){
        text.setText(txt);
        vers.setText(txt1);

    }
}

Screenshots

Run the project, the output appears on the mobile screen.



Tuesday, 3 March 2015

Android Spinner Example

Android Spinner and Toast displays the selected item in a Spinner.

Main Layout Xml

spinner_page.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Spinner
        android:id="@+id/spversions"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" />

</RelativeLayout>



MainActivity File

SpinnerPage.java

package com.example.spinner;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class SpinnerPage extends Activity implements OnItemSelectedListener {
    private Spinner spversions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.spinner_page);
        spversions = (Spinner) findViewById(R.id.spversions);

        String[] android_versions = { "Cupcake", "Donut", "Eclair", "Froyo",
                "Gingerbread", "Honeycomb", "Ice Cream Sandwich", "Jelly Bean",
                "KitKat", "Lolipop" };

        ArrayAdapter<String> adapter_versions = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, android_versions);

        adapter_versions
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spversions.setAdapter(adapter_versions);
        spversions.setOnItemSelectedListener(this);

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position,
            long id) {
        // TODO Auto-generated method stub
        spversions.setSelection(position);
        String selected_version = (String) spversions.getSelectedItem();
        Toast.makeText(getApplicationContext(),
                "Selected item:" + selected_version, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // TODO Auto-generated method stub

    }
}












Screen Shots

Selected item displays in a Toast



Android ListView

Android ListView with Toast displaying the Item selected.


MainLayout File

list_view_page.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ListView
        android:id="@+id/listversions"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</RelativeLayout>


ListView row TextView from different xml file (res/layout/samplerow.xml)

samplerow.xml 

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:textSize="15sp" >

</TextView>



Java Code in MainActivity oncreate method

ListViewPage.java

package com.example.listview;

import java.util.ArrayList;
import java.util.Arrays;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ListViewPage extends Activity {
    private ListView samplelistview;
    private ArrayAdapter<String> listadapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_view_page);

        // Find the Listview resource
        samplelistview = (ListView) findViewById(R.id.listversions);

        // Create List of android versions
        String[] android_versions = { "Cupcake", "Donut", "Eclair", "Froyo",
                "Gingerbread", "Honeycomb", "Ice Cream Sandwich", "Jelly Bean",
                "KitKat", "Lolipop" };
        ArrayList<String> androidlist = new ArrayList<String>();
        androidlist.addAll(Arrays.asList(android_versions));

        // create array adapter for androidlist
        listadapter = new ArrayAdapter<String>(this, R.layout.samplerow,
                androidlist);

        // set array adapter as ListView's adapter
        samplelistview.setAdapter(listadapter);

        samplelistview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                String Item_clicked = parent.getItemAtPosition(position)
                        .toString();
                Toast.makeText(getApplicationContext(),
                        "Version Selected:" + Item_clicked, Toast.LENGTH_SHORT)
                        .show();

            }
        });

    }
}


Screen Shots:
 Run the application.



Click on the item in the list, Toast displays the selected item.