I have a fragment layout to which i want to add another sub layout dynamically ,
I tried this https://stackoverflow.com/a/22592269/8476022
sub-layout xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:hint="Question"
android:textColor="#color/colorTextBlack"
android:textSize="16dp" />
</android.support.constraint.ConstraintLayout>
this is my main layout
<?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:background="#f2f2f2"
android:orientation="vertical">
<FrameLayout
android:id="#+id/flContainer"
android:layout_width="match_parent"
android:layout_margin="20dp"
android:layout_height="wrap_content"/>
</LinearLayout>
inside my fragment i included the sublayout
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_start_question, container, false);
flContainer = (FrameLayout) v.findViewById(R.id.flContainer);
flContainer.addView(R.layout.sub_layout);
return v;
}
but i am getting error near addView the screenshot is below
can anyone please help to find the error
You are getting Error because
addView(View child) need parameter a view and you are adding FrameLayout which is ViewGroup
Try this way
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View myView = inflater.inflate(R.layout.sub_layout, null);;
flContainer = (FrameLayout) v.findViewById(R.id.flContainer);
flContainer.addView(myView);
Add view takes View object. You are passing the layout Id.
addView(View view)
you do not pass the layout Id
flContainer.addView(R.layout.sub_layout);// Wrong
First inflate a view using inflater then add it to container.
View view=LayoutInflater.from(getContext()).inflate(R.layout.yourView,null);
yourContainer.addView(view);
You need to inflate layout to add the view refer this
View child = getLayoutInflater().inflate(R.layout.sub_layout, null);
flContainer = (FrameLayout) v.findViewById(R.id.flContainer);
flContainer.addView(child);
You want to use:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_start_question, container, false);
// flContainer = (FrameLayout) v.findViewById(R.id.flContainer);
// flContainer.addView(R.layout.sub_layout);
LinearLayout linPhoneNumber = (LinearLayout) v.findViewById(R.id.linPhoneNumber);
View viewPhoneNumber = getActivity().getLayoutInflater().inflate(R.layout.fragment_contact_phone, null);
LinearLayout linPhoneLayout = (LinearLayout) viewPhoneNumber.findViewById(R.id.linPhoneLayout);
TextView txtPhoneNumber = (TextView) viewPhoneNumber.findViewById(R.id.txtPhoneNumber);
txtPhoneNumber.setText(phone);
linPhoneNumber.addView(viewPhoneNumber);
return v;
}
put one linear layout in fragment_start_question.xml :
<LinearLayout
android:id="#+id/linPhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"></LinearLayout>
then fragment_contact_phone.xml
<LinearLayout
android:id="#+id/linPhoneLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/margin_top_10"
android:orientation="horizontal"
android:padding="#dimen/margin_top">
<ImageView
android:id="#+id/imgCall"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:src="#drawable/icon_call" />
<Textview
android:id="#+id/txtPhoneNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="#dimen/margin_top_1"
android:textColor="#color/menu_text_color"
android:textSize="#dimen/margin_top_1"/>
</LinearLayout>
Replace your onCreateView with the following,
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_start_question, container, false);
flContainer = (FrameLayout) v.findViewById(R.id.flContainer);
View subView = inflater.inflate(R.layout.sub_layout, null);
flContainer.addView(subView);
return v;
}
You are passing the layout id only,Define a layout first like:
LinearLayout sublayout = (LinearLayout)v.findViewBYId(R.layout.sub_layout)
flContainer.addView(sublayout);
Related
I'm creating a custom ListFragment for my Android application. I want to add a child to #id/listContainer. But I'm not able to get the reference to the view using findViewById(android.R.id.listContainer).
This is the default layout for ListFragment.
list_content.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:id="#+id/progressContainer"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:gravity="center">
<ProgressBar style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/loading"
android:paddingTop="4dip"
android:singleLine="true" />
</LinearLayout>
<FrameLayout android:id="#+id/listContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false" />
<TextView android:id="#+android:id/internalEmpty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
</FrameLayout>
</FrameLayout>
This is my custom ListFragment code.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(android.R.layout.list_content, container, false);
ProgressBar progressBar = new ProgressBar(getActivity());
progressBar.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER));
progressBar.setIndeterminate(true);
ViewGroup listContainer = (ViewGroup) view.findViewById(android.R.id.listContainer);
listContainer.addView(progressBar);
return view;
}
Android Studio says error: cannot find symbol variable listContainer.
What I am doing wrong?
Try getListView().getParent(). since getListView() gives you the list view widget,it's parent should be the frame layout that you need.
The problem is the way you are accessing resource:
ViewGroup listContainer = (ViewGroup) view.findViewById(android.R.id.listContainer);
listContainer.addView(progressBar);
change it to
ViewGroup listContainer = (ViewGroup) view.findViewById(com.android.internal.R.id.listContainer);
listContainer.addView(progressBar);
Since the listContainer is an internal resource, call it using
com.android.internal.R.id.RESOURCE_ID
Change this line :
ViewGroup listContainer =
(ViewGroup)view.findViewById(android.R.id.listContainer);
To
ViewGroup listContainer =
(ViewGroup)view.findViewById(R.id.listContainer);
Change View view = inflater.inflate(android.R.layout.list_content, container, false);
To
View view = inflater.inflate(R.layout.list_content, container, false);
Here R must be import import com.your_package_name.R;
Now you can change
ViewGroup listContainer = (ViewGroup) view.findViewById(android.R.id.listContainer);
To
ViewGroup listContainer = (ViewGroup) view.findViewById(R.id.listContainer);
I created a fragment inside the activity's layout file. For eg:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<fragment android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="2"
android:id="#+id/article_fragment"
android:name="com.user.domain.ArticleFragment"/>
</LinearLayout>
and inflate a view in ArticleFragment::onCreateView function:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
return inflater.inflate(R.layout.article_view, container, false);
}
ariticle_view layout xml looks as below:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/article"
android:textSize="18sp"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TextView>
Issue is here:
why I can only find the view through fragment's id (R.id.article_fragment) not view's id (R.id.article) after attached, and view instance's id also changed to fragment's id.
Can anybody help to explain the strange behavior?
Document for fragments api has such a description:
The system inserts the View returned by the fragment directly in place of the <fragment> element.
Does that the reason for this issue?
You have to find you Fragment's child Views like
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.article_view, container, false);
TextView yourText = v.findViewById(R.id.article);
return v;
}
I am trying to programmatically add. Here is the parent view.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal" >
</LinearLayout>
Here is the view to add.
<?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:layout_marginTop="50dp"
android:layout_centerHorizontal="true" >
<TextView
android:id="#+id/textview"
android:text="TEST"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
Here is the code.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.test, container, false);
LinearLayout linear = (LinearLayout) view.findViewById(R.id.test);
RelativeLayout layout;
ImageView image;
TextView text;
LayoutParams lp;
layout = (RelativeLayout) inflater.inflate(R.layout.test_row, null, false);
linear.addView(layout);
return view;
}
It adds the view, but it doesn't center and it also ignores the topmargin property. What am I doing wrong?
Answer: I replaced null when inflating my view with container, did the trick.
This seems to be a repeat,all you need to do is specify the parent view instead of added it later to the parent. Hope this helps. See
Layout params of loaded view are ignored
This is my Home.xml layout file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rellay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/placesgradient">
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="click"
/>
**<I want to include another layout here on a click of a button dynamically on click of a button >**
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_below="#id/btn2"
android:text="hello this is text..see above" />
</RelativeLayout>
This is my Home.java file
public class Home extends Fragment implements OnClickListener {
Button b1;
RelativeLayout r1;
View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.home, container, false);
b1 = (Button) rootView.findViewById(R.id.btn2);
b1.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View arg0) {
ViewGroup con = null;
r1 = (RelativeLayout) rootView.findViewById(R.id.rellay);
LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
r1.addView(layoutInflater.inflate(R.layout.test1, con , false));
}
}
When I click on the button it places the test1.xml layout file at the top of the home.xml layout. I need it to be between the button and the text. I checked this code on stack before but it doesn't seem to work with the FragmentActivity. Here 1 indicates the index or the second child in this case.
rl.addView(1, layoutInflater.inflate(R.layout.content_layout, this, false) );
How can I achieve it? Help!!
I would put some container view at the position you want your layout to be like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rellay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/placesgradient">
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="click"
/>
<FrameLayout
android:id="#+id/flContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_below="#id/btn2"
android:text="hello this is text..see above" />
</RelativeLayout>
Then you can retrieve the container in your Fragment like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.home, container, false);
// Retrieve your container
flContainer = rootView.findViewById(R.id.flContainer);
b1 = (Button) rootView.findViewById(R.id.btn2);
b1.setOnClickListener(this);
return rootView;
}
And later when you want to add your layout you can add a child to the FrameLayout like this:
flContainer.addView(subLayout);
If you later want to change the layout in your container you can do it like this:
flContainer.removeAllViews();
flContainer.addView(otherLayout);
Use LayoutParams.
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.btn2);
r1.addView(layoutInflater.inflate(R.layout.test1, con , false),params);
Or put the view in your layout and toggle visibility with View.setVisibility(View.GONE/View.VISIBLE)
I include a layout in my fragment1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include
android:id="#+id/family_header"
layout="#layout/header" />
header xml code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dip"
android:background="#33cc66"
android:orientation="horizontal" >
<ImageView
android:id="#+id/nav_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/nav_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical" />
<ImageView
android:id="#+id/nav_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
I want to change TextView "nav_text" value dynamically in my Fragment1.class . how to do it
you can create class that extends fragement....
public class DetailFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_rssitem_detail,
container, false);
return view;
}
public void setText(String item) {
TextView view = (TextView) getView().findViewById(R.id.detailsText);
view.setText(item);
}
}
in this class you can change value of textview dynemically.....please put your code here.so we can understand what is your actual problem.
Use this code in your fragment oncreateview method like this
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment1, container,false);
final LinearLayout headerLayout= (LinearLayout ) v.findViewById(R.id.family_header);
TextView txtHeaderText=(TextView)headerLayout.findViewById(R.id.nav_text);
txtHeaderText.setText("My New Text");
return v;
}
I hope it's work for you.