Here I'm implementing TabLayout with viewPager in a Fragment using library of com.astuetz.PagerSlidingTabStrip. Everything is fine but now I want to add custom Tab and some more functionalities.
test_tab_fragment.xml
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.astuetz.PagerSlidingTabStrip
android:id="#+id/mainTabLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#111"
app:pstsDividerColor="#color/text_color"
app:pstsIndicatorColor="#color/green"
app:pstsIndicatorHeight="2dp"
app:pstsShouldExpand="true"
app:pstsTextAllCaps="true"
app:tabGravity="fill"
app:tabMode="fixed" />
<com.cws.widget.NonSiwpablePager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
TestTabFragment.java
public class TestTabFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.test_tab_fragment, container, false);
PagerSlidingTabStrip myTabLayout = (PagerSlidingTabStrip) view.findViewById(R.id.mainTabLayout);
NonSiwpablePager pager = (NonSiwpablePager) view.findViewById(R.id.pager);
ViewPagerAdapter mAdapter = getViewPagerAdapter();
pager.setAdapter(mAdapter);
pager.setOffscreenPageLimit(4);
myTabLayout.setViewPager(pager);
for (int i = 0; i < mAdapter.getCount(); i++) {
View tabView = mAdapter.getCustomeView(getActivity(), i);;
tabView = LayoutInflater.from(getContext()).inflate(R.layout.custom_tab_view, myTabLayout,
false);
String title_arr[] = {"ADVISORY", "CALENDER", "TOP ADVISORS", "EXPERT VIEW"};
int[] drawables = new int[]{R.drawable.advisory_normal, R.drawable.calender_normal, R.drawable.topadvisor_normal, R.drawable.expertview_normal};
TextView mTextView = (TextView) tabView.findViewById(R.id.textView);
ImageView mImageView = (ImageView) tabView.findViewById(R.id.imageView2);
mImageView.setImageResource(drawables[i]);
mTextView.setText(title_arr[i]);
myTabLayout.addView(tabView);
}
return view;
}
And custom_tab_view.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:orientation="horizontal"
android:paddingBottom="#dimen/et_topPadding"
android:paddingTop="#dimen/et_topPadding">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="3dp" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#color/text_color"
android:textSize="#dimen/custom_tab_tSize" />
</LinearLayout>
</LinearLayout>
If anyone has done it so please help me to solve it.
Related
There are some views and one FrameLayout in the layout. I want to custom NavHostFragment replace FrameLayout with RelativeLayout, it works but the views hierarchy is wrong. The ImageView should be top, but it is in the bottom.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#id/lb_nav_host_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/logo" />
</RelativeLayout>
and my custom NavHostFragment:
public abstract class BaseModuleFragment extends NavHostFragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = null;
if (getContentViewId() > 0) {
view = inflater.inflate(getContentViewId(), container, false);
View frameLayout = view.findViewById(R.id.lb_nav_host_container);
if (!(frameLayout instanceof FrameLayout)) {
throw new RuntimeException("frame layout must exist");
}
frameLayout.setId(getId());
} else {
view = super.onCreateView(inflater, container, savedInstanceState);
}
return view;
}
}
Was the method wrong, or how to solve?
Change your layout to this one. It should be ok
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_below="#+id/ivTop"
android:id="#id/lb_nav_host_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/ivTop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/logo" />
</RelativeLayout>
These pictures show:
Wrong:
FrameLayout cover ImageView, it is wrong
Right
ImageView should be top, this is right
FrameLayout top
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/logo" />
<FrameLayout
android:id="#id/lb_nav_host_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
ImageView top
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#id/lb_nav_host_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/logo" />
</RelativeLayout>
Here is my layout for Activity. And I want to display android.widget.ProgressBar in the middle of the screen dynamically.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:id="#+id/myCoordinator"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<include layout="#layout/toolbar" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/frameContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<RelativeLayout
android:id="#+id/rlSearchToolContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/colorPrimary"
android:gravity="bottom"
android:layout_gravity="bottom"
android:visibility="gone">
<LinearLayout
android:id="#+id/llSearchComponentContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/colorPrimary"
android:layout_toLeftOf="#+id/btnDone"
android:gravity="bottom"
android:orientation="horizontal"></LinearLayout>
<Button
android:id="#+id/btnDone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Done"/>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
<FrameLayout
android:id="#+id/frameLeftSlide"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start" />
</android.support.v4.widget.DrawerLayout>
Now In that FrameLayout (id = frameContainer) I replace a Fragment that is BaseFragment.(fragment_base.xml)
MainActivity.java
class MainActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BaseFragment mFileListFragment = new BaseFragment();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.frameContainer, mFileListFragment);
transaction.commit();
}
}
fragment_base.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:orientation="vertical">
<FrameLayout
android:id="#+id/frameContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"/>
</LinearLayout>
BaseFragment.java
public class BaseFragment extends Fragment{
private View mView;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
if (mView == null) {
mView = inflater.inflate(R.layout.fragment_phone_file_base, null);
setLayoutViews(mView);
}
return mView;
}
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FileListFragment mFileListFragment = new FileListFragment();
FragmentManager manager = getChildFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.frameContainer, mFileListFragment);
transaction.commit();
}
}
Now In FrameLayout(id = frameContainer) of that BaseFragment I open child fragment that is FileListFragment. (fragment_file_list.xml)
fragment_file_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rvFileList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"/>
</RelativeLayout>
FileListFragment.java
public class FileListFragment extends Fragment{
private View mView;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
if (mView == null) {
mView = inflater.inflate(R.layout.fragment_file_list, null);
setLayoutViews(mView);
}
return mView;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Here there is a server call which retrieves File Data from server. When I send server request I start progressBar and after response I hide progressBar programatically.
ProgressBar mProgressBar = new ProgressBar(activity);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
mProgressBar.setLayoutParams(params);
mProgressBar.getIndeterminateDrawable().setColorFilter(activity.getResources().getColor(R.color.colorPrimary), PorterDuff.Mode.MULTIPLY);
FrameLayout listFrame = getParentFragment().getView().findViewById(R.id.frameContainer);
if(listFrame != null) {
listFrame.addView(mProgressBar);
}
}
}
But it doesn't show progressBar in middle of the screen. It displays below of middle of screen like image attached below.
You can try with android:layout_gravity
Standard gravity constant that a child supplies to its parent.
You can align a view in center of the Framelayout by setting the layout_gravity of the child view .
android:layout_gravity="center"
Place the object in the center of its container in both the vertical and horizontal axis, not changing its size .
This is because you adding progressbar in center of framelayout, try this
<android.support.design.widget.CoordinatorLayout
android:id="#+id/myCoordinator"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="#+id/progressBar2"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<include layout="#layout/toolbar" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/frameContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<RelativeLayout
android:id="#+id/rlSearchToolContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/colorPrimary"
android:gravity="bottom"
android:layout_gravity="bottom"
android:visibility="gone">
<LinearLayout
android:id="#+id/llSearchComponentContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/colorPrimary"
android:layout_toLeftOf="#+id/btnDone"
android:gravity="bottom"
android:orientation="horizontal"></LinearLayout>
<Button
android:id="#+id/btnDone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Done"/>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
<FrameLayout
android:id="#+id/frameLeftSlide"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start" />
Try it using Relativelayout, use Relativelayout as container of progressbar and use centerinparent attribute to center the progressbar in the middle of screen :
RelativeLayout layout = new RelativeLayout(this);
progressBar = new ProgressBar(this);
progressBar.setIndeterminate(true);
progressBar.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100,100);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(progressBar,params);
setContentView(layout);
I don't know whay when i setting longClickListener to my a view , it is not fire,It is my layout :
<?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="match_parent">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/map"
android:layout_below="#+id/toolbar"
android:layout_height="match_parent">
</RelativeLayout>
<TextView
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="16"
android:id="#+id/zoom_level"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<RelativeLayout
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:padding="10dp"
android:background="#drawable/zoom_in_bg"
android:layout_above="#+id/zoom_out"
android:src="#drawable/ic_add_black_24dp"
android:layout_width="wrap_content"
android:id="#+id/zoom_in"
android:layout_marginBottom="1dp"
android:layout_height="wrap_content"/>
<ImageView
android:padding="10dp"
android:background="#drawable/zoom_out_bg"
android:id="#+id/zoom_out"
android:layout_alignParentBottom="true"
android:src="#drawable/ic_remove_black_24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
</RelativeLayout>
and it is fragment
public class MapFragment extends BaseFragment implements View.OnLongClickListener
{
private View rootView;
private org.mapsforge.android.maps.MapView mapView ;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
rootView = inflater.inflate(R.layout.fragment_navigation, container, false);
mapView = new org.mapsforge.android.maps.MapView(getContext());
mapView.setOnLongClickListener(this);
RelativeLayout relativeLayout = (RelativeLayout) rootView.findViewById(R.id.map);
relativeLayout.addView(mapView, new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
return rootView;
}
#Override
public boolean onLongClick(View view)
{
KLog.error("Long Click called");
return true;
}
}
i have tried SetClickable(true) , setLongClickable(true), setFocusable(true) but i cannot solve this problem.what is the solution?
please advise
You can add this code to your class:
mapView.setLongClickable(true);
Or add android:longClickable="true" to xml.
Put android:longClickable="true" in your Relative layout where you want to add onLongClickListener:
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/map"
android:layout_below="#+id/toolbar"
android:layout_height="match_parent"
android:longClickable="true">
</RelativeLayout>
Try with set long click listener on Relative layout instead of mapView:
RelativeLayout relativeLayout = (RelativeLayout) rootView.findViewById(R.id.map);
relativeLayout.setOnLongClickListener(this);
I'm using this library for the fab implementation.
As you can see in this video, the fab position is changing when the fragment loads.
After debugging it, I found that the listview "setAdapter()" method causes the position change.
I don't have a clue why it's happening..
My layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="#style/AppTheme.ActionBar" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="#id/toolbar">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sad_face"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/no_leagues" />
</LinearLayout>
</FrameLayout>
<com.melnykov.fab.FloatingActionButton
android:id="#+id/fab_new_league"
android:src="#drawable/ic_action_add"
app:fab_colorNormal="#color/orange"
app:fab_colorPressed="#color/redDark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="#id/toolbar"
android:layout_marginBottom="-28dp"
android:layout_marginRight="28dp"
android:elevation="8dp"/>
</RelativeLayout>
My fragment
public class LeagueListFragment extends MyFragment {
#InjectView(android.R.id.list) ListView mListView;
#InjectView(android.R.id.empty) LinearLayout mEmpty;
public static Fragment newInstance() {
return new LeagueListFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_league_list, container, false);
ButterKnife.inject(this, view);
// Fetch league list and set up adapter
List<League> list = League.getAll();
mAdapter = new LeagueAdapter(getActivity(), list);
// Set up ListView
mListView.setEmptyView(mEmpty);
mListView.setDivider(new ColorDrawable(getResources().getColor(R.color.gray)));
mListView.setDividerHeight(2);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(this);
return view;
}
}
Margin changes in the runtime on pre-Lollipop because shadow mustn't be taken into account when margins are set.
Try to disable the shadow by :
fab:fab_shadow="false"
Fixing the issue is not expected for now.. (See makovkastar answer)
I have a master-detail activity with fragments. I try to add TextView dynamically in detail fragment. But it's not adding to detail fragment layouts. But the layouts are displayed properly.
Fragment code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.fragment_configuration_detail, container, false);
RelativeLayout cdf_detail_part1=((RelativeLayout) rootView.findViewById(
R.id.cdf_detail_part1));
Log.i("config", "mItem1->"+mItem1);
if (mItem1 != null) {
TextView name_text = (TextView) cdf_detail_part1.findViewById(
R.id.cdf_tv_name);
name_text.setText(name);
}
return rootView;
}
My layout XMl file:
<LinearLayout 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:baselineAligned="false"
android:orientation="horizontal"
tools:context="com.x.y.configurations.ConfigurationDetailFragment" >
<RelativeLayout
android:id="#+id/cdf_detail_part1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00FF00" >
<TextView
android:id="#+id/cdf_tv_name"
style="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/cdf_detail_part2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#0000FF" >
</RelativeLayout>
</LinearLayout>