How to implement ViewPager or Viewpager 2 in AIDE

275 views Asked by At

I am using AIDE on Android to code. Currently it claims to support Android 10 on which I am also coding. I would like to have a tab at the top of my app where the number of entries is larger than the with of the screen so I can scroll the entries with my thumb and click to display different data entry fields corresponding to the tab I click on for my app.

So, I have tried the following:

main.xml

<!--  ViewPager2 element with a TabLayout -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>

MainActivity.java

package com.mycompany.vp;

import android.app.*;
import android.os.*;

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

CollectionDemoFragment.java

package com.mycompany.vp;

// Integrating TabLayout with ViewPager2
public class CollectionDemoFragment : Fragment() {
    ...
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        TabLayout tabLayout = view.findViewById(R.id.tab_layout);
        new TabLayoutMediator(tabLayout, viewPager,
                (tab, position) -> tab.setText("OBJECT " + (position + 1))
        ).attach();
    }
    ...
}

However, in my fragment code, I get unexpected end of declaration errors. I'm not sure whether I need fragments for ViewPager or ViewPager2 to work and I'm not clear what code I need, especially what code I need in AIDE since it might not support every Android Studio construct and I don't have a PC.

If I delete the CollectionDemoFragment.java I can compile my code, but then when I try to run it it crashes. Any ideas about how I can get the tabs working and the app compiling from AIDE and running without crashing?

Thanks.

0

There are 0 answers