ScrollView not scrolling in android - android

i am trying to add images to my relative layout with an for loop but it isn't working.
I tried to remove the for loop and making one image longer and the scroll view seems to work the but when i try to add more than one image to the relative layou it adds it but i cant scroll through them.
Meaning that i can see that there are 5 images but i can only see the top half of it and cant scroll to see the bottom half.
Here is my xml that i use for the activity.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none" >
<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/MainRelativeLayout"
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="com.tutecentral.tvtimeschedule.app.MainActivity"
android:background="#87CABE"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</RelativeLayout>
</ScrollView>
</LinearLayout>
And here is the activity that i use to add the images
int amountOfChannels = 9;
int paddingTopOfImage = 0;
for(int i = 1; i <= amountOfChannels; i++) {
//ImageView Setup
ImageView imageView = new ImageView(this);
//setting image resource
imageView.setImageResource(R.drawable.channel_1);
//setting image position
imageView.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT));
imageView.getLayoutParams().height = 400;
imageView.getLayoutParams().width = 1000;
imageView.setPadding(10, 10, 10, 10);
imageView.setBackgroundColor(Color.WHITE);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setY(paddingTopOfImage);
//adding view to layout
RelativeLayout mainRelativeLayout = (RelativeLayout) findViewById(R.id.MainRelativeLayout);
mainRelativeLayout.addView(imageView);
paddingTopOfImage = paddingTopOfImage + 450;
}

I don't think you have to put ScrollView under LinearLayout, because ScrollView by default is LinearLayout. Try Removing the LinearLayout.

Related

Inflated images go out of screen

What should I do to place 4th and 5th image in the next line, here how it looks:
4th and 5th image go out of screen how to fix that?
MainActivity:
LayoutInflater l = getLayoutInflater();
LinearLayout ll = (LinearLayout) findViewById(R.id.main);
Integer odpowiedzi[] = {R.drawable.kwiaty1, R.drawable.kwiaty2, R.drawable.kwiaty3, R.drawable.kwiaty4, R.drawable.kwiaty5};
for (Integer odp : odpowiedzi) {
View v = l.inflate(R.layout.activ2, null);
ImageView b = (ImageView) v.findViewById(R.id.imageView6);
b.setImageResource(odp);
ll.addView(v);
activ2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/imageView6"/>
</LinearLayout>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/main">
</LinearLayout>
Make linear layout main layout vertically align
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/main">
</LinearLayout>
Now handle adding layout dynamically like this
get screen width
public static int getScreenWidth() {
return Resources.getSystem().getDisplayMetrics().widthPixels;
}
public static float convertDpToPixel(float dp, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
return px;
}
if screen width is more than 500dp add another horizontal aligned linear layout in main layout and add imageViews in it.
else if screen width is less than 500dp add first horizontal aligned linear layout in main layout and add imageViews in it. And then add another horizontal aligned linear layout in main layout and add remain imageViews in it
// Create new LinearLayout
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
// Add textviews
TextView textView1 = new TextView(this);
textView1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
textView1.setText("programmatically created TextView1");
textView1.setBackgroundColor(0xff66ff66); // hex color 0xAARRGGBB
textView1.setPadding(20, 20, 20, 20); // in pixels (left, top, right, bottom)
linearLayout.addView(textView1);
TextView textView2 = new TextView(this);
LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.RIGHT;
layoutParams.setMargins(10, 10, 10, 10); // (left, top, right, bottom)
textView2.setLayoutParams(layoutParams);
textView2.setText("programmatically created TextView2");
textView2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
textView2.setBackgroundColor(0xffffdbdb); // hex color 0xAARRGGBB
linearLayout.addView(textView2);
// Set context view
mainLinearLayout.addView(linearLayout);
Try change the width of the LinearLayout on activ2 to 0px and weight 1.
In the ImageView add android:adjustViewBounds="true" to fit parent.
In activ1 change the width to match_parent with some padding.
That way the images sizes will be even distributed and should match their parent.
Try with:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/imageView6"
android:adjustviewbounds="true"
android:layout_weight="1"/>
</LinearLayout>
And your activity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/main"
android:weightsum="NUMBER_OF_OBJECTS" >
</LinearLayout>
Hope it helps!

How can I set TextView into LinearLayout programmatically in Android?

I am creating a TextView dynamically. Here, I am creating a mainLayout in which I have two child layout and I wan gave them weight.
Here, is my Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:baselineAligned="false"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/Layout_second_overs"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="6"
android:orientation="vertical">
<!--Second textview overs-->
</LinearLayout>
<LinearLayout
android:id="#+id/Layout_second_balls"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal">
<!--Second textview balls--> //here i inflate 12 textview dynamically but not 12 seen on the device while i run the app.
</LinearLayout>
</LinearLayout>
</LinearLayout>
Code to create a 12 TextViews and put into layout.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
for(int a = 0; a <= 12; a++)
{
TextView first = new TextView(getApplicationContext());
first.setLayoutParams(params);
first.setTextColor(Color.parseColor("#000000"));
first.setTextSize(12);
first.setGravity(Gravity.LEFT);
first.setPadding(5, 0, 5, 0);
first.setText("12");
layout_second_balls.addView(first); //This is my linear layout which id is Layout_second_balls
}
Problem is my all data are print into log but not visible on the device.
Please, help me to solve out this problem.
There are two issues
One is for the Layout Design related
Second based on the layout design and property you also need to
handle is while you are adding dynamically view into linear layout
Here what i have changed in Your xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:baselineAligned="false"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/Layout_second_overs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--Second textview overs-->
</LinearLayout>
<LinearLayout
android:id="#+id/Layout_second_balls"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" android:background="#FF13C7FF"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
</LinearLayout>
In Layout_second_overs linearlayout height , Width & weight is the issue.
& in Layout_second_balls linear layout you have used weight as 1 so you also required layout_height as 0dp if orientation is horizontal
Now below things i have changed in code
LinearLayout.LayoutParams params = new
LinearLayout.LayoutParams
(0, LinearLayout.LayoutParams.WRAP_CONTENT);
for(int a = 0;a<=12;a++)
{
TextView first = new TextView(getActivity());
first.setLayoutParams(params);
first.setTextColor(Color.parseColor("#000000"));
first.setTextSize(12);
params.weight = 1; /// Need to give weight if you want equal size of textview inside Linear
first.setGravity(Gravity.LEFT);
first.setPadding(5, 0, 5, 0);
first.setText("12");
Layout_second_balls.addView(first); //This is my linear layout which id is Layout_second_balls
}
above things are based on the question which you ask
Suggestion
Avoid adding dynamic view like you asked . prefer to use ListView, GridView, RecyclerView for this type of functionality.
Your layout_second_overs requires all of your screen space - so your layout_second_balls is moved out of your screen.
If you want to see your two LinearLayouts beside each other, you need to set the layout_width to "0dp":
//pseudo, simply change width to 0dp
<LinearLayout
android:layout_height="40dp"
.... >
<LinearLayout
android:layout_width="0dp"
android:layout_weight="6"/>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"/>
</LinearLayout>
To replace this kind of loops you are supposed to use a RecyclerView. It is easy to use and responsive (since your list will be scrollable).
The things you have to remember while implementing a RecyclerView are to add an adapter and a LayoutManager to it.
For your example :
LayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, true);
YourAdapter adapter = new YourAdapter([...]);
RecyclerView recyclerView = findViewById(R.id.your_recyclerview_here);
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(adapter);
If you need any advices for creating your adapter just ask.
You can use this line of code.
LinearLayout layout_second_balls=(LinearLayout) findViewById(R.id.Layout_second_balls);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
for(int a = 0;a<=12;a++)
{
TextView first = new TextView(getApplicationContext());
first.setLayoutParams(params);
first.setTextColor(Color.parseColor("#000000"));
first.setTextSize(12);
first.setGravity(Gravity.LEFT);
first.setPadding(5, 0, 5, 0);
first.setText("12");
layout_second_balls.addView(first);
}
i run this code in my emulator and i got this out put.
You have to assign index for a view while performing addview(view,index) set index for each textview created in for loop like layout_second_balls.addView(first,index);
Code should look like :
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
for(int a = 0; a <= 12; a++)
{
TextView first = new TextView(getApplicationContext());
first.setLayoutParams(params);
first.setTextColor(Color.parseColor("#000000"));
first.setTextSize(12);
first.setGravity(Gravity.LEFT);
first.setPadding(5, 0, 5, 0);
first.setText("12");
layout_second_balls.addView(first,a);
}
Hope it will work.

Horizontal Scroll View(Image View)

I have a Horizonal scroll view with Images. Then the output is this.
I loaded 2 same pictures but the Gap is too much.
I just want it to make like 5Margins on left but didn't get the result.
Here's my Code.
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginBottom="1dp"
android:background="#FFF"
android:scrollbars="none"
>
<LinearLayout
android:id="#+id/imgLayout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
>
</LinearLayout>
</HorizontalScrollView>
And here is my code of Adding imageviews to linear layout.
public void setImageViews() {
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.imgLayout);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
parms.setMargins(5,5,5,5);
for(int i=0;i<6;i++){
ImageView imageView =new ImageView(this);
imageView.setLayoutParams(parms);
imageView.setImageResource(R.drawable.bsu);
linearLayout.addView(imageView);
}
}
The results i got.
I want it to be 5margins apart.

Determining button's random topMargin

I am trying to place a button in a random location in a relative layout.
As suggested in similar questions I set the leftMargin and topMargin of the button.
I think the calculation should be:
Button b = (Button) findViewById(R.id.randBtn);
LayoutParams params = (LayoutParams) b.getLayoutParams();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
params.leftMargin =
new Random().nextInt(metrics.widthPixels - activity_h_margin * 2 - b.getWidth());
params.topMargin =
new Random().nextInt(metrics.heightPixels - activity_v_margin * 2 - b.getHeight());
b.setLayoutParams(params);
For some reason this code does not work well in the vertical axis - sometimes the button gets cut at the bottom of the screen.
This is my layout xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_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" >
<Button
android:id="#+id/randBtn"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/randBtn_text" />
</RelativeLayout>
Get rid of the padding attributes of the relative layout.

Adapt ImageView for show picture correctly

I have some pictures stored on internal storage, that have different sizes and i would like to show it inside a scrollview contains imageview. I would that in each dimension case (image wider than higher, image higher than wider, squared image) image occupy entire screen width.
But code i've write for do that doesn't work well. If an image is squared or higher than wider everithing works well, but not if image is wider than higher.
this is xml of scrollview
<RelativeLayout
....
>
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/linearInsideScroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</ScrollView>
</RelativeLayout>
and this is xml of single imageView that i will inflate inside layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!--
othed widget
-->
<ImageView
android:id="#+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
and this is how i fill my view (pics is a list object contains image informations):
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearInsideScroll);
for (int i = 0; i < pics.size(); i++) {
View new_element = inflater.inflate(R.layout.single_element, null,
false);
if (pics.get(i).getWidth() > pics.get(i).getHeight()) {
imageView.getLayoutParams().width = width_px;
imageView.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
} else if (pics.get(i).getWidth() < pics.get(i).getHeight()) {
imageView.getLayoutParams().height = height_px;
imageView.getLayoutParams().width = LayoutParams.MATCH_PARENT;
}
else {
imageView.getLayoutParams().height = width_px;
imageView.getLayoutParams().width = width_px;
}
linearLayout.addView(new_element);
}
For the imageView, set scaleType.
http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

Categories

Resources