Making a fragment translucent in android - android

I have a fragment A that covers up the whole screen and another fragment B that is at the bottom of the screen of 50dp.Fragment B overlaps some bottom portion of fragment A.I want to make fragment B translucent so the overlapped portion of fragment A is seen.
I tried using Theme.Translucent,used framelayouts,used setAlpha() method,but didn't get the desired result.Just like we have a translucent actionbar in android,similarly I want to make my fragment B translucent.
I tried referring to these links...
link1 :
making the background translucent
link 2:
https://zaman91.wordpress.com/2010/03/22/android-how-to-create-transparent-or-opeque-background/
link3:
Making a android button’s background translucent,
some code to help you understand..
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.examples"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#null"
android:fitsSystemWindows="true" >
<!-- Your normal content view -->
<com.examples.views.SlidingUpPanelLayout
android:id="#+id/sliding_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|top" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/icn_actionbar_background"
android:minHeight="?attr/actionBarSize" >
<com.examples.views.CustomTextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:singleLine="true"
android:text="Toolbar Title"
android:textColor="#color/action_bar_text"
android:textSize="18sp"
android:textStyle="bold"
app:font="#string/font_avenirMedium" />
</android.support.v7.widget.Toolbar>
<!-- Say this is Fragment A -->
<fragment
android:id="#+id/frag_fragmentA"
android:name="com.examples.fragments.FragmentA"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
<!-- The rest of your content view -->
</LinearLayout>
<!-- this is fragment B -->
<fragment
android:id="#+id/fragment B"
android:name="com.examples.fragments.MyFragmentB"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|top" />
</com.examples.SlidingUpPanelLayout>
<LinearLayout
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:fitsSystemWindows="true" >
<!-- Your drawer content -->
<include
android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
layout="#layout/drawer_layout" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
the offset of 50dp is given in the SlidingPanelLayout itself..so only 50 dp of fragment B is visible.
In all these links Theme.Translucent is added to activity theme..but I want to create a theme that makes only that fragment translucent.
Any suggestions or help guys..deeply appreciated.Thanks.

You can add fragment in frame layout it will overlap..
Activity
public class MainBaseFragmentActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.main_fragment_container);
getSupportFragmentManager().beginTransaction()
.add(R.id.main_container, new FragmentScreenA()).commit();
}
}
FragmentA layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff00ddff"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/btn_screen_a"
android:gravity="center_horizontal"
android:text="A"
android:textColor="#ffffff"
android:textSize="100sp" />
<Button
android:id="#+id/btn_screen_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Go to Screen B" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Text From Fragment A" android:gravity="center" android:textSize="30sp"
android:textColor="#ffffff" />
</RelativeLayout>
Fragmnet A Class
public class FragmentScreenA extends Fragment {
private Button btn_screen_a;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private View rootView;
#Override
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
rootView= inflater.inflate(R.layout.fragment_screen_a, container, false);
return rootView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
btn_screen_a = (Button) rootView.findViewById(R.id.btn_screen_a);
btn_screen_a.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
getActivity().getSupportFragmentManager()
.beginTransaction()
.add(R.id.main_container, new FragmentScreenB())
.addToBackStack("FragmentScreenB").commit();
}
});
}
FragmentB class
public class FragmentScreenB extends Fragment {
private Button btn_screen_b;
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_screen_b, container,
false);
return rootView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
btn_screen_b = (Button) view.findViewById(R.id.btn_screen_b);
btn_screen_b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
getActivity()
.getSupportFragmentManager()
.beginTransaction()
.replace(R.id.main_container, new FragmentScreenC())
.addToBackStack("FragmentScreenB").commit();
}
});
}
private View rootView;
}
Fragment B XMl layout
<?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="#android:color/transparent"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" android:layout_weight="9"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="B"
android:textSize="100sp" />
<Button
android:id="#+id/btn_screen_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to Screen C" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
![enter image description here][1]![enter image description here][2]
this is for the solution which you asked..
Suggestion you can create multiple fragment in same screen. And that is proper way AFAIK

Related

Fragment does not create its corresponding view

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

Showing Toast in fragments

I want to show toast on button click, and that button is in the fragment. I have tried methods to get context for the toast, but it is not showing on button click.
This is my code
public class Bottom_Sheet_Fragment extends Fragment {
Button addComment;
public Bottom_Sheet_Fragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_bottom__sheet, container, false);
addComment=(Button) container.findViewById(R.id.addCommentBtn);
addComment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getActivity(), "Added", Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
This is fragment layout
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/bottom_sheet1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_light"
android:fillViewport="true"
android:orientation="vertical"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_light"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/commentList"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/etComment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:hint="Write comment" />
<Button
android:id="#+id/addCommentBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Add" />
</RelativeLayout>
</LinearLayout>
this layout file is included in another activity, with in include statement.dont know where is the problem. and this fragment works like a bottomsheet.
Try this: addComment = view.findViewById(R.id.addCommentBtn);
Because you must get button from layout was inflated.
You need to use view.findViewById.
Try This
addComment=(Button) view.findViewById(R.id.addCommentBtn);
addComment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getActivity(), "Added", Toast.LENGTH_SHORT).show();
}
});

Replacing fragment with fragment, nothing happens

I want to go from fragment5 to fragment6 by clicking on imageview.
Here is my fragment5 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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"/>
<TextView
android:id="#+id/textfrag5label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="header" />
<TextView
android:id="#+id/textfrag5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="39dp"
android:textStyle="bold"
android:text="foo "
android:layout_below="#+id/toolbar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:id="#+id/forwardfrag5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textfrag5label"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="23dp"
android:layout_marginRight="23dp"
app:srcCompat="#drawable/ic_arrow_forward_black_24dp"
android:onClick="clickEvent"/>
<FrameLayout
android:id="#+id/show_fragment"
android:layout_width="match_parent"
android:layout_height="0dp">
</FrameLayout>
</RelativeLayout>
And this is my fragment5 class in which I have declared an Imageview called forward5. So, if I click on this Imageview I want the app to show fragment6's layout. But if I click on the Imageview, nothing happens. I don't know why. Can anyone help me please?
public class Fragment5 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragmentdescrip5, container, false);
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
ImageView forward5 = (ImageView) rootView.findViewById(R.id.forwardfrag5);
toolbar.setNavigationIcon(R.drawable.ic_keyboard_backspace_black_24dp);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getActivity().onBackPressed();
} // this is for going back, e.g from fragment5 to fragment 4, this works
});
forward5.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.show_fragment, new Fragment6(), "NewFragmentTag");
ft.commit();
}
});
return rootView;
}
}
that's because you set you container's height is 0dp,In fact you fragment6 had attached, but you can't see it.
you can modify the height of container
<FrameLayout
android:id="#+id/show_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"><!--modify 0dp to some you need-->
</FrameLayout>

Get button from a fragment in Android

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();

Add fragment programmatically on button click

I know this is kind of a noob question, but I come from php and javascript, and I've just started learning java and android. I've read the dev documentation on fragments, and I'm trying to add a fragment on button click. App starts, but dies on button click. I don't know if my logic is wrong here? (Adding fragments directly in xml works).
Fragment class:
public class ExampleFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.example_fragment, container, false);
}
}
Fragment xml:
<?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/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/seekBar1"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:text="Button" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editText1"
android:layout_marginTop="14dp" />
</RelativeLayout>
Activity class:
public class MainActivity extends Activity implements OnClickListener{
Button addFragment;
android.app.FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
}
private void initialize()
{
addFragment = (Button) findViewById(R.id.bAddF);
addFragment.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ExampleFragment fragment = new ExampleFragment();
transaction.add(R.id.fragment_holder, fragment);
transaction.commit();
}
}
Activity xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${packageName}.${activityClass}" >
<TextView
android:id="#+id/tvHello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/bAddF"
android:layout_below="#id/tvHello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Fragment"/>
<View
android:id="#+id/fragment_holder"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="#id/bAddF">
</View>
</RelativeLayout>
Your fragment_holder, needs to be a ViewGroup, I'll recommend a FrameLayout since it is the simplest ViewGroup you can use.

Categories

Resources