linear layout not scrolling horizontally when populated programatically - android

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

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

Add views dynamically 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);

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

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

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