How to define column and row in a LinearLayout dynamically? - android-layout

I'm trying to create dynamically this XML:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:rowCount="2"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/topleft"
android:layout_width="160dp"
android:layout_height="200dp"
android:layout_column="0"
android:layout_row="0"
>
<ImageView
android:id="#+id/myimage1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/tetona"
/>
></LinearLayout>
<LinearLayout
android:id="#+id/topRigth"
android:layout_width="160dp"
android:layout_height="200dp"
android:layout_column="1"
android:layout_row="0"
>
<ImageView
android:id="#+id/myimage2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/tetona" />
></LinearLayout>
<LinearLayout
android:id="#+id/bottomRight"
android:layout_width="160dp"
android:layout_height="200dp"
android:layout_column="1"
android:layout_row="1"
>
<ImageView
android:id="#+id/myimage3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/tetona" />
></LinearLayout>
<LinearLayout
android:id="#+id/BottonLeft"
android:layout_width="160dp"
android:layout_height="200dp"
android:layout_column="0"
android:layout_row="1"
>
<ImageView
android:id="#+id/myimage4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/tetona" />
></LinearLayout>
</GridLayout>
I can create LinearLayouts inside a loop such as:
GridLayout gridlay = (GridLayout)findViewById(R.id.gridpadre);
gridlay.setColumnCount(2);
gridlay.setRowCount(2);
for (int i=0; i<(gridlay.getRowCount()); ++i){
for(int j=0; j<gridlay.getColumnCount(); ++j){
LinearLayout layout = new LinearLayout(this);
layout.setId(id);
layout.setLayoutParams(new LinearLayout.LayoutParams(160,200));
gridlay.addView(layout);
But... how can I set LinearLayout android:layout_column="i" and android:layout_row="j" dynamically in order to put it in the position i and j relative to the gridlayout?
Is it possible?
Thanks

try doing the following: layout.setOrientation(VERTICAL); then try layout.setHorizontalGravity(j) since I don't have a computer equipped with any way to test it I'm not sure it will work, but its worth a shot.

Related

Error first time fragment is displayed with cardview

I have an error the first time that my fragment with cardviews is shown, they become much larger and only fit 2 on the screen, that is, only one row of two columns when they should show 6, first I saw that it happened and now I I realized that when re-entering the fragment since this is the one by default, if I re-enter if it is shown as it should, this fragment is inside a navigation draw
I attach a capture of how it looks at the beginning, when entering the application:
enter image description here
And in the next one it looks like this when I re-enter the fragment or I refresh it:
enter image description here
Here I leave you as seen in the android designer:
enter image description here
And finally the xml of the layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".NumericalMethods.NonlinearEquations.NonlinearEquationsFragment"
android:background="#drawable/bg_ne"
>
<TextView
android:id="#+id/title_nonlinear_ecuations"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Resolucion de \nEcuaciones No lineales "
android:textColor="#color/white"
android:textSize="30dp"
android:textAlignment="center"
android:textStyle="bold"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
/>
<GridLayout
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="3"
android:columnCount="2"
android:layout_below="#id/title_nonlinear_ecuations">
<androidx.cardview.widget.CardView
android:id="#+id/card_fp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="0"
android:layout_gravity="fill"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="8dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
>
<LinearLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical|center_horizontal"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/ic_pf" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Punto Fijo"
android:textStyle="bold"
android:textAlignment="center"
android:textSize="20dp"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/car_nr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="1"
android:layout_gravity="fill"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="8dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
>
<LinearLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical|center_horizontal"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/ic_nr"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Newton Raphson"
android:textStyle="bold"
android:textAlignment="center"
android:textSize="20dp"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/card_sec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="0"
android:layout_gravity="fill"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="8dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
>
<LinearLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical|center_horizontal"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/ic_sec"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="La secante"
android:textStyle="bold"
android:textAlignment="center"
android:textSize="20dp"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/car_fake_pos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="1"
android:layout_gravity="fill"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="8dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
>
<LinearLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical|center_horizontal"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/ic_pos_falsa"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Posicion Falsa"
android:textStyle="bold"
android:textAlignment="center"
android:textSize="20dp"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/card_bisec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_column="0"
android:layout_gravity="fill"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="8dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical|center_horizontal"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_bisection"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Biseccion"
android:textStyle="bold"
android:textAlignment="center"
android:textSize="20dp"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/card_muller"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_column="1"
android:layout_gravity="fill"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="8dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical|center_horizontal"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_muller"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Muller"
android:textStyle="bold"
android:textAlignment="center"
android:textSize="20dp"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
</GridLayout>
</RelativeLayout>
And emphasize that it only happens when the application starts, and when I take a step back, when I say this I mean the button this:
enter image description here
My Activity:
class MainActivity : AppCompatActivity() {
private lateinit var appBarConfiguration: AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar: Toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
val fab: FloatingActionButton = findViewById(R.id.fab)
fab.setOnClickListener { view ->
Snackbar.make(view, "Aplicacion Realizada por E.R.", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
val navView: NavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
appBarConfiguration = AppBarConfiguration(setOf(
R.id.nav_ne, R.id.nav_sne, R.id.nav_fai, R.id.nav_id, R.id.nav_edo, R.id.nav_edp), drawerLayout)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main, menu)
return true
}
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.nav_host_fragment)
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}
}
My mobile_navigartion.xml:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/mobile_navigation"
app:startDestination="#+id/nav_ne">
<fragment
android:id="#+id/nav_ne"
android:name="com.yr.mnlite.NumericalMethods.NonlinearEquations.NonlinearEquationsFragment"
android:label="#string/menu_ne"
tools:layout="#layout/fragment_nonlinear_equations" />
<fragment
android:id="#+id/nav_sne"
android:name="com.yr.mnlite.NumericalMethods.SistemNonlinearEquations.SistemNonlinearEquationsFragment"
android:label="#string/menu_sne"
tools:layout="#layout/fragment_sistem_nonlinear_equations" />
<fragment
android:id="#+id/nav_fai"
android:name="com.yr.mnlite.NumericalMethods.FunctionalApproximationAndInterpolation.FunctionalApproximationAndInterpolationFragment"
android:label="#string/menu_fai"
tools:layout="#layout/fragment_functional_approximation_and_interpolation" />
<fragment
android:id="#+id/nav_id"
android:name="com.yr.mnlite.NumericalMethods.IntegrationDifferentiation.IntegrationDifferentiationFragment"
android:label="#string/menu_id"
tools:layout="#layout/fragment_integration_differentiation" />
<fragment
android:id="#+id/nav_edo"
android:name="com.yr.mnlite.NumericalMethods.OrdinaryDifferentialEquations.OrdinaryDifferentialEquationsFragment"
android:label="#string/menu_edo"
tools:layout="#layout/fragment_ordinary_differential_equations" />
<fragment
android:id="#+id/nav_edp"
android:name="com.yr.mnlite.NumericalMethods.PartialDifferentialEquations.PartialDifferentialEquationsFragment"
android:label="#string/menu_edp"
tools:layout="#layout/fragment_partial_differential_equations" />
</navigation>
Thank you all very much I am new to android.
Can you try this?
<androidx.cardview.widget.CardView
android:id="#+id/card_fp"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_gravity="fill"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="8dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
>

Nested LinearLayout does not align to parent ScrollView?

I have a fragment with two horizontal Buttons (yellow = fragment_test_button_container) at the bottom of my fragment. I would like to use the remaining area (red = fragment_test_scrollview) for a ScrollView It is reuiqred that my ScrollView consist of one layout (turquoise = fragment_test_check) only . This can then in turn have further layouts, as you can see in the attached fragment_test.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.TestFragment">
<ScrollView
android:id="#+id/fragment_test_scrollview"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#id/fragment_test_button_container"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent=".84"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="#+id/fragment_test_check"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/fragment_test_front_photo_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".28"
android:orientation="horizontal">
<TextView
android:id="#+id/fragment_test_front_photo_title"
style="#style/myapp_MediumTextStyle"
android:layout_width="#dimen/myapp_test_fragment_textview_width"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="24dp"
android:text="#string/fragment_test_front_photo_title_text" />
<ImageView
android:id="#+id/fragment_test_front_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/fragment_test_back_photo_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".28"
android:orientation="horizontal">
<TextView
android:id="#+id/fragment_test_back_photo_title"
style="#style/myapp_MediumTextStyle"
android:layout_width="#dimen/myapp_test_fragment_textview_width"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="24dp"
android:text="#string/fragment_test_back_photo_title_text" />
<ImageView
android:id="#+id/fragment_test_back_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
</LinearLayout>
<!-- ADDITIONAL DATA -->
<LinearLayout
android:id="#+id/fragment_test_data_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".34"
android:orientation="vertical"
android:paddingStart="24dp"
android:paddingEnd="24dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/fragment_test_value1_label"
style="#style/myapp_MediumTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text=„Value1^“ />
<TextView
android:id="#+id/fragment_test_value1_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="24dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/fragment_test_value2_label"
style="#style/myapp_MediumTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text=„Value2“ />
<TextView
android:id="#+id/fragment_test_value2_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="24dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/fragment_test_value3_label"
style="#style/myapp_MediumTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Ausstellungsdatum" />
<TextView
android:id="#+id/fragment_test_value3_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="24dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/fragment_test_button_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingStart="24dp"
android:paddingEnd="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/fragment_test_scrollview">
<Button
android:id="#+id/fragment_test_back_button"
style="#style/myapp_ContinueBackButtonStyle"
android:layout_width="0dp"
android:layout_height="#dimen/myapp_continueBackButton_height"
android:layout_weight="1"
android:background="#color/myapp_colorPrimary"
android:text=„back“
android:textColor="#FFFFFF" />
<Button
android:id="#+id/fragment_test_next_button"
style="#style/myapp_ContinueBackButtonStyle"
android:layout_width="0dp"
android:layout_height="#dimen/myapp_continueBackButton_height"
android:layout_marginStart="24dpVerySmall"
android:layout_weight="1"
android:background="#color/myapp_colorPrimary"
android:text="continue"
android:textColor="#FFFFFF" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
But my turqoise (fragment_test_check) area is not aligned fully with the blue area (fragment_test_scrollview), altough I wrote match_parent. I would like the heigth of turqoise area as the blue area.
Why is my turqoise area dangling in half the blue area?
Set android:fillViewport="true" in your ScrollView.
fillViewport stretches the content's height to the viewport's boundaries, when set true. In simple words, fillViewport decides whether the nested contents of the layout should match the parent's boundaries or not. Thus, to make the nested layout match the parent's boundary, set fillViewport to true
Moreover, the layout_gravity for nested components should be 1 or whatsoever you define in weight_sum of parent layout, whereas in your case it is 0.90 .

Unable to set text view at the bottom of other text view

I have an ImaveView images and corresponding to each image there is TextView img_name and img_source, I want to align them like 1st image should be placed and at right of image image_name should be displayed and below img_name, img_source should be display.
I have tried with below layout code but couldn't work as expected
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="start"
>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageview_cover_art"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:gravity="center_horizontal"/>
<ImageView
android:id="#+id/imageview_favorite"
android:layout_width="24dp"
android:layout_height="24dp"
android:src="#drawable/star_disabled"
android:layout_gravity="bottom|right"/>
</FrameLayout>
<TextView
android:id="#+id/textview_book_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
tools:text="Are You My Mother"
android:textStyle="bold"
android:paddingTop="4dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:gravity="center_horizontal"/>
<TextView
android:id="#+id/textview_book_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
tools:text="Dr. Seuss"
android:gravity="center_horizontal"
android:orientation="vertical"/>
</LinearLayout>
It is displaying like
Image > TextView > TextView
but i require like:
Image > TextView
TextView
Also if first TetView is bigger then need to break in another line
Please help.
Just add the two textview into another linearview with vertical orientation :-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="start"
>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageview_cover_art"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:gravity="center_horizontal"/>
<ImageView
android:id="#+id/imageview_favorite"
android:layout_width="24dp"
android:layout_height="24dp"
android:src="#drawable/star_disabled"
android:layout_gravity="bottom|right"/>
</FrameLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textview_book_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
tools:text="Are You My Mother"
android:textStyle="bold"
android:paddingTop="4dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:gravity="center_horizontal"/>
<TextView
android:id="#+id/textview_book_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
tools:text="Dr. Seuss"
android:gravity="center_horizontal"
android:orientation="vertical"/>
</LinearLayout>
</LinearLayout>
Simply place views in Linear Layout after that add android:orientation="vertical"
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textview_book_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
tools:text="Are You My Mother"
android:textStyle="bold"
android:paddingTop="4dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
/>
<TextView
android:id="#+id/textview_book_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
tools:text="Dr. Seuss" />
</LinearLayout>

How to adjust screen size for all android devices

I just can't seem to find a way to fit the photo onto my home page screen, I've tried many different ways but it wasn't successful. I have an image that pushes the layout that is under it and squeezes the buttons on the screen. Is there any way I can auto resize the height it for different screens? I only need the height since the width should be match_parent. Below is the xml file.
<?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:background="#color/white"
android:orientation="vertical"
>
<RelativeLayout
android:id="#+id/headerLayout"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="#drawable/nav_bar_homepage" >
<TextView
android:id="#+id/pageTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="WELCOME!" />
<ImageView
android:id="#+id/hiddenIcDrawer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:src="#drawable/nav_icon"
android:visibility="invisible" />
</RelativeLayout>
<ImageView
android:id="#+id/horLine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/headerLayout"
android:scaleType="center"
android:src="#drawable/line_big_white" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:scaleType="fitStart" >
<ImageView
android:id="#+id/image_homepage_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_gravity="center_horizontal"
android:src="#drawable/image_homepage"
/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:layout_margin="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/field_red_homepage"
android:orientation="vertical"
android:scaleType="fitXY" >
<ImageView
android:id="#+id/logo_homep"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
android:paddingBottom="10dp"
android:scaleType="fitXY"
android:src="#drawable/logo_homepage" />
<ImageView
android:id="#+id/redeembytton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/btn_redeemrewards"
android:scaleType="fitXY" >
</ImageView>
<ImageView
android:id="#+id/orderbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:background="#drawable/btn_orderonline"
android:scaleType="fitXY" >
</ImageView>
<ImageView
android:id="#+id/rewardsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:background="#drawable/btn_earnrewards"
android:scaleType="fitXY" >
</ImageView>
<ImageView
android:id="#+id/referFriendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:background="#drawable/btn_referafriend"
android:scaleType="fitXY" >
</ImageView>
</LinearLayout>
</LinearLayout>
Wrap you xml code into ScrollView if you are okay with scrolling down to reach your contents under ImageView. If you don't want to scroll down, it is best to use "weight" attribute. I am giving you an idea how to implement it in your code.
Let's say the height of the whole screen is 1. You have an image and a button to put on the screen for all devices. So split the screen into two parts... 0.8 for imageview and 0.2 for the button. You can use the code below to get this result.
<?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="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp" <!-- Important >
android:orientation="vertical"
android:layout_weight=".8" > <!-- 80% of your screen size>
<!-- Your ImageView >
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp" <!-- Important >
android:orientation="vertical"
android:layout_weight=".2" > <!-- 20% of your screen size>
<!-- Your Button >
</LinearLayout>
</LinearLayout>
Try this
I think you should use
android:layout-weight="1" for adjusting the height according to different devices.use it in your <ImageLayout/>.

Android ListActivity doesn't center the items

I have a ListActivity which displays an image and some text.
The imageView has a fixed size and I want to center the list horizontally.
After endless tries, I set any gravity to center_horinzontal, but nothing works.
The list is shown over the whole screen, as I can see by the scrollbar on the right side. But my frame remains on the left side.
In the adapter class, which entends ArrayAdapter, I even tried to set the gravity during runtime, but when I try to manipulate then basic LinearLayout, I get the error:
"java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams"
My attempt to change gravity in the method getView() which throws the error:
LinearLayout layout = (LinearLayout) rowView.findViewById(R.id.bootsFrameLayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
params.gravity = Gravity.CENTER_HORIZONTAL;
layout.setLayoutParams(params);
The list row is designed in a layout file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/bootsFrameLayout"
android:layout_width="300dp"
android:layout_height="250dp"
android:layout_gravity="center_horizontal"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:background="#drawable/card_blank2"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/bootsName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="TextView"
android:textAppearance="#style/BootNameStyle" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginEnd="30dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginStart="30dp" >
<ImageView
android:id="#+id/bootImage"
android:layout_width="240dp"
android:layout_height="180dp"
android:layout_gravity="center_horizontal"
android:scaleType="centerCrop"
android:src="#drawable/test_boot" />
<TextView
android:id="#+id/textWatchList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:background="#color/gray_transparent"
android:gravity="end"
android:lines="2"
android:text="#string/watchlist"
android:textAppearance="#style/BootWatchListStyle" />
<ProgressBar
android:id="#+id/bootProgressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:visibility="invisible" />
</FrameLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="25dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginStart="25dp" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/bootMasse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="70"
android:text="TextView"
android:textAppearance="#style/BootInfoStyle" />
<TextView
android:id="#+id/bootBaujahr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="30"
android:gravity="right"
android:text="TextView"
android:textAppearance="#style/BootInfoStyle" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/bootLand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="70"
android:text="TextView"
android:textAppearance="#style/BootInfoStyle" />
<TextView
android:id="#+id/bootPreis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="30"
android:gravity="right"
android:text="TextView"
android:textAppearance="#style/BootPreisStyle" />
</TableRow>
</TableLayout>
The ListView is defined here:
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_marginEnd="12dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginStart="12dp"
android:layout_weight="8" >
</ListView>
The activity occupies the whole screen, as it should do.
But although there is place enough, the frame won't center.
Any ideas?

Resources