How to alter fragments visibility on main activity - android

In android I am trying to move more activities into one (refactor code and I try to replace set of activities with fragments), but I need only one fragment to be visible at time.
How on click button in activity which has this xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/fragment_container"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:orientation="vertical" >
<fragment
android:id="#+id/lm_fragment"
android:name="com.example.loyalty.MerchantsActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
/>
<fragment
android:id="#+id/pm_fragment"
android:name="com.example.loyalty.RewardsActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2" />
<com.touchmenotapps.widget.radialmenu.semicircularmenu.SemiCircularRadialMenu
android:id="#+id/radial_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:padding="0dp" />
</RelativeLayout>
to make that on click change which fragment is visible (only one is visible and use whole screen)

If you are trying to show one fragment at a time and also want many fragment to change, you have to create fragment dynamically in activity on a view click. use a xml like this one.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- This will hold your fragment's view-->
<FrameLayout
android:id="#+id/fragment_layout"
android:layout_width="match_parent"
android:background="#android:color/white"
android:layout_height="0dp"
android:layout_weight="1" />
<!-- This part will be your activity specific -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<Button
android:id="#+id/open_fragmen1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="#+id/open_fragmen2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
Here FrameLayout will be replaced with your Fragment that you want to show on Activity at that time, on Button click you can make this transition.
In activity to change the Fragment you have to write the following code.
public class MenuActivity extends Activity {
private Button btn1, btn2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btn1 = (ImageButton) findViewById(R.id.open_fragment1);
btn2 = (ImageButton) findViewById(R.id.open_fragment2);
btn1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// I am assuming your fragment class is MerchantsActivity
changeFragmentTo(new com.example.loyalty.MerchantsActivity());
}
});
btn2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
changeFragmentTo(new com.example.loyalty.RewardsActivity());
}
});
}
public void changeFragmentTo(Fragment fragment) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.menu_fragment, fragment).commit();
}
}
If you need to change another fragment to activity call the function changeFragmnetTo(new NewFragment())

Related

Hiding the MainActivty's placeholder when a new fragment appears on top of it

This is inside the LinearLayout of my activtiy_main.xml. It contains an emply frame layout and a button outside the frame.
<FrameLayout
android:layout_weight="0.8"
android:background="#color/colorAccent"
**android:id="#+id/placeholder"**
android:layout_width="match_parent"
android:textAlignment="center"
android:layout_height="match_parent">
</FrameLayout>
<Button
android:id="#+id/button"
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
This is my fragment1.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:text="Hello Sir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> </LinearLayout>
I have successfully created a java file named hello for this fragment.
And lastly this is my MainActivty.java
public class MainActivity extends AppCompatActivity {
Button change , change2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
change = (Button) findViewById(R.id.button);
change.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment = new hello();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.animator.fragment_slide_left_enter,
R.animator.fragment_slide_left_exit,
R.animator.fragment_slide_right_enter,
R.animator.fragment_slide_right_exit);
ft.replace(R.id.placeholder, fragment);
ft.commit();
}
});
}
}
This executes fine but the problem is the FrameLayout(from the activtiy_main.xml) stays even after the fragment appears on top of it.
Screen Shot after the button is clicked
I want to make the frameLayout(id - placeholder) to disappear or hide after the fragment has come on top of it.
Please help me.
P.S: This just a test application. Can't share the original one.
Thanks in advance.
Just keep mainActivity as place holder
<FrameLayout
android:layout_weight="0.8"
android:background="#color/colorAccent"
android:id="#+id/placeholder"
android:layout_width="match_parent"
android:textAlignment="center"
android:layout_height="match_parent">
</FrameLayout>
Add one fragment_one with button in activity and replace fragment_one with fragment_two it will work fine.
fragment_one
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button"
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>
fragment_two
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:text="Hello Sir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> </LinearLayout>

Fragment Transaction

I have a ListFragment with a floating action button. When the button is clicked, i want to replace the ListFragment with a CreateListFragment. Instead, the CreateListFragment is placed on top of the ListFragment and the button and textview from the ListFragment are still visible. I am not sure what went wrong. How do I completely replace the ListFragment with the CreateListFragment?
Here is the screenshot before the button is clicked
http://i.stack.imgur.com/k2HxP.png
Here is a screenshot after the button is clicked
http://i.stack.imgur.com/TYN47.png
list_fragment
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listFrame">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fab"
android:layout_gravity="bottom|end"
app:backgroundTint="#color/colorAccent"
android:src="#drawable/create"/>
<TextView
android:id="#+id/tvEmpty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="No Shopping Lists Yet"
android:layout_gravity="center"
android:textSize="20sp"/>
</FrameLayout>``
createlist_fragment
<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:padding="10dp"
tools:context="com.lavineoluoch.helloworld.swiftpay.app.CreateListFragment">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/etListTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/title"/>
<EditText
android:id="#+id/etAddItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/items"/>
<RelativeLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btnEditItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btnEdit"
android:background="#color/colorPrimary"
android:textColor="#color/white"
/>
</RelativeLayout>
</LinearLayout>
ListFragment.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View view = inflater.inflate(R.layout.list_fragment, container, false);
fab = (FloatingActionButton)view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CreateListFragment createListFragment = new CreateListFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.listFrame,createListFragment);
fragmentTransaction.commit();
}
});
return view;
}
In your root view for your Fragments insert this line of code:
android:background="?android:windowBackground"
As you have a see through background your view can still see the reminiscence of the replaced Fragment as the view hasn't yet updated.
The above answer should solve your problem, however I would recommend re-structuring your approach and instead launching a new Activity which contains the CreateListFragment when the FAB is pressed. Doing so would result in easier to maintain code which is also easier to read and would be best practice. It would also mean that the user can press their back button and navigate back to ListActivity/ListFragment without you needing to manually handle Fragment back stacks.

Activity layout is still visible after adding fragment to the activity

This is a really lame question, but I really cannot figure out why it happens, so please help me out.
I have a simple activity and I add a fragment to it dynamically. The problem is that once the fragment is added to the activity, the activity layout is also visible. Why does this happen?
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnLoad = (Button) findViewById(R.id.btn1);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
HelloFragment hello = new HelloFragment();
fragmentTransaction.add(R.id.rootView, hello, "HELLO");
fragmentTransaction.commit();
}
};
btnLoad.setOnClickListener(listener);
}
}
activity_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" tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rootView"
android:orientation="vertical">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="OK"/>
</LinearLayout>
</LinearLayout>
HelloFragment.java
public class HelloFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/** Inflating the layout for this fragment **/
View v = inflater.inflate(R.layout.hello_fragment_layout, null);
return v;
}
}
hello_fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="fragment 1"/>
</LinearLayout>
Convention is to have a FrameLayout that you use fragmentTransaction.replace()/add() on.
You shouldn't have views like TextView and Button in your MainActivity layout file. Instead, have those in another fragment and just have a FrameLayout in your MainActivity xml. Load the fragment with TextView/Button first, and call replace on button click with your new fragment.
Set a background color to your fragment linearLayout and make it clickable
The fragment is just another element on the layout. Adding it to the layout does not automatically remove other views. If you want the fragment to replace the content of rootView you need to remove rootview's children views first. Something like:
((LinearLayout)findViewById(R.id.rootView)).removeAllViews();
So your onClick method should look like this:
#Override
public void onClick(View v) {
((LinearLayout)findViewById(R.id.rootView)).removeAllViews();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
HelloFragment hello = new HelloFragment();
fragmentTransaction.add(R.id.rootView, hello, "HELLO");
fragmentTransaction.commit();
}
your rootview work as container for both fragment and your views on activity. check it and make separate view for fragment.
I know it's very late to answer this question but for those who are still looking do the following in your activity layout:
<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=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="OK"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rootView"
android:orientation="vertical">
</RelativeLayout>
And fragment layout to following:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="#color/white" <!--Add this-->
android:layout_height="match_parent">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="OK"/>
</LinearLayout>
activity_main.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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rootView"
android:orientation="vertical">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="OK"/>
</LinearLayout>
<FrameLayout
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</FrameLayout>
MainActivity.java
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
HelloFragment hello = new HelloFragment();
fragmentTransaction.add(R.id.fragmentContainer, hello, "HELLO");
fragmentTransaction.commit();
}
};
I have a solution for this to achieve what you can do is make two fragment one for textview and button your showing as default when activity is opened and another which you have already created as HelloFragment.
From Activity onCreate() method call the first fragment with default textview and button and from the click listener of that button call HelloFramgent.
Step1: Crate another fragment with any name MainFragment having a xml layout as follows:-
main_fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="OK"/>
</LinearLayout>
MainFragment.java - Class
public class MainFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/** Inflating the layout for this fragment **/
View v = inflater.inflate(R.layout.main_fragment_layout, container, false);
Button btnLoad = (Button) v.findViewById(R.id.btn1);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
HelloFragment hello = new HelloFragment();
fragmentTransaction.add(R.id.rootView, hello, "HELLO");
fragmentTransaction.commit();
}
};
btnLoad.setOnClickListener(listener);
return v;
}
}
Step2: Call the MainFragment from activity onCreate() method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MainFragment mainFragment = new MainFragment();
fragmentTransaction.add(R.id.rootView, mainFragment, "MAIN_FRAGMENT");
fragmentTransaction.commit();
}
Build your user interface in Fragments (In most cases one Fragment equals one screen) and then use Activities to arrange and display those Fragments.
check this: click here to view detailed answer
You have two linear layouts, one inside another, so you need to do the following steps:
Set id to 1 linear layout rootView and next to the childview.
Find your child view:
View view = findViewById(R.id.child_view);
view.setVisibility(View.INVISIBLE);
Replace your rootView with your fragment:
getSupportFragmentManager().beginTransaction().add(R.id.rootview, new blankFragment())
.addToBackStack(null).commit();
Then override:
#override
public void onBackPressed() {
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
if (fm.getBackStackEntryCount() > 0) {
view.setVisibility(View.VISIBLE);
}
super.onBackPressed();
}
The Activity views are not being masked against the Fragment's views, so both appear. One solution is to load the fragment into a frame in the main activity's layout which has a slight elevation (2 dp). When the fragment is loaded its base elevation will cause it to always draw over the activity. Set the fragments parent container with a color to hide the activity views.
MasterActivity layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Activity Title"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:textSize="24sp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_centerHorizontal="true"
android:textAllCaps="false"
android:text="Button1" />
<Button
android:id="#+id/button2"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_centerHorizontal="true"
android:text="Button2" />
<!-- Fragment holder, with elevation -->
<FrameLayout
android:translationZ="2dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragHolder"
/>
The Fragment layout has a background color:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:layout_margin="10dp">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Test Background Task"
android:textSize="24sp" />
</RelativeLayout>
Make your First Layout invisible while calling fragment and again while returning from fragment make your main view visible and remove fragment at that time so it will not conflict with each other it worked for really well just check it once

Replacing layout with another in Android

I have an app where user can change the content of the layout by pressing a button but when i want to replace a layout i get error in replacement line:
This is addtotry.xml code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:orientation="horizontal"
android:layout_width="150px"
android:layout_height="fill_parent"
android:background="#e7e7e7"
android:id="#+id/linearLayout">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="adrese"
android:id="#+id/adreseButton"
android:layout_below="#+id/infoButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="350px"
android:layout_toRightOf="#+id/linearLayout"
android:layout_toEndOf="#+id/linearLayout"
android:id="#+id/imageLayout">
<ImageView
android:layout_width="fill_parent"
android:layout_height="350px"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="#drawable/imageview"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toRightOf="#+id/linearLayout"
android:layout_toEndOf="#+id/linearLayout"
android:layout_below="#+id/imageLayout"
android:id="#+id/fragment_container">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Text"
android:id="#+id/textView7"/>
</RelativeLayout>
</RelativeLayout>
This is MainActivity :
public class MainActivity extends FragmentActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addtotry);
Button btnLoad = (Button) findViewById(R.id.adreseButton);
btnLoad.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentVieta fh = new FragmentVieta();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_container, fh);
ft.commit();
}
});
}
}
I do not now where i have made mistake because i have created FragmentView and it extends fragment. Do you have any idea how to fix my problem which is shown in picture? Because i changed to (int, com.wunderlist.slidinglayersample) and it said expression needed.
Check whether your FragmentVieta extends android.app.Fragment or support one ?
As you are using getFragmentManager() the FragmentVieta must extend from android.app.Fragment only.

Android View pager with listview and adding text dynamically

I have a Viewpager in a Fragment activity and it has a botton frame with the edit text and send button.
In the fragment layout i have a Listview and am attaching an adapter to it in fragment. Now am implementing the send button from the Parent inside the fragment, on click am trying to add the text in the edit text(again from the parent) to the List(in the fragment) via adapter. But issue here is when i enter some text and click send its adding the text not to the current view in the pager but to next, and when i go to the last item some times it adds to the same view(which is expected). Some times its random not in the order of next item.
Fragment Activity layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/detailLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/form" >
</android.support.v4.view.ViewPager>
<RelativeLayout
android:id="#+id/form"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:padding="2dp" >
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/btnSend"
android:drawableRight="#drawable/edit_keyboard"
android:inputType="textMultiLine"
android:maxLines="3" />
<ImageButton
android:id="#+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/send" />
</RelativeLayout>
</RelativeLayout>
The fragment Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"
android:divider="#android:color/transparent"
android:listSelector="#android:color/transparent"
android:scrollbars="none" />
</RelativeLayout>
Here's how am implementing the send ImageButton:
ImageButton sendBtn = (ImageButton) getActivity().findViewById(R.id.btnSend);
sendBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String msg = msgBox.getText().toString();
if (msg.length() > 0) {
long date = new Date().getTime();
// TODO Auto-generated method stub
commentAdapter.add(new OneComment(false, msg, "Me",
String.valueOf(date), "0"));
msgBox.setText("");
} else
Toast.makeText(getActivity(), "type in some text..", Toast.LENGTH_SHORT).show();
}
});
You can access to the current fragment (the one with list) doing something like this:
Fragment currentFragment = (Fragment) viewPager.getAdapter().instantiateItem(viewPager, viewPager.getCurrentItem()); //gets current fragment
//now you have to cast it to your fragment with list, let's say it's name is SukarnoListFragment
((SukarnoListFragment)currentFragment).messageList.add(...); // you have now access to public fields and methods that are inside your fragment
And I think that if you have your button in Activity then you should implement onClick of this button in your Activity, not in your fragment.

Categories

Resources