Skip to content

Commit

Permalink
updated to latest libs - androidx
Browse files Browse the repository at this point in the history
  • Loading branch information
Suleiman19 committed Nov 30, 2019
2 parents f7c1da6 + fc570bb commit d6a5732
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 50 deletions.
Binary file modified MaterialSample/.idea/caches/build_file_checksums.ser
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
Expand All @@ -37,7 +38,7 @@ protected void onCreate(Bundle savedInstanceState) {

final Toolbar toolbar = findViewById(R.id.tabanim_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (getSupportActionBar() != null) getSupportActionBar().setDisplayHomeAsUpEnabled(true);

final ViewPager viewPager = findViewById(R.id.tabanim_viewpager);
setupViewPager(viewPager);
Expand Down Expand Up @@ -82,9 +83,12 @@ void showToast(String msg) {

private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
adapter.addFrag(new DummyFragment(getResources().getColor(R.color.accent_material_light)), "CAT");
adapter.addFrag(new DummyFragment(getResources().getColor(R.color.ripple_material_light)), "DOG");
adapter.addFrag(new DummyFragment(getResources().getColor(R.color.button_material_dark)), "MOUSE");
adapter.addFrag(new DummyFragment(
ContextCompat.getColor(this, R.color.blue_grey)), "CAT");
adapter.addFrag(new DummyFragment(
ContextCompat.getColor(this, R.color.amber)), "DOG");
adapter.addFrag(new DummyFragment(
ContextCompat.getColor(this, R.color.cyan)), "MOUSE");
viewPager.setAdapter(adapter);
}

Expand All @@ -108,7 +112,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}

static class ViewPagerAdapter extends FragmentPagerAdapter {
private static class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
Expand All @@ -15,6 +16,7 @@
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.tabs.TabLayout;
import com.suleiman.material.R;
import com.suleiman.material.adapter.DessertAdapter;
import com.suleiman.material.adapter.SimpleRecyclerAdapter;
import com.suleiman.material.model.VersionModel;

Expand All @@ -24,6 +26,7 @@

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
Expand All @@ -33,6 +36,7 @@
import androidx.viewpager.widget.ViewPager;

public class TabsHeaderActivity extends AppCompatActivity {
private static final String TAG = TabsHeaderActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -41,7 +45,7 @@ protected void onCreate(Bundle savedInstanceState) {

final Toolbar toolbar = findViewById(R.id.htab_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Parallax Tabs");
if (getSupportActionBar() != null) getSupportActionBar().setTitle("Parallax Tabs");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

final ViewPager viewPager = findViewById(R.id.htab_viewpager);
Expand All @@ -51,29 +55,39 @@ protected void onCreate(Bundle savedInstanceState) {
TabLayout tabLayout = findViewById(R.id.htab_tabs);
tabLayout.setupWithViewPager(viewPager);

final CollapsingToolbarLayout collapsingToolbarLayout = findViewById(R.id.htab_collapse_toolbar);
collapsingToolbarLayout.setTitleEnabled(false);
final CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.htab_collapse_toolbar);

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.header);
try {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.header);
Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
@SuppressWarnings("ResourceType")
@Override
public void onGenerated(Palette palette) {

Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
@SuppressWarnings("ResourceType")
@Override
public void onGenerated(Palette palette) {

int vibrantColor = palette.getVibrantColor(R.color.primary_500);
int vibrantDarkColor = palette.getDarkVibrantColor(R.color.primary_700);
collapsingToolbarLayout.setContentScrimColor(vibrantColor);
collapsingToolbarLayout.setStatusBarScrimColor(vibrantDarkColor);
}
});
int vibrantColor = palette.getVibrantColor(R.color.primary_500);
int vibrantDarkColor = palette.getDarkVibrantColor(R.color.primary_700);
collapsingToolbarLayout.setContentScrimColor(vibrantColor);
collapsingToolbarLayout.setStatusBarScrimColor(vibrantDarkColor);
}
});

} catch (Exception e) {
// if Bitmap fetch fails, fallback to primary colors
Log.e(TAG, "onCreate: failed to create bitmap from background", e.fillInStackTrace());
collapsingToolbarLayout.setContentScrimColor(
ContextCompat.getColor(this, R.color.primary_500)
);
collapsingToolbarLayout.setStatusBarScrimColor(
ContextCompat.getColor(this, R.color.primary_700)
);
}

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {

viewPager.setCurrentItem(tab.getPosition());
Log.d(TAG, "onTabSelected: pos: " + tab.getPosition());

switch (tab.getPosition()) {
case 0:
Expand All @@ -98,16 +112,18 @@ public void onTabReselected(TabLayout.Tab tab) {
});
}


void showToast(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
private void showToast(String text) {
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
}

private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
adapter.addFrag(new DummyFragment(getResources().getColor(R.color.accent_material_light)), "CAT");
adapter.addFrag(new DummyFragment(getResources().getColor(R.color.ripple_material_light)), "DOG");
adapter.addFrag(new DummyFragment(getResources().getColor(R.color.button_material_dark)), "MOUSE");
adapter.addFrag(new DummyFragment(
ContextCompat.getColor(this, R.color.cyan_50)), "Cyan");
adapter.addFrag(new DummyFragment(
ContextCompat.getColor(this, R.color.amber_50)), "Amber");
adapter.addFrag(new DummyFragment(
ContextCompat.getColor(this, R.color.purple_50)), "Purple");
viewPager.setAdapter(adapter);
}

Expand All @@ -130,7 +146,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}

static class ViewPagerAdapter extends FragmentPagerAdapter {
private static class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

Expand Down Expand Up @@ -162,7 +178,6 @@ public CharSequence getPageTitle(int position) {

public static class DummyFragment extends Fragment {
int color;
SimpleRecyclerAdapter adapter;

public DummyFragment() {
}
Expand All @@ -185,14 +200,9 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setHasFixedSize(true);

// List<String> list = new ArrayList<>();
// list.addAll(Arrays.asList(VersionModel.data));
// for (int i = 0; i < VersionModel.data.length; i++) {
// list.add(VersionModel.data[i]);
// }
List<String> list = Arrays.asList(VersionModel.data);

adapter = new SimpleRecyclerAdapter(list);
SimpleRecyclerAdapter adapter = new SimpleRecyclerAdapter(list);
recyclerView.setAdapter(adapter);

return view;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public SimpleRecyclerAdapter(List<String> versionModels) {
@Override
public VersionViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerlist_item, viewGroup, false);
VersionViewHolder viewHolder = new VersionViewHolder(view);
return viewHolder;
return new VersionViewHolder(view);
}

@Override
Expand Down
25 changes: 17 additions & 8 deletions MaterialSample/app/src/main/res/layout/activity_tabs_header.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
android:layout_height="256dp"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
app:titleEnabled="false">

<ImageView
android:id="@+id/htab_header"
Expand All @@ -32,22 +33,30 @@
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"/>

<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.3"
android:background="@android:color/black"
android:fitsSystemWindows="true"/>

<androidx.appcompat.widget.Toolbar
android:id="@+id/htab_toolbar"
android:layout_width="match_parent"
android:layout_height="104dp"
android:gravity="top"
android:minHeight="?attr/actionBarSize"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="top"
android:layout_marginBottom="48dp"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:titleMarginTop="13dp"/>
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

<com.google.android.material.tabs.TabLayout
android:id="@+id/htab_tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:tabIndicatorColor="@android:color/white"/>
app:tabIndicatorColor="@android:color/white"
app:tabSelectedTextColor="@android:color/white"
app:tabTextColor="@color/white_70"/>

</com.google.android.material.appbar.CollapsingToolbarLayout>

Expand Down
13 changes: 8 additions & 5 deletions MaterialSample/app/src/main/res/layout/dummy_fragment.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dummyfrag_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg_light">
android:id="@+id/dummyfrag_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg_light">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/dummyfrag_scrollableview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_horizontal_margin"/>

</FrameLayout>
7 changes: 7 additions & 0 deletions MaterialSample/app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@
<color name="amber">#FFC107</color>
<color name="orange">#FF9800</color>
<color name="deep_orange">#FF5722</color>
<color name="brown">#795548</color>
<color name="grey">#E0E0E0</color>

<color name="white_70">#B3FFFFFF</color>

<color name="white_40">#66FFFFFF</color>

<color name="cyan_50">#E0F7FA</color>
<color name="amber_50">#FFF8E1</color>
<color name="purple_50">#F3E5F5</color>

</resources>

0 comments on commit d6a5732

Please sign in to comment.