Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make ListView fragment in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 5.93k
    Comment on it

    In the below example code I have created a ListView fragment app. In first step, first I have created list_fragment.xml layout here I have added a ListView and TextView. In Step second, I have added a fragment in actvity_main.xml layout and then in step third I have created string-array for adding items name. Now See coding area, step fourth I have created MyListFragment class , here first I have extended ListFragment class and implements AdapterView , OnItemClickListener(interface) and here I have also used toast function. You can see below example code it clearly describe you "How to make ListView fragment in android".

    Step(1)Created list_fragment.xml layout -

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>
    
        <TextView
            android:id="@android:id/empty"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </TextView>
    
    </LinearLayout>

    Step(2)activity_main.xml layout-

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <fragment
            android:id="@+id/fragment1"
            android:name="com.tiwari.rajshekhar.listfragment.MyListFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </LinearLayout>

    Step(3)-Created String-array in string.xml folder-

    <resources>
        <string name="app_name">ListFragment</string>
        <string name="action_settings">Settings</string>
    
        <string-array name="Company">
            <item>Evon</item>
            <item>HCL</item>
            <item>HP</item>
            <item>Ericsson</item>
            <item>TCS</item>
            <item>Dell</item>
            <item>Microsoft</item>
    
        </string-array>
    </resources>


    Step(4)-Created MyListFragment class-

    public class MyListFragment extends ListFragment implements AdapterView.OnItemClickListener {
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
                View view = inflater.inflate(R.layout.list_fragment, container, false);
                return view;
            }
    
            @Override
            public void onActivityCreated(Bundle savedInstanceState) {
    
                super.onActivityCreated(savedInstanceState);
    
                ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Company, android.R.layout.simple_list_item_1);
    
                setListAdapter(adapter);
                getListView().setOnItemClickListener(this);
    
            }
    
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
                String[] mess = getResources().getStringArray(R.array.Company);
    
                Toast.makeText(getActivity(), "Item: " + mess[position], Toast.LENGTH_SHORT).show();
    
            }
        }


    Step(5)MainActivity-
     

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

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: