I have recently seen an Android app in which number line was added before each line in separate Column .The main column is also scrolling along with multiline TextView.How to add such line numbers along with textview ? See the Screenshot :
I have tried to implement it by adding 2 multiline textview with equal fontsize.
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<TextView
android:id="#+id/ct"
android:layout_weight="0.075"
android:textColor="#android:color/darker_gray"
android:layout_width="0dp"
android:layout_marginRight="1dp"
android:background="#303133"
android:layout_height="match_parent"
android:inputType="textMultiLine"
android:textSize="15sp"/>
<ScrollView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="#303133">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txt"
android:textColor="#android:color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:textSize="15sp" />
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
But how to add same number of line in NumberLine TextView according to other Textview ? and how to scroll NumberLine TextView along with other Textview ? afterall is there any other way to implement same?
Maybe you can use two RecyclerView , one for line number , the other one for the code, where each line is a item.
A tutorial for example.
EDIT
I think you can use a second Scroll view for you text , for example :
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<ScrollView
android:id="#+id/scroll_view_ct"
android:layout_width="0dp"
android:layout_weight="0.075"
android:layout_height="match_parent"
android:background="#303133">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/ct"
android:layout_weight="match_parent"
android:textColor="#android:color/darker_gray"
android:layout_width="0dp"
android:layout_marginRight="1dp"
android:background="#303133"
android:layout_height="match_parent"
android:inputType="textMultiLine"
android:textSize="15sp"/>
</HorizontalScrollView>
</ScrollView>
<ScrollView
android:id="#+id/scroll_view_txt"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="#303133">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txt"
android:textColor="#android:color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:textSize="15sp" />
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
and then coordinate the scroll listener
scrollViewCT.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
#Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
scrollViewTxt.scrollTo(scrollx, scrollY)
}
});
scrollViewTxt.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
#Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
scrollViewCT.scrollTo(scrollx, scrollY)
}
});
Another solution is to have directly the two text views in the same scrollview , maybe something like that:
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<ScrollView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="#303133">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<TextView
android:id="#+id/ct"
android:layout_weight="0.075"
android:textColor="#android:color/darker_gray"
android:layout_width="0dp"
android:layout_marginRight="1dp"
android:background="#303133"
android:layout_height="match_parent"
android:inputType="textMultiLine"
android:textSize="15sp"/>
<TextView
android:id="#+id/txt"
android:textColor="#android:color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:gravity="end"
android:textSize="15sp" />
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
Hope this helps.
You can use the getLineCount() in TextView to get the actual number of lines that are displayed on the screen for the current textview.
The getLineCount() works after the layout has been measured and drawn, otherwise it return 0. So you can use the method, the following way, which will notify you when a layout change happens to the TextViw :
textView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
// call textview.getLineCount() ;
}
});
Related
Here is my MainActivity:
public class MainActivity extends AppCompatActivity {
private LinearLayout mStatusBarAllContents;
private FrameLayout mStatusBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStatusBar = findViewById(R.id.status_bar);
mStatusBarAllContents = findViewById(R.id.status_bar_all_contents);
mStatusBar.setOnClickListener(v -> {
Log.d("TAG", "status");
});
mStatusBarAllContents.setOnClickListener(v -> {
Log.d("TAG", "statusall");
});
}
My Layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/status_bar"
android:layout_width="match_parent"
android:layout_height="60px"
android:background="#drawable/sysheader_bg"
android:descendantFocusability="afterDescendants"
android:orientation="vertical">
<ImageView
android:id="#+id/notification_lights_out"
android:layout_width="22dip"
android:layout_height="match_parent"
android:paddingStart="6dp"
android:paddingBottom="2dip"
android:scaleType="center"
android:visibility="gone"
tools:ignore="RtlCompat" />
<LinearLayout
android:id="#+id/status_bar_all_contents"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<FrameLayout
android:layout_width="174px"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/clock_relativelayout"
android:layout_width="174px"
android:layout_height="match_parent"
android:clickable="true"
android:visibility="visible">
<TextView
android:id="#+id/clock_hour"
android:layout_width="60px"
android:layout_height="54px"
android:gravity="center"
android:includeFontPadding="false"
android:text="05"
android:textColor="#color/white"
android:textSize="52px"
android:textStyle="bold" />
<ImageView
android:id="#+id/clock_coron"
android:layout_width="12px"
android:layout_height="48px"
android:layout_marginTop="9px"
android:layout_toRightOf="#+id/clock_hour"
android:scaleType="fitCenter"
android:src="#drawable/num_12x48_colon" />
<TextView
android:layout_width="60px"
android:layout_height="54px"
android:layout_toRightOf="#+id/clock_coron"
android:gravity="center"
android:includeFontPadding="false"
android:text="30"
android:textColor="#color/white"
android:textSize="52px"
android:textStyle="bold">
</TextView>
</RelativeLayout>
</FrameLayout>
<LinearLayout
android:id="#+id/status_bar_info_layout"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:clickable="true"
android:visibility="visible">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/audio_area"
android:layout_width="786px"
android:layout_height="match_parent"
android:background="#drawable/da_header_sections_background_click_over_info"
android:clickable="true"
android:orientation="horizontal"
android:splitMotionEvents="false">
<FrameLayout
android:id="#+id/audio_info_area"
android:layout_width="708px"
android:layout_height="match_parent"
android:clickable="false">
<TextView
android:id="#+id/audio_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="18px"
android:layout_marginLeft="18px"
android:ellipsize="end"
android:gravity="left"
android:singleLine="true"
android:text="ALOLO"
android:textSize="20sp"
android:visibility="visible" />
<LinearLayout
android:id="#+id/audio_off"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="24px"
android:paddingLeft="24px"
android:paddingEnd="56px"
android:paddingRight="56px"
android:visibility="gone">
<TextView
android:id="#+id/audio_off_text"
android:layout_width="648px"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="6px"
android:layout_marginLeft="6px"
android:ellipsize="end"
android:gravity="left"
android:singleLine="true"
android:text="PowerOFF"
android:textColor="#color/white"
android:textSize="30px">
</TextView>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</LinearLayout>
I set onclick on FrameLayout and LinearLayout, i wanna show log is status but when i touch it, nothing happen.
And then, i set android:layout_width="1dp", android:layout_height="1dp" of id: status_bar_all_contents, it show log: status, but status_bar_all_contents will gone, so i dont want that.
So how to fix when i touch it and show log status?
Ps: Sorry for my english is not good.
Layouts typically pass-through clicks to their children.
You can try this solution here disabling all children onclicks and enabling the FrameLaoyuts onclicks. But I don't think that will work with the LinearLayout on top of that.
Android frame layout click listener not working
My advice wrap the entire view in a ConstraintLayout, and position 2 basic <View>s
One <View> that is constrained to match the FrameLayout
One <View> that is constrained to match the LinearLayout
These <View>s will act as overlays.
<View
android:id="#+id/status_bar_overlay"
app:layout_constraintEnd_toEndOf="#id/status_bar"
app:layout_constraintStart_toStartOf="#id/status_bar"
app:layout_constraintTop_toTopOf="#id/status_bar"
app:layout_constraintBottom_toBottomOf="#id/status_bar" />
<View
android:id="#+id/status_bar_all_contents_overlay"
app:layout_constraintEnd_toEndOf="#id/status_bar_all_contents"
app:layout_constraintStart_toStartOf="#id/status_bar_all_contents"
app:layout_constraintTop_toTopOf="#id/status_bar_all_contents"
app:layout_constraintBottom_toBottomOf="#id/status_bar_all_contents" />
Set the two View's onClickListener in the Activity and there you go.
The main problem here is that the parent layout is nested scroll view so the height must be wrap content
so the view is like first image
what I want is to make the "don't have an account? register" TextView
to be at the bottom of the screen so I calculate the height of my
phone then the height of layout to subtract to subtract them and
assign the result to the top margin of textview
and here's my code
ViewTreeObserver observer = binding.parentConstraint.getViewTreeObserver();
if (observer.isAlive()) {
observer.addOnGlobalLayoutListener(() -> {
DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
float parentViewHeightInDP = (int) (binding.parentConstraint.getHeight() / displayMetrics.density);
if (getContext() != null) {
float heightInDP = displayMetrics.heightPixels / displayMetrics.density;
float distanceBetweenLayoutBottomAndParenBottom = heightInDP - parentViewHeightInDP;
if (distanceBetweenLayoutBottomAndParenBottom > 32) {
if (topMargin == 0) {
topMargin = 1;
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) binding.loginTextDontHaveAccount.getLayoutParams();
layoutParams.topMargin = Math.round(distanceBetweenLayoutBottomAndParenBottom);
Handler handler = new Handler();
handler.postDelayed(() -> {
binding.loginTextDontHaveAccount.requestLayout(); },
50);
}
}
but that the result I got
so my real question here is why there still some space here, I mean
the TextView should be exactly at the bottom
What I would suggest you to make NestedScrollView as a child instead of parent.
Make RelativeLayout as your parent and use layout_alignParentBottom to set TextView at bottom. Something like this.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:colorBackground"
android:clickable="true"
android:fillViewport="true"
android:focusable="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="650dp"
android:background="#mipmap/ic_launcher" />
<ImageView
android:layout_width="match_parent"
android:layout_height="650dp"
android:layout_below="#+id/img1"
android:background="#color/colorPrimary" />
</LinearLayout>
</RelativeLayout>
</androidx.core.widget.NestedScrollView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
Try 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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<AutoCompleteTextView
android:id="#+id/emailtext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:hint="Email"
android:inputType="textEmailAddress"
android:textColorHint="#80000000" />
<EditText
android:id="#+id/passwordtext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:hint="Password"
android:inputType="textPassword"
android:textColorHint="#80000000" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<Button
android:id="#+id/enter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Enter"
android:textAllCaps="false"
app:layout_constraintBottom_toTopOf="#id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="This is my app"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/enter" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
You can use constraint layout. Pay attention to layout_constraintBottom_toTopOf button and layout_constraintTop_toBottomOf textview.
I want to dynamically change textviews height to fit cells in other linearlayout.
I tried to make this in this way:
#Override
public void onBindViewHolder(#NonNull final MyViewHolder holder, final int position) {
holder.numberCage.setText(cages.get(position).getNumberCage());
holder.eggsDescription.setText(cages.get(position).getEggsDescription());
holder.eggsFrom.setText(cages.get(position).getNumberCageFrom());
if(cages.get(position).getBasicEggs().toString().equals("true"))
holder.basicEggs.setText("Tak");
else
holder.basicEggs.setText("Nie");
holder.dateEggs.setText(cages.get(position).getDateEggs());
holder.description.setText(cages.get(position).getDescription());
holder.pigeonsInside.setText(cages.get(position).getPigeonsInside());
holder.numberCageTitle.setHeight(holder.numberCage.getHeight());
holder.eggsDescriptionTitle.setHeight(holder.eggsDescription.getHeight());
holder.eggsFromTitle.setHeight(holder.eggsFrom.getHeight());
holder.basicEggsTitle.setHeight(holder.basicEggs.getHeight());
holder.dateEggsTitle.setHeight(holder.dateEggs.getHeight());
holder.descriptionTitle.setHeight(holder.description.getHeight());
holder.pigeonsInsideTitle.setHeight(holder.pigeonsInside.getHeight());
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView numberCage,pigeonsInside,description, dateEggs, basicEggs, eggsFrom, eggsDescription, numberCageTitle, pigeonsInsideTitle, descriptionTitle, dateEggsTitle, basicEggsTitle, eggsFromTitle, eggsDescriptionTitle;
LinearLayout linearLayout, linearLayoutLeft, linearLayoutRight;
public MyViewHolder(View view){
super(view);
numberCage = view.findViewById(R.id.numberCage);
pigeonsInside = view.findViewById(R.id.pigeonsInside);
description = view.findViewById(R.id.description);
dateEggs = view.findViewById(R.id.dateEggs);
basicEggs = view.findViewById(R.id.basicEggs);
eggsFrom = view.findViewById(R.id.eggsFrom);
eggsDescription = view.findViewById(R.id.eggsDescription);
numberCageTitle = view.findViewById(R.id.numberCageTitle);
pigeonsInsideTitle = view.findViewById(R.id.pigeonsInsideTitle);
descriptionTitle = view.findViewById(R.id.descriptionTitle);
dateEggsTitle = view.findViewById(R.id.dateEggsTitle);
basicEggsTitle = view.findViewById(R.id.basicEggsTitle);
eggsFromTitle = view.findViewById(R.id.eggsFromTitle);
eggsDescriptionTitle = view.findViewById(R.id.eggsDescriptionTitle);
linearLayout = view.findViewById(R.id.linearLayout);
}
}
I have good results but my problem is that I have to scroll down and scroll up my list to "load" this heights, I don't know how to tell this so I show you screenshot.
left table is not showing
After scroll down: left table after scroll
This is my content xml:
<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="wrap_content"
android:baselineAligned="true"
android:weightSum="2"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".CageList"
tools:showIn="#layout/activity_cage_list"
android:divider="#android:color/black"
android:dividerPadding="1dip"
android:showDividers="end"
android:id="#+id/linearLayout"
>
<LinearLayout
android:id="#+id/linearLayoutLeft"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:orientation="vertical">
<TextView
android:id="#+id/numberCageTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Numer klatki:"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/pigeonsInsideTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Gołębie w klatce:"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/descriptionTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Opis klatki:"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/dateEggsTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Data złożenia jaj:"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/basicEggsTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jajka macierzyste?"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/eggsFromTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jajka przełożone skąd? (klatka)"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/eggsDescriptionTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Opis do jajek:"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<View
android:layout_width="200dp"
android:layout_height="1dp"
android:background="#color/colorPrimary"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayoutRight"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.75"
android:orientation="vertical">
<TextView
android:id="#+id/numberCage"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/pigeonsInside"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/dateEggs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/basicEggs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/eggsFrom"
android:layout_width="match_parent"
android:layout_height="35.65dp" />
<TextView
android:id="#+id/eggsDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorPrimary"/>
</LinearLayout>
</LinearLayout>
create another file named sub_layout.xml which will have below code :-
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="horizontal">
<TextView
android:id="#+id/txtTitleStatic"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.45"
android:text="Jajka przełożone skąd? (klatka)"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/txtTitle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".55" />
</LinearLayout>
and then use below code to include in main layout (You Content XML File):-
<include
android:id="#+id/numberCageLayout"
layout="#layout/sub_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<include
android:id="#+id/pigeonsInsideLayout"
layout="#layout/sub_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<include
android:id="#+id/descriptionLayout"
layout="#layout/sub_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Now in your Java File get their definition like :-
View numberCageLayout = findViewById(R.id.numberCageLayout);
View pigeonsInsideLayout = findViewById(R.id.pigeonsInsideLayout);
View descriptionLayout = findViewById(R.id.descriptionLayout);
TextView numberCageStatic =numberCageLayout.findViewById(R.id.txtTitleStatic);
TextView numberCage =numberCageLayout.findViewById(R.id.txtTitle);
TextView pigeonsInsideStatic =pigeonsInsideLayout.findViewById(R.id.txtTitleStatic);
TextView pigeonsInside =pigeonsInsideLayout.findViewById(R.id.txtTitle);
Now like this you don't have to worry about cell height.
I have two LinearLayout like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#drawable/border"
android:orientation="horizontal"
android:padding="10dp"
android:weightSum="2"
android:onClick="viewTipsDetail">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:src="#mipmap/img_breakfast" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Đừng bỏ bữa sáng"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="false"
android:text="Các nghiên cứu cho thấy bữa sáng là một những điê"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#drawable/border"
android:orientation="horizontal"
android:padding="10dp"
android:weightSum="2"
android:onClick="viewTipsDetail">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:src="#mipmap/img_waterglass" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Uống đủ nước"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity=""
android:singleLine="false"
android:text="Bổ sung đủ nước cho cơ thể, ngay cả các bạn làm việc trong môi trường máy lạnh hoặc ngoài trời. Việc mất nước có thể khiến bạn căng thẳng, khó tập trung vào công việc"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
I'm going to plan implement an onClickEvent with same method for both of them. In this occasion, the event will call viewTipsDetail() method. So how can I get the image src, the text in specific LinearLayout with only one method? And is there any possible way to auto-generate a LinearLayout like this with given detail?
This is sample, I'm going to use more LinearLayout than this, so what I need in general is a possible method for getting detail in specific LinearLayout.
<LinearLayout
id="#+id/layout1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5"
onClick="onLayoutClick"
android:orientation="vertical">
<LinearLayout
id="#+id/layout2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5"
onClick="onLayoutClick"
android:orientation="vertical">
In code:
#Override
public void onLayoutClick(View view) {
// click click
// view.getTag()
}
Or just use list view for achieve that...
I have a layout wich is nested 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" >
<ScrollView
android:id="#+id/scrollAplicacoes"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
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" >
<TextView
android:id="#+id/titulo"
android:layout_width="match_parent"
android:layout_height="60dp"
android:fontFamily="Arial"
android:lineSpacingExtra="-5dp"
android:text="#string/titulo_aplicacao"
android:textColor="#3c3c3c"
android:textSize="25sp"
android:textStyle="bold"/>
<ImageView
android:id="#+id/btnAplicacaoUm"
android:layout_width="match_parent"
android:layout_height="150dp"
android:onClick="selectAplicacao"
android:src="#drawable/aplicacoes_item1" />
<ImageView
android:id="#+id/btnAplicacaoDois"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginTop="10dp"
android:onClick="selectAplicacao"
android:src="#drawable/aplicacoes_item2" />
<ImageView
android:id="#+id/btnAplicacaoTres"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginTop="10dp"
android:onClick="selectAplicacao"
android:src="#drawable/aplicacoes_item3" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/menuFixo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/faixa_below" >
<ImageView
android:id="#+id/belowHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="#string/descBtnHome"
android:layout_marginTop="5dp"
android:src="#drawable/below_home_sel" />
<ImageView
android:id="#+id/belowCalculadora"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="abreCalculadora"
android:contentDescription="#string/descBtnCalculadora"
android:layout_marginTop="5dp"
android:src="#drawable/below_calc" />
<ImageView
android:id="#+id/belowProdutos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/descFaixaProdutos"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:src="#drawable/below_produtos" />
</LinearLayout>
</RelativeLayout>
The problem is that the LinearLayout outside the ScrollView, at the end is fixed on the bottom, and the scroll is placed behind it. So i need the Scroll to be the screen height minus this fixed LinerLayout height minus a little margin.
I tried this:
(...)
int scrollFInalHeight = scrollHeight - fixedMenuHeight - 10;
ViewGroup.LayoutParams param = new ViewGroup.LayoutParams(scrollStartWidth, scrollFInalHeight);
scroll.setLayoutParams(param);
But the app crashes when I start this activity.
This java code is within the public void onWindowFocusChanged(boolean hasFocus) method.
Any suggestions?
Thanks in advance! ;)
You need to set Height
scroll_view.setLayoutParams(new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
or
scroll_view.setLayoutParams(new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, 350));
#aNikeT's answer is correct, but it will be safer with
ViewGroup.LayoutParams layoutParams = mDescriptionScrollView.getLayoutParams();
layoutParams.height = scrollViewHeight;
Have you tried to put
android:layout_above="#+id/menuFixo"
in your ScrollView?