I am using JakeWharton's ViewPagerIndicator on my landscape app but I think I am messing up with the layout. The ViewPagerIndicator does not show up. I just started learning Android Development so any help is appreciated!
My guess is that it has to do with my Linear Layout...should it be relative? Am I covering up the pager indicator? I know that if I move the indicator code above the view pager, I see the indicator but everything else is white. My fragment's layout is a relative layout with height and width matching the parent.
My app is an activity with fragments.
Edit: I set the indicator height to wrap content but now it floats on the top of the screen. How do I make it go down?
My_Activity.java
public class MyActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
MyPagerAdapter mAdapter = new MyPagerAdapter(getSupportFragmentManager());
pager.setAdapter(mAdapter);
CirclePageIndicator circleInd = (CirclePageIndicator) findViewById(R.id.circles);
circleInd.setViewPager(pager);
circleInd.setStrokeColor(0xAA000000);
//circleInd.setCurrentItem(mAdapter.getCount() - 1);
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
switch (pos) {
case 0: return FirstFragment.newInstance("First");
case 1: return SecondFragment.newInstance("Second");
default: return FirstFragment.newInstance("First");
}
}
#Override
public int getCount() {
return 2;
}
}
}
and the XML
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.frontrowqa.myfirstapplication.MyActivity">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/circles"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="10dp"
android:layout_gravity="bottom" />
</LinearLayout>
And my fragment
<?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"
android:padding="20dp">
<EditText
android:id="#+id/PasscodeText"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:layout_width="fill_parent"
android:hint="Passcode"
android:backgroundTint="#android:color/black" />
<EditText
android:id="#+id/ipAddressText"
android:layout_below="#id/PasscodeText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="IP Address"
android:backgroundTint="#android:color/black" />
<EditText
android:id="#+id/intercomText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/ipAddressText"
android:hint="Intercom Event Number"
android:backgroundTint="#android:color/black"/>
<TextView
android:id="#+id/tvFragFirst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="26dp"
android:text="TextView" />
</RelativeLayout>
Set your activity xml like this:
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/circles"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:padding="10dp" />
</LinearLayout>
Related
I have never seen a bug as weird as that before, I'm linking a viewpager with a tablayout, inside the viewpager there are three fragments.
Two of the fragments (let's say fragment 1 & 3) have a button inside them and their colours are both set to "pink". When I first load the app, it shows fragment 1, the button is pink at that moment, however, whenever I switch to fragment 3, the background color of the button is changed to "white" and other attributes of the button stay the same.
This won't happen if I switch to fragment 2. However, if I switch to fragment 3 once, both fragments 1 & 3's button have white background for the rest of the lifetime.
Can someone tell me what's happening here or what could cause this? I'm not changing the style of the buttons in Java files at all.
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
setUpWithViewPager(binding.viewPager);
// setupWithViewPager is a built-in method for TabLayout, setting up this TabLayout with a ViewPager
// The TabLayout will be automatically populated from the PagerAdapter's page titles. By doing that,
// when the user tabs on the tab, the appropriate fragment will be shown in the ViewPager
// TabLayout provides a horizontal layout to display tabs.
// Without this line you can still swipe left/right to see all the pages, just cannot tab on the
// tab to switch pages
binding.tabLayout.setupWithViewPager(binding.viewPager);
// This method sets the toolbar as the app bar for the activity
setSupportActionBar(binding.toolbar);
// change the fab icon when the page is changed
binding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
changeFabIcon(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void setUpWithViewPager(ViewPager viewPager) {
// An inner class defined in MainActivity
SectionsPagerAdapter adapter = new SectionsPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new ChatsFragment(), "Chats");
adapter.addFragment(new StatusFragment(), "Status");
adapter.addFragment(new CallsFragment(), "Calls");
// We need 3 fragments
viewPager.setAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// getMenuInflater is used to instantiate menu XML files into Menu objects.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_search: Toast.makeText(this, "Action Search", Toast.LENGTH_SHORT).show(); break;
case R.id.menu_more: Toast.makeText(this, "Action More", Toast.LENGTH_SHORT).show(); break;
}
return super.onOptionsItemSelected(item);
}
private void changeFabIcon(final int index) {
binding.fabAction.hide();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
switch (index) {
case 0: binding.fabAction.setImageDrawable(getDrawable(R.drawable.ic_baseline_chat_24)); break;
case 1: binding.fabAction.setImageDrawable(getDrawable(R.drawable.ic_baseline_camera_alt_24)); break;
case 2: binding.fabAction.setImageDrawable(getDrawable(R.drawable.ic_baseline_call_24)); break;
}
binding.fabAction.show();
}
}, 400);
}
private static class SectionsPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public SectionsPagerAdapter(#NonNull FragmentManager fm) {
super(fm);
}
#NonNull
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout
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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".view.MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
app:titleTextColor="#android:color/white"
app:title="PepperChat"/>
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.tabs.TabLayout
android:id="#+id/tab_layout"
android:layout_below="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="50dp"
app:tabBackground="#color/colorPrimary"
app:tabGravity="fill"
app:tabIndicator="#color/colorPrimary"
app:tabIndicatorHeight="3dp"
app:tabIndicatorColor="#android:color/white"
app:tabSelectedTextColor="#android:color/white"
app:tabTextColor="#android:color/white">
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager.widget.ViewPager
android:id="#+id/view_pager"
android:layout_below="#id/tab_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_action"
android:src="#android:drawable/stat_notify_chat"
android:tint="#android:color/white"
app:backgroundTint="#color/colorPrimary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_margin="15dp"/>
</RelativeLayout>
</layout>
fragment_calls.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".menu.CallsFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:id="#+id/ln_invite"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginTop="30dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Invite your friends"
android:textSize="25dp"
android:textColor="#android:color/black"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:textColor="#color/colorPrimary"
android:gravity="center_horizontal"
android:text="Name of your contact are using PepperChat.\nUse the bottom below to invite them."/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/black"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:text="Invite a friend"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:gravity="center_horizontal"
android:text="Chat with your friends who are using PepperChat on iphone,\nAndroid or KaiOS phone"/>
</LinearLayout>
</FrameLayout>
</layout>
fragment_chats.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".menu.ChatsFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
<LinearLayout
android:id="#+id/ln_invite"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginTop="30dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Invite your friends"
android:textSize="25dp"
android:textColor="#android:color/black"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:textColor="#color/colorPrimary"
android:gravity="center_horizontal"
android:text="Name of your contact are using PepperChat.\nUse the bottom below to invite them."/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/black"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:text="Invite a friend"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:gravity="center_horizontal"
android:text="Chat with your friends who are using PepperChat on iphone,\nAndroid or KaiOS phone"/>
</LinearLayout>
</FrameLayout>
</layout>
After adding the following line in both of the xml files, the button appears normally, not sure why though, can someone explain what happened? (is that something related to the theme I'm using?)
android:backgroundTint="#color/colorPrimary"
and my theme is
<style name="Theme.PepperChat" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
if I change the parent to Theme.MaterialComponent.Light.NoActionBar, then the app works fine without adding backgroundTint.
Again, would like some explanations on why this happens.
I'm trying to set a TextView that's in a fragment but the text won't update in the view. The text is 1 of 2 pages in a ViewPager. The ViewPager is inside another fragment that has a button which when clicked uses an interface to the MainActivity that calls the changeText() that's inside the fragment with my text.
Fragment Inside ViewPager:
public class PlaceholderFragment extends Fragment {
public TextView textFV;
public PlaceholderFragment() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_pull, container, false);
textFV = rootView.findViewById(R.id.textFV);
return rootView;
}
#Override
public void onViewCreated(#NonNull View view, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void changeText() {
textFV.setText("Test"); - Won't update view.
System.out.println(textFV.getText()); - Shows correct text.
}
}
Relevant part of Fragment that contains ViewPager:
OnHeadlineSelectedListener callback;
public void setOnHeadlineSelectedListener(OnHeadlineSelectedListener callback) {
this.callback = callback;
}
public interface OnHeadlineSelectedListener {
void onArticleSelected();
}
//...
#Override
public void onClick (View v) {
callback.onArticleSelected();
}
Relevant MainActivity Part:
public void onArticleSelected() {
PlaceholderFragment articleFrag = (PlaceholderFragment) getSupportFragmentManager().findFragmentById(R.id.viewpager);
if (articleFrag != null) {
articleFrag.changeText();
}
}
Here is the layout that contains the TextView:
<android.support.constraint.ConstraintLayout
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.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">
<LinearLayout
android:id="#+id/tab_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:id="#+id/containerPull"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:id="#+id/textsLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/label_left_margin"
android:layout_marginTop="5dp"
android:text="#string/label_common"
android:textColor="#color/colorLabelText"
android:textSize="18sp" />
<TextView
android:id="#+id/textFV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello, World!"
android:textColor="#android:color/black"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.constraint.ConstraintLayout>
Here's where the button and ViewPager is. The button is the ImageButton:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white">
<android.support.v7.widget.Toolbar
android:id="#+id/pullToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="Information"
app:titleTextAppearance="#style/TextAppearance.Widget.AppCompat.Toolbar.Title.Montserrat">
<ImageButton
android:id="#+id/toolbarButtonPull"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginEnd="1dp"
android:background="#drawable/button_toolbar_default"
android:contentDescription="Pull"
android:padding="15dp"
android:src="#drawable/ic_arrow_downward_black_24dp" />
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabTextAppearance="#style/TextAppearance.Design.Tab.NoCaps">
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab1" />
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab2" />
</android.support.design.widget.TabLayout>
</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="android.support.design.widget.AppBarLayout$ScrollingViewBehavior" />
</android.support.design.widget.CoordinatorLayout>
Looks like your textFV is inside layout that has visibility gone. Can it be the problem?
<LinearLayout
android:id="#+id/containerPull"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
**android:visibility="gone">**
<TextView
android:id="#+id/textsLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/label_left_margin"
android:layout_marginTop="5dp"
android:text="#string/label_common"
android:textColor="#color/colorLabelText"
android:textSize="18sp" />
<TextView
android:id="#+id/textFV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello, World!"
android:textColor="#android:color/black"
android:textSize="18sp" />
I want to open a fragment in activity but under objects of Activity.
in my code RelativeLayout is parent and LinearLayout is child
but when open fragment on relativelayout I lose linearlayout :(
this is my code:
Main Activity Java:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Context context;
private Button fragment1 , fragment2;
private RelativeLayout relativeLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
fragment1 = this.findViewById(R.id.fragment1);
fragment2 = this.findViewById(R.id.fragment2);
relativeLayout = this.findViewById(R.id.relativeLayout);
fragment1.setOnClickListener(this);
fragment2.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.fragment1:
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.relativeLayout , new fragment1());
ft.commit();
break;
case R.id.fragment2:
FragmentManager fm2 = getSupportFragmentManager();
FragmentTransaction ft2 = fm2.beginTransaction();
ft2.replace(R.id.relativeLayout , new fragment2());
ft2.commit();
break;
}
}
}
Main Activity XML:
<?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"
tools:context=".MainActivity"
android:id="#+id/relativeLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="#+id/layout"
android:background="#F00"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment 1"
android:id="#+id/fragment1"
android:gravity="center"
android:layout_gravity="center"/>
<Button
android:id="#+id/fragment2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="fragment 2" />
</LinearLayout>
</RelativeLayout>
fragment1 XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0F0">
</android.support.constraint.ConstraintLayout>
fragment2 XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00F">
</android.support.constraint.ConstraintLayout>
This is Main Activity: enter image description here
and when click on fragment1: enter image description here
Plz Help Me :)
TNX
Add a framelayout in relativelayout before the linearlayout and inflate the fragment in the framelayout.
<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"
tools:context=".MainActivity"
android:id="#+id/relativeLayout">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="#+id/linearLayout"
android:background="#F00"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment 1"
android:id="#+id/fragment1"
android:gravity="center"
android:layout_gravity="center"/>
<Button
android:id="#+id/fragment2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="fragment 2" />
</LinearLayout>
</RelativeLayout>
I think you should change your Fragment1 XML and Fragment2 XML from Constraint Layout to LinearLayout. It should work else initiate your LineaLayout in your MainActivity.
You are inflating fragments on Parent Layout. Make two child of Relativelayout and inflate fragment one of them like this:
<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"
tools:context=".MainActivity"
android:id="#+id/relativeLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="#+id/linearLayout"
android:background="#F00"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment 1"
android:id="#+id/fragment1"
android:gravity="center"
android:layout_gravity="center"/>
<Button
android:id="#+id/fragment2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="fragment 2" />
</LinearLayout>
<!-- inflate fragments on FrameLayout -->
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout">
</FrameLayout>
</RelativeLayout>
Inflate your fragments on this FrameLayout
....Hope this helps :)
I want to achieve the design like this using BottomSheetDialogFragment. But the problem is the bottom layout get scrolled when i drag. I want the bottom layout always in bottom until the BottomSheetDialogFragment gets dismissed.
Please find the screenshot
Here is my code
bottomsheet_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--The main content goes over here-->
</LinearLayout>
<!--The footer view-->
<LinearLayout
android:id="#+id/footer_purchase_layout"
android:layout_width="match_parent"
android:layout_height="52dp"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="#+id/txt_cancel_purchase"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="left|center_vertical"
android:text="CLOSE"
android:textSize="14sp" />
<TextView
android:id="#+id/txt_item_purchase_action"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="right|center_vertical"
android:text="ADD STICKERS"
android:textSize="14sp" />
</LinearLayout>
</FrameLayout>
ModalBottomSheetFragment.java
public class ModalBottomSheetFragment extends BottomSheetDialogFragment {
public ModalBottomSheetFragment() {}
private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onStateChanged(#NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
dismiss();
}
}
#Override
public void onSlide(#NonNull View bottomSheet, float slideOffset) {}
};
#TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
#Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
View mContentView = View.inflate(getContext(), R.layout.bottomsheet_layout, null);
dialog.setContentView(mContentView);
CoordinatorLayout.LayoutParams layoutParams =
(CoordinatorLayout.LayoutParams) ((View) mContentView.getParent()).getLayoutParams();
CoordinatorLayout.Behavior mBehavior = layoutParams.getBehavior();
if (mBehavior != null && mBehavior instanceof BottomSheetBehavior) {
((BottomSheetBehavior) mBehavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
int height = getScreenHeight(getActivity());
final double desiredHeight = 0.85 * height;
mContentView.getLayoutParams().height = height;
((BottomSheetBehavior) mBehavior).setPeekHeight((int) desiredHeight);
}
}
public static int getScreenHeight(Context ctx) {
DisplayMetrics metrics = ctx.getResources().getDisplayMetrics();
return metrics.heightPixels;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private Button mShowBottomSheetDialog;
private BottomSheetDialogFragment bottomSheetDialogFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mShowBottomSheetDialog = (Button) findViewById(R.id.showBottomSheet);
mShowBottomSheetDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
bottomSheetDialogFragment = new ModalBottomSheetFragment();
bottomSheetDialogFragment.show(getSupportFragmentManager(), bottomSheetDialogFragment.getTag());
}
});
}
}
use this layout instead of your layout ....
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:orientation="vertical">
<!--The main content goes over here-->
</LinearLayout>
</ScrollView>
<!--The footer view-->
<LinearLayout
android:id="#+id/footer_purchase_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="#+id/txt_cancel_purchase"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="left|center_vertical"
android:text="CLOSE"
android:textSize="14sp" />
<TextView
android:id="#+id/txt_item_purchase_action"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="right|center_vertical"
android:text="ADD STICKERS"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
Note:- Weight divide your Layout into different View which is constant in your layout ......
use this layout:
<?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="#android:color/white"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Title"
android:textSize="18sp" />
</FrameLayout>
</androidx.appcompat.widget.Toolbar>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp">
<!--your content here-->
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<FrameLayout
android:id="#+id/bottomView"
android:layout_width="match_parent"
android:layout_height="70dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Footer"
android:textSize="18sp"/>
</FrameLayout>
</LinearLayout>
I have implemented a TabLayout which has a few tabs & one of the tabs has a table & when a user clicks on a row in the table I need it to get rid of the tab layout, except for the toolbar & open up a new Fragment.
I've tried a few different ways but nothing seems to work, if anyone has any ideas, it'll will be much appreciated!
Thank you!
Cheers!
This is the activity_main.xml
<RelativeLayout
android:id="#+id/main_layout"
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=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/toolbar"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="#id/tab_layout"/>
</RelativeLayout>
& this is the MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
& this is tab1.xml
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#F0F0F0" >
<ScrollView
android:id="#+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center" >
<TableLayout
android:id="#+id/table"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
</TableLayout>
<RelativeLayout
android:id="#+id/loadingPanel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true" />
</RelativeLayout>
</FrameLayout>
& this is Tab1.java
public class TopStories extends Fragment {
A lot of code goes here............
public void createTable() {
TableLayout table = (TableLayout) getActivity().findViewById(R.id.table);
table.setBackgroundColor(Color.rgb(240, 240, 240));
table.setStretchAllColumns(true);
table.setShrinkAllColumns(true);
table.removeAllViews();
TableRow row = new TableRow(getActivity());
row .setGravity(Gravity.CENTER);
row .setPadding(0, 10, 0, 10);
row .setBackgroundColor(Color.WHITE);
row .setClickable(true);
TableRow.LayoutParams params = new TableRow.LayoutParams();
row .setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
This is where I need to start up the new fragment (NewFragment.Java).
}
});
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_topstories, container, false);
}
}
NewFragment.java
public class NewFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.newfragment, container, false);
}
}
newfragment.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" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.04" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="17dp"
android:layout_marginTop="18dp"/>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="37dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_marginTop="87dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
</LinearLayout>
I think you need to give the fragment somewhere to replace. Here is the XML from my app, and when I swap fragments, they post to flcontent.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk
xmlns:app="http://schemas.android.com/apk/res
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- The ActionBar -->
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
<!-- The main content view -->
<FrameLayout
android:id="#+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
</LinearLayout>
<!-- The navigation drawer -->
<android.support.design.widget.NavigationView
android:id="#+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
app:menu="#menu/drawer_view"
app:headerLayout="#layout/nav_header"/>
and the class code
// Insert the fragment by replacing any existing fragment
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.flContent, fragment);
ft.addToBackStack(null);
ft.commit();
Alright so I think that I figured out a really cheap & a cheeky way to do this & probably not very healthy for the app but it works perfect!
Thank you very much for the help Brian! Appreciate it! Maybe you can do this for the backstack problem of your app as well, don't know if it will work or the right thing that you need to do though.
Cheers!
Added a FrameLayout to my main_activity.xml
<RelativeLayout
android:id="#+id/main_layout"
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=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/toolbar"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="#id/tab_layout"/>
<FrameLayout
android:id="#+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</RelativeLayout>
Changed my newfragment.xml to this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fullStoryLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/fullStoryRelativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.04" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="17dp"
android:layout_marginTop="80dp"/>
<TextView
android:id="#+id/fullStorytitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/fullStory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/image"
android:layout_marginTop="50dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
</LinearLayout>
& added this to the onClick event of the table row
row.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
getActivity().findViewById(R.id.tab_layout).setVisibility(View.GONE);
getActivity().findViewById(R.id.pager).setVisibility(View.GONE);
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("New Fragment");
Fragment fragment = null;
Class fragmentClass = NewFragment.class;
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.flContent, fragment);
fragmentTransaction.commit();
}
});
& to go back to the main TabLayout from the NewFragment, I added this at MainActivity.java
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch(keyCode){
case KeyEvent.KEYCODE_BACK:
if (getSupportActionBar().getTitle() != "Main Activity"){
onBackPressed();
}
else {
System.exit(0);
}
return true;
}
return super.onKeyDown(keyCode, event);
}
public void onBackPressed(){
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setTitle("Main Activity");
findViewById(R.id.fullStoryLinearLayout).setVisibility(View.GONE);
findViewById(R.id.tab_layout).setVisibility(View.VISIBLE);
findViewById(R.id.pager).setVisibility(View.VISIBLE);
}