|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2017 Raphaël Bussa |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package rebus.header.view.sample; |
| 26 | + |
| 27 | +import android.os.Bundle; |
| 28 | +import android.util.Log; |
| 29 | +import android.widget.Toast; |
| 30 | + |
| 31 | +import com.google.android.material.navigation.NavigationView; |
| 32 | + |
| 33 | +import androidx.appcompat.app.AppCompatActivity; |
| 34 | +import androidx.appcompat.widget.Toolbar; |
| 35 | +import androidx.core.content.ContextCompat; |
| 36 | +import androidx.core.view.GravityCompat; |
| 37 | +import androidx.drawerlayout.widget.DrawerLayout; |
| 38 | +import rebus.header.view.HeaderCallback; |
| 39 | +import rebus.header.view.HeaderView; |
| 40 | +import rebus.header.view.Item; |
| 41 | +import rebus.header.view.Profile; |
| 42 | +//import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper; |
| 43 | + |
| 44 | +public class CompactHeaderActivity extends AppCompatActivity { |
| 45 | + |
| 46 | + private static final String TAG = CompactHeaderActivity.class.getName(); |
| 47 | + |
| 48 | + private HeaderView headerView; |
| 49 | + private DrawerLayout drawerLayout; |
| 50 | + |
| 51 | + @Override |
| 52 | + protected void onCreate(Bundle savedInstanceState) { |
| 53 | + super.onCreate(savedInstanceState); |
| 54 | + setContentView(R.layout.activity_compact_header); |
| 55 | + Toolbar toolbar = findViewById(R.id.toolbar); |
| 56 | + toolbar.setTitle(getString(R.string.app_name)); |
| 57 | + toolbar.setNavigationOnClickListener(view -> { |
| 58 | + if (drawerLayout != null && !drawerLayout.isDrawerOpen(GravityCompat.START)) { |
| 59 | + drawerLayout.openDrawer(GravityCompat.START); |
| 60 | + } |
| 61 | + }); |
| 62 | + drawerLayout = findViewById(R.id.drawer_layout); |
| 63 | + NavigationView navigationView = findViewById(R.id.nav_view); |
| 64 | + headerView = navigationView.getHeaderView(0).findViewById(R.id.header_view); |
| 65 | + |
| 66 | + Profile profile = new Profile.Builder() |
| 67 | + .setId(2) |
| 68 | + .setUsername("Raphaël Bussa") |
| 69 | + .setEmail("raphaelbussa@gmail.com") |
| 70 | + .setAvatar("https://github.com/rebus007.png?size=512") |
| 71 | + .setBackground("https://images.unsplash.com/photo-1473220464492-452fb02e6221?dpr=2&auto=format&fit=crop&w=767&h=512&q=80&cs=tinysrgb&crop=") |
| 72 | + .build(); |
| 73 | + |
| 74 | + Profile profile2 = new Profile.Builder() |
| 75 | + .setId(4) |
| 76 | + .setUsername("Federico Gentile") |
| 77 | + .setEmail("fgentile95dev@icloud.com") |
| 78 | + .setAvatar("https://github.com/FedeGens.png?size=512") |
| 79 | + .setBackground("https://images.unsplash.com/photo-1469173479606-ada03df615ac?dpr=2&auto=format&fit=crop&w=767&h=511&q=80&cs=tinysrgb&crop=") |
| 80 | + .build(); |
| 81 | + |
| 82 | + Profile profile3 = new Profile.Builder() |
| 83 | + .setId(6) |
| 84 | + .setUsername("Luca Rurio") |
| 85 | + .setEmail("rurio.luca@gmail.com") |
| 86 | + .setAvatar("https://github.com/RurioLuca.png?size=512") |
| 87 | + .setBackground("https://images.unsplash.com/photo-1473789810014-375ed569d0ed?dpr=2&auto=format&fit=crop&w=767&h=511&q=80&cs=tinysrgb&crop=") |
| 88 | + .build(); |
| 89 | + |
| 90 | + Profile profile4 = new Profile.Builder() |
| 91 | + .setId(8) |
| 92 | + .setUsername("Krzysztof Klimkiewicz") |
| 93 | + .setEmail("krzkz94@gmail.com") |
| 94 | + .setAvatar("https://github.com/krzykz.png?size=512") |
| 95 | + .setBackground("https://images.unsplash.com/photo-1452509133926-2b180c6d6245?dpr=2&auto=format&fit=crop&w=767&h=431&q=80&cs=tinysrgb&crop=") |
| 96 | + .build(); |
| 97 | + |
| 98 | + Item item = new Item.Builder() |
| 99 | + .setId(1) |
| 100 | + .setTitle("Remove all profile") |
| 101 | + .build(); |
| 102 | + |
| 103 | + Item item2 = new Item.Builder() |
| 104 | + .setId(2) |
| 105 | + .setTitle("Remove active profile") |
| 106 | + .build(); |
| 107 | + |
| 108 | + |
| 109 | + headerView.setStyle(HeaderView.STYLE_COMPACT); |
| 110 | + headerView.setTheme(HeaderView.THEME_LIGHT); |
| 111 | + headerView.setShowGradient(true); |
| 112 | + headerView.setHighlightColor(ContextCompat.getColor(this, R.color.colorAccent)); |
| 113 | + headerView.addProfile(profile, profile2, profile3, profile4); |
| 114 | + headerView.addDialogItem(item, item2); |
| 115 | + headerView.setShowAddButton(true); |
| 116 | + //headerView.setAddIconDrawable(R.drawable.ic_action_settings); |
| 117 | + headerView.setDialogTitle("Choose account"); |
| 118 | + headerView.setShowArrow(true); |
| 119 | + headerView.setOnHeaderClickListener(v -> drawerLayout.closeDrawer(GravityCompat.START, true)); |
| 120 | + headerView.setFragmentManager(getSupportFragmentManager()); |
| 121 | + headerView.setCallback(new HeaderCallback() { |
| 122 | + |
| 123 | + @Override |
| 124 | + public boolean onSelect(int id, boolean isActive) { |
| 125 | + Log.d(TAG, "profile selected [" + id + "] isActive [" + isActive + "]"); |
| 126 | + Toast.makeText(CompactHeaderActivity.this, "profile selected [" + id + "] isActive [" + isActive + "]", Toast.LENGTH_SHORT).show(); |
| 127 | + drawerLayout.closeDrawer(GravityCompat.START, true); |
| 128 | + return true; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public boolean onItem(int id) { |
| 133 | + switch (id) { |
| 134 | + case 1: |
| 135 | + headerView.clearProfile(); |
| 136 | + break; |
| 137 | + case 2: |
| 138 | + int profileId = headerView.getProfileActive(); |
| 139 | + headerView.removeProfile(profileId); |
| 140 | + break; |
| 141 | + } |
| 142 | + return true; |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public boolean onAdd() { |
| 147 | + Profile newProfile = new Profile.Builder() |
| 148 | + .setId(100) |
| 149 | + .setUsername("Mattia Novelli") |
| 150 | + .setEmail("nove.mattia@gmail.com") |
| 151 | + .setAvatar("https://github.com/mattinove.png?size=512") |
| 152 | + .setBackground("https://images.unsplash.com/photo-1478194409487-fa5c1eb18622?dpr=2&auto=format&fit=crop&w=767&h=496&q=80&cs=tinysrgb&crop=") |
| 153 | + .build(); |
| 154 | + headerView.addProfile(newProfile); |
| 155 | + headerView.setProfileActive(100); |
| 156 | + headerView.dismissProfileChooser(); |
| 157 | + return false; |
| 158 | + } |
| 159 | + |
| 160 | + }); |
| 161 | + |
| 162 | + //headerView.addProfile(profile, profile2, profile3); |
| 163 | + |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public void onBackPressed() { |
| 168 | + if (drawerLayout != null && drawerLayout.isDrawerOpen(GravityCompat.START)) { |
| 169 | + drawerLayout.closeDrawer(GravityCompat.START); |
| 170 | + return; |
| 171 | + } |
| 172 | + super.onBackPressed(); |
| 173 | + } |
| 174 | + |
| 175 | +/* |
| 176 | + @Override |
| 177 | + protected void attachBaseContext(Context newBase) { |
| 178 | + super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase)); |
| 179 | + } |
| 180 | +*/ |
| 181 | + |
| 182 | +} |
0 commit comments