build.gradle (Project: MyApplication)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.1.4' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() jcenter() maven { url 'https://jitpack.io' } } } task clean(type: Delete) { delete rootProject.buildDir } |
build.gradle (Module: app)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
apply plugin: 'com.android.application' android { compileSdkVersion 26 defaultConfig { applicationId "com.phaisarn.myapplication" minSdkVersion 21 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.android.support:design:26.1.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation 'com.github.matrixxun:MaterialBadgeTextView:c5a27e8243' } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout 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" tools:context=".MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay"> <FrameLayout android:layout_width="48dp" android:layout_height="48dp" android:layout_gravity="center"> <ImageView android:id="@+id/imageView_toolbar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:layout_marginTop="10dp" android:src="@drawable/ic_cart" android:tint="#fff" /> <com.matrixxun.starry.badgetextview.MaterialBadgeTextView android:id="@+id/badge_toolbar" android:layout_width="28dp" android:layout_height="28dp" android:layout_gravity="right|top" /> </FrameLayout> </android.support.v7.widget.Toolbar> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_main" /> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" app:srcCompat="@android:drawable/ic_dialog_email" /> </android.support.design.widget.CoordinatorLayout> |
content_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<?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:gravity="center_horizontal" android:orientation="vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.user.tn_materialbadgetextview.MainActivity" tools:showIn="@layout/activity_main"> <FrameLayout android:layout_width="58dp" android:layout_height="58dp"> <ImageView android:id="@+id/imageView" android:layout_width="48dp" android:layout_height="48dp" android:layout_marginRight="10dp" android:layout_marginTop="10dp" android:src="@mipmap/ic_launcher" /> <com.matrixxun.starry.badgetextview.MaterialBadgeTextView android:id="@+id/badge" android:layout_width="28dp" android:layout_height="28dp" android:layout_gravity="right|top" /> </FrameLayout> <Button android:id="@+id/add" android:layout_width="150dp" android:layout_height="wrap_content" android:text="Add +" /> <Button android:id="@+id/hundred_plus" android:layout_width="150dp" android:layout_height="wrap_content" android:text="100 +" /> <Button android:id="@+id/clear" android:layout_width="150dp" android:layout_height="wrap_content" android:text="Clear" /> </LinearLayout> |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
package com.phaisarn.myapplication; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.graphics.Color; import com.matrixxun.starry.badgetextview.MaterialBadgeTextView; public class MainActivity extends AppCompatActivity { private MaterialBadgeTextView mBadge; private MaterialBadgeTextView mBadgeToolbar; private int mCount = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.setTitle(""); //toolbar.setBackgroundColor(Color.DKGRAY); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); mBadge = (MaterialBadgeTextView) findViewById(R.id.badge); mBadge.setHighLightMode(); mBadge.setBackgroundColor(Color.RED); mBadge.setBadgeCount(0); mBadge.setTextColor(Color.WHITE); mBadgeToolbar = (MaterialBadgeTextView) findViewById(R.id.badge_toolbar); mBadgeToolbar.setHighLightMode(); mBadgeToolbar.setBackgroundColor(Color.RED); mBadgeToolbar.setBadgeCount(0); mBadge.setTextColor(Color.WHITE); findViewById(R.id.add).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mCount++; mBadge.setBadgeCount(mCount); mBadgeToolbar.setBadgeCount(mCount); } }); findViewById(R.id.hundred_plus).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mCount = 100; mBadge.setBadgeCount(mCount); mBadgeToolbar.setBadgeCount(mCount); } }); findViewById(R.id.clear).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mCount = 0; mBadge.setBadgeCount(mCount); mBadgeToolbar.setBadgeCount(mCount); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } |
รูปที่ใช้