Hi i am running a loop 10 times and each time i am creating a new TextView and I am adding that textview to my Linear layout which has orientation set to 'HORIZONTAL'. I want to show these texts in the form of paragraph where another sentence starts form where one sentence ends.
This is my layout:
<ScrollView 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_height="match_parent"
android:layout_width="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".ui.QuestionActivity"
android:padding="16dp"
android:orientation="vertical"
tools:showIn="#layout/activity_question">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/title"
android:layout_gravity="center"
android:textAppearance="?android:textAppearanceLarge"
android:textColor="#android:color/black"
android:textStyle="bold|italic"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:id="#+id/base_layout"/>
</LinearLayout>
</ScrollView>
This is my java code:
for(int i=0; i<10; i++){
TextView textView = Utility.getTextView(this, i);
if(i == 5) {
textView.setText(sentences.get(i) + "\n" + "\n");
}
else {
textView.setText(sentences.get(i) + " ");
}
mLayout.addView(textView);
}
But the issue is only first textview is shown when orientation is set to 'horizontal' and if i set my orientation to 'vertical' i am able to see all textviews below one another.
Please help me on this.
You should need to wrap your base_layout LinearLayout under HorizontalScrollView.
Try below, should solve your issue. I have verified myself.
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="horizontal">
<LinearLayout
android:id="#+id/base_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:orientation="horizontal" />
</HorizontalScrollView>
Related
I have a ScrollView and a FrameLayout inside a ConstraintLayout. When I click a button, I want the ScrollView layout to resize so the bottom of the ScrollView is aligned with the top of the FrameLayout. However, the constraints won't change when the button is clicked.
I've tried to put the ScrollView inside a LinearLayout instead, but that didn't work either.
When button is clicked:
private void fixTableConstraint() {
ConstraintLayout constraintLayout = findViewById(R.id.home_constraint_layout);
View scrollView = findViewById(R.id.homeScreenScroll);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);
constraintSet.connect(R.id.homeScreenScroll, ConstraintSet.BOTTOM, R.id.timeMenuOverlay, ConstraintSet.TOP, 0);
constraintSet.applyTo(constraintLayout);
ConstraintLayout.LayoutParams cl = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
scrollView.setLayoutParams(cl);
}
XML layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="#+id/home_constraint_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:id="#+id/homeScreenScroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.2"
tools:layout_editor_absoluteX="0dp">
<TableLayout
android:id="#+id/homescreenTable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</ScrollView>
<FrameLayout
android:id="#+id/timeMenuOverlay"
android:layout_width="match_parent"
android:layout_height="152dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<fragment
android:id="#+id/timeMenuFragment"
android:name="multicus.com.scheduleplannerv2.TimeMenuFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
</FrameLayout>
</android.support.constraint.ConstraintLayout>
Normal Layout:
https://imgur.com/VsfSgVa
What happens in my case when button is clicked:
https://imgur.com/pXT8v6W
What I actually want:
https://imgur.com/6OML007
Your constraints changes will not work because you have
ScrollView layout_height = "match_parent"
You can do it like this
<ScrollView
android:id="#+id/homeScreenScroll"
android:background="#color/green_dark"
android:layout_width="match_parent"
android:layout_height="0dp" // this is important
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#id/timeMenuOverlay" // and this
tools:layout_editor_absoluteX="0dp">
<TableLayout
...
/>
</ScrollView>
<FrameLayout
android:id="#+id/timeMenuOverlay"
android:visibility="gone" // !!
android:layout_width="match_parent"
android:layout_height="152dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
Instead of changing your layout constraints, change only the visibility.
timeMenuOverlay visibility => GONE, expand your ScrollView to the bottom
timeMenuOverlay visibility => VISIBLE => expected result
So I'm building this app where each time the floatActionButton is clicked, a button is created. That aspect of the app works fine. But my problem is that the buttons are being created on top of one another and are constantly created. I need to create only 4 buttons click after click, one below another.
This is my for-loop. I appreciate any help.
addingSemester.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i <= 4; i++){
Button semesterButton = new Button(MainActivity.this);
semesterButton.setId(i);
semesterButton.setText("Semester " + i);
semesterButton.setLayoutParams(lp);
semesterLayout.addView(semesterButton);
i++;
}
}
});
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/main_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="#FFFFFF"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="myapp.onur.journeygpacalculator.MainActivity">
<!--<LinearLayout-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:orientation="horizontal"-->
<!--android:padding="6dp"-->
<!-->-->
<!--<Button-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="Semester 1"-->
<!--android:layout_weight="4"-->
<!--android:textColor="#FFFFFF"-->
<!--android:background="#3F51B5"-->
<!--/>-->
<!--<TextView-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="DELETE"-->
<!--android:layout_weight="0.6"-->
<!--android:clickable="true"-->
<!--android:textAlignment="center"-->
<!--/>-->
<!--</LinearLayout>-->
<android.support.design.widget.FloatingActionButton
android:id="#+id/addActionButton"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:layout_weight="1"
android:clickable="true"
android:scaleType="fitXY"
android:src="#drawable/ic_add_black_24dp"
app:borderWidth="0dp"/>
</LinearLayout>
I have this code:
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener()
{
//Se aggiungo un fragment A al container X e aggiungo il fragment B allo stesso container, il fragment B andrĂ sopra
//il fragment A
#Override
public void onClick(View v)
{
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if(ko==0)
{
MyFragment fragment = MyFragment.createFragment(0);
fragmentTransaction.add(R.id.formazione3,fragment);
ko++;
}
else if(ko==1)
{
MyFragment fragment = MyFragment.createFragment(1);
fragmentTransaction.add(R.id.formazione2,fragment);
ko++;
}
else if(ko==2)
{
MyFragment fragment = MyFragment.createFragment(2);
fragmentTransaction.add(R.id.moduli2,fragment);
ko++;
}
else if(ko==3)
{
MyFragment fragment = MyFragment.createFragment(1);
fragmentTransaction.add(R.id.moduli5,fragment);
}
fragmentTransaction.commit();
}
});
Well, I'm experiencing the order of addition of fragments. I have 4 fragments and I don't understand why some fragments, when I add them, go under the bigger fragment, while other ones go on the bigger fragment in their container.
formazione3>formazione2 (formazione2 is contained in formazione3)
moduli2>moduli5 (moduli5 is contained in moduli2, moduli2 is contained in part in formazione3)
When I add the second fragment, it doesn't show, I think that it goes under the previous fragment, so it goes under the bigger. When I add the third fragment, it goes in part under the first fragment, the first is bigger. When I add the fourth fragment, it goes on the third, but the third is bigger than the fourth and above all the the fourth is contained in the third. How does it work? I don't understand at all!
This is the layout:
<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"
tools:context="com.example.utente.fantacalcio.FormazioniActivity"
android:weightSum="1"
android:id="#+id/activity_formazioni_layout">
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
android:id="#+id/button2" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/button" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<RelativeLayout
android:layout_width="107dp"
android:layout_height="match_parent"
android:id="#+id/moduli">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/formazione">
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="92dp"
android:layout_height="match_parent"
android:id="#+id/moduli1">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/formazione1">
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="71dp"
android:layout_height="match_parent"
android:id="#+id/moduli2">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/formazione2">
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="51dp"
android:layout_height="match_parent"
android:id="#+id/moduli3">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/formazione3">
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="29dp"
android:layout_height="match_parent"
android:id="#+id/moduli4">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/formazione4">
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="11dp"
android:layout_height="match_parent"
android:id="#+id/moduli5">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/formazione5">
</RelativeLayout>
</LinearLayout>
Your parent layout is a RelativeLayout , the default behavior for this layout is that first child view is under the last child view.
In your xml this child :
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="51dp"
android:layout_height="match_parent"
android:id="#+id/moduli3">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/formazione3">
</RelativeLayout>
</LinearLayout>
is under this child view :
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="71dp"
android:layout_height="match_parent"
android:id="#+id/moduli2">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/formazione2">
</RelativeLayout>
</LinearLayout>
so when the views are inflating , this is the opposite formazione3 is over formazione2.
Moreover your LinearLayout have these attributes :
android:layout_width="match_parent"
android:layout_height="match_parent"
so they take all the place available.
Try to set fixe size in dp instead of match_parent to see what's really happen.
Hope this helps.
Sorry for my poor english.
I have this layout :
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:orientation="vertical"
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_goster" tools:context="com.example.GosterActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_line1"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_line2"
android:layout_centerHorizontal="true"
android:layout_below="#+id/text_line1"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rl_root"
android:layout_below="#+id/text_line2">
</LinearLayout>
</LinearLayout>
</ScrollView>
In Java, I set texts of this layout like this :
tvline1.setText("asdf1");
tvline1.setText("asdf2");
And I programmatically generate TextViews in rl_root like this :
LinearLayout relativeLayout2 = (LinearLayout) findViewById(R.id.rl_root);
for(int i = 0; i < 5; i++)
{
TextView textvw = new TextView(this);
textvw.setText(Integer.toString(i));
relativeLayout2.addView(textvw);
for(int j = 0; j < 10; j++)
{
TextView textvw2 = new TextView(this);
textvw2 .setText(Integer.toString(j+5));
relativeLayout2.addView(textvw2);
}
}
With this configuration, I can only see the content of generated textviews, not the first two ones that I wrote by hand. Can you tell me how I can show those two TextViews as well? Thanks.
I am able to get the both kind of textviews - generated ones and handwritten. Code seems to be fine. I just removed these lines
tools:showIn="#layout/activity_goster"
tools:context="com.example.GosterActivity"
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.