Inflate layout in an activity with PreferenceFragment - android

My custom FragmentSettings() extends android.preference.PreferenceFragment and I simply add it in my activity.
I want to inflate (add) as well the layout.logo which has gravity:bottom. However with my code the inflated layout.logo is not visible. I guess because I use inflate() with getFragmentManager()? How can I correctly add a layout in this case?
public class ActivitySettings extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentSettings details = new FragmentSettings();
details.setArguments(getIntent().getExtras());
getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
getLayoutInflater().inflate(R.layout.logo, null );
}
}
PS: R.layout.logo is an ImageView.

Try this
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentSettings details = new FragmentSettings();
details.setArguments(getIntent().getExtras());
getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
LayoutInflater inflater = LayoutInflater.from(context);
View view =inflater.inflate(R.layout.logo, null);
// may be logo is a layout file, if not then put your layout file which contain an ImageView
ImageView imageView = (ImageView)view.findViewById(R.id.imageView1);
}
but you said that R.layout.logo in an ImageView . you know, ImageView has an id and you will find it from R.id.(your ImageView's id ) hear. just i assume that imageView1 is and id of your ImageView.

Related

Get reference to LinearLayout in fragment

In the XML file for my layout I added a fragment.
The layout for my fragment is a LinearLayout and it contains a text view.
I want to get a reference to the LinearLayout object in my fragment class, but despite trying everything I can think of or find online, it is always null.
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
text = (TextView) getActivity().findViewById(R.id.text);
layout = (LinearLayout) getActivity().findViewById(R.id.layout);
if(layout == null){
text.setText("null");
}
}
This code is in my fragment class and results in the TextView's text being set to "null" since the layout is null. How could this be since I am trying to get the references to the layout and the textview in the exact same way?
onCreate in activity class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
manager = getFragmentManager();
MyFragment = (MyFragment) manager.findFragmentById(R.id.fragment);
}
Method 2
I tried getting a reference to the LinearLayout in my activity. Here is the activity's onCreate with this attempt.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
manager = getFragmentManager();
MyFragment = (MyFragment) manager.findFragmentById(R.id.fragment);
LinearLayout ll = (LinearLayout) findViewById(R.id.layout);
TextView tv = (TextView) findViewById(R.id.text);
if(ll == null){
tv.setText("null");
}
}
This also changed the text to "null."
The problem is fixed in both cases by saying layout = (LinearLayout) getView() in the fragment class or layout = (LinearLayout) fragment.getView() in the activity. This doesn't answer why what I was doing didn't work (which could be helpful) but this solves the problem.

How to change the background image of a parent activity depending on what fragments are currently being displayed

I have two fragments, fragment_one (fragmentOne.class) and fragment_two (fragmentTwo.class). These fragments are being displayed inside ActivityMain.class.
When fragment_one is being displayed I want to set Image A as the background for ActivityMain.class.
When fragment_two is being displayed I want to set Image B as the background for ActivityMain.class
I can swap and set the background of ActivityMain.class when I am not using fragments (i use buttons as a test)....But, when I modify this for fragments I cannot get it to work.
Listed here you will see my ActivityMain.class
public class ActivityMain extends FragmentActivity {
//set variables for use of pageradapter and swipe screen
MyPageAdapter adapter;
ViewPager pager;
Context context = this;
LinearLayout swipeHomeScreen;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_activity_nav);
//Implement the adapater for the swipe screen on launch page and circle indicator
adapter = new MyPageAdapter(getSupportFragmentManager());
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
swipeHomeScreen = (LinearLayout) findViewById(R.id.swipeHomeScreen);
}
}
Here I have listed the code of one of my fragments where I am trying to share/pass the set the background image of ActivityMain.class...
public class ActivityNavSwipeTwo extends Fragment {
ActivityMain main;
public void onAttach(Activity activity) {
main = (ActivityMain) activity;
};
public static ActivityNavSwipeTwo newInstance() {
ActivityNavSwipeTwo fragment = new ActivityNavSwipeTwo();
return fragment;
}
public ActivityNavSwipeTwo() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_two, null);
main.swipeHomeScreen.setBackgroundResource(R.drawable.Image2);
return root;
}
}
My activity inflates the fragments fine, but the background image does not change.
How could I implement this for target api 23??
Thanks for your support.
Implement viewpager.onpagechangedlistener in your activity. Then add view.setonpageselecterlistener(this) in oncreated method. You can control selected fragment in implemented on pageselected method
Definitely you have any parent layout(Relative/Linear) for your activity. So you have to do following:
Create id of parent layout if not created.
access from fragment and change the background color of that parent layout.
Write this in Fragment's onCreateView() method.
relativeLayout = (RelativeLayout)getActivity().findViewById(R.id.relativelayout);
relativeLayout.setBackground(background);

searchview in android to search in textview

Is it possible to implement a searchview widget to search from textview. My textview looks like this
TextView TextView TextView
and I inflate these 3 textviews using layout inflater to show multiple values from a fragment.
Fragment Class
public class MainFragment extends Fragment {
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// set the layout and inflater
//perform some operations for button click
return view;
}
Activity class
public class MainActivity extends FragmentActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new MainFragment()).commit();
}
}
public boolean onOptionsItemSelected(MenuItem item) {
//do some operations with overflow menu
}
public ArrayList<Asset> getData() {
//perform operations so that data can be sorted to an array and then passed to textviews
}
I am confused as to where to include the searchview widget and how to make the textview searchable..
you can include the search view widget in on-create method and you can follows these links to get your code :)
please visist this : How to set edittext to show search button or enter button on keyboard?
and also visist this : Android: Edittext acting as search engine
i hope this will help you to find your answer thanks...:)

Referring to Views from different Fragments in same Activity

Let say the target application is built from 3 fragments which are all in the same activity public class MainActivity extends android.support.v4.app.FragmentActivity implements ActionBar.TabListener. Starting fragment is public class ButtonSectionFragment extends Fragment where there is a Button:
public class ButtonSectionFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.btn, container, false);
Button mybutton = (Button) rootView.findViewById(R.id.mybutton);
mybutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
????????????????????
}
});
}
There are ?? in the onClick method, I will get to that. And there is another fragment like this:
public static class TextSectionFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tv, container, false);
TextView textv = (TextView) rootView.findViewById(R.id.texty);
}
Both of the fragments are using different layouts so this is why rootView is being used right in front of findViewById.
The outcome I would like to achieve is: by Button from 1st fragment click set the TextView from 2nd fragment to Hello. What should I put in the ??? part to make it all work?
You need to use interface as callback to the activity then set the textview in fragment.
Two framgents should never communicate directly.
http://developer.android.com/training/basics/fragments/communicating.html.
Framgent1 -->Activity -->Fragment2.
You can comunicate value from fragment2 to activity first then from activity to fragment2. Then set text in fragment2
You just have to use getActivity().findViewById() instead of getView().findViewById()
final TextView tv = (TextView) getActivity().findViewById(R.id.texty);
if (tv != null)
tv.setText("Hello");
Fragments are just branches inside the common layout tree of activity. All fragment views of a common activity can be accessed through Activity.findViewByXXX(). The only complication is that fragments can be dynamically added, removed, replaced, etc. So you to be sure that the needed fragment is already inflated into the layout hierarchy. You can make initialization of the UI in onViewCreated() of the other fragment. That guarantees you the layout has been loaded already.
Fragment frag1 = new ButtonSectionFragment ();
Fragment frag2 = new TextSectionFragment();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(layout, frag1);
ft.add(layout, frag2);
ft.commit();
View frag1RootView = frag1.getView();
View frag2RootView = frag2.getView();
Button btn = (Button)frag1RootView.findViewById(id);
TextView tv = (TextView)frag2RootView.findViewById(id);
untested but... I think that would do it...
EDIT: You should get the root views onActivityCreated(); or it'll throw you a null...
In continuation to Raghunandan's answer, you could check similar implementation in the link.
update TextView in fragment A when clicking button in fragment B
Do not forget to get a reference of the textview of the TextSectionFragment into the MainActivity.

Dynamically Adding Information to Fragments

The issue I'm having is that I don't know how to get a pointer to a layout within a fragment. It's obvious that to get a layout pointer in Java you would do something like this:
LinearLayout llTemp = (LinearLayout) findViewById(R.id.llTemp)
Something along those lines.
Now what I'm doing is grabbing information from a server in the main class and load a fragment within the same class. I would like to populate the fragment with the information loaded from the outer class. Is there any way to do this? I would have just grabbed the layout from within the fragment and do it that way but I cannot make a reference to it as it's in the fragment.
I'm sure this is a common problem but I couldn't find anything on it specifically like this.
Thanks in advance,
Cheers,
Jake
To Answer the comment:
View view = inflater.inflate(R.layout.main_frag, container, false);
mainLayout = (LinearLayout) view.findViewById(R.id.ll_MainFrag);
return view;
This is what is in my onCreateView.
Okay, just to add how I'm instantiating the Fragment:
private int MAIN = 1;
FragmentManager fm = getSupportFragmentManager();
fragments[MAIN] = new MainFragment();
FragmentTransaction transaction = fm.beginTransaction();
transaction.commit();
getSupportFragmentManager().beginTransaction().add(R.id.flMain, fragments[MAIN]).commit();
From here I would like to be able to do something like:
fragments[MAIN].createTextView();
When creating a Fragment, create public methods to set data:
public class MyFragment extends Fragment {
private TextView text1;
private TextView text2;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = LayoutInflater.from(getActivity()).inflate(R.layout.simple_list_item_2,container,false);
text1 = (TextView) layout.findViewById(R.id.text1);
text2 = (TextView) layout.findViewById(R.id.text2);
return super.onCreateView(inflater, container, savedInstanceState);
}
public void setData(String t1, String t2){
text1.setText(t1);
text2.setText(t2);
}
}
When adding a fragment in parent activity, give it a unique tag:
MyFragment f = new MyFragment();
getFragmentManager().beginTransaction().add(f,"my_fragment").commit();
Later, you can search the fragment from parent activity and call some methods on it:
MyFragment frg = (MyFragment) getFragmentManager().findFragmentByTag("my_fragment");
if(frg != null){
frg.setData("abc","def");
}
Also, if fragment was added from a layout, you can find the fragment by its id.

Categories

Resources