Displaying fragments side-by-side - android

I want to develop an Android application for a tablet in landscape mode with 2 fragments:
Fragment 1 contains a list of buttons.
Fragment 2 is a space for one the button-related fragments.
With "button-related fragments" I mean the following: When the user presses a button in fragment 1, a fragment associated with that button should be displayed in fragment 2.
Fragment 1 is kind of navigation panel.
In order to implement this, I wrote following code:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="mypackage.MainActivity"
android:label="#string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".impl.activities.IntroActivity"></activity>
<activity android:name=".impl.activities.SimulationActivity"></activity>
</application>
MainActivity looks like this:
public class MainActivity extends Activity implements IMainActivity {
#Override
public void onCreate(final Bundle aSavedInstanceState) {
super.onCreate(aSavedInstanceState);
setContentView(R.layout.main);
}
#Override
public void show(final View aView, final Class<? extends Activity> aActivityClass) {
startActivity(new Intent(this, aActivityClass));
}
}
main.xml has following content:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="mypackage.fragments.ContentFragment"/>
ContentFragment looks like this:
public class ContentFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.mainfrag, container, false);
IMainActivity mainActivity = (IMainActivity) this.getActivity();
final IntroButtonClickListener introListener = new IntroButtonClickListener(
mainActivity, IntroActivity.class);
final IntroButtonClickListener simulationListener = new IntroButtonClickListener(
mainActivity, SimulationActivity.class);
view.findViewById(R.id.intro_button).setOnClickListener(introListener);
view.findViewById(R.id.simulation_button).setOnClickListener(simulationListener);
return view;
}
IntroButtonClickListener:
class IntroButtonClickListener implements View.OnClickListener {
private IMainActivity mainActivity;
private Class<? extends Activity> activityClass;
public IntroButtonClickListener(final IMainActivity aMainActivity,
final Class<? extends Activity> aActivityClass) {
this.mainActivity = aMainActivity;
this.activityClass = aActivityClass;
}
#Override
public void onClick(final View aView) {
this.mainActivity.show(aView, this.activityClass);
}
}
mainfrag.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="25"
android:id="#+id/intro_button"
android:text="#string/intro_button"/>
<Button
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="25"
android:id="#+id/simulation_button"
android:text="#string/simulation_button"/>
<Button
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="25"
android:id="#+id/statistics_button"
android:text="#string/statistics_button"/>
<Button
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="25"
android:id="#+id/help_button"
android:text="#string/help_button"/>
</LinearLayout>
This code leads to following results:
When I launch the application, there are 4 buttons on the screen (from mainfrag.xml).
When I press intro_button or simulation_button, the view changes to IntroFragment or SimulationFragment, respectively.
When I press the Back button, the four buttons are visible again.
My problem: When IntroFragment or SimulationFragment get visible, the button panel disappears.
How should I modify my code such that both button panel and the respective "detail" view (IntroFragment, SimulationFragment) are visible at the same time (there is enough screen space for this) ?

This kind of example is shown in the APIDemos which are there in the Android SDK. where they have the left panel as listview and the selected item shows the related fragment in right side.
You can find that in
SDK\android-sdk_r11-windows\android-sdk-windows\samples\android-14\ApiDemos

Related

How can I enable split-screen programmatically from inside my android app through on click button

This bounty has ended. Answers to this question are eligible for a +50 reputation bounty. Bounty grace period ends in 6 hours.
M. Usman Khan wants to draw more attention to this question:
Please somebody tell us how to trigger multi-window. Specially for Android 13 and above
My app contains a button through that button just want to trigger split screen functionality
Try to use setSplitScreenState(boolean) to trigger split screen functionality
Example
inside button
onClick{
ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
if(manager != null){
manager.setSplitState(true);
}
}
R.layout.activity_main (xml)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:onClick="startNewActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
tools:layout_editor_absoluteX="148dp"
tools:layout_editor_absoluteY="297dp" />
</android.support.constraint.ConstraintLayout>
R.layout.activity_second (xml)
(Just a textView to keep it simple)
<?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:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:text="Second Activity" />
</LinearLayout>
Then your MainActivity launch second activity from first activity when Start button is clicked and set Flags. Rect takes care of the switching in division and activity option takes care of the transiting animation;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startNewActivity(View view) {
Intent i = new Intent(this, SecondActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT |
Intent.FLAG_ACTIVITY_MULTIPLE_TASK |
Intent.FLAG_ACTIVITY_NEW_TASK);
Rect rt = new Rect(0, 0, 100, 100)
ActivityOptions actoptions = ActivityOptions.makeBasic();
ActivityOptions bounds = actoptions.setLaunchBounds(rt);
startActivity(i);
}
}
For this example, nothing I am going to leave the second activity as is.
SecondActivity
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
Finally and most importantly MainActivity.this in your manifest.xml file should look like this;
<activity android:name=".MainActivity"
android:resizeableActivity="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
To switch into multi window mode long press on the overview button and drag the app to one side of the screen
That's all.

How to change fragment position when rotate device

I want to change from layout 1 to layout 2, after rotate still keep content. Can somebody show me how to do that?
from this: https://www.dropbox.com/sc/y2nyrzard859hf2/AAD0qVjWoLzKcnQV9a4FTQi_a
to this: https://www.dropbox.com/sc/29hhlbfm31cfs0j/AADCWsFNzD7DKHx4q9i2FlbDa
this is my code but seem like it didn't work
if(config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.activity_create_bill3);
}
else {
setContentView(R.layout.activity_create_bill2);
}
if(fragmentManager.findFragmentByTag("fragment_product")==null) {
fragment_product = new Fragment_Product();
fragmentTransaction.replace(R.id.fragment_product,fragment_product,"fragment_product");
}
else
fragmentTransaction.replace(R.id.fragment_product,fragmentManager.findFragmentByTag("fragment_product"));
if(fragmentManager.findFragmentByTag("fragment_product_chosen")==null) {
fragment_product_chosen = new Fragment_Product_Chosen();
fragmentTransaction.replace(R.id.fragment_product_chosen,fragment_product_chosen,"fragment_product_chosen");
}
else
fragmentTransaction.replace(R.id.fragment_product_chosen,fragmentManager.findFragmentByTag("fragment_product_chosen"),"fragment_product_chosen");
fragmentTransaction.commit();
I using 2 diffent layout, it has a same view but one in horizontal and another in vertical, when rotate, fragment_product still keep content, but fragment_product_chosen are disappear.
You should have 3 clases:
FragmentMain
FragmentSide
MainActivity
click here to see your layout folder
Code in your MainActivity Class:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Code in your FragmentMain Class:
public class FragmentMain extends Fragment{
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
}
Code in your FragmentSide Class:
public class FragmentSide extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_side, container, false);
}
}
Then in your activity_main.xml:
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_main"
android:layout_marginTop="230dp"
class="au.com.example.multi_fragments.FragmentMain" />
<fragment
android:layout_width="match_parent"
android:layout_height="220dp"
android:id="#+id/fragment_side"
class="au.com.example.multi_fragments.FragmentSide" />
/>
same way in your activity_main.xml(land):
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="250dp"
android:id="#+id/fragment_main"
class="au.com.example.multi_fragments.FragmentMain" />
<fragment
android:layout_width="240dp"
android:layout_height="match_parent"
android:id="#+id/fragment_side"
class="au.com.example.multi_fragments.FragmentSide" />
in your fragment_main.xml:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View 2"
android:textSize="20sp"
android:padding="20dp"
android:textStyle="bold"
android:id="#+id/textViewMain" />
In your fragment_side.xml:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View 1"
android:textSize="20sp"
android:padding="20dp"
android:textStyle="bold"
android:id="#+id/textViewMain" />
Click here to see the output
I hope this solution is the one you want. Good luck :)
To have different layouts for landscape and portrait modes, create two folders under the res folder: layout and layout-land. All XML files should have the same names in both folders. For more details, read Designing for Multiple Screens. Even though this article is for different screen sizes, the techniques apply to different device orientations as well.
As for saving and restoring data, this is the same as destroying the activity without an orientation change.

Add Fragment overlay on activity's view

I am new bie in android.
Below is my senerio what i have and what i actually need.
I have one activity where i have declare one custom view which contains a set of button which click event display respective fragments.
This custom view will be appear on top of each fragment.
Now suppose on first button of custom view i am display fragment a fragment which display list-view..
on click on list-view it show another fragment i.e detail fragment
In detail fragment.. i have one button where i need to show an fragment overlay on main activity ... as on full screen.. how can i achive this?
In my opinion easiest way lead to this behavior is use Activity Overlay or DialogFragment. Example:
Activity Overlay
MainActivity.java
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.buttonShow);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
}
MainActivity.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:id="#+id/mainLayout"
tools:context=".MainActivity"
android:background="#android:color/holo_orange_dark">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Main Activity"
android:textSize="30dp"
android:layout_centerInParent="true"/>
<Button
android:layout_width="150dp"
android:layout_height="50dp"
android:text="show"
android:id="#+id/buttonShow"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
SecondActivity.java
public class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
SecondActivity.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:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".SecondActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_orange_light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Activity"
android:textSize="30dp"
android:layout_centerInParent="true"/>
</RelativeLayout>
</RelativeLayout>
Styles.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
</style>
<style name="TransparentFloatingActivity" parent="AppTheme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.arturszymanski.test" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:theme="#style/TransparentFloatingActivity"
android:name=".SecondActivity"
android:label="#string/title_activity_second" >
</activity>
</application>
</manifest>
DialogFragment
MyDialogFragment.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MyDialogFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_orange_light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FullScreen Fragment"
android:textSize="30dp"
android:layout_centerInParent="true"/>
</RelativeLayout>
MyDialogFragment.java
public class MyDialogFragment extends DialogFragment
{
private int margin;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
margin = 10;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_my_dialog, container, false);
}
#Override
public void onResume() {
int dialogHeight = MainActivity.displayMetrics.heightPixels - (margin * 2) - MainActivity.StatusBarHeight;
int dialogWidth = MainActivity.displayMetrics.widthPixels - (margin * 2);
getDialog().getWindow().setLayout(dialogWidth, dialogHeight);
super.onResume();
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity
{
public static DisplayMetrics displayMetrics;
public static int StatusBarHeight;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
StatusBarHeight = getStatusBarHeight();
Button button = (Button) findViewById(R.id.buttonShow);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
MyDialogFragment dialogFragment = new MyDialogFragment();
dialogFragment.show(getFragmentManager(), "Dialog");
}
});
}
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
}
Other Files
Other files are the same as in example above.
If i follow your question than, You should use a RelativeLayout as overlay in main activity's layout. and set visibility according to your need.
layout_activity_main.xml
<RelativeLayout
android:width="match_parent"
android:height="match_parent">
<LinearLayout>
// your toolbar
// You fragment container
// your main layout goes here
</LinearLayout>
<RelativeLayout
android:id="#+id/overlay"
android:width="match_parent"
android:height="match_parent">
</RelativeLayout>
</RelativeLayout>
like this :
<LinearLayout id = "#+id/container">
<ToolBar id ="#+id/toolbar>
</ToolBar>
<FrameLayout id ="#+id/main_containt"/>
</LinearLayout>
getSupportFragmentManager().add(new Fragment(),R.id.container).commit();
use container as the container of fragment;

Error message when trying to load second activity?

I'm very new to Android development, and I'm following the development guide as close as I can, but I seem to have hit a snag.
I have a simple main activity with a text edit and an image button.
When the image button is clicked, the text from the text edit element should be displayed in a second activity.
But when the button is clicked...!
I've been trying to use LogCat to diagnose the issue as many other people have suggested but my understanding is too limited to find the relevant issue!
This is what it comes up with in logCat when I click the button ...
06-12 19:01:11.830: E/AndroidRuntime(853): java.lang.RuntimeException:
Unable to start activity
ComponentInfo{com.azura_apps.pylon/com.azura_apps.pylon.BeginHoax}:
java.lang.IllegalArgumentException: No view found for id 0x7f05003c
(com.azura_apps.pylon:id/container) for fragment
PlaceholderFragment{b2d445c0 #0 id=0x7f05003c}
As i have come to understand it, there could be an issue with adding the activity to the manifest correctly, but I fail to see an issue.
This is my manifest -
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.azura_apps.pylon"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.azura_apps.pylon.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.azura_apps.pylon.BeginHoax"
android:label="#string/title_activity_begin_hoax" >
</activity>
</application>
</manifest>
I create an intent in my Main Activity with the method name I specified in my layout xml -
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.azura_apps.Pylon.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
public void LoadHoax(View view)
{
Intent intent = new Intent(this, BeginHoax.class);
EditText editText = (EditText) findViewById(R.id.test_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
This is where i retrive the intent in my second activity -
public class BeginHoax extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_begin_hoax);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
Main Activity Layout XML File -
<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="com.azura_apps.pylon.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="#string/welcome_message" />
<EditText
android:id="#+id/test_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageButton1"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="15dp"
android:ems="10"
android:text="#string/enter_test_message" />
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/test_message"
android:layout_marginTop="23dp"
android:layout_toLeftOf="#+id/textView1"
android:onClick="LoadHoax"
android:src="#drawable/start_scan_icon" />
</RelativeLayout>
I'm aware that this question has been answered before, but I'm having trouble applying the answers to my own code!
If anyone could point me in the right direction it would be greatly appreciated!
Thanks!
This is a guess, since you haven't posted your activity_begin_hoax xml file, but from trying to put the pieces together, this is what I'm getting:
Solution:
Erase this from your BeginHoax activity:
setContentView(R.layout.activity_begin_hoax);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Why:
The logcat message is telling you that there is no view with id "container" to put the PlaceholderFragment in.
By default, when you create a new activity in Eclipse, it automatically generates:
a) the activity xml, which is just a fragment container named "container"
b) the fragment xml, where you can put your layout that you want that fragment to make
c) the activity java class file, including an automatic PlaceholderFragment that, by default, builds the fragment xml and puts it in the container view in the activity xml.
If at any point in your testing, you modified the activity_begin_hoax xml file to not include a fragment container with id "container", then that would explain why the app can't find it and crashes.
Since you are not actually using the PlaceholderFragment in your BeginHoax activity (instead setting the content view by making the TextView), erasing the default bits there is an option.
A BETTER option for future real-app making would be to keep the fragment container in your activity xml file, put a textview in your fragment xml file (it probably has one by default anyway), and then inside BeginHoax's fragment you can get the textview from the fragment xml file, and put the message in.
public class BeginHoax extends ActionBarActivity {
private String message;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_begin_hoax);
Intent intent = getIntent();
message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new BeginHoaxFragment()).commit();
}
}
public static class BeginHoaxFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
TextView tv = rootView.findViewById(R.id.text1);
tv.setText(message);
return rootView;
}
}

Fragment is not fully covering parent activity

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

Categories

Resources