android add button to title bar - java

I am trying to add a button to title bar but I couldn't succeed. I have read many articles about this but they all explain the way that doesn't fit my situation. I am using ScrollView to make the screen scrollable. but articles suggest me to use linear layout. How can I make it scrollable and have a button on the title bar?
here is my XML
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#C0C0C0"
android:id="#+id/sw_layout">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:id="#+id/ln_layout">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft=<"#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
.........................
.........................
.........................
</TableLayout>
</LinearLayout>
</ScrollView>
And in my MainActivity, I have this code....
LinearLayout ln = (LinearLayout) getWindow().findViewById(R.id.ln_layout);
Button btn = new Button(this);
btn.setText("Test");
ln.addView(btn);
But this doesn't display anything and it doesn't give me any error. Please give me an idea. how can I add the button to title bar?

make title.xml and design your title : place button etc
on activity
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
you will access to elements on title the same way you do on main layout .using findviewbyid
for avoiding some look problems
<style name="theme">
<item name="android:windowTitleBackgroundStyle">#style/themeTitleBackground</item>
<item name="android:windowTitleSize">65dip</item>
</style>
<style name="themeTitleBackground">
<item name="android:layout_height">wrap_content</item>
</style>
inside androidmanifest
<activity android:theme="#style/theme"

Related

First item in bottom navigation bar is not visible

First item in bottom navigation bar is not visible, although its clickable and working properly.
Tried the solutions given to this question: KOTLIN - First item in BottomNavigationBar isn't visible, title issues
But nothing seems to work out.
<RelativeLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MapsActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="#color/white"
app:itemIconTint="#drawable/selector"
app:itemTextColor="#drawable/selector"
app:labelVisibilityMode="labeled"
app:menu="#menu/nav_menu" />
This is the code in my xml file regarding the bottom navigation bar.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Information"
android:id="#+id/information_nav_bar"
android:icon="#drawable/ic_info" />
<item android:title="Table"
android:id="#+id/table_nav_bar"
android:icon="#drawable/ic_info" />
This is my navigation menu xml.

How to disable the line divider in the navigation menu?

I want to disable the line divider in my navigation menu. I tried a code out, but it doesnt worked. I hope someone can help me :)
That is the code that I tried:
<item name="android:listDivider">#android:color/transparent</item>
Ok you have to add the Style :
<item name="android:listDivider">#android:color/transparent</item>
but you have to define it as background in your divider view :
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/listDivider"/>
</FrameLayout>

Actionbar title and subtitle alignment and textSize in fragments

I am using actionbar and displaying some title and subtitle of the actionbar. I want the size of subtitle to be smaller than the title. And also I have a logo icon in left. So how should I adjust the alignment.
There's an another fragment in which I am using back button as home button. So, I also want to reduce the space between the back button and the Title of actionbar.
I tried using this code. But it didn't help.
UserListFragment.java
View viewActionBar = getActivity().getLayoutInflater().inflate(R.layout.actionbar_layout, null);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT,ActionBar.LayoutParams.MATCH_PARENT,Gravity.CENTER);
TextView textviewTitle = (TextView) viewActionBar.findViewById(R.id.mytext);
textviewTitle.setText("DemoIosAppDoctor"+"\n"+"Doctor");
actionBar.setCustomView(viewActionBar, params);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
}
actionbar_layout.xml
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:id="#+id/mytext"
android:textSize="18sp"
android:layout_marginTop="5dp"
android:layout_marginLeft="-2dp"/>
</LinearLayout>
To completely customise Actionbar, you should define Toolbar in design XML and add you own component there. and, then use like this
Toolbar tb = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(tb);
Use XML like this:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
Note: You have to set this activity theme as NoActionBar like this:
<activity
android:name=".YourActivity"
android:parentActivityName=".index.IndexActivity"
android:theme="#style/AppTheme.NoActionBar" />
And theme like this:
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>

transparent toolbar for Navigational Drawer theme of Android Studio with image overlapping toolbar

I am a fresher to Android Design. I Have Android Studio's Navigational DrawerLayout, when my app loads I'm loading Initial Fragment for the app.
I want to make the toolbar transparent
I want to have an image that is overlapping the DrawerLayout's toolbar
and for other Fragments I want the same toolbar to be opaque.
First Screen when app Loads
When any other fragment is called
For No :1.
All you need to is to define theme which hide action bar, define action bar style with transparent background and set this style to the toolbar widget. Please note that toolbar should be drawen as last view (over all view tree)
<style name="Theme.Custom" parent="#android:style/Theme.AppCompat">
<item name="windowActionBar">false</item>
<item name="windowActionBarOverlay">true</item>
<item name="android:windowActionBarOverlay">true</item>
</style>
<style name="CustomActionBar" parent="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:windowActionBarOverlay">true</item>
<!-- Support library compatibility -->
<item name="windowActionBarOverlay">true</item>
</style>
Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Toolbar should be above content-->
<include layout="#layout/toolbar" />
</RelativeLayout>
Toolbar layout:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="#style/CustomActionBar"/>
For No : 2
Put ImageView in relative layout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Toolbar should be above content-->
<include layout="#layout/toolbar" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:transitionName="photo_hero"
android:id="#+id/image"
/>
</RelativeLayout>
Toolbar layout:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="#style/CustomActionBar"/>

How to Design a Android Overlay Layout

I am trying to make a alert dialog which will show like the picture.
Im trying to use frame layout but can't make the it right like the picture. In my current layout I used an imageView and textview.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src=""/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#strings/text"
/>
</FrameLayout>
<!---<other layouts--->
</LinearLayout>
Create an Activity it represents your overlay.
Put this in your onCreate
requestWindowFeature(Window.FEATURE_NO_TITLE);
#Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE); //importan no title show
super.onCreate(savedInstanceState);
setContentView(R.layout.overlay);
}
put this in styles.xml
<style name="FloatingPopup" parent="#android:style/Theme.Black.NoTitleBar.Fullscreen">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:background">#android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowNoTitle">true</item>
</style>
In your manifiest declare the activity thath looks like a overlay and add the theme
<activity
android:name=".YourOverlayActivity"
android:screenOrientation="portrait"
android:theme="#style/FloatingPopup" >
</activity>
It looks like a semitransparent activity and now you can custom with xml layout editor.
A FrameLayout will stack the Views, causing them to overlap each other. If you want one directly above the other, consider using a LinearLayout with the orientation set to vertical.
Update: Try this, replacing your FrameLayout with a LinearLayout:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#strings/text"
/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src=""/>
</LinearLayout>
Obviously fill in the src parameter for the ImageView with your image.

Categories