Showing Toast in fragments - android

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

Related

Show/Hide Layout onTouch in framelayout

I have a full image fragment. There is a RelativeLayout and LinearLayout in framelayout. LinearLayout contain viewpager.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayoutViewpager">
<com.bogdwellers.pinchtozoom.view.ImageViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<RelativeLayout
android:id="#+id/relativelayoutforbuttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/save_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" />
</RelativeLayout>
I want When user first open it it should only show image(Viewpager layout) and if user click on it that show other option like - share and save(RelativeLayout).
We can find this type of feature in facebook and whatsapp.
You can simply hide or unhide the relativeLayout's instance using setVisibility.(..) method on linearLayout or imageviewpager click.
layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayoutViewpager">
<ImageViewPager
android:id="#+id/viewpager"
android:onClick="onPagerClick"
android:layout_width="200dp"
android:layout_height="200dp" />
</LinearLayout>
<RelativeLayout
android:id="#+id/relative_layout"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/save_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onSave"
android:text="Save" />
</RelativeLayout>
</FrameLayout>
Activity:
public class TestActivity extends AppCompatActivity {
private boolean isButtonShown = false;
ImageViewPager viewPager;
RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
viewPager = findViewById(R.id.viewpager);
layout = findViewById(R.id.relative_layout);
}
public void onPagerClick(View view) {
if (isButtonShown) {
isButtonShown = false;
layout.setVisibility(View.GONE);
} else {
isButtonShown = true;
layout.setVisibility(View.VISIBLE);
}
}
}
Try this type code for visiablity ..
private void share(){
relativeLayout.setVisibility(View.VISIBLE);
linearLayoutViewpager.setVisibility(View.GONE);
}
linearLayoutViewpager=findViewById(R.id.linearLayoutViewpager);
linearLayoutViewpager.setVisibility(View.VISIBLE);
relativeLayout=findViewById(R.id.relativelayoutforbuttons);
relativeLayout.setVisibility(View.GONE);
it only sample code but mange according your needs.
final boolean flag=true;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (flag){
relativeLayout.setVisibility(View.VISIBLE);
flag=false;
}
else{
relativeLayout.setVisibility(View.GONE);
}
}
});

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>

Linear Layout onclick listener not working with recycle view

I have a problem with Recycleview in linearLayout. In the below code When I click on chat section(Linear Layout) Toast is not getting visible when I set LayoutManager to RecycleView. Please help me the toast will be visible when I click on linear layout.
Activity code:
public class PracticeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_practice);
setLayoutViews();
}
private void setLayoutViews(){
// setting up messages list view
RecyclerView mMessagesView = (RecyclerView) findViewById(R.id.messages);
// if i uncomment below line Toast will not be visible.
//mMessagesView.setLayoutManager(new LinearLayoutManager(this));
(findViewById(R.id.chat_section)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(PracticeActivity.this, " Chat section was clicked", Toast.LENGTH_SHORT).show();
}
});
}
activity_practice.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"
android:orientation="vertical"
android:id="#+id/activity_main">
<LinearLayout
android:id="#+id/chat_section"
android:layout_width="match_parent"
android:layout_height="320dp"
android:background="#color/chat_back_ground">
<android.support.v7.widget.RecyclerView
android:id="#+id/messages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:scrollbars="vertical"
android:scrollbarStyle="outsideOverlay"/>
</LinearLayout>
<TextView
android:layout_below="#id/chat_section"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Just text"/>
</RelativeLayout>
You can follow this tutorial. The process followed is to create a separate itemtouchlistener class from which the selection is identified
http://sapandiwakar.in/recycler-view-item-click-handler/

Making a fragment translucent in 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

Android FrameLayout is not replace by Fragments

This app contains activity_main.xml, mainactivit.java and one Fragment class.
activity_main.xml has one Button and FrameLayout inside RelativeLayout.
When the Button is clicked, FrameLayout should be replaced by fragments, but it doesn't get replaced.
activity_main.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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="loadd"
android:text="Fragment No.2" />
<FrameLayout
android:id="#+id/maincontainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>
mainactivity.java
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void loadd(View v) {
try {
Fragment fragment = new fr();
FragmentManager frgManager = getSupportFragmentManager();
FragmentTransaction trans = frgManager.beginTransaction();
trans.replace(R.id.maincontainer, fragment);
trans.commit();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "error " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
Fragment class
public class fr extends Fragment{
public View OnCreateView(LayoutInflater inf,ViewGroup parent,Bundle save){
return inf.inflate(R.layout.send,parent, false);
}
}
send.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textmsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 3" />
</LinearLayout>
There is no compile time or run time error, but when I click that Button the FrameLayout is not replaced by the Fragment.
Everything that you posted looks good, except you aren't overriding Fragment.onCreateView, instead you've made a typo.
What you have
public View OnCreateView(LayoutInflater inf, ViewGroup parent, Bundle save) {
return inf.inflate(R.layout.test, parent, false);
}
What you should have
#Override
public View onCreateView(LayoutInflater inf, ViewGroup parent, Bundle save) {
return inf.inflate(R.layout.test, parent, false);
}
Pay close attention to the name of the method. It should begin with a lowercase "o".

Categories

Resources