Reputation: 33
I am trying to put a scroll view in the main linear layout, and inside the scroll view is a linear layout containing a custom view, I am not quite sure about what I am supposed to do to make it works. I am a newbie in android studio, would appreciate a lot if you point out my mistakes. Thank you! code in my custom view class looks like this
package com.example.myapplication;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class mview extends View {
public int lines = 0;
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void addLine(){
postInvalidate();
lines++;
}
public mview(Context context) {
super(context);
init(null, 0);
}
public mview(Context context, AttributeSet attr) {
super(context, attr);
init(attr, 0);
}
public mview(Context context, AttributeSet attr, int def) {
super(context, attr, def);
init(attr, def);
}
private void init(AttributeSet set, int def) {
line1.setColor(Color.BLACK);
line1.setAntiAlias(true);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//draw grids
for (int i = 0; i < lines; i++) {
canvas.drawLine(...);
}
}
}
and code in my mainactivity class looks like this
package com.example.myapplication;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private void clicked() {
mview.addLine();
}
private mview mview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mview = findViewById(R.id.mview);
setContentView(R.layout.activity_main);
ScrollView scrollView = findViewById(R.id.scrollview);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clicked();
}
});
}
}
and my activity_main layout looks like this
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.example.myapplication.mview
android:id="@+id/mview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Upvotes: 3
Views: 1715
Reputation: 405
your view in linear layout will never be scrolled, because you have set height of the custom view to match_parent you must change it to wrap_content
<com.example.myapplication.mview
android:id="@+id/mview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
to
<com.example.myapplication.mview
android:id="@+id/mview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
and move findViewById to after setContentView:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //moved up
mview = findViewById(R.id.mview); //moved down
ScrollView scrollView = findViewById(R.id.scrollview);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clicked();
}
});
}
Upvotes: 1
Reputation: 43
Nothing to worry about it . There is just a small mistake as mentioned below , When you take the Layout_weight = 1 into consideration then the height is matched to the full screen height so instead remove the attribute from the code:
<ScrollView
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" // Remove this line from here
>
Upvotes: 0