Close an inflated window layout - android

I've got a fragment activity that appears as a dialog window. I call to this activity this way:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/** Inflates the main layout to be used by this fragment*/
final View detailView =
inflater.inflate(R.layout.contact_detail_fragment, container, false);
In other part of the code, when I push a button of this layout, I need this activity to be closed, because it remains opened.
So, what will the way to finish this?

Related

java.lang.RuntimeException: Unable to bind views for fragment-different layout sizes

I'm trying to create my app support for different layouts (eg : layout-large-tvdpi).inside layout-large-tvdpi folder i have inserted tow layout (one layout for activity other for fragment).
when i run my app on Tablet activity layout showing fine.but when i move to fragment, app crashed and show me errorjava.lang.RuntimeException: Unable to bind views.it's show error on ButterKnife.bind(this,v)
This is my Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
ButterKnife.bind(this);
}
This is My Fragment
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v;
v = inflater.inflate(R.layout.fragment_home, container, false);
ButterKnife.bind(this,v);
return v;
}
Log Cat
Fragment layout
Activity Layout
Check if your #InjectViews has correct type. I've used ImageView instead of LinearLayout. That might be your problem too.

Fragment have 2 toolbars, its own and that of activity

I have an HomeAcivity with 4 fragments. 3 of the fragments have no Toolbar of their own and I am showing the Activity's Toolbar in them. However, the 4th Fragment have Toolbar of its own.
Currently I am seeing 2 Toolbars in 4th Fragment, one its own and other that of Activity.How can I hide the Activity's Toolbar and just show its own Toolbar?
Please note that I don't want to update the content of Activity's Toolbar instead of showing new Toolbar inside Fragment. I want to show Fragments own Toolbar and hide that of Activity.
Please help me out.
You can hide activity's toolbar from your 4th fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_view, container, false);
((ToolbarView)getActivity().findViewById(R.id.toolbarView)).setVisibility(View.GONE);
return rootView;
}
and show activity's toolbar from other three fragments
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_view, container, false);
((ToolbarView)getActivity().findViewById(R.id.toolbarView)).setVisibility(View.VISIBLE);
return rootView;
}

How to get & use Widget Element's Action in fragment android

In need of a good Ui design for my app with tab layout, i searched in google and i found one UI which is using View Pager and Action bar Tabs.
Link is:
http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/
i downloaded the template & imported to eclipse.i to tried use that template, don't know where to add my activity coding.
i uploaded one of the fragment below, and i tried to get action of a button. but "it shows create a method finViewById".
public class MoviesFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_movies, container, false);
Button bu=(Button) findViewById(R.id.button1);
return rootView;
}
i don't know much about fragment. I searched in google but i am not able get solution related to this. anyone pls can help me how and where to add my activity coding using above UI Template. Thanks in advance.
try this,
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_movies, container, false);
Button bu=(Button) rootView.findViewById(R.id.button1);
return rootView;
}
use this
Button bu=(Button)rootView.findViewById(R.id.button1);

Inflate view in a single fragment

The app I'm currently developing has an action bar with 5 tabs (fragments). One of these fragment shows an alert dialog, but the layout is blank. I want to put a background image, so I created a layout for that fragment and used inflater.inflate(...) method to set the layout.
Problem is that line of code sets that layout to ALL fragments. How can I limit it just to the fragment I need? Here's my code:
public class MyFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
//...
AlertDialog ad = new AlertDialog.Builder(getActivity()).setTitle("Title")
//...
ad.show();
inflater.inflate(R.layout.fragment_my, container); //this is the layout I want to inflate to my fragment
return super.onCreateView(inflater, container, savedInstanceState);
}
Also tried to replace the last two lines with:
return inflater.inflate(R.layout.fragment_trovami, container);
but i'm getting this error:
07-27 16:19:46.768: E/AndroidRuntime(1998): java.lang.IllegalStateException:
The specified child already has a parent. You must call removeView() on the child's parent first.
Answer :
return inflater.inflate(R.layout.fragment_trovami, container,false);

How to set data to layout inflated by a fragment

I have a FragmentActivity which sets a main layout with 3 tabs. Each tab has it's own fragment which inflates is own layout into main layout.
For example, one of the three Fragments:
public class Fragment1 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
return inflater.inflate(R.layout.fragment1, container, false);
}
}
When I add all the fragments and connect them with tabs everything works. When I click on tab1, layout of the Fragment1 shows, when I click on Tab2, layout of the Fragment2 shows.
The problem is that when I want to change something in that inflated layout (for example execute setText to textView in fragment1 layout), I receive a NullPointerException in that line and application stops.
This doesn't work:
TextView test = (TextView) findViewById(R.id.fragmentOneText1);
test.setText("Test text!");
How to set data to inflated layout?
You have to inflate your fragment specific layout first by using LayoutInflater and map the control in your Fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.R.layout.fragment1, container, false);
TextView text = (TextView) fragmentView.findViewById(R.id.fragmentOneText1);
mTextNoResult=(TextView)fragmentView.findViewById(R.id.text_noresult);
return fragmentView;
}

Categories

Resources