How can I make a Viewpager with custom tabs like this? - java

How can I achieve a Tab/Viewpager style like this ?
So its not looking like the normal Sliding Tab Layout like here.
Are there some guides to follow out there ? It looks very custom to me

First define the slidingTabLayout with your view pager as:
<com.forthcode.androidoze.Utils.SlidingTabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF"/>
<android.support.v4.view.ViewPager
android:id="#+id/vpPager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
In above code I have taken the sliding tab color as transparent, you can take any as per your need.
Now define a custom view thatrepresent your single tab.
customtab.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:padding="10dp">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>
Now create a selector.xml inside the drawable folder that defines the text color of the tab when it will be selected and not selected.
selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#ccc" />
<item android:state_focused="true" android:color="#ccc" />
<item android:state_pressed="true" android:color="#ccc" />
<item android:color="#fff" />
</selector>
In the last line of selector.xml is the default tab text color when the tab is not selected.
Now finally in your void populateTabStrip() method of SlidingTabLayout class just add the code to implement the selector with the tab as shown below:
tabTitleView.setTextColor(getResources().getColorStateList(R.drawable.selector));
Remember this line should be inside the populateTabStrip() inside the for loop, just to make it easy i'm writing the complete method populateTabStrip(){} of slidingTabLayout, so your populateTabStrip() should look like this:
private void populateTabStrip() {
final PagerAdapter adapter = mViewPager.getAdapter();
final OnClickListener tabClickListener = new TabClickListener();
for (int i = 0; i < adapter.getCount(); i++) {
View tabView = null;
TextView tabTitleView = null;
if (mTabViewLayoutId != 0) {
// If there is a custom tab view layout id set, try and inflate it
tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip,
false);
tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId);
}
if (tabView == null) {
tabView = createDefaultTabView(getContext());
}
if (tabTitleView == null && TextView.class.isInstance(tabView)) {
tabTitleView = (TextView) tabView;
}
if (mDistributeEvenly) {
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) tabView.getLayoutParams();
lp.width = 0;
lp.weight = 1;
}
tabTitleView.setText(adapter.getPageTitle(i));
tabView.setOnClickListener(tabClickListener);
tabTitleView.setTextColor(getResources().getColorStateList(R.drawable.selector));
String desc = mContentDescriptions.get(i, null);
if (desc != null) {
tabView.setContentDescription(desc);
}
mTabStrip.addView(tabView);
if (i == mViewPager.getCurrentItem()) {
tabView.setSelected(true);
}
}
}
Now in the Activity or Fragment where you are using the tab define the viewpager,slidingtablayout and the viewpager adapter, and set the adapter with view pager and set the slidingtab with view pager as shown below(Note i have used inside fragment, if you are using Activity then modify as per your need):
SlidingTabLayout mSlidingTabLayout;
ViewPager mViewPager;
PagerAdapter mPagerAdapter;
SharedPreferences mySharedpref;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_home, container, false);
mPagerAdapter=new PagerAdapter(getChildFragmentManager());
mViewPager= (ViewPager) view.findViewById(R.id.vpPager);
mSlidingTabLayout= (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
mViewPager.setAdapter(mPagerAdapter);
mSlidingTabLayout.setCustomTabView(R.layout.customtab, R.id.textView1);
mSlidingTabLayout.setSelectedIndicatorColors(R.color.tabIndicator);
mSlidingTabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {//change the color of the tab indcator
#Override
public int getIndicatorColor(int position) {
// TODO Auto-generated method stub
return getResources().getColor(R.color.tabIndicator);
}
});
mSlidingTabLayout.setViewPager(mViewPager);
mViewPager.setCurrentItem(tabPosition);
return view;
}
This should work fine. if you find any issue then comment, i will try to reply

You can have a top layout in your activity, in top of the view pager.
In the OnPageSelected of the ViewPager you can change the text color of the top layout to simulate tab selection

Related

Set a title and description for each photo in carouse android

I am using a carouselView library for my app. https://github.com/sayyam/carouselview
The images are displaying and the carousel is working but how do I set a title and description below each image as it slides for each image.
I have a string containing all the title and text for each image.
e.g
<string name="first_image_title">First image title</string>
<string name="first_image_desc">This is a description for first image title</string>
<string name="second_image_title">2nd image title</string>
<string name="second_image_desc">This is a description for the second image title</string>
<string name="third_image_title">3rd image title</string>
<string name="third_image_desc">This is a description for the third image title</string>
All these are supposed to be placed below the sliding images (for each image.)
fragment_safety.xml
Containing the image and where the title and description for each image supposed to be
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="fragments.SafetyFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tabContent"
android:background="#drawable/rounded_corner2">
<com.synnapps.carouselview.CarouselView
android:id="#+id/carouselView"
android:layout_width="match_parent"
android:layout_height="200dp"
app:fillColor="#FFFFFFFF"
app:pageColor="#00000000"
app:radius="6dp"
app:slideInterval="5000"
app:strokeColor="#FF777777"
app:strokeWidth="1dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textTitle"
android:layout_below="#+id/carouselView"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textDesc"
android:layout_below="#+id/textTitle"/>
</RelativLayout>
SafetyFragment.java
According to the doc, I am supposed to implement ViewListener for customView but I cant wrap my head around how to do this....Get all the titles and description in the string and set them for each of the images sliding.
public class SafetyFragment extends Fragment {
private CarouselView carouselView;
private int[] sampleImages = {R.drawable.image1,
R.drawable.image2, R.drawable.image3,
R.drawable.image4, R.drawable.image5,
R.drawable.image6};
public SafetyFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root = inflater.inflate(R.layout.fragment_safety, container, false);
carouselView = root.findViewById(R.id.carouselView);
ImageListener imagesListenerx = new ImageListener() {
#Override
public void setImageForPosition(int position, ImageView imageView) {
Glide.with(SafetyFragment.this).load(sampleImages[position]).into(imageView);
}
};
ViewListener viewListener = new ViewListener() {
#Override
public View setViewForPosition(int position) {
View customView = getLayoutInflater().inflate(R.layout.fragment_safety, null);
//set view attributes here
return customView;
}
};
carouselView.setPageCount(sampleImages.length);
carouselView.setViewListener(viewListener);
return root;
}
}
I found a way to do this.
1.Declare a string array for each text you want to display in each carousel
private String[] titles = {"One", "Two", "Three"}
2. Set a custom view listener and get the view in the layout
ViewListener viewListener = new ViewListener() {
int i;
#Override
public View setViewForPosition(int position) {
View customView = getLayoutInflater().inflate(R.layout.fragment_safety, null);
//set view attributes here
safety_title = customView.findViewById(R.id.safetyTextTitle);
safety_images = customView.findViewById(R.id.safetyImages);
Glide.with(SafetyFragment.this).load(sampleImages[position]).into(safety_images);
//Very Important
safety_title.setText(titles[position]);
return customView;
}
};
As per the documentation you can create a custom view and bind views with the data
ViewListener viewListener = new ViewListener() {
#Override
public View setViewForPosition(int position) {
View customView = getLayoutInflater().inflate(R.layout.view_custom, null);
//set view attributes here
return customView;
}
};
I use this way:
ViewListener viewListener = new ViewListener() {
#Override
public View setViewForPosition(int position) {
View customView = getLayoutInflater().inflate(R.layout.custom_carousel, null);
//set view attributes here
TextView car_title = (TextView) customView.findViewById(R.id.textTitle);
ImageView car_image = (ImageView) customView.findViewById(R.id.imageView);
car_title.setText(sampleTitle[position]);
car_image.setImageResource(sampleImages[position]);
return customView;
}
};
While custom view in custom_carousel.xml will be like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/textDesc"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
tools:src="#tools:sample/avatars[1]" />
<TextView
android:id="#+id/textTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="test" />
</RelativeLayout>

Android studio java - Showing bottom sheet behavior in a map fragment inside a view pager

I have an application composed by a pageviewer with 3 fragments.
one of this fragment is a map and the idea is to show a bottosheetbehaviuor on map marker click.
here is my layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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="com.myapp.android.activities.MainActivity"
android:nestedScrollingEnabled="true">
<RelativeLayout
android:id="#+id/rlMap"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
tools:layout="#layout/content_main"
/>
<androidx.core.widget.NestedScrollView
android:id="#+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#android:color/white"
app:behavior_hideable="true"
app:behavior_peekHeight="155dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<TextView
android:id="#+id/bottom_sheet_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:textSize="16sp" />
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
then... during oncreate I setup both view pager , map and bottom sheet
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
bottomSheetText = findViewById(R.id.bottom_sheet_text);
View bottomSheet = findViewById(R.id.bottom_sheet);
mBottomSheetBehaviour = BottomSheetBehavior.from(bottomSheet);
mBottomSheetBehaviour.setState(BottomSheetBehavior.STATE_HIDDEN);
ViewPager viewPager = findViewById(R.id.viewpager);
setupViewPager(viewPager);
TabLayout tabLayout = findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(viewPager);
the viewpager and map via a call to a a func
private void setupViewPager(ViewPager viewPager) {
Log.d(TAG, "setupViewPager");
ViewPagerAdapter adapter = new ViewPagerAdapter
(getSupportFragmentManager());
createUpMap();
adapter.addFragment(mapFrag, "Map");
//TEMP
adapter.addFragment(new SupportMapFragment(), "List");
adapter.addFragment(new SupportMapFragment(), "Session");
viewPager.setAdapter(adapter);
private void createUpMap() {
Log.d(TAG, "createUpMap");
if (mapFrag == null) {
Log.d(TAG, "mapFrag is null, creation of map...");
mapFrag = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.rlMap);
mapFrag = new SupportMapFragment();
lastly, I ovveride marker click to display the bottomsheet:
mMap.setOnMarkerClickListener(marker -> {
Log.e(TAG, "POINT HAS BEEEN CLICKED status: " + mBottomSheetBehaviour.getState());
if (mBottomSheetBehaviour.getState() == BottomSheetBehavior.STATE_EXPANDED) {
bottomSheetText.setText(marker.getTag().toString());
mBottomSheetBehaviour.setPeekHeight(200);
mBottomSheetBehaviour.setState(BottomSheetBehavior.STATE_EXPANDED);
I launch the application, click on the marker and in the log in log I can see correct state ( I'm printing it as you see from calls) but absolutely no way to see it on screen. I 'm little out of ideas as there's no error and i don't know how to move.
Someone can move me in the right direction ?
many thanks!
You can try to build your own BottomSheetDialogFragment and show it with
val bottomSheetDialogFragment = MytBottomSheetDialogFragment.newInstance()
//show it
bottomSheetDialogFragment.show(fragmentManager!!, bottomSheetDialogFragment.tag)
So you don't have to create the view inside your layout file and can simple reuse it.
Update:
You can set your layout in the setupDialog function. Just overrride it and inflate your custom BottomSheetLayout.
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
View contentView = View.inflate(getContext(), R.layout.custom_layout, null);
dialog.setContentView(contentView);
}

Using BottomBar prevents fragments from opening?

I am using the Support Library to add a bottom bar similar to the material design one. The bottom bar works great but it seems that if I have the bar displayed, if I try to open any fragment from my custom adapter, the fragment does not open...or maybe it opens behind my main layout? I have no idea how to figure this out. Below is my code.
I've read more posts on SO and around the web and I think this is related to the fragment being properly loaded but below or next to the bottom bar...and that is why it isn't visible? Why does this happen? Is it because the bottom bar has a LinearLayout? I defined it as a menu so I'm not sure if I can control it being a LinearLayout....
Setting up the bottom bar, this method is called from the onCreate of my activity:
public void setupBottomToolbar(Bundle savedInstanceState) {
mBottomBar = BottomBar.attach(MainActivity.this, savedInstanceState);
mBottomBar.setItems(R.menu.bottombar_menu);
mBottomBar.setOnMenuTabClickListener(new OnMenuTabClickListener() {
#Override
public void onMenuTabSelected(#IdRes int menuItemId) {
if (menuItemId == R.id.toolbar_jobs) {
} else if (menuItemId == R.id.toolbar_messages) {
} else if (menuItemId == R.id.toolbar_recentJobs) {
} else if (menuItemId == R.id.toolbar_employerPools) {
}
}
#Override
public void onMenuTabReSelected(#IdRes int menuItemId) {
if (menuItemId == R.id.toolbar_jobs) {
// The user reselected item number one, scroll your content to top.
} else if (menuItemId == R.id.toolbar_messages) {
} else if (menuItemId == R.id.toolbar_employerPools) {
} else if (menuItemId == R.id.toolbar_recentJobs) {
}
}
});
// Setting colors for different tabs when there's more than three of them.
// You can set colors for tabs in three different ways as shown below.
mBottomBar.getBar().setBackgroundColor(getResources().getColor(R.color.laborswipe_darkgray));
mBottomBar.setActiveTabColor(getResources().getColor(R.color.laborswipe_lightgray));
// Make a Badge for the second tab, with red background color and a value of "13".
BottomBarBadge unreadMessages = mBottomBar.makeBadgeForTabAt(1, getResources().getColor(R.color.laborswipe_orange), 5);
// Control the badge's visibility
unreadMessages.show();
//unreadMessages.hide();
// Change the displayed count for this badge.
//unreadMessages.setCount(4);
// Change the show / hide animation duration.
unreadMessages.setAnimationDuration(200);
// If you want the badge be shown always after unselecting the tab that contains it.
unreadMessages.setAutoShowAfterUnSelection(true);
// If you don't want this badge to be hidden after selecting the tab contains it.
unreadMessages.setAutoShowAfterUnSelection(false);
}
In my adapter, I am trying to open the fragment when you click a button, like this:
holder.desc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context, "Open Description", Toast.LENGTH_SHORT).show();
JobDescFragment firstFragment = new JobDescFragment();
((MainActivity)context).getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
}
});
If I comment out the call to setupBottomToolbar() in my onCreate of the activity, the fragment opens up fine...but that means I don't have the bottom bar...
What am I missing? There has to be a way to use the bottom bar and also open a fragment?
Thanks!
EDIT:
Here is the top part of my activity.
public class MainActivity extends AppCompatActivity {
private ArrayList<String> swipecardsList;
private ArrayList<Job> jobList = new ArrayList<Job>();
private JobAdapter arrayAdapter; //arrayadapter
private BottomBar mBottomBar;
SharedPreferences settings;
#InjectView(R.id.frame) SwipeFlingAdapterView flingContainer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove title bar
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//color the notification bar with our company colors
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(this.getResources().getColor(R.color.laborswipe_notificationbar));
//remove title from action bar and add the logo to the top left of the action bar
setupTopToolbar();
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
//set up the bottom toolbar using the roughike library to mimic android material design
setupBottomToolbar(savedInstanceState);
My adapter:
public class JobAdapter extends ArrayAdapter<Job> {
private final Context context;
private final ArrayList<Job> jobs;
private final int layoutResourceId;
private final SwipeFlingAdapterView flingContainer;
private boolean isExpanded = false;
public JobAdapter(Context context, int layoutResourceId, ArrayList<Job> jobs, SwipeFlingAdapterView flingContainer) {
super(context, layoutResourceId, jobs);
this.context = context;
this.jobs = jobs;
this.layoutResourceId = layoutResourceId;
this.flingContainer = flingContainer;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
final ViewHolder holder;
String pay, hrs;
final Bundle fragmentParams = new Bundle();
LayoutInflater inflater = LayoutInflater.from(context);
if (view == null) {
view = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.title = (TextView)view.findViewById(R.id.tv_jobTitle);
holder.desc = (TextView) view.findViewById(R.id.tv_JobDesc);
view.setTag(holder);
} else {
holder = (ViewHolder)view.getTag();
}
Job j = jobs.get(position);
holder.title.setText(j.getJobTitle());
holder.desc.setText(j.getDescription());
//when user clicks apply, swipe the card right
holder.apply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Open up a fragment to display the entire job description
Toast.makeText(context, "Applied", Toast.LENGTH_SHORT).show();
flingContainer.getTopCardListener().selectRight();
}
});
//when user clicks dismiss, swipe the card left
holder.dismiss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Open up a fragment to display the entire job description
Toast.makeText(context, "Dismissed", Toast.LENGTH_SHORT).show();
flingContainer.getTopCardListener().selectLeft();
}
});
//on click event listener for the job description field - open larger window to read description
holder.desc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Toast.makeText(context, "Open Description", Toast.LENGTH_SHORT).show();
JobDescFragment firstFragment = new JobDescFragment();
Fragment frag = new Fragment();
frag = firstFragment.newJobDescFrag(j.getDescription());
((MainActivity) context).getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, frag)
.addToBackStack("JobDesc").commit();
}
});
return view;
}
public class ViewHolder
{
TextView title;
TextView payrate;
TextView dateRange;
TextView workinghrs;
TextView location;
TextView companyname;
TextView desc;
TextView experience;
TextView equipment;
Button apply, dismiss, expand;
}
}
activity_main.xml:
<merge
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">
<com.lorentzos.flingswipe.SwipeFlingAdapterView
android:id="#+id/frame"
android:background="#color/laborswipe_lightgray"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity"
android:layout_gravity="top" />
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</merge>
Fragment Layout:
<FrameLayout 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"
tools:context=".JobDescFragment">
<LinearLayout
android:id="#+id/outerDescriptionLayout"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_gravity="center_horizontal|top"
android:orientation="vertical"
android:background="#drawable/swipecard_shadow"
android:gravity="top"
android:layout_marginLeft="5dp">
<LinearLayout
android:id="#+id/DescriptionLayout"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_gravity="center_horizontal|top"
android:orientation="vertical"
android:weightSum="1"
android:gravity="top"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#00FF00"
android:clickable="true">
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:text="Detailed Description:"
android:textColor="#000000"
android:id="#+id/tv_title" />
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:text="THIS IS THE FULL DESCRIPTION"
android:textColor="#000000"
android:id="#+id/tv_fullDescription" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
Logcat:
08-07 11:20:47.799 13896-13896/com.lorentzos.swipecards.example I/System.out: DEBUG: job desc fragment loaded!
08-07 11:20:47.855 13896-13941/com.lorentzos.swipecards.example W/EGL_emulation: eglSurfaceAttrib not implemented
08-07 11:20:47.855 13896-13941/com.lorentzos.swipecards.example W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaaa7f880, error=EGL_SUCCESS
08-07 11:20:48.002 13896-13941/com.lorentzos.swipecards.example V/RenderScript: 0xa1408000 Launching thread(s), CPUs 2
08-07 11:20:49.798 13896-13941/com.lorentzos.swipecards.example E/Surface: getSlotFromBufferLocked: unknown buffer: 0xae433ca0
When I use the bottom bar (not working- no fragment opened but toast displayed):
When I don't use the bottom bar (workin-fragment opened, background is green):
try to link a pic of problem and without problem(no bottombar) and since you are using merge the layout hierarchy will be laid off according to your activity's viewgroup(linear,relative) constraints(we don't know what they are like).
as you said when there is no bottombar ,you fragment displays perfectly though when the bottombar it there ,problem stats ,as per your log in fragment indicating that your fragment is loading perfectly even though when bottombar is visible mean fragment is there but is not visible ,seems like your fragment didn't get the appropriate space to get displayed.
other solution can be adding bottom bar to your fragment instead of activity to avoid any overlapping ,like
mBottomBar.attach(findViewById(R.id.fragmentContainer), savedInstanceState);
OK, I think the solution for this should be simple, from what I can see in your code, you are attaching the BottomBar to your activity, I think this is the problem. If you were to read the readme.md in the roughike/BottomBar github page you'd find this
Why is it overlapping my Navigation Drawer?
All you need to do is instead of attaching the BottomBar to your Activity, attach it to the view that has your content. For example, if your fragments are in a ViewGroup that has the id fragmentContainer, you would do something like this:
mBottomBar.attach(findViewById(R.id.fragmentContainer), savedInstanceState);
So, since navigation drawer works with transition a fragment in and out of view with animation, the same thing is happening when you are adding a new fragment to your activity.
The Solution
From what I can see in your code, your fragment container id is this: fragment_container in your activity layout. So according to the documentation you'd just need to do attach your bottomBar to the fragment_container instead of MainActivity.this
mBottomBar.attach(findViewById(R.id.fragment_container), savedInstanceState);
If the above doesn't work try this
What you'd need to do is add an extra FrameLayout to hold your bottombar, which has a transparent background, but is on top of your fragment.
So change your main_activity layout to
<merge
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">
<com.lorentzos.flingswipe.SwipeFlingAdapterView
android:id="#+id/frame"
android:background="#color/laborswipe_lightgray"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity"
android:layout_gravity="top" />
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="#+id/holder_bottombar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"/>
</merge>
Now in the code instead of attaching the bottom bar to mainactivity, just attach it to the holder like so
mBottomBar.attach(findViewById(R.id.holder_bottombar), savedInstanceState);

Unable to select GridView item

I'm trying to highlight background of one GridView element.
Activity.java:
private CustomAdapter ca;
public void onCreate(Bundle _) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
// ...
GridView gw = (GridView) findViewById(R.id.gridview);
gw.setAdapter(ca = new CustomAdapter(this));
gw.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// attempt 1:
view.setSelection(true);
// attempt 2:
gw.setSelection(position);
// attempt 3:
gw.setItemChecked(position, true);
// tried everything WITH and WITHOUT this:
ca.notifyDataSetChanged();
}
});
}
CustomAdapter.java
public class CustomAdapter extends BaseAdapter {
private LayoutInflater inflater;
public CustomAdapter(Context c) {
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Download items async, and call...
notifyDataSetChanged();
// ... in a callback
}
// getItem(), getCount() and getItemId() implemented here
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if( convertView!=null ) view = convertView;
else {
view = inflater.inflate(R.layout.grid_item, parent, false);
// calculate height so that an item is a square
int height = parent.getHeight();
if( height>0 ) {
ViewGroup.LayoutParams params = view.getLayoutParams();
params.height = height/3;
}
}
ImageView image = (ImageView) view.findViewById(R.id.image);
image.setImageBitmap( getItemImageBitmap(position) );
return view;
}
}
activity_layout.xml
<GridView android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchMode="columnWidth"
android:numColumns="3"
android:drawSelectorOnTop="false"
android:choiceMode="singleChoice"
android:listSelector="#drawable/grid_selector"
/>
<!-- I've tried any combination of those last three params I could think of -->
grid_item.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/image"
android:background="#drawable/grid_selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
grid_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#android:color/holo_red_dark" />
<item android:drawable="#android:color/transparent" />
</selector>
I tried putting grid_selector as item background OR as listSelector attribute in GridView.
My goal is to have one item highlighted upon user click (and after until other item is selected).
So the magic combination for me was:
gw.setItemChecked(position, true); in Activity.java is enough,
No changes in CustomAdapter.java,
listSelector="#drawable/grid_selector" MUST be removed from activity_layout.xml,
grid_item.xml must have background,
Use android:state_checked="true" instead of android:state_selected="true" in grid_selector.xml.

Fragment Not Displaying In Activity

Currently when running my application, going to this activity does not display my fragment. Am I adding the fragment wrong and so nothing in the fragment is displayed? Or is there something wrong the dynamic behavior in the fragment onCreateView method?
My Activity Code
public class AskQuestionActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ask_question);
}
}
My Activity XML
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="#string/header_text"/>
<fragment android:name="com.mypackage.AskQuestionFragment"
android:id="#+id/question_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
My Fragment Code
public class AskQuestionFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
String questionText = "What is your favorite color?";
List<String> answers = Arrays.asList("Red", "Blue", "Yellow");
LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.question, container);
Resources res = getResources();
int id = res.getIdentifier("question_text", "id", layout.getContext().getPackageName());
TextView questionTextView = (TextView)layout.findViewById(id);
questionTextView.setText(questionText);
id = res.getIdentifier("question_answers", "id", layout.getContext().getPackageName());
RadioGroup radioGroup = (RadioGroup)layout.findViewById(id);
Collections.reverse(answers);
for (String s : answers) {
RadioButton button = new RadioButton(layout.getContext());
button.setText(s);
radioGroup.addView(button, 0);
}
return layout;
}
}
My Fragment XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/question_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/question_text"/>
<RadioGroup android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/question_answers"></RadioGroup>
<Button
android:id="#+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/submit_button_text"
/>
</LinearLayout>
try to put framelayout instead of fragment in xml. and in your activity's onCreate create the instance of your fragment you want to display and add it to the fragment container
public class AskQuestionActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ask_question);
AskQuestionFragment fragment = new AskQuestionFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.id_of_your_container, fragment).commit();
}
}
Your problem might be when doing this:
LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.question, container);
Consider to replace that line with one of the following:
LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.question, null);
or
LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.question, container, false);
change your layout from linear to be framelayout, and attach your fragment in activity .
getSupportFragmentManager().beginTransaction()
.add(R.id.your_fragment_container, fragment).commit();

Categories