Fragment hide not working in Android - android

My requirement is, I've a main activity and I want to display an image and text instead of progress bar in a separate fragment.
I've a separate fragment and I want to display the fragment when user clicks on a button in MainActivity.
My issue is, when I start the app, the fragment gets displayed by default. I want the fragment to be displayed only when user clicks on button. When I start the app I want the activity to be displayed.
I've an activity_main xml as below
<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:background="#drawable/background" >
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:orientation="vertical" >
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip" >
<TextView
android:id="#+id/text1"
style="#style/textForLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:text="#string/text1" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="20dip"
android:layout_weight="1"
android:drawSelectorOnTop="true"
android:prompt="#string/spinner1"
android:spinnerMode="dialog" >
</Spinner>
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/text2"
style="#style/textForLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:text="#string/label2" />
<Spinner
android:id="#+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="20dip"
android:layout_span="6"
android:layout_weight="1"
android:drawSelectorOnTop="true"
android:prompt="#string/spinner2"
android:spinnerMode="dropdown" />
</TableRow>
</TableLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#color/red"
android:textSize="#dimen/text_medium"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<fragment
android:id="#+id/progress_bar_fragment"
android:name="com.test.pushnotificationeclipse.ProgressBarFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
The fragemnt_progress_bar.xml is as below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/NoActionBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_all" >
<ImageView
android:id="#+id/imageView_frag_pgbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/text_frag_pgbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/imageView_frag_pgbar"
android:layout_centerHorizontal="true"
android:layout_marginBottom="74dp"
android:textColor="#color/basic_color"
android:textSize="#dimen/text_medium_large" />
</RelativeLayout>
My ProgressBarFragment.java is as below
public class ProgressBarFragment extends Fragment {
#InjectView(R.id.text_frag_pgbar)
TextView textView;
#InjectView(R.id.imageView_frag_pgbar)
ImageView imageView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_progress_bar, container,
false);
ButterKnife.inject(this, view);
return view;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onDetach() {
super.onDetach();
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onResume() {
super.onResume();
}
}
My MainActivity.java is as below: When I pass true tohideAndShowFragment(), the fragment should be hidden and when I pass false the fragment should be shown.
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
context = getApplicationContext();
hideAndShowFragment(true);
}
public void hideAndShowFragment(boolean hide) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ProgressBarFragment pf = new ProgressBarFragment();
if (hide) {
ft.hide(pf).commit();
} else {
ft.show(pf).commit();
}
}
}

The issue here is your hideAndShowFragment method. You are creating a new instance of ProgressBarFragment, but the FragmentTransaction hide and show methods only work on an attached fragment.
Instead, you should get the fragment you already defined in your XML by id.
public void hideAndShowFragment(boolean hide) {
FragmentManager fm = getFragmentManager();
ProgressBarFragment pf =
(ProgressBarFragment) fm.findFragmentById(R.id.progress_bar_fragment);
FragmentTransaction ft = fm.beginTransaction();
if (hide) {
ft.hide(pf).commit();
} else {
ft.show(pf).commit();
}
}
Additionally, this line is concerning:
context = getApplicationContext();
Your Activity is also a Context object, so unless you need the application specific context for something, you generally want to be using this where you need a context inside your activity.

FragmentManager fm = getFragmentManager();
fm.beginTransaction()
.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
.show(yourfragment)
.commit();
Ps:-Dont Don't mess with the visibility flags of the container - FragmentTransaction.hide/show does that internally for you.
Src:- "https://stackoverflow.com/a/16490344/1632286"

You might be better of getting an instance of the fragment in code and using the technique from the official docs as shown below and here (search for 'newInstance'). Add the following static method to your fragment.
public static ProgressFragment newInstance() {
DetailsFragment f = new DetailsFragment();
// Supply args here if needed.
/*Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);*/
return f;
}
Once you have that you can add the fragment to the top view layout with the following fragment transaction code which is inside the button click method.
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction tr = fm.beginTransaction();
tr.add(android.R.id.content, ProgressFragment.newInstance(), "ProgressFragmentTag");
tr.commit();
You can then use the following code to remove the fragment if needed;
FragmentManager fm = getSupportFragmentManager();
ProgressFragment frag = fm.findFragmentByTag("ProgressFragmentTag")
if (frag!=null)
{
FragmentTransaction tr = fm.beginTransaction();
tr.remove(frag).commit();
}
Hope this helps!

I resolved it by adding the fragment instead of having it shown:
public void hideAndShowFragment(boolean hide) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ProgressBarFragment pf = (ProgressBarFragment) fm.findFragmentById(R.id.progress_bar_fragment);
if (hide) {
ft.hide(pf).commit();
} else {
ft.add(android.R.id.content, pf, "ProgressFragmentTag").commit();
}
}

int count =1;
if (count == 1) {
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction =
fm.beginTransaction();
fragmentTransaction.replace(R.id.labelframe, new
labelfregaments());
fragmentTransaction.commit();
count =0;
}
else if (count == 0) {
FragmentManager fm = getFragmentManager();
labelfregaments fs = (labelfregaments)fm.findFragmentById(R.id.labelframe);
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.hide(fs);
fragmentTransaction.commit();
count=1;
}

Related

Navigating fragments

I'm new to the android. I've an activity with 5 fragments and I've used addTobackStack so that I can move to the previous fragments when back button is clicked. It does the work but I want to call the functions in which I've been replacing the fragments when back button is pressed.
Any help?
Here is the code.
Dashboard.java
package com.example.sheikhspc.design;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.Fragment;
import android.content.Intent;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class dashboard extends AppCompatActivity {
ImageView home1, subs1, noti1,settings1,date1;
Fragment frag = null;
TextView home, subs,noti,set,date;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
home1 = (ImageView) findViewById(R.id.home1);
subs1 = (ImageView) findViewById(R.id.subs1);
noti1 = (ImageView) findViewById(R.id.noti1);
settings1 = (ImageView) findViewById(R.id.settings1);
date1 = (ImageView) findViewById(R.id.date1);
frag = new dashboard1();
home = (TextView)findViewById(R.id.hometv);
subs = (TextView)findViewById(R.id.substv);
noti = (TextView)findViewById(R.id.notitv);
set = (TextView)findViewById(R.id.settingstv);
date = (TextView)findViewById(R.id.datestv);
FragmentManager fm = getFragmentManager();
fm.beginTransaction().add(R.id.container1,frag).commit();
Toolbar mytoolbar = (Toolbar)findViewById(R.id.mytoolbar);
setSupportActionBar(mytoolbar);
getSupportActionBar().setTitle("Home");
home.setTextColor(0xFF000000);
}
public void dbfragment(View view)
{
if(frag != null)
{
home1.setBackgroundDrawable(getResources().getDrawable(R.drawable.home_icon_black) ome1.setBackgroundDrawable(getResources().getDrawable(R.drawable.home_icon_black) );
home.setTextColor(0xFF000000);
subs1.setBackgroundDrawable(getResources().getDrawable(R.drawable.subscription_ic on));
noti1.setBackgroundDrawable(getResources().getDrawable(R.drawable.noti_icon));
settings1.setBackgroundDrawable(getResources().getDrawable(R.drawable.setting_icon));
date1.setBackgroundDrawable(getResources().getDrawable(R.drawable.date_icon));
date1.setBackgroundDrawable(getResources().getDrawable(R.drawable.d))
frag = new dashboard1();
getSupportActionBar().setTitle("Home");
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.container1,frag);
fragmentTransaction.commit();
}
}
public void dates(View view)
{
if(frag != null)
{
home1.setBackgroundDrawable(getResources().getDrawable(R.drawable.home_icon));
subs1.setBackgroundDrawable(getResources().getDrawable(R.drawable.subscription_icon));
noti1.setBackgroundDrawable(getResources().getDrawable(R.drawable.noti_icon));
settings1.setBackgroundDrawable(getResources().getDrawable(R.drawable.setting_icon));
date1.setBackgroundDrawable(getResources().getDrawable(R.drawable.date_icon_black));
date.setTextColor(0xFF000000);
getSupportActionBar().setTitle("Available Dates");
frag = new AvailableDates();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.container1,frag);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
public void subscription(View view)
{
if (frag != null)
{
home1.setBackgroundDrawable(getResources().getDrawable(R.drawable.home_icon));
subs1.setBackgroundDrawable(getResources().getDrawable(R.drawable.subscription_icon_black));
noti1.setBackgroundDrawable(getResources().getDrawable(R.drawable.noti_icon));
settings1.setBackgroundDrawable(getResources().getDrawable(R.drawable.setting_icon));
date1.setBackgroundDrawable(getResources().getDrawable(R.drawable.date_icon));
subs.setTextColor(0xFF000000);
getSupportActionBar().setTitle("Subscriptions");
frag = new Subscription();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.container1,frag);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
public void noti(View view)
{
getSupportActionBar().setTitle("Notifications");
if (frag != null)
{
home1.setBackgroundDrawable(getResources().getDrawable(R.drawable.home_icon));
subs1.setBackgroundDrawable(getResources().getDrawable(R.drawable.subscription_icon));
noti1.setBackgroundDrawable(getResources().getDrawable(R.drawable.noti_icon_black));
settings1.setBackgroundDrawable(getResources().getDrawable(R.drawable.setting_icon));
noti.setTextColor(0xFF000000);
date1.setBackgroundDrawable(getResources().getDrawable(R.drawable.date_icon));
frag = new Notification();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.container1,frag);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
public void settings(View view)
{
getSupportActionBar().setTitle("Settings");
if (frag != null)
{
set.setTextColor(0xFF000000);
home1.setBackgroundDrawable(getResources().getDrawable(R.drawable.home_icon));
subs1.setBackgroundDrawable(getResources().getDrawable(R.drawable.subscription_icon));
noti1.setBackgroundDrawable(getResources().getDrawable(R.drawable.noti_icon));
settings1.setBackgroundDrawable(getResources().getDrawable(R.drawable.setting_ion_black));
date1.setBackgroundDrawable(getResources().getDrawable(R.drawable.date_icon));
frag = new Settings();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.container1,frag);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
public void signout(View view)
{
Intent intent = new Intent(this, Login.class);
startActivity(intent);
}
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0 ){
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
}
Activity_dashboard.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_dashboard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
tools:context="com.example.sheikhspc.design.dashboard">
<include layout="#layout/toolbar"
android:id="#+id/mytoolbar"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background"
android:layout_below="#id/mytoolbar">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/headrl"
android:paddingTop="5dp"
android:paddingRight="20dp"
android:paddingLeft="20dp"
android:paddingBottom="5dp"
android:background="#color/white"
tools:ignore="NotSibling">
<!-- <Button
android:text=""
android:background="#drawable/subscription_icon_black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/notificationbtn"
android:onClick="subscription"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/adbtn"
android:layout_toEndOf="#+id/adbtn"
android:layout_marginTop="7dp" />-->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/home_icon_black"
android:onClick="dbfragment"
android:id="#+id/home1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home"
android:layout_below="#+id/home1"
android:id="#+id/hometv"
android:textSize="10dp"
android:layout_marginTop="2dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/date_icon"
android:onClick="dates"
android:id="#+id/date1"
android:layout_marginLeft="30dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/home1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dates"
android:layout_below="#+id/date1"
android:textSize="10dp"
android:id="#+id/datestv"
android:layout_marginLeft="30dp"
android:layout_toRightOf="#+id/home1"
android:layout_marginTop="0dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/noti_icon"
android:onClick="noti"
android:id="#+id/noti1"
android:layout_marginLeft="35dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/date1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notifications"
android:layout_below="#+id/noti1"
android:textSize="10dp"
android:id="#+id/notitv"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/date1"
android:layout_marginTop="0dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/subscription_icon"
android:onClick="subscription"
android:id="#+id/subs1"
android:layout_marginLeft="35dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/noti1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subscription"
android:layout_below="#+id/noti1"
android:textSize="10dp"
android:layout_marginLeft="25dp"
android:id="#+id/substv"
android:layout_toRightOf="#+id/noti1"
android:layout_marginTop="0dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/setting_icon"
android:onClick="settings"
android:id="#+id/settings1"
android:layout_marginLeft="35dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/subs1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Settings"
android:layout_below="#+id/noti1"
android:textSize="10dp"
android:id="#+id/settingstv"
android:layout_marginLeft="30dp"
android:layout_toRightOf="#+id/subs1"
android:layout_marginTop="0dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/logout_icon"
android:onClick="signout"
android:id="#+id/signout1"
android:layout_marginLeft="35dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/settings1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Logout"
android:layout_below="#+id/noti1"
android:textSize="10dp"
android:layout_marginLeft="30dp"
android:layout_toRightOf="#+id/settings1"
android:layout_marginTop="0dp"/>
<!--<Button
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/adbtn"
android:onClick="dates"
android:layout_marginTop="7dp"
android:layout_marginLeft="10dp"
android:background="#drawable/date_icon_black"
android:layout_toRightOf="#+id/dbbtn" />
<Button
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/dbbtn"
android:layout_marginTop="7dp"
android:background="#drawable/home_icon_black"
android:onClick="dbfragment"
android:layout_marginLeft="10dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />-->
</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/headrl"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/Black"
android:id="#+id/container1">
</RelativeLayout>
</ScrollView>
</RelativeLayout>
You can implement the FragmentManager.OnBackStackChangedListener and call your methods there depending upon which fragment is currently in the back stack. As I understand, currently the methods are called via touch interactions like button clicks. You wouldn't be able to do this on backstack change and need to figure out an alternate way.
You need to implement OnBackStackChangedListener in the activity. Better do the following in onCreate in your dashboard.java:
fragmentManager = getFragmentManager();
fragmentManager.addOnBackStackChangedListener(this);
Then override onBackStackChanged in your dashboard.java like this:
#Override
public void onBackStackChanged() {
//get the current fragment being displayed
Fragment fragment = fragmentManager.findFragmentById(R.id.container1);
if (fragment instanceof AvailableDates) {
//do your stuff
} else if (fragment instanceof Subscription) {
//do other stuff
} //and so on for each fragment
}
You can make different implementations for your onClick methods like thus:
public void dates(View view)
{
if(frag != null)
{
openAvailableDates();
}
}
and then implement openAvailableDates thus:
void openAvailableDates() {
home1.setBackgroundDrawable(getResources().getDrawable(R.drawable.home_icon));
subs1.setBackgroundDrawable(getResources().getDrawable(R.drawable.subscription_icon));
noti1.setBackgroundDrawable(getResources().getDrawable(R.drawable.noti_icon));
settings1.setBackgroundDrawable(getResources().getDrawable(R.drawable.setting_icon));
date1.setBackgroundDrawable(getResources().getDrawable(R.drawable.date_icon_black));
date.setTextColor(0xFF000000);
getSupportActionBar().setTitle("Available Dates");
frag = new AvailableDates();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.container1,frag);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
and so on for your other methods and then call these new methods from onBackStackChanged.
One more tip. It's not good practice to name your classes starting with a lowercase letter. It's best to refactor dashboard.java to Dashboard.java.
Let me know if you need more help.

How to handle back-stack for Map fragment used in Fragment - Android

I have a Map fragment used in my layout, and the layout is inflated to the class extending Fragment.
So I have 2 fragment now
The Layout
The Mapfragment
On the App launch I launch this fragment, then on Button click I launch another Fragment.
public void launchFragment(Fragment fragment, String tag)
{
// TODO Auto-generated method stub
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
fragmentTransaction.replace(R.id.container, fragment, tag);
fragmentTransaction.addToBackStack(tag);
fragmentTransaction.commit();
}
But on Back press Map fragment crashes.
I have included this code on back press
#Override
public void onBackPressed()
{
// TODO Auto-generated method stub
FragmentManager fm = getSupportFragmentManager();
if (fm.getBackStackEntryCount() > 0)
{
Log.i("MainActivity", "popping backstack");
fm.popBackStack();
}
else
{
Log.i("MainActivity", "nothing on backstack, calling super");
super.onBackPressed();
}
}
Please suggest how to I cope up with this.
I Have attached the screen shot of my full logcat
My 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"
tools:context="com.winjit.fudtag.BaseAct$PlaceholderFragment" >
<Button
android:id="#+id/xbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:background="#drawable/button_bg_orange"
android:text="#string/strTag"
android:textColor="#android:color/white"
android:textStyle="bold" />
<Button
android:id="#+id/xbtnSearchMyJunctn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/xbtnTag"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:background="#drawable/button_bg_green"
android:text="#string/strSearchJunctn"
android:textColor="#android:color/white"
android:textStyle="bold" />
<fragment
android:id="#+id/xmapMyloc"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/xbtnSearchMyJunctn"
android:layout_marginTop="30dp" />
</RelativeLayout>

inflating view not working corrrectly

I have used futuresimple's FloatingActionMenu. https://github.com/futuresimple/android-floating-action-button
<com.getbase.floatingactionbutton.FloatingActionsMenu
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="#+id/floatingbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layoutDirection="rtl"
android:layout_alignParentBottom="true"
fab:fab_plusIconColor="#color/white"
fab:fab_colorNormal="#0277bd"
fab:fab_colorPressed="#color/pink"/>
Activity code :
add_item_button = (FloatingActionsMenu)findViewById(R.id.floatingbutton);
view = View.inflate(this, R.layout.view_1, null);
add_item_button.addView(view);
add_item_button.canResolveLayoutDirection();
add_item_button.callOnClick();
task = (TextView)view.findViewById(R.id.reminder);
note = (TextView)view.findViewById(R.id.notee);
bday = (TextView)view.findViewById(R.id.bday);
task.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
view.clearFocus();
add_item_button.collapse();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft;
add_task_frag aTF = new add_task_frag();
ft = fm.beginTransaction();
ft.setCustomAnimations(R.anim.slide_up, R.anim.slide_down, R.anim.slide_up, R.anim.slide_down);
ft.addToBackStack("aTF").replace(R.id.MainContent, aTF, "aTF");
ft.commit();
}
});
note.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
view.clearFocus();
add_item_button.collapse();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft;
add_note_frag aNF = new add_note_frag();
ft = fm.beginTransaction();
ft.setCustomAnimations(R.anim.slide_up, R.anim.slide_down, R.anim.slide_up, R.anim.slide_down);
ft.addToBackStack("aNF").replace(R.id.MainContent, aNF, "aNF");
ft.commit();
}
Now, when I click in the floating button, one of the textviews (task, note and bday) gets clicked instead of the button. Any idea whats going on here?
The View i am inflating :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:id="#+id/reminder"
android:padding="5dp"
android:layout_width="wrap_content"
android:textColor="#color/background_floating_material_light"
android:layout_height="wrap_content"
android:text="Add Task"
android:textSize="18dp"
android:background="#drawable/blue_rectangle"/>
<TextView
android:id="#+id/notee"
android:layout_marginTop="10dp"
android:layout_below="#+id/reminder"
android:padding="5dp"
android:layout_width="wrap_content"
android:textColor="#color/background_floating_material_light"
android:layout_height="wrap_content"
android:text="Add Note"
android:textSize="18dp"
android:background="#drawable/blue_rectangle"/>
<TextView
android:layout_marginTop="10dp"
android:layout_below="#+id/notee"
android:id="#+id/bday"
android:padding="5dp"
android:layout_width="wrap_content"
android:textColor="#color/background_floating_material_light"
android:layout_height="wrap_content"
android:text="Add B'day"
android:textSize="18dp"
android:background="#drawable/blue_rectangle"/>
</RelativeLayout>

move from ListFragment to Fragment

I want to move from my Custom ListView(which is a ListFragment) to another Fragment by onClick() of a Item. I want to also pass some data which I do by
Fragment f = null;
f = new DescriptionFragment();// it extends Fragment
Bundle args = new Bundle();
args.putString("title", "This is title");
args.putString("desc", "This is Description");
f.setArguments(args);
But Nothing happens using this code.
I tried at my level but now I think that I should use FragmentManager & FragmentTransaction. I tried to implement it by writing
FragmentManager fragmentManager;
android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(f);
fragmentTransaction.commit();
fragmentManager.executePendingTransactions();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(containerViewId, fragment);// this line gives error & i dont know what to write here
fragmentTransaction.commit();
I think my destination class is fine. It looks like this
public class DescriptionFragment extends Fragment {
public DescriptionFragment(){}
TextView title,desc;
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.description,container,false);
Bundle bundle = this.getArguments();
String stitle = bundle.getString("title");
String sdesc = bundle.getString("desc");
TextView title = (TextView)rootView.findViewById(R.id.textView4);
TextView desc = (TextView)rootView.findViewById(R.id.textView3);
title.setText(stitle);
desc.setText(sdesc);
return rootView;
}
}
Does In description.xml do I have to implement FrameLayout??
However description.xml looks like this
<?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"
>
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#drawable/flagc">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="80dp"
android:gravity="center"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/ashokb" >
<ScrollView
android:id="#+id/scrollView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
</ScrollView>
</RelativeLayout>
</LinearLayout>
I hope I made my problem clear
Any help?
Create a Frame Layout in your activity.
Create 2 different Fragment Layouts for each fragment.
When a fragment is called use
transaction.replace(frameLayoutId, fragmentToReplace);
transaction.commit()
Here is a sample for a fragment transition that I am using in my App
public void changeTab(View view) {
int id = view.getId();
if (modelSelected) {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
InfoFragment info = new InfoFragment();
SettingsFragment settings = new SettingsFragment();
ReportFragment reports = new ReportFragment();
switch (id) {
case R.id.infoTab:
transaction.replace(R.id.loadTabFrag, info);
break;
case R.id.settingsTab:
transaction.replace(R.id.loadTabFrag, settings);
break;
case R.id.reportsTab:
transaction.replace(R.id.loadTabFrag, reports);
break;
default:
break;
}
transaction.commit();
estheticTabHandler(id);
} else {
createSelectModelToast();
}
}
If you want to pass data from one fragment to another you should use a Bundle. In that case you should create a fragment and set its arguments.
Example:
MyFragment myFrag = new MyFragment();
Bundle myBundle = new Bundle();
myBundle.putWhateverData(data); // Where whatever data is the object that you are passing
myFragment.setArguments(myBundle);
And them inside your MyFragment onCreateView() you can use:
Bundle args = this.getArguments();
fragmentTransaction.add(containerViewId, fragment);
in above line where you declare fragment object so it gives error. you remove f fragment but no any valid fragment give so it gives you error.just declare your fragment same as f fragment. thats it...

Fragment UI components null TextViews

New android developer here. I am trying to create a dynamic UI that loads based on the users selection of a RadioGroup. Based on their selection, one of 3 possible fragments will be loaded into a LinearLayout section. This is my first attempt at my own sample problem that is not just a walk-through tutorial. Here is the main activity:
public class BaseConverter extends Activity {
RadioGroup convert;
Fragment toFragment;
RadioGroup toRadioGroup = null;
TextView inputDisplay = null;
TextView outputDisplay = null;
TextView resultTitle = null;
#Override
public void onCreate(Bundle sIS) {
super.onCreate(sIS);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.setContentView(R.layout.base_converter);
convert = (RadioGroup) this.findViewById(R.id.bc_convert_group);
convert.setOnCheckedChangeListener(new ConvertListener());
FragmentManager fm = getFragmentManager();
FragmentTransaction converterFragment = fm.beginTransaction();
ConvertEmptyFragment emptyTo = new ConvertEmptyFragment();
converterFragment.replace(R.id.bc_converter_fragment, emptyTo);
converterFragment.commit();
FragmentTransaction toFragment = fm.beginTransaction();
ConvertEmptyFragment emptyConverter = new ConvertEmptyFragment();
toFragment.replace(R.id.bc_to_fragment, emptyConverter);
toFragment.commit();
}
#Override
public void onResume() {
convert.clearCheck();
super.onResume();
}
#Override
public void onPause() {
convert.clearCheck();
super.onPause();
}
// I put a little null check so you can see how I'm trying to access the TextViews and what results
public void updateUIComponents(){
View converterView = this.findViewById(R.id.bc_converter_fragment);
inputDisplay = (TextView)converterView.findViewById(R.id.bc_display_input);
outputDisplay = (TextView)converterView.findViewById(R.id.bc_display_output);
if (inputDisplay == null){
Log.d("BaseConverter", "inputDisplay == null");
} else {
Log.d("BaseConverter", "inputDisplay != null");
}
}
class ConvertListener implements OnCheckedChangeListener {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
Fragment toFragment;
Fragment converterFragment;
switch (checkedId) {
case R.id.bc_convert_binary:
toFragment = new ConvertRBFragmentBinary();
converterFragment = new ConverterFragmentBinary();
break;
case R.id.bc_convert_decimal:
toFragment = new ConvertRBFragmentDecimal();
converterFragment = new ConverterFragmentDecimal();
break;
case R.id.bc_convert_hex:
toFragment = new ConvertRBFragmentHex();
converterFragment = new ConverterFragmentHex();
break;
default:
toFragment = new ConvertEmptyFragment();
converterFragment = new ConvertEmptyFragment();
break;
}
FragmentManager fm = getFragmentManager();
FragmentTransaction converterTransaction = fm.beginTransaction();
converterTransaction.replace(R.id.bc_converter_fragment, converterFragment);
converterTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
converterTransaction.commit();
FragmentTransaction toTransaction = fm.beginTransaction();
toTransaction.replace(R.id.bc_to_fragment, toFragment);
toTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
toTransaction.commit();
updateUIComponents();
}
}
So, based on what a user chooses, the proper fragments will be loaded into the respective LinearLayout sections. However, now I want to implement the business logic of the fragments (which is just integer base conversion; i.e. binary number to decimal...) but when I try to access the TextViews, as seen in the updateUIComponents method, I get null pointers. What am I missing?
Here's the ConverterFragmentBinary class for reference:
public class ConverterFragmentBinary extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sIS){
View v = inflater.inflate(R.layout.converter_fragment_binary, container, false);
return v;
}
}
and its respective xml layout for reference:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF000000"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="#+id/bc_binary_converter_logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dip"
android:maxHeight="30dip"
android:src="#drawable/binary_converter" />
<TextView
android:id="#+id/bc_display_input"
style="#style/input_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:layout_marginLeft="15dip"
android:layout_marginRight="15dip"
android:layout_marginTop="5dip"
android:gravity="center_vertical|right"
android:lines="1"
android:minHeight="30sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF000000"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="#+id/button_num_0"
style="#style/op_button_land"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:gravity="center"
android:onClick="num0"
android:text="#string/num_0" />
<Button
android:id="#+id/button_num_1"
style="#style/op_button_land"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:gravity="center"
android:onClick="num1"
android:text="#string/num_1" />
</LinearLayout>
<TextView
android:id="#+id/bc_result_title"
style="#style/radio_button_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_marginTop="10dip"
android:gravity="left"
android:text="#string/choose_convert" />
<TextView
android:id="#+id/bc_display_output"
style="#style/display_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginRight="15dip"
android:layout_marginTop="5dip"
android:gravity="center_vertical|right"
android:lines="1"
android:minHeight="30sp" />
</LinearLayout>
and then heres the main activity it gets loaded into:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/base_conversion_layout"
style="#style/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="5"
android:baselineAligned="false"
android:gravity="center_vertical|left"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical" >
<TextView
style="#style/radio_button_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/convert" />
<RadioGroup
android:id="#+id/bc_convert_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="5dip" >
<RadioButton
android:id="#+id/bc_convert_binary"
style="#style/radio_button"
android:text="#string/binary" />
<RadioButton
android:id="#+id/bc_convert_decimal"
style="#style/radio_button"
android:text="#string/decimal" />
<RadioButton
android:id="#+id/bc_convert_hex"
style="#style/radio_button"
android:text="#string/hex" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:id="#+id/bc_to_fragment"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/bc_converter_fragment"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="13"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
Thanks in advance and sorry for the long code blocks but I figured it was better to include more than less.
Also, you should inflate your Fragments layout to bring it from your XML to your Java code instead of simply referring it using findViewById() method.
So instead of doing this,
View converterView = this.findViewById(R.id.bc_converter_fragment);
Do this inside your onCreateView method of the fragment,
View converterView = infalter.inflate(R.id.bc_converter_fragment,null);
updateUIComponents(converterView);//call this methid and pass your view
new method looks like this,
public void updateUIComponents(View converterView){
inputDisplay = (TextView)converterView.findViewById(R.id.bc_display_input);
outputDisplay = (TextView)converterView.findViewById(R.id.bc_display_output);
if (inputDisplay == null){
Log.d("BaseConverter", "inputDisplay == null");
} else {
Log.d("BaseConverter", "inputDisplay != null");
}
}

Categories

Resources