I spent almost the whole weekend to figure out how to create a table structure with headers for columns and rows and include ImageButtons in the cells of the table. The grid lines should be visible.
I tried lot of different layouts... Grid, Linear, Structured, CoordinatorLayout .... view nested GridViews and so on. But couldn't figure out how to accomplish something like this?
Some kind of nested layout magic?
Create a drawable cell_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape= "rectangle" >
<stroke android:width="0.5dp" android:color="#4E4E4E"/>
</shape>
In your styles.xml define your custom cell style
<style name="Cell">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">match_parent</item>
<item name="android:layout_weight">1</item>
<item name="android:background">#drawable/cell_shape</item>
<item name="android:gravity">center</item>
<item name="android:paddingTop">8dp</item>
<item name="android:paddingBottom">8dp</item>
<item name="android:textSize">12sp</item>
</style>
table_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<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">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
style="#style/Cell"
android:text="\"
android:textStyle="bold" />
<TextView
style="#style/Cell"
android:text="My Title 1"
android:textStyle="bold" />
<TextView
style="#style/Cell"
android:text="My Title 2"
android:textStyle="bold" />
<TextView
style="#style/Cell"
android:text="My Title 3"
android:textStyle="bold" />
</TableRow>
<TableRow>
<TextView
style="#style/Cell"
android:text="Arg 1"
android:textStyle="bold" />
<ImageButton
style="#style/Cell"
android:src="#mipmap/ic_launcher" />
<ImageButton
style="#style/Cell"
android:src="#mipmap/ic_launcher" />
<ImageButton
style="#style/Cell"
android:src="#mipmap/ic_launcher" />
</TableRow>
<!--Next Rows...-->
</TableLayout>
<!--//..-->
</RelativeLayout>
Related
How can I make this? It looks nice and I'd like to use something like this.
The lines at the top should correspond to the end of the title
Actually, these types of view are mostly done with custom views.
This tutorial is useful for custom views.
But you can cheat a little bit, create backgound.xml inside the drawable folder, and paste the code:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:color="#color/blue" android:width="4dp"/>
<corners android:radius="10dp"/>
<solid android:color="#color/white"/>
</shape>
then create custom_background.xml for your layout, and paste the following code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="300dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<LinearLayout
android:background="#drawable/background"
android:layout_marginTop="15dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textSize="20sp"
android:textAlignment="center"
android:textColor="#color/blue"
android:layout_gravity="center"
android:text="Something"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView
android:padding="2dp"
android:textColor="#color/blue"
android:textSize="25sp"
android:background="#color/white"
android:layout_marginStart="50dp"
android:text="Title"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
Then you will get the following picture
Note
I am using androidX
Instead of LinearLayout you can place any layout
1) Migrate your project to androidx.
2) Include dependency in your build.gradle:
implementation 'com.google.android.material:material:1.0.0'
3) Use in your layout:
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_height="wrap_content"
android:hint="#string/hint_text">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
4) Don't forget to change your Base Application Theme AppTheme to Material Component theme.
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
You need to use material component theme!!
its a simple linear layout with 1 ImageView and 6 TextViews and a ScrollView but TextViews are appearing at the end of the screen and not below ImageView. I have tried editing my dp values in styles.xml but still not working please have a look!!
I have also tried editing my ImageView tags but still not working and don't know why TextViews are appearing at the end of the screen and if I remove ScrollView the texts are not visible.
Layout code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#000000"
tools:context="com.example.stan.sportbusinesscard.MainActivity">
<ImageView
android:scaleType="fitStart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/photo_description"
android:src="#drawable/photo"
android:id="#+id/image"/>
<TextView
style="#style/TitleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title" />
<TextView
style="#style/LightText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text1"/>
<TextView
style="#style/TitleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/where" />
<TextView
style="#style/LightText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/address"
android:autoLink="map"/>
<TextView
style="#style/TitleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/when" />
<TextView
style="#style/LightText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/dateOf" />
</LinearLayout>
</ScrollView>
Styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="LightText" parent="AppTheme">
<!-- Customize your theme here. -->
<item name="android:paddingLeft">18dp</item>
<item name="android:paddingRight">18dp</item>
<item name="android:textColor">#aaaaaa</item>
</style>
<style name="TitleText" parent="AppTheme">
<!-- Customize your theme here. -->
<item name="android:paddingLeft">18dp</item>
<item name="android:paddingRight">18dp</item>
<item name="android:paddingTop">20dp</item>
<item name="android:textSize">18sp</item>
<item name="android:textColor">#FFD700</item>
</style>
</resources>
You ImageView hight is wrap_content this the reason why your textview are at the bottom of your screen
Try this set static height to your ImageView
<ImageView
android:scaleType="fitStart"
android:adjustViewBounds="true"
android:layout_width="match_parent"
android:layout_height="150dp"
android:contentDescription="#string/photo_description"
android:src="#drawable/photo"
android:id="#+id/image"/>
Try this!
In your <ImageView> scaleType is android:scaleType="fitStart" change it to android:scaleType="fitXY"
Don't use Scrollview as parent view and add android:adjustViewBounds="true" to Imageview for fit.
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#000000"
>
<ImageView
android:scaleType="fitStart"
android:layout_width="match_parent"
android:adjustViewBounds="true"
android:layout_height="wrap_content"
android:contentDescription="#string/photo_description"
android:src="#drawable/photo"
android:id="#+id/image"/>
<TextView
style="#style/TitleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title" />
<TextView
style="#style/LightText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text1"/>
<TextView
style="#style/TitleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/where" />
<TextView
style="#style/LightText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/address"
android:autoLink="map"/>
<TextView
style="#style/TitleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/when" />
<TextView
style="#style/LightText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/dateOf" />
</LinearLayout>
</ScrollView>
</LinearLayout>
I have long scroll form, and I need to scroll to its position on form is not valid input (for example, scroll to email edit text when it's not valid email or empty field)
I am facing 2 issues with scroll to EditText inside NestedScrollView
When one edit text is focused, all times scroll to take me directly to the focused scroll view, not to provided view coordinate
When there is no focused item, it's scrolling in a bad way, the wanted Edittext showing trimmed from the button
Note: I am using NestedScrollView with CoordinateLayout
Below is the layout
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/full_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f5f6f5">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginEnd="9dp"
android:layout_marginStart="9dp"
android:layout_marginTop="25dp"
android:text="#string/personal_details"
android:textColor="#a1143c"
android:textSize="14sp" />
<RelativeLayout
android:id="#+id/step1.rel.full_name"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/step.1.title.full_name"
style="#style/business_form_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/firstName" />
<EditText
android:id="#+id/step.1.feild.full_name"
style="#style/business_form_field"
android:layout_width="match_parent"
android:layout_height="34dp"
android:layout_below="#+id/step.1.title.full_name"
android:inputType="textPersonName" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/step1.rel.last_name"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/step.1.title.last_name"
style="#style/business_form_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/last_name" />
<EditText
android:id="#+id/step.1.feild.last_name"
style="#style/business_form_field"
android:layout_width="match_parent"
android:layout_height="34dp"
android:layout_below="#+id/step.1.title.last_name"
android:inputType="textPersonName" />
</RelativeLayout>
<!--Rest Of Views-->
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
here is the style
<style name="business_form_field">
<item name="android:background">#drawable/roundedallsideswhite</item>
<item name="android:layout_marginStart">9dp</item>
<item name="android:layout_marginEnd">9dp</item>
<item name="android:textColor">#383838</item>
<item name="android:textSize">14sp</item>
<item name="android:gravity">start|center_vertical</item>
<item name="android:imeOptions">actionNext</item>
<item name="android:singleLine">true</item>
<item name="android:inputType">textAutoComplete</item>
<item name="android:paddingStart">15dp</item>
<item name="android:paddingEnd">15dp</item>
<item name="android:lines">1</item>
<item name="android:maxLines">1</item>
<item name="android:layout_marginTop">10dp</item>
</style>
I am trying to make the back button on the toolbar black, but I can't find a way. For some reason I don't have toolBar in my xml file, so there is no way to change the color for me. Please let me know if there is another way.
I have the primaryColor white, but the back button is also white.
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="colorControlNormal">#color/colorPrimaryDark</item>
<item name="actionMenuTextColor">#color/colorPrimaryDark</item>
</style>
This is my xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Space
android:layout_width="match_parent"
android:layout_height="40dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/taskNameEditText"
android:textAlignment="center"
android:hint="enter task name"/>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/scrollLinearLayout">
</LinearLayout>
</HorizontalScrollView>
<Space
android:layout_width="match_parent"
android:layout_height="40dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/fromTimeEditText"
android:layout_weight="1"
android:hint="fromTime"
android:textAlignment="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/toTimeEditText"
android:layout_weight="1"
android:hint="toTime"
android:textAlignment="center" />
</LinearLayout>
</LinearLayout>
if you are using toolbar then you can change
toolbar.getNavigationIcon().setColorFilter(getResources().getColor(R.color.blue_gray_15), PorterDuff.Mode.SRC_ATOP);
I want to create a layout like this:
I have 3 different images: the background, the white square and the V sign.
How can I position them as in the photo, when I want the box to move from side to side
when onClick is triggered.
I have tried:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/off_background">
<ImageView
android:id="#+id/bottom_layer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/handle" />
<ImageView
android:id="#+id/upper_layer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/switch_v" />
</FrameLayout>
which gives me the order correctly, but not posed correctly:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="#drawable/off_background">
<ImageView android:id="#+id/checkButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/handle"
android:src="#drawable/switch_v"
android:layout_gravity="center"
android:scaleType="centerInside"/>
</FrameLayout>
Give this a try:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/off_background" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" >
<ImageView
android:id="#+id/bottom_layer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/handle" />
<ImageView
android:id="#+id/upper_layer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/switch_v" />
</RelativeLayout>
</RelativeLayout>
Provide android:layout_gravity="center" for ImageView in your xml
You should really be looking at the TobbleButton.
Your layout should look like this
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/off_background">
<ToggleButton
android:id="#+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn=""
android:textOff=""
android:background="#drawable/toggle_bg"
/>
</FrameLayout>
And use a selector (toggle_bg) for the background like below
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/toggle_on" android:state_checked="true" android:state_pressed="true"/>
<item android:drawable="#drawable/toggle_on" android:state_checked="true" android:state_focused="false"/>
<item android:drawable="#drawable/toggle_off" android:state_checked="false" android:state_pressed="true"/>
<item android:drawable="#drawable/toggle_off" android:state_checked="false" android:state_focused="false"/>
</selector>
This is of course assuming you have the drawables for the different states of the ToggleButton.
Hope that helps.