
- Android Basics
- Android - Home
- Android - Overview
- Android - Environment Setup
- Android - Architecture
- Android - Application Components
- Android - Hello World Example
- Android - Resources
- Android - Activities
- Android - Services
- Android - Broadcast Receivers
- Android - Content Providers
- Android - Fragments
- Android - Intents/Filters
- Android - User Interface
- Android - UI Layouts
- Android - UI Controls
- Android - Event Handling
- Android - Styles and Themes
- Android - Custom Components
- Android Advanced Concepts
- Android - Drag and Drop
- Android - Notifications
- Location Based Services
- Android - Sending Email
- Android - Sending SMS
- Android - Phone Calls
- Publishing Android Application
- Android Useful Examples
- Android - Alert Dialoges
- Android - Animations
- Android - Audio Capture
- Android - AudioManager
- Android - Auto Complete
- Android - Best Practices
- Android - Bluetooth
- Android - Camera
- Android - Clipboard
- Android - Custom Fonts
- Android - Data Backup
- Android - Developer Tools
- Android - Emulator
- Android - Facebook Integration
- Android - Gestures
- Android - Google Maps
- Android - Image Effects
- Android - ImageSwitcher
- Android - Internal Storage
- Android - JetPlayer
- Android - JSON Parser
- Android - Linkedin Integration
- Android - Loading Spinner
- Android - Localization
- Android - Login Screen
- Android - MediaPlayer
- Android - Multitouch
- Android - Navigation
- Android - Network Connection
- Android - NFC Guide
- Android - PHP/MySQL
- Android - Progress Circle
- Android - ProgressBar
- Android - Push Notification
- Android - RenderScript
- Android - RSS Reader
- Android - Screen Cast
- Android - SDK Manager
- Android - Sensors
- Android - Session Management
- Android - Shared Preferences
- Android - SIP Protocol
- Android - Spelling Checker
- Android - SQLite Database
- Android - Support Library
- Android - Testing
- Android - Text to Speech
- Android - TextureView
- Android - Twitter Integration
- Android - UI Design
- Android - UI Patterns
- Android - UI Testing
- Android - WebView Layout
- Android - Wi-Fi
- Android - Widgets
- Android - XML Parsers
- Android Useful Resources
- Android - Questions and Answers
- Android - Useful Resources
- Android - Discussion
How to use Constraint Layout with recyclerview?
This example demonstrate about How to use Constraint Layout with recyclerview
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
Step 2 − Add the following code to res/layout/activity_main.xml.
<?xml version = "1.0" encoding = "utf-8"?> <android.support.design.widget.CoordinatorLayout android:layout_width = "match_parent" android:layout_height = "match_parent" xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:app = "http://schemas.android.com/apk/res-auto"> <android.support.design.widget.AppBarLayout android:layout_width = "match_parent" android:layout_height = "wrap_content"> <android.support.v7.widget.Toolbar android:id = "@+id/appbarlayout_tool_bar" android:background = "@color/colorPrimary" android:layout_width = "match_parent" android:layout_height = "?attr/actionBarSize" app:layout_scrollFlags = "scroll|snap|enterAlways" app:theme = "@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme = "@style/ThemeOverlay.AppCompat.Light" /> </android.support.design.widget.AppBarLayout> <android.support.v7.widget.RecyclerView android:id = "@+id/recycler_view" android:layout_width = "match_parent" android:layout_height = "match_parent" app:layout_behavior = "@string/appbar_scrolling_view_behavior"/> </android.support.design.widget.CoordinatorLayout>
In the above code, we have taken app bar layout and recycler view.
Step 3 − Add the following code to src/MainActivity.java
<?xml version = "1.0" encoding = "utf-8"?> import android.annotation.TargetApi; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.support.v4.content.pm.ShortcutInfoCompat; import android.support.v4.content.pm.ShortcutManagerCompat; import android.support.v4.graphics.drawable.IconCompat; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.TextView; import android.widget.Toast; import android.support.v7.widget.Toolbar; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private customAdapter mAdapter; TextView text; ArrayList<String> list = new ArrayList<>(); @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (android.support.v7.widget.Toolbar)findViewById(R.id.appbarlayout_tool_bar); toolbar.setTitle("This is toolbar."); setSupportActionBar(toolbar); recyclerView = (RecyclerView) findViewById(R.id.recycler_view); RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); recyclerView.setLayoutManager(mLayoutManager); recyclerView.setItemAnimator(new DefaultItemAnimator()); mAdapter = new customAdapter(this,list); recyclerView.setAdapter(mAdapter); recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL)); list.add("sairamm"); list.add("Krishna"); list.add("prasad"); list.add("sairamm"); list.add("Krishna"); list.add("prasad"); list.add("sairamm"); list.add("Krishna"); list.add("prasad"); list.add("sairamm"); list.add("Krishna"); list.add("prasad"); list.add("Krishna"); list.add("prasad"); list.add("sairamm"); list.add("Krishna"); list.add("prasad"); list.add("sairamm"); list.add("Krishna"); list.add("prasad"); } }
Step 4 − Add the following code to Manifest.xml
<?xml version = "1.0" encoding = "utf-8"?> <manifest xmlns:android = "http://schemas.android.com/apk/res/android" package = "com.example.myapplication"> <uses-permission android:name = "android.permission.INTERNET" /> <uses-permission android:name = "com.android.launcher.permission.INSTALL_SHORTCUT" /> <application android:allowBackup = "true" android:theme = "@style/AppTheme.NoActionBar" android:icon = "@mipmap/ic_launcher" android:label = "@string/app_name" android:roundIcon = "@mipmap/ic_launcher_round" android:supportsRtl = "true"> <activity android:name = ".MainActivity" android:configChanges = "keyboardHidden|orientation|screenSize"> <intent-filter> <action android:name = "android.intent.action.MAIN" /> <action android:name = "android.intent.action.CREATE_SHORTCUT" /> <category android:name = "android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Step 5 − Add the following code to customAdapter.java
<?xml version = "1.0" encoding = "utf-8"?> import android.content.Context; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.ArrayList; public class customAdapter extends RecyclerView.Adapter<customAdapter.MyViewHolder> { Context context; ArrayList<String> list; public class MyViewHolder extends RecyclerView.ViewHolder { public TextView title; public MyViewHolder(View view) { super(view); title = (TextView) view.findViewById(R.id.title); } } public customAdapter(Context context, ArrayList<String> list) { this.context = context; this.list = list; } @NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, viewGroup, false); return new MyViewHolder(itemView); } @Override public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) { myViewHolder.title.setText(list.get(i)); } @Override public int getItemCount() { return list.size(); } }
Step 6 − Add the following code to list_row.xml
<?xml version = "1.0" encoding = "utf-8"?> <android.support.constraint.ConstraintLayout 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 = "wrap_content" android:background = "@color/colorAccent" tools:context = ".MainActivity"> <ImageView android:id = "@+id/imageView2" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:src = "@drawable/logo" app:layout_constraintBottom_toBottomOf = "parent" app:layout_constraintEnd_toEndOf = "parent" app:layout_constraintStart_toStartOf = "parent" app:layout_constraintTop_toTopOf = "parent" app:layout_constraintVertical_bias = "0.209" /> <TextView android:id = "@+id/title" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_marginTop = "35dp" android:gravity = "center" android:textSize = "30sp" app:layout_constraintTop_toBottomOf = "@+id/imageView2" tools:layout_editor_absoluteX = "0dp" /> <TextView android:id = "@+id/textview2" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_marginTop = "16dp" android:layout_marginBottom = "8dp" android:gravity = "center" android:text = "Sairamkrishan" android:textSize = "30sp" app:layout_constraintBottom_toBottomOf = "parent" app:layout_constraintTop_toBottomOf = "@+id/title" app:layout_constraintVertical_bias = "0.0" tools:layout_editor_absoluteX = "0dp" /> </android.support.constraint.ConstraintLayout>
Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen –
- Related Articles
- How does Constraint Layout works in android?
- How to use cardview in recyclerview?
- How to use GridLayoutManager in RecyclerView?
- How to use the recyclerview with a database in Android?
- How to use the recyclerview with a database in Android using Kotlin?
- How to use RecyclerView inside NestedScrollView in Android?
- How to use Android sequence layout?
- How to use appbar layout in android?
- How to use GridBagConstraints to layout two components in the same line with Java
- How to Scroll top in RecyclerView with LinearLayoutManager
- How to build a Horizontal ListView with RecyclerView?
- How to implement endless list with RecyclerView in Android?
- How to build a Horizontal ListView with RecyclerView in Android?
- How to Scroll top in RecyclerView with LinearLayoutManager in Android?
- How to filter a RecyclerView with a SearchView on Android?
