How to fix my LinearLayout to scroll down when content exceeds view - java

I am adding views to my adapter dynamically and when the views exceeds the screen boundaries I am unable to see the full views.
I have tried android:isScrollContainer="true" and android:scrollbars="vertical" without any luck.
public void onAddField(View v) {
final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.field, null);
spin = rowView.findViewById(R.id.type_spinner2);
weightLayout = rowView.findViewById(R.id.myLayout);
getdata();
spin.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, arrayList2));
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#E5E5E5"
android:orientation="vertical"
android:scrollbars="vertical"
android:isScrollContainer="true">

Try to use this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="50dp"
android:text="TextView" />
</LinearLayout>
</ScrollView>
</LinearLayout>
This way your ScrollView will expand with your LinerLayout.

Related

Not able to put recycler view as ExpandableListView header layout

I want to display a recycler view inside the ExpandableListView header item.But I am getting an issue my header is not clickable when I put a recycler view in it.
Here is my code:-
header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1"
android:padding="8dp"
android:background="#color/grey">
<TextView
android:id="#+id/lblListHeader"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="5dp"
android:textSize="15dp"
android:layout_weight=".25"
android:text="#"
android:textColor="#color/ModeltitleColor" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".65"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/ivRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
/>
</LinearLayout>
<ImageView
android:id="#+id/iv_group_indicator"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_weight=".10"
android:src="#drawable/ic_arrow_drop_down_black_24dp" />
</LinearLayout>
</LinearLayout>
here I am setting the group:-
#Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.zonal_list_group, viewGroup, false);
}
TextView textView = view.findViewById(R.id.lblListHeader);
RecyclerView recyclerView=view.findViewById(R.id.ivRecyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false));
ZoneNameAdapter zoneNameAdapter =new ZoneNameAdapter(zonalSnapShotResponse.keyFigureDataGroups.get(i).zones,context);
recyclerView.setAdapter(zoneNameAdapter);
ImageView imageView = view.findViewById(R.id.iv_group_indicator);
textView.setText(zonalSnapShotResponse.keyFigureDataGroups.get(i).dataGroup);
imageView.setImageResource(R.drawable.ic_arrow_drop_down_black_24dp);
if (b) {
imageView.setRotation(180f);
} else {
imageView.setRotation(0f);
}
return view;
}
getChildItemCount does not get called when I put recycler view in the header.
I have tried multiple solutions but noting worked for me.Please help

Android java - dynamically add ImageViews in LinearLayout

i would like to add to a LinearLayout some imageViews dynamically in my app.
My following code doesn't show errors and prints what it is supposed to on logcat each time an ImageView is added from java code.
My problem is that when i run the app the images don't appear on screen....any idea where i am doing wrong ?
thanks a lot.
public class MesListesFragment extends Fragment {
private RecyclerView mes_listes_recyclerView;
private ArrayList<ListItem> liste_items;
private LinearLayout list_card_categories;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.mes_listes_fragment, container, false);
View view2 = inflater.inflate(R.layout.list_item, container, false);
mes_listes_recyclerView = view.findViewById(R.id.mes_listes_recyclerView);
list_card_categories = view2.findViewById(R.id.list_card_recyclerView);
liste_items = new ArrayList<>();
for (int i = 0; i < 10; i ++){
liste_items.add(new ListItem("Liste des ploucs", "18 Août 198" + i));
}
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
mes_listes_recyclerView.setLayoutManager(linearLayoutManager);
mes_listes_recyclerView.setAdapter(new ListViewAdapter(getContext(), liste_items));
for (int i = 0; i < 8; i++){
ImageView image = new ImageView(getContext());
image.setImageResource(R.drawable.user);
list_card_categories.addView(image);
Log.d("view created", "...");
}
return view;
}
}
Here is the xml, the file's called list_item.xml :
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="150dp"
android:layout_marginHorizontal="10dp"
android:layout_marginTop="10dp"
app:cardCornerRadius="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/list_card_first_container"
android:layout_width="match_parent"
android:layout_height="100dp"
android:baselineAligned="false"
android:orientation="horizontal"
android:weightSum="100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="50"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/list_card_textView_nom_liste"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nom de la liste"></TextView>
<TextView
android:id="#+id/list_card_textView_date_liste"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date liste"></TextView>
</LinearLayout>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="50">
<LinearLayout
android:id="#+id/list_card_recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="50"
android:gravity="center"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
<LinearLayout
android:id="#+id/list_card_second_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/list_card_first_container"
android:gravity="center"
android:orientation="horizontal"
android:paddingVertical="5dp">
<Button
android:id="#+id/list_card_button_editer"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginEnd="20dp"
android:background="#color/bleu_fb"
android:text="#string/editer"></Button>
<Button
android:id="#+id/list_card_button_supprimer"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:background="#android:color/holo_red_light"
android:text="#string/supprimer"></Button>
</LinearLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>

Button Component Cuts off my custom View

My button component takes space of my custom view class,if i put more it takes up more space,any reason why is that
My oncreate method :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_control);
MyCustomPanel view = new MyCustomPanel(this);
DisplayMetrics metrics;
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = getDPI(500, metrics);
int width = getDPI(800,metrics);
layout = (LinearLayout) findViewById(R.id.r2) ;
layout1 = (LinearLayout) findViewById(R.id.l2);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(width,
height);
layout.addView(view,params);
}
This is my xml code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/r2"
android:gravity="bottom|start"
android:layout_gravity="bottom|start"
tools:context="company.ciso.com.wheelchair.Control">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:gravity="top|end"
android:layout_gravity="top|end"
android:id="#+id/l2"
>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/b1"
android:text="S1" />
</LinearLayout>
</LinearLayout>
these are the screen shots
Additionally is there anyway to set gravity of my custom view.??
Update your layout with this code.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/l2"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="top|end"
android:gravity="top|end"
android:orientation="horizontal">
<Button
android:id="#+id/b1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="S1" />
</LinearLayout>
<LinearLayout
android:id="#+id/r2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="horizontal"></LinearLayout>
</RelativeLayout>

Android Listview is not scrolling still not working

Good day!
I have tried a lot of other posts on stackoverflow but it didn't work for me,
maybe its because I'm new to Android Development.
My problem is as follow:
XML Layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/item_detail_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context=".ItemDetailActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:layout_width="match_parent"
android:layout_height="89dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/txtDetails"
android:layout_gravity="center_horizontal|top"
android:padding="5dp" />
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="369dp"
android:layout_gravity="center_horizontal|bottom" />
</FrameLayout>
Preview in Android Studio:
inside the onCreateView:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { }
Binding the listview:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_item_detail, container, false);
List<Events> list = Events.getEvents();
int max = list.size();
String[] Values = new String[max];
for (int i = 0; i < max; i++) {
Events e = list.get(i);
Values[i] = e.getNaam();
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
rootView.getContext(),
android.R.layout.simple_list_item_1, Values);
listView.setAdapter(arrayAdapter);
return rootview;
}
Another point is i can't click a list item wich is strange !
Any ideas or tips?
what do i wrong?
Kind Regards,
Stefan
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/item_detail_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context=".ItemDetailActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/txtDetails"
android:layout_gravity="center_horizontal|top"
android:padding="5dp" />
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom" />
</FrameLayout>
Try something like the following.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/item_detail_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context=".ItemDetailActivity"
tools:ignore="MergeRootFrame" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/linearLayout">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/txtDetails"
android:layout_gravity="center_horizontal|top"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</FrameLayout>
<LinearLayout>
<ScrollView>
<ListView ...>
</listView>
</ScrollView>
</LinearLayout>
You need to add a LinearLayout around the scrollview, that way the scrollview knows inside what it should scroll. If this doesn't work, replace the ListView with another LinearLayout and add your items to the LinearLayout. I'm not sure if a View is capable of scrolling at all. Try first option first, otherwise try the latter. I know the latter will work for sure since I used the exact same design myself in an app I'm developing.
As already mentioned in the comment, you have to add a ScrollView around your ListView, e.g.:
<ScrollView>
<ListView ....>
</ListView>
</ScrollView>

When duplicating view how to get reference to each view duplicated?

I have a hidden layout.
add_event_price.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/add_event_price_layout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<EditText android:id="#+id/add_event_wording"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:hint="#string/event_wording_hint" />
<EditText android:id="#+id/add_event_price"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="#string/event_price_hint" />
<ImageButton android:id="#+id/add_event_cross"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:src="#drawable/cross"
android:background="#null"
android:layout_gravity="center_vertical"
android:contentDescription="#string/delete" />
</LinearLayout>
When clicking on a button in the main layout, the hidden layout appears.
<ScrollView 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"
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:background="#color/app_background_color">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout android:id="#+id/add_event_dynamic"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
<Button android:id="#+id/add_event_add_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:text="#string/add_event_add_field" />
</LinearLayout>
</ScrollView>
add_event_add_field.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout priceLayout = (LinearLayout) rootView.findViewById(R.id.add_event_price_layout);
LinearLayout dynamicLayout = (LinearLayout) rootView.findViewById(R.id.add_event_dynamic);
View hidden = inflater.inflate(R.layout.add_event_price, priceLayout, false);
dynamicLayout.addView(hidden);
}
});
Adding the hidden layout dynamically works but I don't know how to retrieve the values of the EditText in the hidden layout, and delete a view when clicking on the corresponding add_event_cross ImageButton.
First, you can call findViewById on dynamicLayout:
EditText et = (EditText) dynamicLayout.findViewById(R.id.add_event_wording);
Second, I suggest not to show/hide layout by adding view. Just put the dynamicLayout inside your main layout and use something like:
dynamicLayout.setVisibility(LinearLayout.GONE); //makes the layout hidden
I have not tested the code but you can build upon it.

Categories