- Advanced Android - Home
- Advanced Android - Material Design
- Advanced Android - Sliding Menu
- Advanced Android - Tabs
- Advanced Android - Sliders
- Advanced Android - Floating Labels
- Advanced Android - ViewPager
- Material Login & Registration
- Advanced Android - Snackbar
- Expandable List View
- Search Functionality
- Count Down Functionality
- Draw Spritesheet
- Advanced Android - Linkify Class
- Advanced Android - ViewFlipper
- Advanced Android - loader
- Advanced Android - Slider Menu
- SwipeRefreshLayout
- Shake to Refresh
- Advanced Android - Color Palette
- Crumbling Layout
- Advanced Android - Page Flip
- Ken Burns effect
- Advanced Android - Parallax Header
- Borderless Dialog
- Advanced Android - Holder View
- Scrolling TextView by Ticker
- Floating Action Menu
- Spinner Data from Database
- Advanced Android - Flexbox Layout
- Advanced Android - YouTube API
- Video Streaming
- Cardview & Recycleview
Advanced Android - Flexbox Layout
FlexboxLayout is an android layout manager which brings the similar capabilities to the CSS Flexible Box Layout Module
Example
This example demostrate about how to integrate Android Flexbox Layout.
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"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:layout_margin = "16dp"
android:orientation = "vertical">
<TextView
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:padding = "16dp"
android:text = "Android Flexbox Layout Tutorial with Example" />
<Button
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:onClick = "flexboxLayout1"
android:text = "Flexbox Layout Example 1" />
</LinearLayout>
Step 3 − Add the following code to src/MainActivity.java
package myapplication.example.com.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void flexboxLayout1(View view) {
Intent intent = new Intent(getApplicationContext(), FlexboxLayoutExampleOne.class);
startActivity(intent);
}
}
Step 4 − Add the following code to src/FlexboxLayoutExampleOne.java
package myapplication.example.com.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class FlexboxLayoutExampleOne extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.flexbox_layout1);
}
}
Step 5 − Add the following code to res/layout/flexbox_layout1.xml.
<?xml version = "1.0" encoding = "utf-8"?>
<com.google.android.flexbox.FlexboxLayout xmlns:android = "
http://schemas.android.com/apk/res/android"
xmlns:app = "http://schemas.android.com/apk/res-auto"
android:id = "@+id/flexboxLayout"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
app:alignContent = "stretch"
app:alignItems = "stretch"
app:flexWrap = "wrap">
<TextView
android:layout_width = "110dp"
android:layout_height = "90dp"
android:background = "#80da2804"
android:gravity = "center"
android:text = "1"
android:textColor = "#fff"
android:textSize = "30dp"
android:textStyle = "bold"
app:layout_flexBasisPercent = "50%" />
<TextView
android:layout_width = "90dp"
android:layout_height = "90dp"
android:layout_marginLeft = "20dp"
android:background = "#80da2804"
android:gravity = "center"
android:text = "2"
android:textColor = "#fff"
android:textSize = "30dp"
android:textStyle = "bold"
app:layout_alignSelf = "center" />
<TextView
android:layout_width = "160dp"
android:layout_height = "85dp"
android:background = "#80da2804"
android:gravity = "center"
android:text = "3"
android:textColor = "#fff"
android:textSize = "30dp"
android:textStyle = "bold"
app:layout_alignSelf = "flex_end" />
</com.google.android.flexbox.FlexboxLayout>
Step 6 − 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 = "myapplication.example.com.myapplication">
<uses-permission android:name = "android.permission.INTERNET"/>
<application
android:allowBackup = "true"
android:icon = "@mipmap/ic_launcher"
android:label = "@string/app_name"
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>
<activity android:name=".FlexboxLayoutExampleOne"/>
</application>
</manifest>
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 −