Add views dynamically android - android

I am trying to add textviews dynamically to my activity and I am able to do so but the added textviews are getting added next to each other on the right side and what I want is to have each textview underneath each other.
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linearLayout);
for(int x = 0;x < 10; x++){
LinearLayout layout = new LinearLayout(this);
TextView tv = new TextView(this);
tv.setText("Text View "+x);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.addView(tv);
linearLayout.addView(layout);
}
}
Main XML:
<RelativeLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout"
android:orientation="horizontal"></LinearLayout>
</RelativeLayout>

You can do this way:
You have to set orientation vertical of your parent Linear Layout:
Your xml should looks like this:
<RelativeLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout"
android:orientation="vertical"></LinearLayout>
</RelativeLayout>
Your java code should looks like this:
for(int x = 0;x < 10; x++){
TextView tv = new TextView(this);
tv.setText("Text View "+x);
linearLayout.addView(tv);
}
Hope this will help you.

Instead of this:
layout.setOrientation(LinearLayout.HORIZONTAL);
Do this to your linear layout:
layout.setOrientation(LinearLayout.VERTICAL);

Related

How to display last textview in linear layout

Any idea to display the last textview inside a LinearLayout inside a ScrollView?
This is my code
<?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"
tools:context="com.example.qianonnphoon.tradeal.chat.ChatActivity"
android:background="#ffffff"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="#+id/scrollView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/layout1">
</LinearLayout>
</ScrollView>
<include
layout="#layout/message_area"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:layout_marginTop="5dp"/>
</LinearLayout>
TextView will be added to layout1 when receive the data. So I will have multiple textviews in layout1 once finish loading. However, it display the first TextView then I need to scroll down to see the last TextView. Any idea to display the last TextView when it finish loading?
This is the code how I add TextView when receive data
TextView textView = new TextView(ChatActivity.this);
textView.setText(message);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 0, 0, 10);
textView.setLayoutParams(lp);
if(type == 1) {
textView.setBackgroundResource(R.drawable.rounded_corner1);
}
else{
textView.setBackgroundResource(R.drawable.rounded_corner2);
}
layout.addView(textView);
scrollView.fullScroll(View.FOCUS_DOWN);
This solution worked for me
final ScrollView scrollview = ((ScrollView) findViewById(R.id.scrollview));
scrollview.post(new Runnable() {
#Override
public void run() {
scrollview.fullScroll(ScrollView.FOCUS_DOWN);
}
});

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

setBackground null pointer exception

im trying to set background to my activity but it throws nullpointer exception everytime
here is my code
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
RelativeLayout layout = (RelativeLayout) findViewById(R.layout.activity_main);
layout.setBackground(getResources().getDrawable(R.drawable.welcome_2));
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
layout.setBackgroundDrawable( getResources().getDrawable(R.drawable.welcome_1) );
} else {
layout.setBackground(getResources().getDrawable(R.drawable.welcome));
}
setContentView(R.layout.activity_main);
and this is my layout
<RelativeLayout 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" >
move
setContentView(R.layout.activity_main);
before
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
and defined relativelayout Id in XML
<RelativeLayout 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:id="#+id/layout"
tools:context=".MainActivity" >
Firstly Give id to Layout in Xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/layout"
android:layout_height="match_parent"
tools:context=".MainActivity" >
Then Inside java Code Use like this
setContentView(R.layout.activity_main);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
layout.setBackground(R.drawable.welcome_2);

Android: Put two elements in the center of relativeLayout programmatically

I add two elements in the view for code. An imageView and a Spinning Wheel. The two elements shown but in the same place. And I desire to put the ImageView above the other element, in the center of screen.
The code that add the views:
RelativeLayout container= (RelativeLayout)findViewById(R.id.container);
RelativeLayout.LayoutParams position = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
position.addRule(RelativeLayout.CENTER_IN_PARENT);
imgCenter.setLayoutParams(position);
container.addView(imgCenter);
RelativeLayout.LayoutParams position2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
position2.addRule(RelativeLayout.CENTER_IN_PARENT);
position2.addRule(RelativeLayout.BELOW,imgCenter.getId());
spinner.setLayoutParams(position2);
container.addView(spinner);
The xml of relativelayout:
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.app.exemple.LoadingActivity"
android:id="#+id/container" >
Remove center in parent in second child
RelativeLayout container= (RelativeLayout)findViewById(R.id.container);
RelativeLayout.LayoutParams position = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
position.addRule(RelativeLayout.CENTER_IN_PARENT);
imgCenter.setLayoutParams(position);
container.addView(imgCenter);
RelativeLayout.LayoutParams position2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
position2.addRule(RelativeLayout.BELOW,imgCenter.getId());
spinner.setLayoutParams(position2);
container.addView(spinner);
Thanks to #HareshChhelana I found the solution.
I change the xml as follow:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.app.exemple.LoadingActivity"
android:id="#+id/container"
android:orientation="vertical"
android:gravity="center" >
And the code:
LinearLayout container= (LinearLayout)findViewById(R.id.container);
ProgressBar spinner = new ProgressBar(this,null,android.R.attr.progressBarStyleLarge);
container.addView(imgCenter);
container.addView(spinner);

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>

Categories

Resources