Rounded view with title and text inside - java

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!!

Related

How to change font,size of text and set text position center ToolBar Android?

I need to change the font size, the font itself, and center the text in the ToolBar.
I took a template from Bottom Navigation Activity studio.
I was trying setting the text size in themes.xml (I was able to change the background color and text color there)
<style name="CustomToolBarStyle" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
<item name="titleTextColor">#color/black</item>
<item name="android:background">#color/white</item>
I was trying to use this option in themes.xml:
<item name="android:gravity">center</item>
It didnt help. I tried to follow this instruction:
https: //stackoverflow.com/questions/26533510/android-toolbar-center-title-and-custom-font Throws android.support.v7.widget.Toolbar into an error and get a crash.
ToolBar
NavbarTemplate
For API level 21+ you can use app:titleTextAppearance property of toolbar
<android.support.v7.widget.Toolbar
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="?actionBarSize"
android:id="#+id/toolbar"
app:titleTextAppearance="#style/yourstyle" />
and override the default title size in a custom style:
<style name="yourstyle" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
<item name="android:textSize">18sp</item>
<item name="android:fontFamily">yourFont</item>
</style>
For centering, you should follow this answer and if this and none of the other answer works for you from the same post https://stackoverflow.com/a/38175403/9917400, than just use a linear layout inside Toolbar.
You can set constraint-layout inside the toolbar
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
app:layout_collapseMode="pin">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:textColor="#color/blue"
android:textSize="16sp"
app:fontPath="fonts/xxx" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.appcompat.widget.Toolbar>
If you're using androidx don't use android.support.v7.widget.Toolbar.
You have to use androidx.appcompat.widget.Toolbar.
Create toolbar.xml in res/layout.
Put this code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="yourToolBarBackgroundColor"
>
<TextView
android:id="#+id/toolbar_title"
style="#style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TestToolBar"
android:fontFamily="yourFont"
android:textSize="yourSizeOfText"
android:textColor="yourTextColor" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<include layout="#layout/activity_main" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Disable ActionBar in themes.xml:
<style name="Theme.YourProject" parent="Theme.MaterialComponents.DayNight.NoActionBar">
For night theme you have to do same thing in themes.xml(night)
Then in MainActivity.java in onCreate:
setContentView(R.layout.toolbar);

How to create custom Table Layout with headers and grid lines

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>

After Androidx migration MaterialCardView wont't be affected by it's style

I'm developing new android app that shows coffee drinks using google material design components. after I migrate my project to Androidx the style won't applying to MaterialCardView IDK what the problem or why this is happening!!.
I tried to upgrade my gradle files, using latest material library version and search over the web couldn't see any similar problem.
<!--this is the card layout:-->
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/coffee_card"
style="#style/CoffeeItemStyle"
android:layout_width="180dp"
android:layout_height="200dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="#dimen/cardview_margin"
android:layout_marginRight="16dp"
android:layout_marginBottom="#dimen/cardview_margin">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/coffee_image"
android:layout_width="0dp"
android:layout_height="0dp"
android:contentDescription="#string/coffee_drink_image"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/coffee_name"
style="#style/CoffeeNameStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
android:text="Name"
app:layout_constraintBottom_toTopOf="#+id/coffee_image"
app:layout_constraintStart_toStartOf="#+id/coffee_image"
app:layout_constraintTop_toBottomOf="#+id/coffee_image"
app:layout_constraintVertical_bias="0.82" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
<!--this is the style(CoffeeItemStyle):-->
<style name="CoffeeItemStyle" parent="Widget.MaterialComponents.CardView">
<item name="cardBackgroundColor">#android:color/white</item>
<item name="cardCornerRadius">#dimen/cardview_radius</item>
<item name="cardElevation">#dimen/cardview_elevation</item>
<item name="android:clickable">true</item>
<item name="android:stateListAnimator">
#animator/shr_next_button_state_list_anim
</item>
</style>
I expect the card view to appear in 4dp radius, 8dp elevation, white background and changing the Z-axis when the card view is clicked.
the image below shows what It looks like after the migration

Android: Changing color of the back button in tool bar

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);

set one image above another android

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.

Categories