I need to use segmented control in Android. Is there a default control for the same? What might be the best and efficient way to do so?
This will work:
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="#dimen/padding_extreme"
android:stretchColumns="*">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/padding_very_less"
android:background="#color/colorBlack"
android:padding="#dimen/padding_very_less">
<TextView
android:id="#+id/seg_one"
style="#style/barGrapButtons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:background="#color/colorBlack"
android:text="SegmentOne"
android:textColor="#color/colorWhite" />
<TextView
android:id="#+id/seg_two"
style="#style/barGrapButtons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SegmentTwo" />
<TextView
android:id="#+id/seg_three"
style="#style/barGrapButtons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SegmentThree" />
</TableRow>
</TableLayout>
res/values/styles.xml
<style name="barGrapButtons">
<item name="android:textColor">#color/colorBlack</item>
<item name="android:textSize">#dimen/small_text</item>
<item name="android:gravity">center|center_horizontal</item>
<item name="android:clickable">true</item>
<item name="android:padding">#dimen/padding_five_dp</item>
<item name="android:background">#color/colorWhite</item>
<item name="android:layout_marginLeft">1dp</item>
</style>
I guess you are speaking about fragments with tabs on the top. There will be many answers if you search for Tabbed Activity. However, a simple way to start off with is
Right-click in the package - > New -> Activity - > Tabbed Activity
An activity with tabs and fragments will be automatically created which works as the flow which you have shown in the image.
Related
I'm having some troubles setting a theme to PreferenceFragmentCompat trying to achieve this:
I have these styles set:
<style name="AppThemeBase" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowLightStatusBar">true</item>
<item name="android:statusBarColor">#color/status_bar</item>
<item name="colorControlNormal">#color/colorPrimary</item>
<item name="preferenceTheme">#style/AppThemeBase.RecordingPreferencesTheme</item>
</style>
<style name="AppThemeBase.RecordingPreferencesTheme" parent="#style/PreferenceThemeOverlay">
<item name="android:textColorPrimary">#FD0000</item>
<item name="android:textColorSecondary">#1F6F2B</item>
</style>
With this options I can see what I want to achieve in Android Studio layout preview but when it does not have effect when I execute it
The result when I execute:
The only thing it worked so far is using <item name="android:textColor">#FD0000</item> that will change the tittle colour but I can't find a way to change the summary colour.
Another question I have is if there is a way to set the theme just for that preference fragment instead of using it in all preferences fragments. Using android:theme at the root of the fragment layout isn't working for me :(
Update:
As per #Susan question this is how I try to use android:theme at the root of the fragment layout I've created this theme based in the main one
<style name="RecordingPreferenceTheme" parent="AppThemeBase">
<item name="preferenceTheme">#style/AppThemeBase.RecordingPreferencesTheme</item>
</style>
And this is the preference layout where I have a fragment which will inflate the PreferenceFragmentCompat
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:theme="#style/RecordingPreferenceTheme">
<include
android:id="#+id/include"
layout="#layout/toolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<fragment
android:id="#+id/fragment_container_settings"
android:name="com.example.app.RecordSettingsFragment$RecordPreferences"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/include" />
</androidx.constraintlayout.widget.ConstraintLayout>
And at the root of the layout I'll set android:theme="#style/RecordingPreferenceTheme" but it doesn't work, just changes in the main theme set in Activity will have some effect. I've already tried customize PreferenceFragmentCompat layout with same results.
Cheers!!
See this sample project here created for your specific case the the only problem for this solution is you have to create your preference from code cant use xml file to inflate and
for those who just want the gist of this solution is that use setLayoutResource() method to set layout and use custom layout i just copied the original layout and hardcoded colors according to #Bhuntupana need
val messagesCategory = PreferenceCategory(context).apply {
layoutResource = R.layout.sample_preference
key = "messages_category"
title = getString(R.string.messages_header)
screen.addPreference(this)
}
Update
use layout tag to specify layout resource to use like this
<EditTextPreference
android:layout="#layout/sample_preference"
app:key="signature"
app:title="#string/signature_title"
app:useSimpleSummaryProvider="true" />
sample_preference.xml just copy of default layout preference_material.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="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:baselineAligned="false"
android:clipToPadding="false"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:paddingRight="?android:attr/listPreferredItemPaddingRight">
<include layout="#layout/image_frame" />
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingTop="16dp"
android:paddingBottom="16dp">
<TextView
android:id="#android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceListItem"
android:textColor="#FD0000" />
<TextView
android:id="#android:id/summary"
style="#style/PreferenceSummaryTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#android:id/title"
android:layout_alignStart="#android:id/title"
android:layout_alignLeft="#android:id/title"
android:layout_gravity="start"
android:maxLines="10"
android:textAlignment="viewStart"
android:textColor="#1F6F2B" />
</RelativeLayout>
<!-- Preference should place its actual preference widget here. -->
<LinearLayout
android:id="#android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="end|center_vertical"
android:orientation="vertical"
android:paddingStart="16dp"
android:paddingLeft="16dp"
android:paddingEnd="0dp"
android:paddingRight="0dp" />
</LinearLayout>
So I have 3 vertical aligned TextViews. The second one has a larger font size.
Now, if the second TextView holds a string starting with e.g. a 'B' it looks like the View is indented:
Here's my XML layout:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView style="#style/TextAppearance.LabelSmall"
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView1" />
<TextView style="#style/TextAppearance.LabelBig"
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B TextView2" />
<TextView style="#style/TextAppearance.LabelSmall"
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView3" />
</LinearLayout>
And here's the corresponding styles.xml file:
<resources>
<style name="TextAppearance.LabelBig" parent="TextAppearance.MaterialComponents.Headline3">
<item name="fontFamily">#font/opensans_regular</item>
<item name="android:fontFamily">#font/opensans_regular</item>
<item name="android:textSize">48sp</item>
</style>
<style name="TextAppearance.LabelSmall" parent="TextAppearance.MaterialComponents.Body1">
<item name="fontFamily">#font/opensans_regular</item>
<item name="android:fontFamily">#font/opensans_regular</item>
<item name="android:textSize">16sp</item>
</style>
</ressources>
Is there any way to make these TextViews look equally indented?
Thanks for your help.
You don't need to use three textviews for single line text, just go for the spannable.
Find some info here: https://medium.com/androiddevelopers/spantastic-text-styling-with-spans-17b0c16b4568
I have a dialog fragment in my app with an autocompletetextview in it, but the drop-down list instead of align with the soft keyboard's top, is placed behind, not giving access to some of the items.
Here is the layout:
<?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="wrap_content"
android:gravity="center"
android:padding="10dp"
android:orientation="horizontal"
android:windowSoftInputMode="adjustPan|adjustResize">
<android.widget.AutoCompleteTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/customDialogAtocompleteTextview"
android:layout_weight=".7"
android:layout_gravity="top" />
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".3">
<Button
android:id="#+id/customDialogBtOk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Aceptar"/>
<Button
android:id="#+id/customDialogBtSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Buscar"/>
<Button
android:id="#+id/customDialogBtMore"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Mas"/>
</LinearLayout>
</LinearLayout>
So how can I make it to align with the keyboard?
Theory says that android:windowSoftInputMode="adjustPan|adjustResize"should do this but for some reason it doesn't, so you have to do the same programmatically:
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
Aand the magic happens!!!
[update]
in my case.. cause i use bottomShetDialogFragment with autocomplete inside. drop down not show cause i not set dropdownanchor for autocomplete.
just add android:dropDownAnchor="#id/layout_above_this_autocomplete" and work perfect
ad news style in your style.xml
<style name="AppBottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
<item name="bottomSheetStyle">#style/AppModalStyle</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowSoftInputMode">adjustResize</item>
</style>
<style name="AppModalStyle" parent="Widget.Design.BottomSheet.Modal">
<item name="behavior_peekHeight">400dp</item>
</style>
in your bottomshetdialog framgnet.
oncreate -> setStyle(DialogFragment.STYLE_NORMAL, R.style.AppBottomSheetDialogTheme)
override fun getTheme(): Int {
return R.style.AppBottomSheetDialogTheme
}
I'm creating small widget and I'm getting pretty frustrated with it. Earlier I was just setting colors as I seen it and it was ok. But now I would like to make it to match system theme settings. How it can be achieved?
As I was requested to post some code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:padding="#dimen/widget_margin"
android:orientation="vertical"
style="#android:style/Theme.Holo.Light">
<!-- pasek górny z przyciskiem dodawania -->
<GridLayout
android:layout_width="fill_parent"
android:layout_height="37dp"
android:rowCount="1"
android:columnCount="2"
android:id="#+id/Header"
android:background="#android:color/holo_blue_dark">
<ImageButton
android:layout_width="37dp"
android:layout_height="37dp"
android:id="#+id/AddButton"
android:layout_row="0"
android:layout_column="1"
android:contentDescription="Add" />
</GridLayout>
<!-- panel z wiadomościami -->
<ListView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:id="#+id/ListView"
android:layout_weight="1"
style="#android:style/Widget.Holo.ListView"
android:background="#android:color/holo_green_light" />
<!-- stopka widgetu -->
<GridLayout
android:layout_width="fill_parent"
android:layout_height="37dp"
android:rowCount="1"
android:columnCount="2"
android:layout_gravity="end"
android:id="#+id/Footer"
android:background="#android:color/holo_blue_dark">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/copyright"
android:id="#+id/textView"
android:layout_column="1"
android:layout_row="0"
android:textColor="#android:color/white"
android:layout_gravity="center_vertical" />
</GridLayout>
</LinearLayout>
I want my widget to blend in system this time. As I can use themes which provide some icons, some colors and graphics I would my widget to use those colors to match other components. Is it possible?
If you don't set any android:color, android:background or such, the views will inherit colours from your theme.
So first make sure you are inheriting from a theme that satisfies you.
<style name="MyStyle" parent="Theme.Material">
</style>
About your widgets, if their natural tint is not what your looking for, then you have to customize the above mentioned theme from styles.xml. You can easily find how to style buttons, listviews and such.
Here is an example for buttons:
<style name="MyStyle" parent="Theme.Material">
<item name="android:ButtonStyle">#style/MyStyle.Buttons</item>
...
</style>
<style name="MyStyle.Buttons" parent="Widget.Material.Button">
<item name="android:background">#color/my_color</item>
...
</style>
First you say that all buttons should be styled from MyStyle.Buttons. Then you define MyStyle.Buttons and add all the features you want. This way all your buttons will have the same background, and you don't need to do it by hand.
There's plenty of questions about this topic.
I have the following layout XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_gravity="center"> // The layout_gravity doesn't seem to affect
// the button Views
// (I guess this is the wrong way to do it)
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Genre1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Genre2"
/>
</LinearLayout>
How do I make a single general attribute configuration e.g. android:layout_gravity="center" for all my Button Views?
You could wrap common attributes for a certain View under a style.
Create a styles.xml file in your values folder, and just declare the styles:
<style name="list_item_title">
<item name="android:textStyle">bold</item>
<item name="android:textColor">#android:color/black</item>
<item name="android:textSize">18sp</item>
<item name="android:padding">0dp</item>
</style>
Then, on your TextView (or Button, or whatever View...):
<TextView
android:id="#+id/tv1"
style="#style/list_item_title" />
Write below line
android:gravity="center"
instead of
android:layout_gravity="center"
it will solve your problem.
have you tried android:gravity="center" instead of android:layout_gravity="center"
or you can try android:layout_gravity="center" in your buttons xml