Android click on view above the view - android

I have tab Layout with dynamically added custom tabs (Image View + Text View)
I want to change image on select tab and on reselect (2 kind of images)
But there is no click event listeners on Tab, so I added transparent layout to catch clicks.
How do I need to change settings, to make 4 Linear Layouts clickable?
The code:
<RelativeLayout
android:id="#+id/layout_sort_bar_fc"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentBottom="true">
<android.support.design.widget.TabLayout
android:id="#+id/sort_bar_fc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
app:tabMaxWidth="0dp"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabBackground="#color/colorPrimary"
app:tabIndicatorColor="#f00"
app:tabSelectedTextColor="#color/white_text"
app:tabTextColor="#color/colorPrimaryDark"/>
<LinearLayout
android:id="#+id/tab_listenter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/dummy_tab_name"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0"
android:clickable="true"
android:orientation="horizontal"></LinearLayout>
<LinearLayout
android:id="#+id/dummy_tab_rate"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0"
android:orientation="horizontal"></LinearLayout>
<LinearLayout
android:id="#+id/dummy_tab_change"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0"
android:orientation="horizontal"></LinearLayout>
<LinearLayout
android:id="#+id/dummy_tab_24h"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0"
android:orientation="horizontal"></LinearLayout>
</LinearLayout>
</RelativeLayout>

the following answer deals with somewhat the same question and the answer marked as solution is what you're looking for, it explains how to define and use selectors

Well, you can change image on a selected tab by two method which you need to create in Custom Pager Adapter and call it from tab selected listener.
"MainActivity"
public class MainActivity extends AppCompatActivity {
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewpager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
createViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
createTabIcons();
adapter.SetOnSelectView(tabLayout,0);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
int c = tab.getPosition();
adapter.SetOnSelectView(tabLayout,c);
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
int c = tab.getPosition();
adapter.SetUnSelectView(tabLayout,c);
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
for (int i = 0; i < tabLayout.getTabCount() - 1; i++) {
View tab = ((ViewGroup) tabLayout.getChildAt(0)).getChildAt(i);
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) tab.getLayoutParams();
p.setMargins(0, 0, 10, 0);
tab.requestLayout();
}
}
private void createTabIcons() {
try {
View tabOne = LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
TextView textView = tabOne.findViewById(R.id.tab);
ImageView imgViewTabIcon = tabOne.findViewById(R.id.imgViewTabIcon);
imgViewTabIcon.setImageResource(R.drawable.img_11);
textView.setText("A");
tabLayout.getTabAt(0).setCustomView(tabOne);
View tabTwo = LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
TextView textView1 = tabTwo.findViewById(R.id.imgViewTabIcon);
textView1.setText("B");
ImageView imgViewTabIcon1 = tabTwo.findViewById(R.id.imgViewTabIcon);
imgViewTabIcon1.setImageResource(R.drawable.img_22);
tabLayout.getTabAt(1).setCustomView(tabTwo);
View tabThree = LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
TextView textView2 = tabThree.findViewById(R.id.tab);
textView2.setText("C");
ImageView imgViewTabIcon2 = tabThree.findViewById(R.id.imgViewTabIcon);
imgViewTabIcon2.setImageResource(R.drawable.img_33);
tabLayout.getTabAt(2).setCustomView(tabThree);
View tabFour = LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
TextView textView3 = tabFour.findViewById(R.id.tab);
textView3.setText("D");
ImageView imgViewTabIcon3 = tabFour.findViewById(R.id.imgViewTabIcon);
imgViewTabIcon3.setImageResource(R.drawable.img_33);
tabLayout.getTabAt(3).setCustomView(tabFour);
} catch (Exception e) {
e.printStackTrace();
}
}
private void createViewPager(ViewPager viewPager) {
adapter = new ViewPagerAdapter(getSupportFragmentManager(),HomeActivity.this);
adapter.addFrag(new A(), "A");
adapter.addFrag(new B(), "B");
adapter.addFrag(new C(), "C");
adapter.addFrag(new D(), "D");
viewPager.setAdapter(adapter);
}
}
"Custom PagerAdapter"
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
private Context mContext;
public ViewPagerAdapter(FragmentManager manager, Context context) {
super(manager);
mContext=context;
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public int dpToPx(int dp) {
DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
return Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
}
public void SetOnSelectView(TabLayout tabLayout, int position)
{
TabLayout.Tab tab = tabLayout.getTabAt(position);
View selected = tab.getCustomView();
TextView textView = (TextView) selected.findViewById(R.id.tab);
textView.setTextColor(mContext.getResources().getColor(R.color.colorPrimary));
ImageView imgViewTabIcon = selected.findViewById(R.id.imgViewTabIcon);
int height = dpToPx(35);
if(textView.getText().toString().equals("tab1")){
BitmapDrawable bitmapdraw = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.img_1);
Bitmap b = bitmapdraw.getBitmap();
Bitmap smallMarker = Bitmap.createScaledBitmap(b, height, height, false);
imgViewTabIcon.setImageBitmap(smallMarker);
}else if(textView.getText().toString().equals("tab2")){
BitmapDrawable bitmapdraw = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.img_2);
Bitmap b = bitmapdraw.getBitmap();
Bitmap smallMarker = Bitmap.createScaledBitmap(b, height, height, false);
imgViewTabIcon.setImageBitmap(smallMarker);
}else if(textView.getText().toString().equals("tab3")){
BitmapDrawable bitmapdraw = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.img_3);
Bitmap b = bitmapdraw.getBitmap();
Bitmap smallMarker = Bitmap.createScaledBitmap(b, height, height, false);
imgViewTabIcon.setImageBitmap(smallMarker);
}
else {
BitmapDrawable bitmapdraw = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.img_4);
Bitmap b = bitmapdraw.getBitmap();
Bitmap smallMarker = Bitmap.createScaledBitmap(b, height, height, false);
imgViewTabIcon.setImageBitmap(smallMarker);
}
}
public void SetUnSelectView(TabLayout tabLayout,int position) {
TabLayout.Tab tab = tabLayout.getTabAt(position);
View selected = tab.getCustomView();
TextView textView = (TextView) selected.findViewById(R.id.tab);
textView.setTextColor(mContext.getResources().getColor(R.color.white));
ImageView imgViewTabIcon = selected.findViewById(R.id.imgViewTabIcon);
if(textView.getText().toString().equals("tab1")){
imgViewTabIcon.setImageResource(R.drawable.img_11);
}else if(textView.getText().toString().equals("tab2")){
imgViewTabIcon.setImageResource(R.drawable.img_22);
}else if(textView.getText().toString().equals("tab3")){
imgViewTabIcon.setImageResource(R.drawable.img_33);
}else{
imgViewTabIcon.setImageResource(R.drawable.img_44);
}
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
#Override
public int getItemPosition(Object object){
return ViewPagerAdapter.POSITION_NONE;
}
}
"Custom Layout for Tab"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom_tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/tab_color_selector"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center">
<ImageView
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:id="#+id/imgViewTabIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/img_11" />
<TextView
android:id="#+id/tab"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:padding="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
android:textColor="#FFFFFF"
android:layout_gravity="center"
android:textSize="12sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
"MainActivity XML File"
<?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:background="#drawable/bg"
android:orientation="vertical"
tools:context=".activity.MainActivity">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
app:tabBackground="#drawable/tab_color_selector"
app:tabGravity="fill"
app:tabIndicatorColor="#color/gps_btn"
app:tabMode="fixed"
app:tabSelectedTextColor="#ffffff"
app:tabTextColor="#ffffff" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</LinearLayout>

Related

TabLayout icon Not Shown

I use Material TabLayout:
<com.google.android.material.tabs.TabItem
android:text="item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="#drawable/item1" />
<com.google.android.material.tabs.TabItem
android:icon="#drawable/item2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="item2"
/>
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="item3"/>
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="item4" />
</com.google.android.material.tabs.TabLayout>
and androidx ViewPager.
ViewPagerAdapter:
public class ViewPagerAdapter extends FragmentPagerAdapter
{
private List<Fragment> fragmentList=new ArrayList<>();
private List<String> fragmentTitleList=new ArrayList<>();
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return fragmentTitleList.size();
}
#NonNull
#Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return fragmentTitleList.get(position);
}
public void addPage(Fragment fragment, String title)
{
fragmentList.add(fragment);
fragmentTitleList.add(title);
}
}
But when i add my Fragments with addPage() method to viewpager, the icon and text of TabLayout change To my title which add in addPage()(icon remove).
But i want to title be my text and icon which set in TabItem.
Is there any way to do this?
I think you should follow below code make a custom tab layout for you tabs
[1]. custom_tab.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:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="#+id/tabImg"
android:layout_width="wrap_content"
android:layout_height="20dp" />
<TextView
android:id="#+id/tabContent"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginStart="#dimen/_5sdp"
android:layout_marginEnd="#dimen/_5sdp"
android:gravity="center"
android:text=""
android:textAlignment="center"
android:textColor="#color/white" />
</LinearLayout>
[2]. Your Activity
private String[] tabTitles = {"Pending", "Ongoing", "Completed"};
private int[] tabIcons = {android.R.drawable.arrow_down_float,android.R.drawable.checkbox_off_background, android.R.drawable.checkbox_on_background};
TextView tabContent;
private void setUpTabIcon() {
tabLayout.addTab(tabLayout.newTab());
tabLayout.addTab(tabLayout.newTab());
tabLayout.addTab(tabLayout.newTab());
for (int i = 0; i < tabLayout.getTabCount(); i++) {
LinearLayout tabLinearLayout = (LinearLayout) LayoutInflater.from(MainActivity.this).inflate(R.layout.custom_tab, null);
TextView tabContent = (TextView) tabLinearLayout.findViewById(R.id.tabContent);
ImageView tabImg = tabLinearLayout.findViewById(R.id.tabImg);
if (i == 0) {
tabContent.setText(tabTitles[i]);
tabImg.setColorFilter(ContextCompat.getColor(getApplicationContext(), R.color.white), android.graphics.PorterDuff.Mode.MULTIPLY);
}
tabImg.setImageResource(tabIcons[i]);
tabLayout.getTabAt(i).setCustomView(tabLinearLayout);
}
Log.e(TAG, "onCreate: " + tabLayout.getTabCount());
pageAdapter = new ViewPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
binding.viewPager.setAdapter(pageAdapter);
binding.viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
pageAdapter.notifyDataSetChanged();
}
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
LinearLayout linearLayout = (LinearLayout) tab.getCustomView();
TextView text = (TextView) linearLayout.findViewById(R.id.tabContent);
ImageView img = linearLayout.findViewById(R.id.tabImg);
if (text != null) {
text.setText(tabTitles[tab.getPosition()]);
img.setColorFilter(ContextCompat.getColor(getApplicationContext(), R.color.white), android.graphics.PorterDuff.Mode.MULTIPLY);
}
binding.viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
LinearLayout linearLayout = (LinearLayout) tab.getCustomView();
TextView tabContent = linearLayout.findViewById(R.id.tabContent);
ImageView img = linearLayout.findViewById(R.id.tabImg);
if (tabContent != null) {
tabContent.setText("");
img.setColorFilter(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimaryDark), android.graphics.PorterDuff.Mode.MULTIPLY);
}
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});

How to set Custom tab in tab layout in android

actvity_main.xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:background="#ef9f9f"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
// MainActivity :
public class MainActivity extends AppCompatActivity {
private TabLayout tabLayout;
public ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
View headerView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.custom_tab, null, false);
final LinearLayout linearLayoutOne = (LinearLayout) headerView.findViewById(R.id.ll);
final LinearLayout linearLayout2 = (LinearLayout) headerView.findViewById(R.id.ll2);
final TextView text1 = (TextView) headerView.findViewById(R.id.tvtab1);
final TextView text2 = (TextView) headerView.findViewById(R.id.tvtab2);
tabLayout.getTabAt(0).setCustomView(linearLayoutOne);
tabLayout.getTabAt(1).setCustomView(linearLayout2);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
if (tab.getText().equals("ONE")) {
text1.setVisibility(View.VISIBLE);
} else {
text2.setVisibility(View.VISIBLE);
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
if (tab.getText().equals("ONE")) {
text1.setVisibility(View.GONE);
} else {
text2.setVisibility(View.GONE);
}
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new OneFragment(), "ONE");
adapter.addFragment(new TwoFragment(), "TWO");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
This is my code I want to set custom tab like when i select the first tab then indicator and tab width or weight of the first tab I should increase and second tab decrease show only image not text same if we select the second tab then first tab indicator or weight should decrease and second tab text and image should visibility on:
my current Screen:
in this u can see that tab one is selected image and text are visible tab second is not unselected so the only image is visible no text:
but my Expected tab is like this :
look in this when we select tab then indicator and width of tab increase and tab 2 decreases please suggest me how I will achieve this .thanx
you can easily achieve custom tab with tab layout,
try this one:
public void setupTabView(){
for (int i = 0; i < tabLayout.getTabCount(); i++) {
tabLayout.getTabAt(i).setCustomView(R.layout.custom_tab);
TextView tab_name = (TextView) tabLayout.getTabAt(i).getCustomView().findViewById(R.id.txt_tab_name);
tab_name.setText("" + tabNames[i]);
}
}
And make one drawable file with name custom_tab:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/txt_tab_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="14sp" />
</RelativeLayout>
Use Below Code .It is have two option.
(i) Create custom TabLayout
(ii) Change custom tablayout text color
(i) Custom TabLayout
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/tab"
android:layout_width="match_parent"
android:textSize="15sp"
android:gravity="center"
android:textColor="#color/txtbox_text_color_darek"
android:layout_height="match_parent"
/>
Source code is:
TextView tabOne = (TextView)
LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabOne.setText("KPIs" + " ");
tabOne.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.alert_gray,
0);
Objects.requireNonNull(tabLayout.getTabAt(4)).setCustomView(tabOne);
(II) Change Color
private void custom_tablayout_select_unselected_four(final TextView tabOne) {
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
if (Objects.requireNonNull(tabLayout.getTabAt(4)).isSelected()) {
tabOne.setTextColor(getResources().getColor(R.color.text_color_darkblue));
} else {
tabOne.setTextColor(getResources().getColor(R.color.txtbox_text_color_darek));
}
}
#Override`
public void onTabUnselected(TabLayout.Tab tab) {
// tabOne.setTextColor(Color.GREEN);
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
// tabOne.setTextColor(Color.GREEN);
}
});
}
I copied the solution from here, Maybe it will be useful for you
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
TabHost mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("tab_test1")
.setIndicator((""),getResources().getDrawable(R.drawable.mzl_05))
.setContent(new Intent(this, NearBy.class)));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2")
.setIndicator((""),getResources().getDrawable(R.drawable.mzl_08))
.setContent(new Intent(this, SearchBy.class)));
mTabHost.setCurrentTab(0);
mTabHost.getTabWidget().getChildAt(0).setLayoutParams(new
LinearLayout.LayoutParams((width/2)-2,50));
mTabHost.getTabWidget().getChildAt(1).setLayoutParams(new
LinearLayout.LayoutParams((width/2)-2,50));
You can try another code tabHost.getTabWidget().getChildAt(0).getLayoutParams().width = 50;
Create Custom tab layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="horizontal">
<TextView
android:id="#+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#drawable/tab_text_color_selector"
android:textSize="#dimen/medium_text"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_count"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginStart="4dp"
android:background="#drawable/badge_background"
android:gravity="center"
android:textColor="#color/colorPrimary"
android:textSize="#dimen/medium_text"
android:textStyle="bold"
android:visibility="gone" />
</LinearLayout>
tabTitles = new String[]{getString(R.string.main_tab_call), getString(R.string.main_tab_chat), getString(R.string.main_tab_contact)};
private void setupTabIcons() {
for (int i = 0; i < tabTitles.length; i++) {
mTabLayout.getTabAt(i).setCustomView(prepareTabView(i));
}
}
private View prepareTabView(int pos) {
View view = getLayoutInflater().inflate(R.layout.custom_tab, null);
TextView tv_title = view.findViewById(R.id.tv_title);
TextView tv_count = view.findViewById(R.id.tv_count);
tv_title.setText(tabTitles[pos]);
return view;
}

Badge notification with Tablayout

This is my tab layout:
Would anyone help me to add badge notification on each Tablyout icon?
I have searched here found depreciated solutions.
This is my activity class:
public class navigation extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private TabLayout tabLayout;
private InterstitialAd mInterstitialAd;
private Button mNextLevelButton;
private TextView mTextView;
private ViewPager viewPager;
private int[] tabIcons = {
R.drawable.ic_favorite_final,
R.drawable.ic_live_final,
R.drawable.ic_matches_final,
R.drawable.ic_sort
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
MobileAds.initialize(getApplicationContext(), "ca-app-pub-
7294884496094914~4747804788");
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT ) !=
0) {
// Activity was brought to front and not created,
// Thus finishing this will get us to the last viewed activity
finish();
return;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
// tabLayout.getTabAt(3).setIcon(tabIcons[3]);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new OneFragment());
// adapter.addFrag(new TwoFragment(), getString(R.string.livestream));
adapter.addFrag(new ThreeFragment());
adapter.addFrag(new FourFragment());
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment) {
mFragmentList.add(fragment);
// mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
// return mFragmentTitleList.get(position);
return null;
}
}
}
Then i want to add the total count notification on the title icon image if it s possible
UPDATED:
<?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"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.tiger.alahedclub.activity.navigation">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:popupTheme="#style/generalnotitle"
>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
app:titleTextColor="#android:color/black"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/yellow"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/generalnotitle">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/action_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_centerHorizontal="true"
android:text="#string/app_name"
android:textColor="#android:color/black"
android:textSize="24sp" />
<ImageView
android:background="#drawable/ahed_small"
android:id="#+id/imagetitle"
android:layout_width="40dp"
android:layout_height="25dp"
android:layout_centerVertical="true"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_toStartOf="#+id/action_bar_title"
android:layout_toLeftOf="#+id/action_bar_title"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:background="#color/yellow"
app:tabTextColor="#android:color/black"
app:tabSelectedTextColor="#android:color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorHeight="4dp"
app:tabMode="fixed"
app:tabGravity="fill" />
</android.support.design.widget.AppBarLayout>
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
android:layout_gravity="bottom"
ads:adUnitId="#string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/cardview_back"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
UPDATE 2:
private Drawable buildCounterDrawable(int count, int backgroundImageId) {
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.noitification_count, null);
view.setBackgroundResource(backgroundImageId);
if (count == 0) {
View counterTextPanel = view.findViewById(R.id.badge_layout1);
counterTextPanel.setVisibility(View.GONE);
} else {
TextView textView = (TextView) view.findViewById(R.id.badge_notification_1);
textView.setText("" + count);
}
view.measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.setDrawingCacheEnabled(true);
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
return new BitmapDrawable(getResources(), bitmap);
}
and setIcon as this view.
tabLayout.getTabAt(0).setIcon(buildCounterDrawable(1,R.drawable.ic_count_bg));
Try this was worked for me..
tabLayout.getTabAt(0).setIcon(buildCounterDrawable(COUNT));
buildCounterDrawable method,
private Drawable buildCounterDrawable(int count) {
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.cart_overflow, null);
view.setBackgroundResource(R.drawable.ic_tool_cart);
TextView textView = (TextView) view.findViewById(R.id.count);
if (count == 0) {
textView.setVisibility(View.GONE);
} else {
textView.setVisibility(View.VISIBLE);
textView.setText(String.valueOf(count));
}
view.measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.setDrawingCacheEnabled(true);
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
return new BitmapDrawable(getResources(), bitmap);
}
cart_overflow.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/counterPanel"
android:layout_width="32dp"
android:layout_height="32dp"
android:background="#drawable/ic_tool_cart"> <!-- Notication Icon -->
<TextView
android:layout_gravity="top|end"
android:gravity="center"
android:background="#drawable/ic_cart_count"
android:id="#+id/count"
android:layout_width="18dp"
android:layout_height="18dp"
android:textSize="12sp"
android:textColor="#FFFFFF"/>
</FrameLayout>
drawable - ic_cart_count (counter background)
<vector
android:height="18dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0"
android:width="18dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FFEF0777"
android:pathData="M12,12m-10,0a10,10 0,1 1,20 0a10,10 0,1 1,-20 0"/>
</vector>
I made this for a single image. You change as per your needs by adding layout and image as parameters.
Try this. code for onCreateOptionMenu.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem menuItem = menu.findItem(R.id.action_settings);
menuItem.setIcon(buildCounterDrawable(4, android.R.color.transparent));
return true;
}
private Drawable buildCounterDrawable(int count, int backgroundImageId) {
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.noitification_count, null);
view.setBackgroundResource(backgroundImageId);
if (count == 0) {
View counterTextPanel = view.findViewById(R.id.badge_layout1);
counterTextPanel.setVisibility(View.GONE);
} else {
TextView textView = (TextView) view.findViewById(R.id.badge_notification_1);
textView.setText("" + count);
}
view.measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.setDrawingCacheEnabled(true);
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
return new BitmapDrawable(getResources(), bitmap);
}
notification_count.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#android:style/Widget.ActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clickable="true"
android:gravity="center"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/badge_layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/relative_layout_item_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/button1"
android:layout_width="#dimen/_100sdp"
android:layout_height="#dimen/_100sdp"
android:background="#drawable/ic_dollar" />
</RelativeLayout>
<TextView
android:id="#+id/badge_notification_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#id/relative_layout_item_count"
android:background="#drawable/circle_bg"
android:gravity="center"
android:text="12"
android:textColor="#FFF"
android:padding="#dimen/_10sdp"
android:textSize="#dimen/_30sdp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
put your shape file
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#color/green"/>
</shape>

Unable to arrange android tabs in a proper sequence

Hello all I simply want to achieve this.But I am not being able to achieve this
I did this to achieve this
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#42474b"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<ImageView
android:id="#+id/toolbarlogo"
android:src="#mipmap/toolbarlogo"
android:layout_width="wrap_content"
android:layout_marginRight="200dp"
android:layout_height="46dp"
/>
</android.support.v7.widget.Toolbar>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#494e51"
android:gravity="center_vertical"
android:paddingLeft="40dp"
android:textSize="10dp"
android:textColor="#fff"
android:text="The Ongoing survey closes on Dec 31,2016 at 2:00pm GMT"
/>
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="#fff"/>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabTextAppearance="#style/MineCustomTabText"
app:tabMode="fixed" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:background="#drawable/innerpg_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
and this is my class file
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
R.drawable.ongoing,
R.drawable.upcoming,
R.drawable.results
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setPadding(0, getStatusBarHeight(), 0, 0);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
for(int i=0; i < tabLayout.getTabCount(); i++) {
View tab = ((ViewGroup) tabLayout.getChildAt(0)).getChildAt(i);
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) tab.getLayoutParams();
p.setMargins(10, 10, 50, 30);
tab.requestLayout();
}
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new Ongoing(), "Ongoing");
adapter.addFrag(new Upcomming(), "Upcomming");
adapter.addFrag(new Result(), "Result");
viewPager.setAdapter(adapter);
}
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
return true;
}
return super.onOptionsItemSelected(item);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
Using above code I am getting tab text below the icon(i mean in two different line).THe next issue is I have set the margin but I dont know how to set the color of margin.The final issue is I want to change the background color of active tab to yellow as shown in image.Please help me fix this issue
try this
private void setupTabIcons() {
LayoutInflater inflater = LayoutInflater.from(this);
View view1 = inflater.inflate(R.layout.custom_view, null, false);
((TextView) view1.findViewById(R.id.text)).setText("Tab1");
((ImageView) view1.findViewById(R.id.image)).setImageResource(tabIcon[0]);
View view2 = inflater.inflate(R.layout.custom_view, null, false);
((TextView) view2.findViewById(R.id.text)).setText("Tab2");
((ImageView) view2.findViewById(R.id.image)).setImageResource(tabIcon[0]);
View view3 = inflater.inflate(R.layout.custom_view, null, false);
((TextView) view3.findViewById(R.id.text)).setText("Tab3");
((ImageView) view3.findViewById(R.id.image)).setImageResource(tabIcon[0]);
tabLayout.getTabAt(0).setCustomView(view1);
tabLayout.getTabAt(1).setCustomView(view2);
tabLayout.getTabAt(2).setCustomView(view3);
your custom view will look like
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
You can Use android:drawableLeft to display drawable on left of
textview properly.
For Different backgroundColor for Different state You can use
android:background for different color state like this

Dynamically set ViewPager's height with fragments

I'm trying to set ViewPager's height dynamically, so that each Fragment of the ViewPager has its own height. I based my code on the solution found by czaku here: Dynamically set ViewPager height
activity_artwork_page.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_artwork_page"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.andrea.chatbeacon.ArtworkPageActivity">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="4000dp" />
<fragment
class="com.example.andrea.chatbeacon.ChatFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/chat_fragment"
android:layout_marginTop="40dp"
tools:layout="#layout/fragment_chat" />
</LinearLayout>
</ScrollView>
ArtworkPageActivity.java
public class ArtworkPageActivity extends AppCompatActivity {
private ViewPager mPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_artwork_page);
mPager = (ViewPager) findViewById(R.id.pager);
PagerAdapter mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
#Override
protected void onResume() {
super.onResume();
ViewTreeObserver viewTreeObserver = mPager.getViewTreeObserver();
viewTreeObserver
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout artwork_slide_layout = (LinearLayout) findViewById(R.id.artwork_slide_layout);
int viewPagerWidth = mPager.getWidth();
int viewPagerHeight = artwork_slide_layout.getHeight();
layoutParams.width = viewPagerWidth;
layoutParams.height = viewPagerHeight;
mPager.setLayoutParams(layoutParams);
mPager.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
}
#Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
// If the user is currently looking at the first step, allow the system to handle the
// Back button. This calls finish() on this activity and pops the back stack
super.onBackPressed();
} else {
// Otherwise, select the previous step
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = new ArtworkSlideFragment();
Bundle args = new Bundle();
args.putInt("position", position);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
int id = getIntent().getIntExtra("id", 0);
return Variables.artworks[id].getTitles().length;
}
}
}
fragment_artwork_slide.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/artwork_slide_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ArtworkSlideFragment">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/artwork_image"
android:contentDescription="#string/artwork_image_description" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/artwork_title"
android:textColor="#android:color/black"
android:textSize="18sp"
android:layout_marginTop="#dimen/activity_vertical_margin" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/artwork_text"
android:textColor="#android:color/black"
android:textSize="15sp"
android:layout_marginTop="10dp" />
</LinearLayout>
ArtworkSlideFragment.java
public class ArtworkSlideFragment extends Fragment {
public ArtworkSlideFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle args = getArguments();
int id = getActivity().getIntent().getIntExtra("id", 0);
int position = args.getInt("position", 0);
View view = inflater.inflate(R.layout.fragment_artwork_slide, container, false);
ImageView image = (ImageView) view.findViewById(R.id.artwork_image);
TextView title = (TextView) view.findViewById(R.id.artwork_title);
TextView text = (TextView) view.findViewById(R.id.artwork_text);
Glide.with(getActivity().getApplicationContext()).load(Variables.artworks[id].getImages()[position]).into(image);
title.setText(Variables.artworks[id].getTitles()[position]);
text.setText(Variables.artworks[id].getTexts()[position]);
return view;
}
}
I tried to set
viewPagerHeight = artwork_slide_layout.getHeight();
but it seems not to work. How can I set the viewPagerHeight?
I found a working solution from vabhishek at:
https://github.com/vabhishek/WrapContentViewPagerDemo
This works for me even without the CustomScrollView defined by vabhishek.

Categories

Resources