Android snippets


Bron Website Project name Grid
compileSdkVersion 28 minSdkVersion 15
buildToolsVersion 28.0.3 targetSdkVersion 28 
Opmerking plaats afbeeldingen drawable ic_back1 – 6

build.gradle

dependencies {
    ...
    implementation 'com.android.support:cardview-v7:28.0.3'
    ...
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:background="#D3D3D3"
    android:weightSum="10"
    tools:context="com.example.grid.MainActivity">
    <RelativeLayout
        android:layout_weight="2"
        android:layout_width="match_parent"
        android:layout_height="0dp">
        <TextView
            android:id="@+id/textGrid"
            android:text="GRID LAYOUT"
            android:textSize="34sp"
            android:textColor="#000"
            android:layout_centerInParent="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>
    <GridLayout
        android:id="@+id/mainGrid"
        android:rowCount="3"
        android:columnCount="2"
        android:alignmentMode="alignMargins"
        android:columnOrderPreserved="false"
        android:layout_weight="8"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:padding="14dp">
        <!-- ROW 1 -->
        <!-- COLUMN 1 -->
        <androidx.cardview.widget.CardView> android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_marginBottom="16dp" android:layout_rowWeight="1" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" app:cardElevation="8dp" app:cardCornerRadius="8dp"&gt; <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"><ImageView
                    android:src="@drawable/ic_back1"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" /><TextView
                    android:textAlignment="center"
                    android:text="Text"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" /></LinearLayout></androidx.cardview.widget.CardView>
        <!-- COLUMN 2 -->
        <androidx.cardview.widget.CardView> android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_marginBottom="16dp" android:layout_rowWeight="1" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" app:cardElevation="8dp" app:cardCornerRadius="8dp"&gt; <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"><ImageView
                    android:src="@drawable/ic_back2"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" /><TextView
                    android:textAlignment="center"
                    android:text="Text2"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" /></LinearLayout></androidx.cardview.widget.CardView>
        <!-- ROW 2 -->
        <!-- COLUMN 1 -->
        <androidx.cardview.widget.CardView> android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_marginBottom="16dp" android:layout_rowWeight="1" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" app:cardElevation="8dp" app:cardCornerRadius="8dp"&gt; <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"><ImageView
                    android:src="@drawable/ic_back3"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" /><TextView
                    android:textAlignment="center"
                    android:text="Text3"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" /></LinearLayout></androidx.cardview.widget.CardView>
        <!-- COLUMN 2 -->
        <androidx.cardview.widget.CardView> android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_marginBottom="16dp" android:layout_rowWeight="1" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" app:cardElevation="8dp" app:cardCornerRadius="8dp"&gt; <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"><ImageView
                    android:src="@drawable/ic_back4"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" /><TextView
                    android:textAlignment="center"
                    android:text="Text4"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" /></LinearLayout></androidx.cardview.widget.CardView>
        <!-- ROW 3 -->
        <!-- COLUMN 1 -->
        <androidx.cardview.widget.CardView> android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_marginBottom="16dp" android:layout_rowWeight="1" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" app:cardElevation="8dp" app:cardCornerRadius="8dp"&gt; <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"><ImageView
                    android:src="@drawable/ic_back5"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" /><TextView
                    android:textAlignment="center"
                    android:text="Text"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" /></LinearLayout></androidx.cardview.widget.CardView>
        <!-- COLUMN 2 -->
        <androidx.cardview.widget.CardView> android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_marginBottom="16dp" android:layout_rowWeight="1" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" app:cardElevation="8dp" app:cardCornerRadius="8dp"&gt; <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"><ImageView
                    android:src="@drawable/ic_back6"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" /><TextView
                    android:textAlignment="center"
                    android:text="Text2"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" /></LinearLayout></androidx.cardview.widget.CardView>
    </GridLayout>
</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    GridLayout gridLayout;

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

        gridLayout=(GridLayout)findViewById(R.id.mainGrid);

        setSingleEvent(gridLayout);

    }
    private void setSingleEvent(GridLayout gridLayout) {
        for(int i = 0; i&lt;gridLayout.getChildCount();i++){
            CardView cardView=(CardView)gridLayout.getChildAt(i);
            final int finalI= i;
            cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(MainActivity.this,"Clicked at index "+ finalI,
                            Toast.LENGTH_SHORT).show();
                }
            });
        }
    }


}

Bron Website Project name Tab
compileSdkVersion 29 minSdkVersion 15
buildToolsVersion 29.0.2 targetSdkVersion 29 
Opmerking per tab 1 fragment

build.gradle

dependencies {
    ...
    implementation 'com.android.support:design:29.0.0'
    ...
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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" tools:context=".MainActivity">
    <com.google.android.material.tabs.TabLayout android:id="@+id/tabLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#1db995">
    </com.google.android.material.tabs.TabLayout>
    <androidx.viewpager.widget.ViewPager android:id="@+id/viewPager" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tabLayout" android:layout_centerInParent="true" android:layout_marginTop="100dp" tools:layout_editor_absoluteX="8dp" />
</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
    TabLayout tabLayout;
    ViewPager viewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabLayout = findViewById(R.id.tabLayout);
        viewPager = findViewById(R.id.viewPager);
        tabLayout.addTab(tabLayout.newTab().setText("Appel"));
        tabLayout.addTab(tabLayout.newTab().setText("Peer"));
        tabLayout.addTab(tabLayout.newTab().setText("Mandarijn"));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        final MyAdapter adapter = new MyAdapter(this,getSupportFragmentManager(),
                tabLayout.getTabCount());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }
}

MyAdapter.java

class MyAdapter extends FragmentPagerAdapter {
    Context context;
    int totalTabs;
    public MyAdapter(Context c, FragmentManager fm, int totalTabs) {
        super(fm);
        context = c;
        this.totalTabs = totalTabs;
    }
    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                Appel appelFragment = new Appel();
                return appelFragment;
            case 1:
                Peer peerFragment = new Peer();
                return peerFragment;
            case 2:
                Mandarijn mandarijnFragment = new Mandarijn();
                return mandarijnFragment;
            default:
                return null;
        }
    }
    @Override
    public int getCount() {
        return totalTabs;
    }
}

fragment_appel.xml


<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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" tools:context=".Appel">
    <!-- TODO: Update blank fragment layout -->
    <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:textAlignment="center" android:text="Appel Fragment" android:textSize="16sp" android:textStyle="bold"/>
</FrameLayout>

Appel.java


public class Appel extends Fragment {
    public Appel() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_appel, container, false);
    }
}

fragment_peer.xml


...

Peer.java


...

fragment_mandarijn.xml


...

Mandarijn.java


...

AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.tab">
    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>