I am trying to inflate a custom view on activity load and then do something with the child elements of that view. Below I try to inflate the view and then get a reference to a TextView within the layout XML to update the text value. However in the below code, after the last line, actionBarTitle is null - when I would expect it to be a reference to a TextView. What am I doing wrong?
Activity onCreate() method
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_userseed);
LayoutInflater inflator = LayoutInflater.from(this);
actionBarView = inflator.inflate(R.layout.fragment_actionbar_viewseed, null);
TextView actionBarTitle = (TextView) actionBarView.findViewById(R.id.action_bar_title);
}
And the layout 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"
android:orientation="vertical" >
<TextView
android:id="#+id/actionbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp" />
<android.support.v4.view.ViewPager
android:id="#+id/actionbar_pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.PagerTitleStrip
android:id="#+id/actionbar_pagerstrip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</android.support.v4.view.ViewPager>
</LinearLayout>
Try:
TextView actionBarTitle = (TextView) actionBarView.findViewById(R.id.actionbar_title);
Related
I have a fragment and I want to use a ViewStub for some data.
The problem I have is once I have inflated the ViewStub from the fragments Java class, how can I reference in the fragments java class components inside the ViewStub?
For example I currently use when the component is in the inflated view of the fragment;
TextView txtAwayPenStat = (TextView) myResInfoView.findViewById(R.id.txtAwayPenStat);
This will not work if the txtAwayPenStat is moved to the ViewStub.
I have tried a couple of approaches;
ViewStub viewStub = (ViewStub) getActivity().findViewById(R.id.info_detail_stub);
View inflatedView = viewStub.inflate();
Where getActivity() is I have also tried getView().
You could do like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewStub stub = (ViewStub) findViewById(R.id.stub);
View inflated = stub.inflate();
TextView txtAwayPenStat = (TextView) inflated.findViewById(R.id.txtAwayPenStat);
txtAwayPenStat.setText("gdgad");
}
The activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<ViewStub
android:id="#+id/stub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="#layout/mysubtree"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</android.support.constraint.ConstraintLayout>
mysubtree.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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=".MainActivity"
>
<TextView
android:id="#+id/txtAwayPenStat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="HELLO WORLD"
></TextView>
</android.support.constraint.ConstraintLayout>
The android:layout attribute in ViewStub tag is a reference to the View that will be inflated next to a call of inflate().
I am trying to have dynamic number of cardviews in my layout.For test i am trying to insert 3 cardviews in my layout,but instead of three only one card is added with "hello 3" written on it.
Here's my code for activity.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int i;
LinearLayout layout=(LinearLayout)findViewById(R.id.mainl);
for(i=1;i<=3;i++)
{
setContentView(R.layout.card);
LayoutInflater layoutInflater=(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView=layoutInflater.inflate(R.layout.card,null);
TextView t=(TextView)findViewById(R.id.text1);
t.setText("hello "+String.valueOf(i));
layout.addView(addView);
}
}
}
`
code for cardlayout
<android.support.v7.widget.CardView
android:id="#+id/cardv"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_height="50dp"
android:layout_width="match_parent"
android:layout_margin="10dp"
card_view:cardCornerRadius="10dp">
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:text="Hello"
android:layout_gravity="center"/>
</android.support.v7.widget.CardView>
For contentmain
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/activity_main"
android:orientation="vertical"
android:id="#+id/mainl">
</LinearLayout>
Because in loop cycle, you set content view each loop.
Please just remove this line setContentView(R.layout.card);
So your onCreate() method will look like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int i;
LinearLayout layout=(LinearLayout)findViewById(R.id.mainl);
for(i=1;i<=3;i++)
{
LayoutInflater layoutInflater=(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView=layoutInflater.inflate(R.layout.card,null);
TextView t=(TextView) addView.findViewById(R.id.text1);
t.setText("hello "+String.valueOf(i));
layout.addView(addView);
}
}
I'm trying to inflate a layout in another layout but it doesn't work...
In my activity I show activity_serie.xml which is black, and then I try to inflate on image_item.xml which is white. But I never see image_item.xml.
public class Serie extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_serie);
ViewPager mainLayout = (ViewPager) findViewById(R.id.serie_pager);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View menuLayout = inflater.inflate(R.layout.image_item, mainLayout, true);
}
#Override
protected void onResume() {
super.onResume();
}}
The activity_serie.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"
android:background="#000000
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/serie_pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
The image_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#FFFFFF"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
Can someone tell me why?
Thanks.
findViewById(R.layout.activity_serie)
You should specify a view id here, not a layout name.
Why you wanna do that? Just put both views into one xml, and set android:visibility="gone" for one element. Than you can address layout by id and change visibility as much as you want.
I have the follwing layout:
<LinearLayout 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:orientation="vertical" >
<View
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:id="#+id/bottomNav"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username: " />
</LinearLayout>
Programatically I'm replacing the view "content" with my Fragment:
View view = inflater.inflate(R.layout.kwiz_game, container, false);
contentView = view.findViewById(R.id.content);
currentFragment = new KwizCardBackFragment(android.R.drawable.btn_plus);
getFragmentManager().beginTransaction().replace(R.id.content,
currentFragment);
However the fragment is taking all the space and the Layout with the TextView is not shown.
I also tried to use RelativeLayouts with alignParentBottom=true and other stuff...
View of the Fragment which replaces the content:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_kwiz_item" >
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:scaleType="centerCrop" />
</RelativeLayout>
OnCreateView of the fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_card_back, container,
false);
ImageView imageView = (ImageView) view.findViewById(R.id.image);
if (drawable != null) {
imageView.setImageDrawable(drawable);
} else {
imageView.setBackgroundResource(drawableId);
}
return view;
}
After hours of debugging I finally found the mistake... Some of my layouts had views with the id "content". I thought when I look for R.id.content that the "nearest" element will be used... that's not the case!
Thanks anyone!
I modified your layout a little bit and this seems to achieve what you want:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_above="#+id/bottomNav" />
<LinearLayout
android:id="#+id/bottomNav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username: " />
</LinearLayout>
</RelativeLayout>
And a small side note, I see you used fill_parent in your layout, but that's deprecated since API level 8, so you should use match_parent instead :)
Hope this helps, cheers!
You should use FrameLayout as your fragment container, and put (and then replace) your fragments into that FrameLayout container.
I've a layout activity_main.xml which contains this:
<LinearLayout 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=".MainActivity" >
</LinearLayout>
And I've a layout, new_bucket.xml which contains this code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/newEntry"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/main"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="#253514"
android:clickable="true"
android:onClick="toggleAmagar"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text1"
android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout
android:id="#+id/child"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_below="#id/main"
android:background="#666666"
android:clickable="true"
android:onClick="toggleAmagar"
android:orientation="vertical">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text2" />
</LinearLayout>
</LinearLayout>
What I want to achieve, is to programatically add several "instances" of new_bucket.xml to the main layout in activity_main.xml. Those instances, should appear inside the LinearLayout of activity_main.xml.
I'm a bit stuck at the moment. I've tried to just add a TextView to activity_main.xml but I can't even handle that...
LinearLayout my_root = (LinearLayout) getLayoutInflater().inflate(R.layout.activity_main, null);
LinearLayout A = new LinearLayout(this);
A.setOrientation(LinearLayout.HORIZONTAL);
TextView tv = new TextView(this);
tv.setText("This text should appear somewhere");
A.addView(tv);
my_root.addView(A);
But that TextView is never shown.
Can you help me out?
You mean like this?
LinearLayout container = (LinearLayout)findViewById(R.id.container);
View child = getLayoutInflater().inflate(R.layout.new_bucket, container);
container.addView(child);
See documentation.
layout activity_main.xml
// i added an Id for LinearLayout
<LinearLayout 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=".MainActivity"
>
</LinearLayout>
This code will add your new_bucket to your main LinearLayout
LinearLayout my_root = (LinearLayout) findViewById(R.id.container, null);
LayoutInflater mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View bucket= (View) mInflater.inflate(R.layout.new_bucket, null);
my_root.addView(bucket);
Hope this work for you.
LinearLayout container = (LinearLayout)findViewById(R.id.container);
View child = getLayoutInflater().inflate(R.layout.new_bucket, container, false);
child.setId(0);
container.addView(child);
if u want to inflate multiple layout use for loop for this.
After so many help from so nice guys, I managed to do that.
I have not changed any Layout from the main question.
BUT java code is like this now:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout my_root = (LinearLayout) findViewById(R.id.container);
LayoutInflater mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View bucket= (View) mInflater.inflate(R.layout.new_bucket, null);
my_root.addView(bucket);
}