The slidingPaneLayout does not slide!
pane.isSlideable() is false!
I inflate the layout in this way:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
slidingView = inflater.inflate(R.layout.available_nodes_fragment, container, false);
SlidingPaneLayout pane = (SlidingPaneLayout)slidingView.findViewById(R.id.sliding_pane_layout);
Fragment fragmentA = new ListNodesFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.child_fragmentA, fragmentA );
Fragment fragmentB = new ReserveNodesFragment();
transaction.add(R.id.child_fragmentB, fragmentB);
transaction.commit();
//pane.setPanelSlideListener(new SliderListener());
pane.openPane();
Log.i("Available Nodes","PANE IS SLIDABLE" + pane.isSlideable());
return slidingView;
}
The available_nodes_fragment layout that i inflate is :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/sliding_pane_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="#+id/child_fragmentA"
android:layout_width="180dp"
android:layout_height="match_parent"
android:layout_gravity="left" />
<FrameLayout
android:id="#+id/child_fragmentB"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_gravity="right" />
</LinearLayout>
</android.support.v4.widget.SlidingPaneLayout>
I do not know why the SlidingPaneLayout does not slide. I do not know if this problem has to do with the fact that i use nested fragments.
I have some progress.
I changed the available_nodes_fragment layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/reserve_nodes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical" >
<android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/sliding_pane_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/child_fragmentA"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_gravity="left"
android:layout_marginRight="25dp"
android:background="#A588DC" />
<FrameLayout
android:id="#+id/child_fragmentB"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:background="#B6D4D3"
android:layout_gravity="right" />
</android.support.v4.widget.SlidingPaneLayout>
<Button
android:id="#+id/btnReserveNodes"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:text="Reserve"/>
</RelativeLayout>
Now, the SlidePaneLayout layout slides. I do not know why. When i changed the fixed layout_width of the FrameLayouts to wrap_content, the SlidePaneLayout started to slide.
My problem now is that i want the FrameLayout with id= "child_fragmentA" with the ListView to get the 30% of the screen horizontally. I did not found a way to do it, since FrameLayout does not support weights.
I also provide the code of ListNodesFragment and ReserveNodesFragment.
ListFragments.java
public class ListNodesFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View listNodesView = inflater.inflate(R.layout.nodes_categories_listview, container, false);
String[] items = { getResources().getString(R.string.orbit_nodes),
getResources().getString(R.string.grid_nodes),
getResources().getString(R.string.usrp_nodes),
getResources().getString(R.string.diskless_nodes),
getResources().getString(R.string.icarus_nodes) ,
getResources().getString(R.string.base_station)
};
ListView nodesListView = (ListView) listNodesView.findViewById(R.id.nodes_list);
ArrayAdapter<String> adapt = new ArrayAdapter<String>(getActivity(), R.layout.nodes_categories_listview_item, items);
nodesListView.setAdapter(adapt);
return listNodesView;
}
}
ReserveNodesFragment.java
public class ReserveNodesFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View reserveNodesView = inflater.inflate(R.layout.reserve_nodes_fragment, container, false);
TextView txtView = (TextView) reserveNodesView.findViewById(R.id.textView1);
txtView.setText("fragment 2");
return reserveNodesView;
}
}
Related
I'm am trying to add dynamycally multiple fragments I'm creating to a framelayout, using FragmentTransaction's add() method. Each time I try to do it, Fragments are just replacing one an other.
Here is my code :
public class FragmentMerchandSearch extends Fragment {
public FragmentMerchandSearch() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_merchand_search, container, false);
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
for(int i = 0; i < 10; i++){
Fragment newFragment = new FragmentMerchandPresentation();
ft.add(R.id.container_merchand_presentation_for_search, newFragment);
}
ft.commit();
return view;
}
}
Here is the code of FragmentMerchandPresentation:
public class FragmentMerchandPresentation extends Fragment {
public FragmentMerchandPresentation(){
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_presentation_merchand, container, false);
return view;
}
}
Here is XML of fragment_merchand_search:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/container_merchand_presentation_for_search">
</FrameLayout>
</LinearLayout>
</ScrollView>
And XML of fragment_presentation_merchand:
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hey !"/>
</LinearLayout>
You are adding your fragments inside a FrameLayout, this means one is above another.
To add them vertically, use a LinearLayout as a container.
Just change your fragment_merchand_search.xml to:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/container_merchand_presentation_for_search"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
Just change this line:
ft.add(R.id.container_merchand_presentation_for_search, newFragment);
to this:
ft.replace(R.id.container_merchand_presentation_for_search, newFragment);
I'm facing an issue regarding intro screen in android.
I create button in one fragment but i'm trying to call this button in welcomeActivity through layoutinflate in activity but it doesn't perform any functionality.
XML Fragment(custom_intro):
<?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"
>
<Button
android:id ="#+id/explore"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Explore"
android:background="#drawable/round_button"
android:padding="15dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="114dp" />
</RelativeLayout>
WelcomeActivityLayout
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimaryDark">
<!--tools:showIn="#layout/welcome_activity"-->
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
WelcomeActivityJava:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome_activity);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
customView = inflater.inflate(R.layout.custom_intro, null);
Button button=(Button)customView.findViewById(R.id.explore);
button.setText("Test");
Int[] layouts = new int[]{
R.layout.custom_intro,
R.layout.custom_intro1,
R.layout.custom_intro2,
R.layout.custom_intro3,
R.layout.custom_intro4};
myViewPagerAdapter = new MyViewPagerAdapter();
viewPager.setAdapter(myViewPagerAdapter);
viewPager.addOnPageChangeListener(viewPagerPageChangeListener);
viewPagerIndicator.setupWithViewPager(viewPager);
viewPagerIndicator.addOnPageChangeListener(viewPagerPageChangeListener);
}
If you want to access the Button of Fragment inside its parent Activity then you should define a method inside your Fragment class like this:
public class MyFragment extends Fragment {
TextView mTextView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main, container, false);
mButton = (Button) view.findViewById(R.id.textView1);
return view;
}
public void setButtonText(String value){
mTextView.setText(value);
}
}
Then you can use this inside your Activity like this:
myFragment.setButtonText("Test");
I am using a class that extends Fragment and not FragmentActivity and I cannot seem to make horizontal scroll to work. I first used a layout as simple as the first code below and it works perfectly (without the scroll of course). I decided to put a horizontal scroll because when my tab reaches 6, the text is so hard to read and when the text is too long, the complete text is not written. I tried to use the layout of other people that said "This works!". Please refer to the second code below; the third code is my fragment class. Thank you!
<?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"/>
The layout that other people said "It works"
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
</HorizontalScrollView>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
Lastly, my Fragment class. Maybe I'm missing something out?
public class AlarmTab extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
FragmentTabHost tabHost;
tabHost = new FragmentTabHost(getActivity());
tabHost.setup(getActivity(), getChildFragmentManager(), R.layout.alarm_tab);
for(int i=0; i<SmashDeviceData.get_instance().devices.size(); i++) {
Bundle bundle = new Bundle();
bundle.putInt("Arg for Frag" + i, 1);
tabHost.addTab(tabHost.newTabSpec("Tab"+i).setIndicator(SmashDeviceData.get_instance().devices.get(i).deviceName), AlarmActivity.class, bundle);
}
return tabHost;
}
public static Fragment newInstance() {
Fragment fragment = new AlarmTab();
return fragment;
}
}
try the below
for xml
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/tabsColor"
android:scrollbars="none" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/tabsColor"
android:gravity="center_horizontal" />
</HorizontalScrollView>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
for your fragment
public class HomeFragment extends Fragment {
private FragmentTabHost mTabHost;
TabWidget widget;
HorizontalScrollView hs;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container,
false);
initView(rootView);
return rootView;
}
private void initView(View rootView) {
mTabHost = (FragmentTabHost) rootView
.findViewById(android.R.id.tabhost);
widget = (TabWidget) rootView.findViewById(android.R.id.tabs);
hs = (HorizontalScrollView) rootView
.findViewById(R.id.horizontalScrollView);
mTabHost.setup(getActivity(), getChildFragmentManager(),
android.R.id.tabcontent);
}
}
I have 2 fragments FragmentA and FragmentB. FragmentA is on top of FragmentB. Now I want to add tabs on FragmentB. Please Help.
My code so far :-
layout/fragmentA.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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top Fragment" />
</LinearLayout>
layout/fragmentB.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:background="#F5F6CE"
android:orientation="vertical" >
<TabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include
android:id="#+id/tabdata1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout=“#layout/tabdata1_view"/>
<include
android:id="#+id/tabdata2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout=“#layout/tabdata2_view"
/>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
FragmentB.java
public class Fragment2 extends Fragment{
TabHost myTabHost;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment2_layout, container, false);
myTabHost = (TabHost) getView().findViewById(R.id.tabhost)
return rootView;
}
}
Error:-
The line “myTabHost = (TabHost) getView().findViewById(R.id.tabhost)”gives an error as it cannot find tabhost.
Please suggest a way to achieve this.
Try this
myTabHost = (TabHost) rootview.findViewById(android.R.id.tabhost);
This should remove the error which you are facing now
That's because until you return from onCreateView, the fragment does not actually have a view yet. You just need to call findViewById relative to rootView. Like this:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment2_layout, container, false);
myTabHost = (TabHost) rootView.findViewById(R.id.tabhost)
return rootView;
}
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>