Androidx TabLayout in Sketchware

This example provided below shows how to create a simple scrollable TabLayout and add a TabLayout.OnTabSelectedListener to it in Sketchware.

1. In VIEW area of main.xml add a TextView textview1.

2. Switch On AppCompat and design.


3. In onCreate event, use an add source directly block and put following codes.
// Create a TabLayout (tabLayout)
com.google.android.material.tabs.TabLayout tabLayout = new com.google.android.material.tabs.TabLayout(this);
// Make TabLayout scrollable
tabLayout.setTabMode(com.google.android.material.tabs.TabLayout.MODE_SCROLLABLE);
// Add Tabs to the TabLayout
tabLayout.addTab(tabLayout.newTab().setText("Sunday")); tabLayout.addTab(tabLayout.newTab().setText("Monday")); tabLayout.addTab(tabLayout.newTab().setText("Tuesday"));
tabLayout.addTab(tabLayout.newTab().setText("Wednesday")); tabLayout.addTab(tabLayout.newTab().setText("Thursday")); tabLayout.addTab(tabLayout.newTab().setText("Friday"));
tabLayout.addTab(tabLayout.newTab().setText("Saturday"));
// Add TabLayout to AppBarLayout
((com.google.android.material.appbar.AppBarLayout)_toolbar.getParent()).addView(tabLayout);
// Set text of textview1
textview1.setText(tabLayout.getTabAt(0).getText().toString());
// Add OnTabSelectedListener to the TabLayout
tabLayout.addOnTabSelectedListener(new com.google.android.material.tabs.TabLayout.OnTabSelectedListener(){
@Override
public void onTabSelected(com.google.android.material.tabs.TabLayout.Tab tab){
textview1.setText(tab.getText().toString()); }
@Override
public void onTabReselected(com.google.android.material.tabs.TabLayout.Tab tab){ }
@Override
public void onTabUnselected(com.google.android.material.tabs.TabLayout.Tab tab){ }
});

4. Save and run the project.

Comments

  1. Hi Sanjeev, Just need a small help. In the latest update in sketchware i am unable to upload the pic to Firbase storage can u please help me to reslove it.

    ReplyDelete
  2. Sir, i see error messages of code please help.

    ReplyDelete
  3. Error in last code i provide you right code here👇

    // Add OnTabSelectedListener to the TabLayout

    tabLayout.addOnTabSelectedListener(new com.google.android.material.tabs.TabLayout.OnTabSelectedListener(){
    @Override
    public void onTabSelected(com.google.android.material.tabs.TabLayout.Tab tab){
    // Set text of selected Tab as text of Textview
    textview1.setText(tab.getText().toString());
    }
    @Override
    public void onTabReselected(com.google.android.material.tabs.TabLayout.Tab tab){ }
    @Override
    public void onTabUnselected(com.google.android.material.tabs.TabLayout.Tab tab){ }
    });

    ReplyDelete
  4. ----------
    1. ERROR in /storage/emulated/0/.sketchware/mysc/605/app/src/main/java/com/my/newproject/MainActivity.java (at line 56)
    ((com.google.android.material.appbar.AppBarLayout)_toolbar.getParent()).addView(tabLayout);
    ^^^^^^^^
    _toolbar cannot be resolved
    ----------
    1 problem (1 error)


    Pls help

    ReplyDelete

Post a Comment

Popular posts from this blog

Simple car racing android game in Sketchware

Simple Audio recorder app in Sketchware

Retrieve contact list in Sketchware

How to enable upload from webview in Sketchware?

Creating a Drawing View in Sketchware