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.
Related
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>
I have a recyclerview list. Each item in the recyclerview contains the following layout. Later when one clicks on the sample1 id, then sample3.xml should get inserted inbetween sample1 and sample2. For that I have created a linearlayout with id insertat in this layout.
<?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="fill_parent"
android:layout_height="40dp"
android:id="#+id/sample1"
android:text="Sample1"/>
<LinearLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/insertat">
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:id="#+id/sample2"
android:text="Sample2"/>
</LinearLayout>
onclicking id sample1 I want to insert a layout file sample3.xml inside linearlayout id insertat.
sample3.xml is as follows
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/toinsert">
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:text="Sample1"
android:id="#+id/sample3"/>
</LinearLayout>
How can I achieve this in recyclerview code.
I did not get exact requirement but , if you want just visible the view after on click, you can keep your layout by merge both as like below and can visible and invisible based on click event.
<?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="fill_parent"
android:layout_height="40dp"
android:id="#+id/sample1"
android:text="Sample1"/>
<LinearLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:id="#+id/insertat">
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:text="Sample1"
android:id="#+id/sample3"/>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:id="#+id/sample2"
android:text="Sample2"/>
</LinearLayout>
LinearLayout layout = (LinearLayout)findViewbyId(R.id.insertat);
mKeepPlayingButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
layout.setVisibility(View.visible);
}
});
First get a reference of the insertat:
LinearLayout insertAtView = (LinearLayout) findViewById(R.id.insertat);
Inflate sample3 view:
View sample3View = getLayoutInflater().inflate(R.layout.sample3, insertAtView, false);
Then add sample3 to insertat:
insertAtView.addView(sample3View);
Full code:
findViewById(R.id.sample1).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout insertAtView = (LinearLayout) findViewById(R.id.insertat);
View sample3View = getLayoutInflater().inflate(R.layout.sample3, insertAtView, false);
insertAtView.addView(sample3View);
}
});
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.
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())
I created a sample application to test how sliding menu works. Shown below in the screenshot is what i get as of now. But when I click on the categories button (shown in image below) I should get a secondary menu as shown in the zomato app's screenshot below. How can I do this ? Am I proceeding in the correct way or not?
my SlidingFragmentActivity :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
con = this;
setSlidingActionBarEnabled(false);
setContentView(R.layout.main);
sm = getSlidingMenu();
sm.setMode(SlidingMenu.RIGHT);
sm.setShadowDrawable(R.drawable.shadowright);
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
sm.setBehindScrollScale(1.0f);
sm.setShadowWidthRes(R.dimen.shadow_width);
sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
sm.setFadeDegree(0.5f);
//sm.setSecondaryMenu(R.layout.properties);
//sm.setSecondaryShadowDrawable(R.drawable.shadow);
setTitle("Sliding Bar");
// set the Behind View
setBehindContentView(R.layout.menu_frame);
FragmentTransaction t = this.getSupportFragmentManager()
.beginTransaction();
mFrag = new SampleListFragment();
t.replace(R.id.menu_frame, mFrag);
t.commit();
}
my SampleListFragment :
public class SampleListFragment extends SherlockFragment {
private static final String[] Radio_buttons = new String[] { "Distance",
"Rating" };
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.list, container, false);
ListView radio_list = (ListView) view.findViewById(R.id.RadioList);
Button categories = (Button) view.findViewById(R.id.sampleButton);
radio_list
.setAdapter(new ArrayAdapter<String>(MainActivity.con,
android.R.layout.simple_list_item_single_choice,
Radio_buttons));
radio_list.setItemsCanFocus(true);
radio_list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
categories.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.sm.showSecondaryMenu();
}
});
return view;
}
}
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"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Sliding menu demo...!!!" />
</RelativeLayout>
menu_frame.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/menu_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
list.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:paddingLeft="10dp"
android:text="SEARCH"
android:textColor="#FF3300"
android:textSize="20dp" >
</TextView>
</LinearLayout>
<RelativeLayout
android:id="#+id/searchTextLayout"
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_marginBottom="20dip"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="20dip" >
<ImageButton
android:id="#+id/searchTextButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="#685E5C"
android:scaleType="fitCenter"
android:src="#drawable/abs__ic_search" />
<EditText
android:id="#+id/searchText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/searchTextButton"
android:background="#drawable/background_black_border_full"
android:padding="8dp"
android:textColor="#android:color/white" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:paddingLeft="10dp"
android:text="SORT BY"
android:textColor="#FF3300"
android:textSize="20dp" >
</TextView>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="134dp" >
<ListView
android:id="#+id/RadioList"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
</ListView>
</RelativeLayout>
<Button
android:id="#+id/sampleButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Categories" />
</LinearLayout>
</ScrollView>
SlidingMenu does NOT do this, zomato uses a custom implementation.
SlidingMenu will let you have a menu on the left and the right, but not two on either side.
I would look at using a view pager or a custom implementation. EitherWay I don't know of anything out of the box to do this. I might be worth looking at Android Views for inspiration.
It's a bit late, but still let me post my answer in case it will help someone in future. If you want to show another menu you can use
setMode(SlidingMenu.LEFT_RIGHT);
setSecondaryMenu(R.layout.yourSecondMenu);
and in your button click
showSecondaryMenu(true);
and perform your actions in that class.