I have a code to call the intent to display the running services screen. But i wanted it to be displayed within a fragment. But am not able to do that. I call this intent from the java class "bottompage.java" which is the fragment for bottom half of the page. But still when the button is clicked this intent occupies the full screen.
Activity main:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_display);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragmentContainer1, new TopPage());
fragmentTransaction.add(R.id.fragmentContainer2, new BottomPage());
fragmentTransaction.commit();
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
Bottompage.java
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View secondview = inflater.inflate(R.layout.bottom_fragment,container,false);
Button dummy = (Button) secondview.findViewById(R.id.dummy);
dummy.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
final String APP_DETAILS_PACKAGE_NAME ="com.android.settings";
// Here you need to define the package name
final String SCREEN_CLASS_NAME ="com.android.settings.RunningServices";
Intent servicesintent = new Intent();
servicesintent.setAction(Intent.ACTION_VIEW);
//intent.setAction(Intent.ACTION_VIEW);
servicesintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
servicesintent.setClassName(APP_DETAILS_PACKAGE_NAME, SCREEN_CLASS_NAME);
BottomPage.this.getActivity().startActivity(servicesintent);
}
});
return secondview;
}
Main.xml
The xml file for 2 fragments
<?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"
android:weightSum="1">
<TextView
android:layout_width="157dp"
android:layout_height="wrap_content"
android:text="Second page"
android:id="#+id/tv2"
android:layout_gravity="center_horizontal"
android:layout_weight="0.06" />
<FrameLayout
android:id ="#+id/fragmentContainer1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:layout_weight="0.06"></FrameLayout>
<FrameLayout
android:id ="#+id/fragmentContainer2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_below="#+id/fragmentContainer1"></FrameLayout>
The xml file for the bottom fragment
BottomPage.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"
android:id="#+id/bottom_fragment">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="call services"
android:id="#+id/dummy"
android:layout_gravity="center_horizontal" />
How do i make the intent "servicesintent" in bottompage.java to display the running services within the fragment "fragmentcontainer2". Currently its taking full screen.
I am not shure what you exactly want
I use this code to display the Intent that activated my Fragment in the Log.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
Activity parent = this.getActivity();
Intent intent = (parent == null) ? null : parent.getIntent();
if (intent != null) {
Log.d(Global.LOG_CONTEXT, "onCreateView " +
intent.toUri(Intent.URI_INTENT_SCHEME));
}
Instead of adding it to the Log you can set the value to a textview.
How do i make the intent "servicesintent" in bottompage.java to display the running services within the fragment "fragmentcontainer2".
You don't. You cannot embed third-party UIs inside your own, except in very specific circumstances (e.g., app widgets and a home screen).
Also note that there is no requirement that any device have a com.android.settings.RunningServices activity in com.android.settings that is exported.
Related
I have a search interface (with an android search widget). When I retrieve results, each results is wrapped into a folding-cell. Once the folding-cell is loaded, I fill the cell-title with a simple TextView. Then, I fill the content with a TextView (which works). In addition to the TextView, my cell-content also contains a LinearLayout. I use this layout to add a fragment, using a FragmentTransaction.
My problem is that each fragment is supposed to fill a different folding-cell, but only they all adds to the first folding-cell (so I guess that FragmentTransaction manager is adding all fragments to the same LinearLayout, the one of the first Folding-Cell).
Here is a screen of 1st and 2nd folding-cells, unfolded:
Here is the code for the search results :
try {
JSONArray jsonarr = new JSONArray(response);
for (int i = 0; i < jsonarr.length(); i++) {
JSONObject row = jsonarr.getJSONObject(i);
Bundle args = new Bundle();
args.putString("email", row.getString("email"));
args.putString("name", row.getString("nom_organisme"));
Fragment newFragment = new FragmentOrganisationFoldingCell();
newFragment.setArguments(args);
getFragmentManager().beginTransaction().add(R.id.container_organisation_presentation_for_search, newFragment).commit();
organisationList.add(newFragment);
}
if(jsonarr.length() == 0){
errorText.setVisibility(View.VISIBLE);
}
} catch (JSONException e) {
e.printStackTrace();
}
Here is the FramgmentOrganisationFoldingCell:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_organisation_folding_cell, container, false);
final FoldingCell fc = (FoldingCell) view.findViewById(R.id.folding_cell);
Bundle args = getArguments();
TextView organisationName = view.findViewById(R.id.search_organisation_default_name);
organisationName.setText(args.getString("name"));
TextView t = view.findViewById(R.id.cell_content);
t.setText(args.getString("name"));
LinearLayout fragContainer = (LinearLayout) view.findViewById(R.id.cell_content_frag);
getFragmentManager().beginTransaction().replace(fragContainer.getId(), new FragmentProfileOrganisation(), "Organisation" + args.getString("name")).commit();
fc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
fc.toggle(false);
}
});
return view;
}
Here is fragment_organisation_folding_cell, which is the view I inflate in FragmentOrganisationFoldingCell :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/block">
<com.ramotion.foldingcell.FoldingCell
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/folding_cell"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="false"
android:clipToPadding="false">
<!-- CONTENT (UNFOLDED) LAYOUT (MUST BE AT LEAST 2x times BIGGER than content layout bellow)-->
<include layout="#layout/cell_content_layout" />
<!-- TITLE (FOLDED) LAYOUT (MUST BE AT LEAST 2x times SMALLER than content layout above) -->
<include layout="#layout/cell_title_layout" />
</com.ramotion.foldingcell.FoldingCell>
</LinearLayout>
Here is the XML code for the cell_content_layout, which contains the LinearLayout I use to add my fragments. (So I beleive that this is the file that creates the trouble).
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="60sp"
android:layout_margin="40dp"
android:id="#+id/cell_content"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cell_content_frag">
</LinearLayout>
</LinearLayout>
Here is the fragment_organisation_search XML file :
...
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/block"
android:id="#+id/container_organisation_presentation_for_search"
android:layout_margin="5dp">
</LinearLayout>
...
Do anyone have any trouble adding fragments into Folding-cells too? If anyone have any idea I would be really thankful !
The following command in your code that is parsing search results will add newFragment to a view with ID R.id.container_organisation_presentation_for_search. That ID is in your XML for the actual fragment.
getFragmentManager().beginTransaction().add(R.id.container_organisation_presentation_for_search, newFragment).commit();
I think instead what you want may be:
getFragmentManager().beginTransaction().add(R.id.cell_content_frag, newFragment).commit();
I called setRetainInstance(true) for a fragment. I added an EditText and TextView on the fragment. When rotated, the text in the EditText survived, but the text in the TextView was gone.
I think I can manually restore the text of the TextView, but I wonder why the system automatically restores the text for EditText, and not for TextView. Did I do something wrong?
Steps to reproduce.
Type "android" in the EditText
Press the [Test 1] button. The TextView now displays "android"
Rotate the device.
Result
The EditText has "android", but the TextView is empty.
MyFragment.java
public class MyFragment extends Fragment
{
private static final String TAG = "MyFragment";
EditText etInput;
Button btnTest1;
TextView tvMessage;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState)
{
Log.d(TAG, "onCreateView()");
View v= inflater.inflate(R.layout.my_fragment, container, false);
etInput = (EditText)v.findViewById(R.id.etInput);
btnTest1 = (Button)v.findViewById(R.id.btnTest1);
btnTest1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
tvMessage.setText(etInput.getText().toString());
Log.d(TAG, "btnTest1 was clicked");
}
});
tvMessage = (TextView) v.findViewById(R.id.tvMessage);
return v;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity
{
String TAG = this.getClass().getName();
#Override
protected void onCreate(Bundle savedInstanceState)
{
Log.d(TAG, "onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
Fragment mf = fm.findFragmentById(R.id.placeholder);
if(mf == null)
{
Log.d(TAG, "creating new my fragment");
mf = new MyFragment();
mf.setRetainInstance(true);
fm.beginTransaction().add(R.id.placeholder, mf).commit();
}
}
}
my_fragment.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">
<EditText
android:id="#+id/etInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/tvMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/btnTest1"
android:text="Test 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:text="Fragment Test"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#+id/placeholder"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
</FrameLayout>
</LinearLayout>
If you want to enable auto restoration of TextView, you need to set freezesText attribute to true in your xml (or call setFreezesText(true)).
For TextView, freezesText default value is false, but in case of EditText, default value is true.
From documentation of freezesText:
If set, the text view will include its current complete text inside of
its frozen icicle in addition to meta-data such as the current cursor
position. By default this is disabled; it can be useful when the
contents of a text view is not stored in a persistent place such as a
content provider. For EditText it is always enabled, regardless of the
value of the attribute.
I have a view pager consisting of 4 tabs. Each tab holds its own fragment.
How would I be able to use fragment transaction to replace the fragment in tab 3 with a new fragment?
I've tried a lot of things, but I have not been able to come up with a solution at all. I've tried looking around on google and stackoverflow, but with no success.
I assume that your fragment has a button that is put in the center. By clicking on it, you can change the layout stays under of this button. The content/layout of the first fragment you mentioned should be replaced with wrapperA and the content/layout of the second one should be replaced with wrapperB. I put a simple red background for wrapperA to distinguish it with wrapperB, wrapperB is also green due to the same reason. I hope this is what you want:
public class SwitchingFragment extends Fragment {
Button switchFragsButton;
RelativeLayout wrapperA, wrapperB;
View rootView;
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.layout_switch, container, false);
wrapperA = (RelativeLayout) rootView.findViewById(R.id.wrapperA);
wrapperB = (RelativeLayout) rootView.findViewById(R.id.wrapperB);
switchFragsButton = (Button) rootView.findViewById(R.id.switchFragsButton);
switchFragsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (wrapperB.getVisibility() == View.GONE) {
wrapperB.setVisibility(View.VISIBLE);
// There is no need to change visibility of wrapperA since it stays behind when wrapperB is visible.
// wrapperA.setVisibility(View.GONE);
}
else {
wrapperB.setVisibility(View.GONE);
// Again, there is no need.
// wrapperA.setVisibility(View.VISIBLE);
}
}
});
return rootView;
}
}
The 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:layout_gravity="center"
android:orientation="vertical">
<!-- The content of first fragment should be in "wrapperA". -->
<RelativeLayout
android:id="#+id/wrapperA"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/red"
android:visibility="visible">
</RelativeLayout>
<!-- The content of second fragment should be in "wrapperB". -->
<RelativeLayout
android:id="#+id/wrapperB"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/green"
android:visibility="gone">
</RelativeLayout>
<!-- As I said, I assume that the layout switcher button is stable
and so it should be in front of the switching layouts. -->
<Button
android:id="#+id/switchFragsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#color/black"
android:text="Switch"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#color/white"/>
</RelativeLayout>
Alternatively, you can try to change the fragment directly by notifying your FragmentPagerAdapter as described in this link:
Replace fragment with another fragment inside ViewPager
Hi there (and thanks in advance),
I have an application with a Google Play Store-like layout (using PagerTabStrip with 5 sub fragments). In some of those fragments I will need to display a little info regarding the last time the data was updated.
I immediately thought of creating a fragment (LastUpdatedFragment) which I would then add (nest) to the fragments I needed. At first, and since the last updated date was supposed to be the same for every screen, things were working (I simply added the fragment in the xml of the parent Fragments I needed and inside onCreateView I would put the date in the TextView), but after some changes I now need to send a specific date for each one of these LastUpdatedFragment instances.
I came up with one way - creating new custom attributes for my Fragment and then I would set the date I wanted. After some reading I stumbled across a easier way (Fragments within Fragments - dinamically adding the fragment using FragmentManager) - I simply needed the parent Fragment to handle the input parameters and pass it to the child fragment.
The problem is that, although I get no errors I also get no child fragment, it just does not display. I would be grateful if you could guide me in the right direction.
ScoreBoardFragment.java -> Parent Fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_scoreboard, container,
false);
for (int i = 0; i < tvCount; i++) {
tvName = "f_scoreboard_header_tv" + String.valueOf(i);
resID = getResources().getIdentifier(tvName, "id",
_activity.getPackageName());
header = (TextView) rootView.findViewById(resID);
header.setTypeface(MyGlobalConfig.getInstance().getHeadersFont());
}
FragmentManager childFragMan = getChildFragmentManager();
FragmentTransaction childFragTrans = childFragMan.beginTransaction();
LastUpdatedFragment fragB = new LastUpdatedFragment();
childFragTrans.add(R.id.FRAGMENT_PLACEHOLDER, fragB);
childFragTrans.addToBackStack("B");
childFragTrans.commit();
return rootView;
}
fragment_scoreboard.xml (simplified but displaying everything else)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/FRAGMENT_PLACEHOLDER"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
style="#style/ListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/f_scoreboard_header_tv0"
style="#style/ListViewRowHeader"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#" />
</LinearLayout>
<ListView
android:id="#+id/f_scoreboard_lvbody"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true" >
</ListView>
</LinearLayout>
LastUpdatedFragment.java -> Child Fragment
public class LastUpdatedFragment extends Fragment{
private View rootView;
private TextView tv;
private Context ctx;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(
getActivity()));
this.ctx = getActivity().getApplicationContext();
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_date, container,
false);
tv = (TextView) rootView.findViewById(R.id.f_date_tv);
tv.setTypeface(MyGlobalConfig.getInstance().getBodyFont());
tv.setText(getString(R.string.lastupdated) + ": " + Util.getLastUpdated(ctx));
return rootView;
}
}
fragment_date.xml
<?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="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/f_date_tv"
style="#style/LastUpdatedTV"
android:layout_width="match_parent"
android:text="Data de última actualização: 27/12/2014 21:30" />
</LinearLayout>
Try to use FrameLayout instead of LinearLayout as Placeholder.
There is Button in Activity when we click on Button fragment's view overlaps Activity's view .
When Button is clicked it is forwarded to Fragment. Problem is it overlaps with Button.
In MainActivity i have created Button and set listener on that when user click on Button it is forwarded to fragment file which contain only textview.
MainActivity.java:
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
fragment fr = new fragment();
getSupportFragmentManager().beginTransaction().add(R.id.parent_frame, fr).commit();
}
});
}
}
fragment.java
public class fragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
return view;
}
}
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">
<TextView
android:id="#+id/detailsText"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text="Default Text ggggggg"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30dip" />
</RelativeLayout>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
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:text="Press to update"
/>
<fragment
class="com.example.demo3.fragment"
android:id="#+id/parent_frame"
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
Why aren't you using the Activity merely as a container, and use Fragments for ALL ui elements, including the button you are clicking to change between them?
Something like this: Programatically switching between Fragments
this is unique thought and i appreciate it.if you want to do this just follow below steps:
1) in your activity main take framelayout with height width system fit(matchparent) and id is parent_frame
2) take button inside it and when you click on it set button visible to gone
3) then you can add fragment to this framlayout
Make sure in your activity_main must contain framlayout with matchparent