I am implementing TabLayout with ViewPager where no of tabs are not fixed and gets increased as user scrolls the page. I have to also implement custom view for each TabLayout.Tab. I am trying to setup TabLayout using following snippet.
private void setupTabLayout() {
tabLayout.setupWithViewPager(viewPager);
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
View v = adapter.getTabView(i, true);
tab.setCustomView(v);
}
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
tab.setCustomView(adapter.getTabView(tab.getPosition(), true));
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
Following is custom view for tab
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:id="#+id/month"
android:text="SEP"
android:layout_gravity="center_horizontal"
android:textColor="#FFF"
android:textSize="14sp"
android:gravity="center_horizontal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/date"
android:textAllCaps="true"
android:text="21"
android:textSize="18sp"
android:textColor="#FFF"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"/>
</LinearLayout>
Following is layout for ViewPager and TabLayout
<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">
<ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorHeight="0dp" />
</ViewPager>
</LinearLayout>
Initially when app is opened, tabs are setup properly and are visible properly. But, then when I dynamically add more tabs and scroll, custom views for tab doesn't get updated. Please help me with adding custom views for dynamically added tabs.
Related
I have made a custom ToolBar but recently decided to include a button on it as a home button. I have also defined it in java and it works when a button is clicked but the ToolBar title text shows half-way, i have tried to alignParentRight but it wouldn't work.
Please help me
Below are my codes and what i have tried.
TIps_4.java Activity
public class Tips_4 extends AppCompatActivity {
Toolbar toolbar;
TabLayout tabLayout;
ViewPager viewPager;
Button Tips3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the start page passed to us or default to first page
Intent mIntent = getIntent();
int startPage = mIntent.getIntExtra("startPage", 0);
setContentView(R.layout.activity_tips_4);
toolbar= findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
Button toolbar_btn = (Button)findViewById(R.id.toolbarButton);
toolbar_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent toolbarIntent = new Intent(Tips_4.this, MainActivity.class);
toolbarIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Tips_4.this.startActivity(toolbarIntent);
setSupportActionBar(toolbar);
}
});
ViewPager viewPager= (ViewPager) findViewById(R.id.view_pager);
viewPager.setAdapter(new SimpleFragmentPageAdapter(getSupportFragmentManager(),this));
// set the current page to the start page
viewPager.setCurrentItem(startPage);
//Attach the page change listener inside the activity
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
// This method will be invoked when a new page becomes selected
#Override
public void onPageSelected(int position) {
Toast.makeText(Tips_4.this, "Selected Maths Page:" + position, Toast.LENGTH_SHORT).show();
}
// This method will be invoked when the current page is scrolled
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetpixels) {
}
// Called when the scroll state changes:
//SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
#Override
public void onPageScrollStateChanged(int state) {
}
});
tabLayout= (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);
}
}
toolbar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:subtitleTextColor="#FF9800"
android:background="#color/colorPrimary">
<Button
android:id="#+id/toolbarButton"
android:layout_width="60dp"
android:layout_height="50dp"
android:foreground="#drawable/ic_home_24dp"
android:layout_marginLeft="300dp"
android:background="#000"/>
</androidx.appcompat.widget.Toolbar>
This is what i did that refused to work
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:orientation="horizontal"
android:layout_alignParentRight="true">
<Button
android:id="#+id/toolbarButton"
android:layout_width="54dp"
android:layout_height="50dp"
android:background="#056359"
android:layout_gravity="end"
android:padding="11dp"
android:foreground="#drawable/ic_home_24dp" />
</RelativeLayout>
This is the screenshot
Here is the complete code for a toolbar with button at the end
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:subtitleTextColor="#FF9800"
android:background="#color/colorPrimary">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mathematics Pages"
android:textSize="23sp"
android:textColor="#ffffff"
android:textStyle="bold"
android:gravity="right"/>
<Button
android:id="#+id/toolbarButton"
android:layout_width="60dp"
android:layout_height="50dp"
android:foreground="#drawable/ic_home_24dp"
android:layout_alignParentEnd="true"
android:background="#000"
android:layout_alignParentRight="true" />
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
You can use a relative layout and set its width and height to match_parent now add the button in you're relative layout and set alignParentEnd to true
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:subtitleTextColor="#FF9800"
android:background="#color/colorPrimary">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/toolbarButton"
android:layout_width="60dp"
android:layout_height="50dp"
android:foreground="#drawable/ic_home"
android:layout_alignParentEnd="true"
android:background="#000"/>
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
If you just want a icon in you're button you can use a ImageButton that works same as a normal button. And If you're icon is of fixed size say 24 X 24 then you can make the button as wrap_content in both width and height.
I have a TabLayout with 2 tabs. For each tab, I created the corresponding fragments.
The activity class (extending AppCompatActivity) is this (ToolsActivity.java):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tools);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
mViewPager = findViewById(R.id.view_pager);
mViewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(mViewPager);
tabs.getTabAt(0).setIcon(R.drawable.ic_tab_reading);
tabs.getTabAt(1).setIcon(R.drawable.ic_tab_writing);
tabs.setTabMode(TabLayout.MODE_SCROLLABLE);
mCurrentSelectedListener = new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
// ...
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
// ...
}
};
tabs.addOnTabSelectedListener(mCurrentSelectedListener);
}
The corresponding layout is this (activity_tools.xml):
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ToolsActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:minHeight="?actionBarSize"
android:padding="#dimen/appbar_padding"
android:text="#string/title_activity_tools"
android:textAppearance="#style/TextAppearance.Widget.AppCompat.Toolbar.Title" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:tabInlineLabel="true" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
The pager adapter contains an array of Fragment object.
mLstFragments.add(new UHFReadFragment());
mLstFragments.add(new UHFWriteFragment());
that adapter also contains this piece of code:
#Override
public Fragment getItem(int position) {
if (mLstFragments.size() > 0) {
return mLstFragments.get(position);
}
throw new IllegalStateException("No fragment at position " + position);
}
First tab is UHFReadFragment. This is part of the code of it:
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mContext = (ToolsActivity) getActivity();
mSound = new Sound(mContext);
mTagList = new ArrayList<HashMap<String, String>>();
mAdapter = new SimpleAdapter(mContext, mTagList, R.layout.listtag_items,
new String[]{"tagUii", "tagLen", "tagCount", "tagRssi"},
new int[]{R.id.TvTagUii, R.id.TvTagLen, R.id.TvTagCount,
R.id.TvTagRssi});
mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
String result = msg.obj + "";
String[] strs = result.split("#");
addEPCToList(strs[0], strs[1]);
mSound.playSound(1);
}
};
mUHF = new UHF(mContext, mHandler);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mTvCount = (TextView) getView().findViewById(R.id.tv_count);
mLvTags = (ListView) getView().findViewById(R.id.LvTags);
mLvTags.setAdapter(mAdapter);
}
And finally, this is the layout for the first fragment (uhf_read_fragment.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
tools:context=".ui.main.UHFReadFragment">
<LinearLayout
android:id="#+id/layout4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:background="#color/white"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp" >
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/tvTagUii"
android:textSize="15sp" />
<TextView
android:id="#+id/tv_count"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="0"
android:textColor="#color/red"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/tvTagLen"
android:visibility="gone" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/tvTagCount"
android:textSize="15sp" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="RSSI"
android:textSize="15sp"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#color/gray" />
<ListView
android:id="#+id/LvTags"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
The problem is that the layout of the fragments does not appear, and in fact, onViewCreated is never called.
What else is missing? I thought that by using "tools:context=".ui.main.UHFReadFragment"" the layout will be associated to the class that controls the Fragment.
Only the layout of the activity is shown (when running the app, it shows only the title and the tabs header).
Regards
Jaime
You need to have the onCreateView method in the Fragment class(UHFReadFragment) to inflate the layout
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.uhf_read_fragment, container, false);
}
Hope this will solve your issue
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:id="#+id/drawer">
<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:menu="#menu/nav_menu"
android:layout_gravity="start"
android:id="#+id/navigation_view"
></android.support.design.widget.NavigationView>
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/appbar_padding_top"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#d3d3d3"
app:tabSelectedTextColor="#000000"
app:tabTextColor="#000000">
<android.support.design.widget.TabItem
android:id="#+id/tabItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="All Terminal" />
<android.support.design.widget.TabItem
android:id="#+id/tabItem1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Terminal 1" />
<android.support.design.widget.TabItem
android:id="#+id/tabItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Terminal 2" />
<android.support.design.widget.TabItem
android:id="#+id/tabItem3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Terminal 3" />
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
This is the coding i have for now on the main page, not tab 1 2 or 3. I want to make navigation appear when u click the 3 bar the side navigation tab will show. How do I change the XML file to do that? for now it shows different thing from what I want.
For this you need to implement logic in your java file:
Add following code in your activity or java file
final DrawerLayout drawerLayout=findViewById(R.id.drawerLayout);
TabLayout tabLayout = findViewById(R.id.tabs);
final int[] counterTabLast = {0};
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
if(tab.getPosition()==2){ //Position 2 means third tab
counterTabLast[0] = counterTabLast[0] +1;
if(counterTabLast[0] ==3){//
drawerLayout.openDrawer(Gravity.START); //Open Drawer
counterTabLast[0]=0;// Reset counter tab
}
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
I am trying to implement custom tabs UI in bottom like that of Instagram (screenshot is attached). I want the middle tab to open another activity instead of opening a fragment inside the same view.
I feel that this is implemented using imagebutton overlay on the tab host. But, I am still not able to place that imagebutton properly so that UI looks proper. Below is my code for the tabs in the bottom
<?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/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<android.support.v4.app.FragmentTabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
Kindly help.
Thanks,
Use TabLayout to do that.
xml:
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:minHeight="100dp"
app:tabGravity="fill"
app:tabMode="fixed"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
code:
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab());
View custom = LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
((TextView) custom.findViewById(R.id.tabTitle)).setText("Tab 3");
(custom.findViewById(R.id.tabIcon)).setBackgroundResource(R.drawable.ic_place_white_18dp);
TabLayout.Tab customTab = tabLayout.getTabAt(2);
customTab.setCustomView(custom);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
switch (tabLayout.getSelectedTabPosition()) {
case 0:
//do what you want when tab 0 is selected
break;
case 1:
//do what you want when tab 1 is selected
break;
case 2:
//do what you want when tab 2 is selected
break;
default:
break;
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
EDIT:
Third tab is using custom layout.
I was working with and slider tab, insaid the main view I have 2 tabs and insaid them, I have a XML to set the UI. Everything is perfect, I get my slide tabs and I can run the app. The problem is when I try to get a button from my fragments. I was reading about LayoutInflater, but I couldn´t solved.
Please, I think I don´t undestand very well how the fragment works and I would like a little of help about this.
This is my main activity (onCreate):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
facebookLogin = new FacebookLogin(getApplicationContext());
setContentView(R.layout.register_tabs);
toolBar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolBar);
adapter = new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true);
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
#Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.colorPrimary);
}
});
tabs.setViewPager(pager);
}
I my main XML:
<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"
tools:context=".MainActivity">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar"
/>
<com.cheescake.clasi.all.SlidingTabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
/>
</LinearLayout>
And the XML and class where I have my button:
<?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">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="6">
<Space
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="3" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Space
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Inicia sesión para comprar y vender los mejores productos "
android:id="#+id/textView"
android:gravity="center" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="#string/user_name"
android:ems="10"
android:id="#+id/editText"
android:layout_weight="2"
android:gravity="bottom" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="#string/user_pass"
android:ems="10"
android:id="#+id/editText2"
android:layout_weight="2"
android:gravity="bottom" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="10dp"
android:text="¿has olvidado tu contraseña?"
android:textColor="#color/colorPrimaryDark"
android:id="#+id/text_loggin"
android:layout_weight="1.3"
android:layout_marginLeft="5dp"
android:gravity="right" />
<Space
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<com.google.android.gms.common.SignInButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_weight="1" />
<com.facebook.login.widget.LoginButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/login_button"
android:layout_weight="1" />
<Space
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3" />
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="3" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:background="#color/bottomBackground">
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/finish"
android:id="#+id/txtNextButton"
android:layout_weight="1"
android:gravity="center" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
The class of the fragment:
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.facebook.login.widget.LoginButton;
public class RegisterTabLogin extends Fragment {
private LoginButton loginButton;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, #Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.register_tab_login,container,false);
return v;
}
}
As I say, I just want to get a View from the fragment and work with it in the Main Activity, I was reading about LayoutInflater but I could´t solved, Also I read some questions here, (Using button from a fragment in main activity) but I couldn´t solved.
Any help will be gratefull, a link o something.
have you tried this?
public class RegisterTabLogin extends Fragment {
private LoginButton loginButton;
private LoginCallback mCallback;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, #Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.register_tab_login,container,false);
loginButton = (LoginButton) v.findViewById(R.id.login_button);
showData.setOnClickListener(onClickListener);
return v;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallback = (LoginCallback) activity;
} catch (ClassCastException e) {
throw new ClassCastException(
"Activity must implement LoginCallback.");
}
}
private final OnClickListener onClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
if (mCallback != null) {
mCallback.onLogin();
}
}
};
public static interface LoginCallback{
void onLogin();
}
}
then on MainActivity
public class MainActivity extends Activity implements
RegisterTabLogin.LoginCallback {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
facebookLogin = new FacebookLogin(getApplicationContext());
setContentView(R.layout.register_tabs);
toolBar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolBar);
adapter = new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true);
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
#Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.colorPrimary);
}
});
tabs.setViewPager(pager);
}
#Override
public void onLogin() {
//DO SOMETHING
}
}
In your Main Activity, access this view in the OnStart() method like this:
#Override
protected void onStart() {
Button yourButton = (Button)findViewById(R.id.yourButton);
super.onStart();
}
EDIT:
Your fragment should be present in your Main Activity.
You can add RegisterTabLogin in a certain containerView present in your Main Activity layout in the OnCreate() method like this:
RegisterTabLogin registerTabLogin= new RegisterTabLogin();
getFragmentManager().beginTransaction().add(R.id.a_certain_layout, registerTabLogin).commit();