How can I add several "instances" of a layout into another layout? - android

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);
}

Related

linear layout not scrolling horizontally when populated programatically

I am working on a layout where i have to populate textView programmaticaly inside a vertical linear layout . But some of my textView contents are too wide for my screen size and they are not being visible . This is my layout
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">
<HorizontalScrollView
android:layout_width="320px"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/diff_layout"
android:layout_width="320px"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
And here is the code where i am programatically populating the linear layout :-
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.diff_layout);
String[] splitLines = content.split("\n");
for(int i=0;i<splitLines.length;i++){
TextView textView = new TextView(this);
textView.setSingleLine(true);
textView.setLines(1);
//textView.setHorizontallyScrolling(true);
textView.setText(splitLines[i]);
//textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT ));
textView.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
/*textView.setMovementMethod(new ScrollingMovementMethod());
textView.setHorizontallyScrolling(true);*/
linearLayout.addView(textView);
}
Please tell me where am i doing things wrong
After much hit and trial , i came up with the right solution :-
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<HorizontalScrollView android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/diff_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</HorizontalScrollView>
</android.support.v4.widget.NestedScrollView>
and my java snippet :-
LinearLayout linearLayout = (LinearLayout)
findViewById(R.id.diff_layout);
String[] splitLines = content.split("\n");
for(int i=0;i<splitLines.length;i++){
TextView textView = new TextView(this);
textView.setText(splitLines[i]);
textView.setMaxLines(1);
textView.setMovementMethod(new ScrollingMovementMethod());
linearLayout.addView(textView);
}

Cannot find child elements of custom view after inflated

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);

Layout below Fragment not visible

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.

Custom view not shown in Horizontal scroll view Not working

When i try to add my custom view to horizontal scroll view it's not showing.
but same custom view is shown in other layout like Relative and Linear Layout.
layout=new LinearLayout(this);
layout.setBackgroundColor(Color.YELLOW);
layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT));
layout.setOrientation(LinearLayout.HORIZONTAL);
l = new PredicateLayout(this);
// l.sendtext(info);
for(int j=0;j<info.length;j++)
{
LinearLayout aa=new LinearLayout(this);
aa.setOrientation(LinearLayout.VERTICAL);
t = new TextView(this);
t.setText(info[j]);
t.setBackgroundColor(Color.GREEN);
t.setTextSize(i);
t.setWidth(screenwidth/2);
aa.addView(t);
l.addView(aa, new PredicateLayout.LayoutParams(0, 0));
}
layout.addView(l);
scroll.addView(layout);
My XML look like this
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroll"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="horizontal"
>
</HorizontalScrollView>
also tried like this
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroll"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="horizontal"
>
<com.test.app.PredicateLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="#+id/predicate_layout"
>
</com.omnion.bible.PredicateLayout>
</HorizontalScrollView>
code look like
test=(PredicateLayout)findViewById(R.id.predicate_layout);
for(int j=0;j<info.length;j++)
{
LinearLayout aa=new LinearLayout(this);
aa.setOrientation(LinearLayout.VERTICAL);
t = new TextView(this);
t.setText(info[j]);
t.setBackgroundColor(Color.GREEN);
t.setTextSize(i);
t.setWidth(screenwidth/2);
aa.addView(t);
test.addView(aa, new PredicateLayout.LayoutParams(0, 0));
}
Can any one help me.
Thank's in advance.
Put your custom layout like this
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroll"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="horizontal"
>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<com.test.app.PredicateLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="#+id/predicate_layout"
>
</com.omnion.bible.PredicateLayout>
</LinearLayout>
</HorizontalScrollView>

Changing relative layout programmatically

I've relative layout i.e. main.xml which I set as follows. But now I've to put view1 on view2 with width=200dp and height =100dp, so that view2 will be large one and view1 will be small one on it.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/MainPanel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/panel_bottom"
android:layout_gravity="center_horizontal" >
<com.proj.demo.view1
android:id="#+id/sheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignWithParentIfMissing="true"
android:layout_centerInParent="true"
android:layout_toLeftOf="#+id/panel_quick_buttons"
/>
<com.proj.demo.view2
android:id="#+id/sheet2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignWithParentIfMissing="true"
android:layout_centerInParent="true"
android:layout_toLeftOf="#+id/panel_quick_buttons"
/>
</RelativeLayout>
</RelativeLayout>
What do you want to achieve? If you just want to change width and heigh than:
RelativeLayout rl = (RelativeLayout) findViewById(R.id.yourId);
rl.getLayoutParams().height = 100;
rl.getLayoutParams().width = 100;

Categories

Resources