Cant set the tab background dynamically by using tabBackground in code - java

Hi friends please help me out i want to make layouts like first two screens but i am getting layouts like 3,4 screens i had made using the selector.here in the xml i used tabBackground by called selector drawable in it if i can able get the tabBackground in the java code then i can able set it by using the position but i cant able found that please help me out guys
Here is my layout code
<LinearLayout 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:orientation="vertical"
>
<android.support.design.widget.TabLayout
android:id="#+id/icon_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/toolbar_bg"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
app:tabBackground="#drawable/tab_background"
app:tabIndicatorColor="#color/transparent"
app:tabGravity="fill"
app:tabMode="fixed" />
<FrameLayout
android:id="#+id/main_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
My Selector code
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/tab_bg_redcolor" android:state_selected="true"/>
<item android:drawable="#drawable/toolbar_bg"/>
</selector>

Related

BottomNavigationBar, doesnt showIcons when adding the menu

I'm trying to implement a bottomr navigation bar for an app.
I'm using the BottomNavigationView
I was following the explanation from a youtube video step by step yet when getting to that part, when adding the menu in it doesnt even display in the activity main
I have not found an answer to a similar problem anywhere else, and im hoping its something im just missing as codes are almost identical to the explanations
This is my activity main
<?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"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bnv"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#d6ecef"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEndtoEndof="parent"
app:layout_constraintStart_toStartOf="parent"
app:labelVisibilityMode="labeled"
app:menu="#menu/m_1"/>
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="409dp"
android:layout_height="673dp"
app:defaultNavHost="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEndtoEndof="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/my_nav"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
This is linked from my menu class, which have ids linked to my fragments in a nav graph
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/firstFragment"
android:icon="#drawable/ic_baseline_home_24"
android:title="Home"></item>
<item
android:id="#+id/secondFragment"
android:icon="#drawable/ic_baseline_directions_bus_24"
android:title="Bus"></item>
<item
android:id="#+id/frag3"
android:icon="#drawable/ic_baseline_settings_24"
android:title="Settings"></item>
</menu>
I struggled with your code for about half an hour. The main problem is choosing a theme.
Change the theme to "Theme.AppCompat.Light.NoActionBar" The problem is solved

Why doesn't the Bottom Navigation Menu appear in Android Studio

I am trying to add a bottom navigation menu for the application i am trying to make, I followed every step of the video and the bottom nav menu still not showing up. Can someone help me?
Dashboard xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="Dashboard.Dashboard">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragmentContainerView"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/bottom_nav" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_nav_view"
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:menu="#menu/bottom_nav_menu"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Bottom_Nav_Menu xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/feedFragment"
android:title="#string/home"
android:icon="#drawable/home_icon"/>
<item
android:id="#+id/searchFragment"
android:title="#string/search"
android:icon="#drawable/search_icon"/>
<item
android:id="#+id/postFragment"
android:title="#string/add"
android:icon="#drawable/add_post"/>
<item
android:id="#+id/notificationFragment"
android:title="#string/notification"
android:icon="#drawable/notification_icon"/>
<item
android:id="#+id/profileFragment"
android:title="#string/profile"
android:icon="#drawable/person_icon"/>
</menu>
never mind, I got it work somehow. Everything was working perfectly. I just needed to add a library before I use the bottom navigation bar.
This library in your build.grandle
implementation 'androidx.navigation:navigation-fragment:2.4.1'
implementation 'androidx.navigation:navigation-ui:2.4.1'

BottomNavigationView stuck at top

I've been trying to get a BottonNavigationView working. It just keeps sticking to the top of the layout and it's driving me nuts.
I've looked at Material Design Documentation for BottomNavigationView and it seems really simple, but I can't get it to work, https://material.io/develop/android/components/bottom-navigation/. I even tried making a completely clean project where I tried it and the same thing happened.
<?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="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity"
android:id="#+id/mainLayout">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
app:menu="#menu/bottom_bar_menu" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/content"
android:enabled="true"
android:icon="#drawable/ic_menu_black_24dp"
android:title="Content"/>
<item
android:id="#+id/document"
android:enabled="true"
android:icon="#drawable/ic_picture_as_pdf_black_24dp"
android:title="Document"/>
<item
android:id="#+id/search"
android:enabled="true"
android:icon="#drawable/ic_search_black_24dp"
android:title="Search"/>
</menu>
Screenshot
Any ideas?
The gravity of your bottom navigation is set to start android:layout_gravity="start" changing it to android:layout_gravity="bottom" should work for you.
I would like to recommend to use ConstraintLayout and set constraints in Designer tool. Will be easier if you are new in android world.
<?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:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/bottom_bar_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>

Make Layout transparent

Ive got a Layout where I have viewpager with sliding tabs.Now I want to make the upper part of my layout transparent so I get the effect that the activity before it is kinda visible.
It should look like this :
My Layout :
<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"
android:orientation="vertical"
tools:context=".Main">
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"
android:layout_marginTop="200dp" //THIS part should make the upper piece transparent
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<com.kas.SlidingTabs.SlidingTabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
android:background="#color/colorPrimary"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_height="match_parent"
android:layout_width="match_parent"
></android.support.v4.view.ViewPager>
My Apptheme I used in this Activity :
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
I allready tried #android:color/transparent as background in LinearLayout but its not helping.I also tried this solution https://stackoverflow.com/a/8831226/4035864
But then my logcat says :You need to use a Theme.AppCompat theme (or descendant) with this activity.
EDIT:
ToolbarLayout :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#color/colorPrimary"
android:elevation="2dp"
android:theme="#style/Base.ThemeOverlay.AppCompat.Dark"
xmlns:android="http://schemas.android.com/apk/res/android" />

Setting Background Image for a ListView

Hey Guys facing a problem with setting a background image on my ListView. Code is as below
history.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:layout_weight="1"
android:drawSelectorOnTop="false"
android:src="#drawable/back">
</ListView>
<TextView android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFff00"
android:text="No data"
android:cacheColorHint="#00000000"
/>
</LinearLayout>
rowlayout.xml
<?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">
<TextView android:id="#+id/ProductName"
android:textSize="25sp"
android:textColor="#FFFF00"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Java Code
...
setListAdapter(new ArrayAdapter<String>(this, R.layout.rowlayout,R.id.ProductName,Names));
...
I dont know what i am doing here but the background does not appear. Its black throughout. If i put the background on the text view in rowlayout.xml then it appears but its like whole screen carries one entry of the ListView. Tried alot of tutorials. Maybe doing some rookie mistake.. help me out please
use android:background="#drawable/icon" for listview like
<ListView android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:layout_weight="1"
android:drawSelectorOnTop="false"
android:background="#drawable/icon">
</ListView>
use android:background="#00000000" on your listview items, then set the parent forms background to your image
As I understand your code concept correctly you want to achieve two Views lying on the LinearLayout, one of which (TextView in your case) cover layout with it's background and another one is transparent. So there is no other way than to set background for your layout and make your ListView transparent, using android:background="#00000000" and also you should set android:cacheColorHint="#00000000" to avoid filling your ListView background with black during scrolling
I believe what you're experiencing is described here: http://android-developers.blogspot.com/2009/01/why-is-my-list-black-android.html
Because of an optimization called "cache color hint", the background of your list is being set by default to the window's background color, #191919 in Android's dark theme.
If you set a background on each row, I think you'll see what I mean. Try setting the following background shape:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<stroke
android:color="#color/mid_gray"
android:width="1dp"
/>
<padding
android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="3dp"
/>
<corners android:radius="10dp" />
<solid android:color="#android:color/white" />
</shape>
like this:
<?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:background="#drawable/shape_data_background">
<TextView android:id="#+id/ProductName"
android:textSize="25sp"
android:textColor="#FFFF00"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

Categories

Resources