Fragment is not fully covering parent activity - android

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

Related

Android Studio Activity Layout Changing

I need help in changing layout of Activity in Android Studio when I click on button.
But I don't want to open new activity I want to change layout and design completely on the current activity by clicking button. Is it possible ?
you can inflate another layout during button click:
public class LayoutInflateDemo extends Activity {
private LinearLayout layoutToAdd;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_existed);
layoutToAdd = (LinearLayout) findViewById(R.id.existedlayout);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater inflater = LayoutInflater
.from(getApplicationContext());
View view = inflater.inflate(R.layout.layout_toinfliate, null);
//Add the inflated layout to your existing view
layoutToAdd.addView(view);
}
});
}
}
layout to inflate 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">
<TextView
android:id="#+id/mytext"
android:text="Inflated layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
And this is your existing layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/existedlayout"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Clicck to inflate view"
android:id="#+id/button"
/>
</LinearLayout>
Yes it is possible by using setContentView() method. But you have to make sure that you didn't use any view (e.g. view by id) that is not available on current layout.
Best way is Fragments and FragmentActivity https://developer.android.com/guide/components/fragments.html

Buttons have no text on Android 4.0.4 device

I've been developing an app on Android 5.1.1, and everything works fine. But when I test it on a 4.0.4 device, none of the buttons display any text. Any idea why this would be?
Each button is the UI of a fragment. This is the layout, named just_a_button.xml:
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/button_copper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:visibility="gone"/>
This is an example of how the fragments set up their views:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final Button button = (Button) inflater.inflate(R.layout.just_a_button, container, false);
button.setText(R.string.member_info);
// set click listener omitted
return button;
}
The button's visibility is controlled by an event receiver. The button is shown as expected, and its click listener works. There's just no text on it.
Edit: The solution was to put the button inside a layout. I'm still curious if this requirement was ever documented, and why only the text seems to be affected when the button is the root view.
Instead of this
button.setText(R.string.member_info);
use
button.setText(getActivity().getResources().getString(R.string.member_info));
You are directly defining string resource, but setText() takes string as parameter.
Edit:
change you xml like below and then findviewbyid for your button
<?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:id="#+id/Mybutton"
style="#style/button_copper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:visibility="gone"/>
</LinearLayout>
In your fragment onCreateView() do as below :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.just_a_button, container, false);
Button button = (Button) view.findViewById(R.id.Mybutton);
button.setText(getActivity().getResources().getString(R.string.member_info));
// set click listener omitted
return view;
}

How to replace an Android fragment with another fragment inside a ViewPager?

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

When declaring buttons different fragments does not recognize

I have a problem when declaring a button.
I will try to explain as specific as possible.
In my main Layout I have a Fragment containing a secondary Layout. In which I have several buttons.
My intention is that my main fichero.java to declare the buttons within the fragment.
Here we put the main 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"
android:baselineAligned="false">
<fragment
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="josejuansosarodriguez.radioecca.conocecanarias.TrueoFalseFragment"
android:id="#+id/fragmentTrueFalse"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Grp1"
android:id="#+id/textGrp1"
android:textSize="25sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"/>
<fragment
android:layout_width="fill_parent"
android:layout_height="450dp"
android:name="josejuansosarodriguez.radioecca.conocecanarias.Grp1FragmentP1"
android:id="#+id/fragmetaskGRP1"
android:layout_below="#+id/textGrp1"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp" />
</RelativeLayout>
Here we put the secondary Layout that has the buttons:
<?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:id="#+id/fragment_TrueorFalse">
<Button
android:layout_width="120sp"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="#string/buttontrue"
android:id="#+id/buttontrue"
android:layout_marginLeft="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" />
<Button
android:layout_width="120sp"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="#string/buttonfalse"
android:id="#+id/buttonfalse"
android:layout_marginRight="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
And here I leave the my main activity:
public class Grp1Fragment extends Fragment {
private Button buttonTrue;
private Button buttonFalse;
private Button buttonNextAsk;
private View view;
public Grp1Fragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
buttonTrue = (Button) view.findViewById(R.id.buttontrue);
buttonTrue.setOnClickListener(this);
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_grp1, container, false);
return view;
}
My problem I find in the following line:
buttonTrue.setOnClickListener (this);
The message reads: View can not be in Applied to josejuansosarodriguez.radioecca.conocecanarias.Grp1Fragment
I hope I have spread far.
Thank you very much for everything, this forum is amazing.
Try this:
Note that you have to FIRST inflate the layout to make accesible the widgets of the layout.
Then you can "bind" the widgets, and finallly in this case, set the listeners to the buttons.
I set you a Log, if you need change it to Toast, or code
public class Grp1Fragment extends Fragment {
private Button buttonTrue;
private Button buttonFalse;
private Button buttonNextAsk;
private View view;
public Grp1Fragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_grp1, container, false);
//Declare your widgets (Buttons)
buttonTrue = (Button) view.findViewById(R.id.buttontrue);
buttonFalse = (Button) view.findViewById(R.id.buttonfalse);
//Set the Listeners
buttonTrue.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Toast or Log, or whateber you need
Log.d("Fragment1" , "Button: True");
}
});
buttonFalse.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Toast or Log, or whateber you need
Log.d("Fragment1" , "Button: FAlse");
}
});
//return the view
return view;
}
void setOnClickListener(View.OnClickListener l)
requires a OnClickListener, which Grp1Fragment is not.
Use something like
buttonTrue.setOnClickListener(new View.OnClickListener() {
void onClick(View v) {
... // reaction on the button
}
});

add android button's on click event in a separate class

I have a button in android which i have defined in fragment_main.xml as -
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/love_button_text"
android:onClick="onLoveButtonClicked"
/>
the main_activity xml file is as follows -
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.newa.newapp2.MainActivity"
tools:ignore="MergeRootFrame" >
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100sp"
android:text="#string/a_button"
android:onClick="onLoveButtonClicked1"
/>
</FrameLayout>
Now as there is a onClick event defined i wanted to define in a separate class file as follows -
public class AndroidLove extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onLoveButtonClicked1(View view) {
TextView tv = (TextView) findViewById(R.id.anotherText);
tv.setVisibility(View.VISIBLE);
}
}
This compiles fine but at run time throws the error
"could not find the event listener named onLoveButtonClicked"
This however is working fine if i write the function in my MainActivity.java
Any ideas?
Thanks
Use an anonymous inner class in your fragment class.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
Button button = (Button)view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//your code here
}
}
}
Lastly remove this from your xml:
android:onClick="onLoveButtonClicked"
Hello the Button is in your fragment_main.xml file
and you are setting content view in activity_main to AndroidLove activity.
i think you need to write onLoveButtonClicked() method inside Fragment not in activity.
Method name from xml and java file must same.
activity/fragment that set that xml file as content view it must be have same name method with view as params.
Thanx.

Categories

Resources