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);
}
});
Related
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);
}
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);
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);
I want to inflate a view with only an image to be above my main layout center screen, so far I only am able to add an inflated view as the first child of my main layout. How can I inflate an imageview to be above my other views and be in the center of the screen?
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.image_preview,(ViewGroup)findViewById(R.id.previewHolder));
((LinearLayout) (LinearLayout)findViewById(R.id.fullScreen)).addView(layout);//add inflated to main view
Main 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:focusable="true"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
>
<LinearLayout
android:id="#+id/fullScreen"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
<TableLayout
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:focusableInTouchMode="true"
android:stretchColumns="*" >
........................
</TableLayout>
</LinearLayout>
Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testing_cam);
badge.setOnClickListener(btnListener);
}
private OnClickListener btnListener = new OnClickListener() {
#Override
public void onClick(View v) {
//SELECTS BUTTON ACTIONS
switch(v.getId()){
case R.id.status:
finish();
break;
default:
previewImages(v.getId());
break;
}
}
};
//CARRY OUT IMAGE FUNCTIONS
public void previewImages(int index){
try{
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.image_preview,(ViewGroup)findViewById(R.id.previewHolder));
((LinearLayout) (LinearLayout)findViewById(R.id.fullScreen)).addView(layout);
ImageView iv = (ImageView)findViewById(R.id.previewImage);
BitmapDrawable drawable = (BitmapDrawable) ((ImageView) showCase.getChildAt(index)).getDrawable();
Bitmap bitmap = drawable.getBitmap();
iv.setImageBitmap(bitmap);
((LinearLayout) (LinearLayout)findViewById(R.id.fullScreen)).bringToFront();
}catch(Exception e){ Log.i("ERROR FLATE", e.toString()); }
return;
}
View being inflated
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:id="#+id/previewImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
If you want to add the preview above fullScreen and root you should modify your parent layout.
LinearLayout arrange its children vertically, while in this case you need to use a FrameLayout or a RelativeLayoutinstead.
Something 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:focusable="true">
<!-- your previous content -->
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<LinearLayout
android:id="#+id/fullScreen"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
<TableLayout
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:focusableInTouchMode="true"
android:stretchColumns="*" >
........................
</TableLayout>
</LinearLayout>
<!-- your preview -->
<ImageView
android:id="#+id/preview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone"/>
</RelativeLayout>
All you have to do on click is to change the visibility of preview ImageView to visible and set its source accordingly.
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>