My activity screenshot having red highlight area where i want to set title
I inflated a view in my activity.I want to set a title for it, setTitle and label tag in manifest is not working i have tried like thousand times.
How to set title in highlighted area of screenshot my code for this is below
FrameLayout frameLayout = (FrameLayout) findViewById(R.id.frameHome);
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View inflate = layoutInflater.inflate(R.layout.activity_main, null, true);
frameLayout.addView(inflate);
If you want to set a title at the right corner of your app bar, you can put a TextView in the Toolbar.
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Your Title"
android:layout_gravity="end"/>
</FrameLayout>
</android.support.v7.widget.Toolbar>
Add this Toolbar in your Activity layout at the top: activity_main.xml
Related
I have an app in which I have a tab layout which contains three tabs with a different fragment.What I want to add a dropdown to tab 0. How do I do that
code:-
mTabLayout.getTabAt(0).setIcon(tabIcons[0]);
mTabLayout.getTabAt(1).setIcon(tabIcons[1]);
mTabLayout.getTabAt(2).setIcon(tabIcons[2]);
enter image description here
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatSpinner
android:layout_width="wrap_content"
android:id="#+id/spinner"
android:layout_height="wrap_content"/>
</LinearLayout>
//Java code
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.test,
(ViewGroup) findViewById(R.id.spinner));
mTabLayout.getTabAt(0).setView(layout);
I have a toolbar in my xml file, with no child views defined in it. I want to add a TextView on exact location of default title in toolbar when we call setSupportActionBar(toolbar). On particular events, I like to fade in or out these two views in case of particular events.
I tried to do the following in onCreate() of the activity, but nothing is visible except the default title in toolbar.
TextView tv = new TextView(this);
tv.setLayoutParams(toolbarTitle.getLayoutParams());
tv.setHeight(toolbarTitle.getHeight());
tv.setWidth(toolbarTitle.getWidth());
tv.setText("Other Title");
tv.setLeft(toolbarTitle.getLeft());
toolbar.addView(tv);
I also tried to add a RelativeLayout inside the toolbar with width and height set to match parent, but this time, default title TextView of toolbar disappears.
How can add another TextView on exact location of default title TextView in toolbar?
You can use AppBarLayout as below
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
>
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/aadhaar_sign_up"
android:textColor="#color/white"
android:textSize="18.0sp"
app:fontName="Font-Medium.ttf"
/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
Then you use the textview as below
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
((TextView) toolbar.findViewById(R.id.toolbar_title)).setText(title);
I disable title in toolbar using getSupportActionBar().setDisplayShowTitleEnabled(false) in onCreate() method of my activity and put two overlapping textViews encapsulating them with relativeLayout inside toolbar in layout. Then I animate them as I want. While changing text, I used titleTextView.setText() rather then toolbar.setTitle() method.
I want to addView into Bottom bar. I used this Bottom bar liabrary. compile 'com.roughike:bottom-bar:1.2.1'. The problem is when i add view, the view appears at top of screen rather than bottom. Kindly tell me how to add and show view in Bottom bar at bottom side of screen accurately.
final BottomBar bottomBar = BottomBar.attachShy((CoordinatorLayout) findViewById(R.id.coordinatorlayout), findViewById(R.id.myScrollingContent), savedInstanceState);
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
addMainTypeView = layoutInflater.inflate(R.layout.add_main_type, null);
bottomBar.addView(addMainTypeView);
add_main_type.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="#dimen/five_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<TextView
android:id="#+id/add_main_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="bottom bar"
android:hint="#string/enter_type"/>
</LinearLayout>
You might try the new Bottom Sheet component in [Android Design Support library]:
Tutorials here: http://www.truiton.com/2016/07/android-bottom-sheet-example/
Following XML is a test layout for my view. It shows a NavigationDrawer on the left side with a toolbar on top of the view. The toolbar shows a title and a hamburger-Icon on the top left to toggle the NavigationDrawer. Everything works fine:
My XML (I removed all height/width attributes because it's better readable):
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:fitsSystemWindows="true">
<LinearLayout
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/my_awesome_toolbar"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
<!-- ** MY VIEW ELEMENTS **-->
</LinearLayout>
<LinearLayout
android:id="#+id/drawer_leftpane"
android:orientation="vertical"
android:layout_gravity="left|start"
android:background="#FFFFFF"
android:fitsSystemWindows="true">
<TextView
android:text="Test string on top of list" />
<android.support.v7.widget.RecyclerView
android:id="#+id/drawer_menulist"
android:scrollbars="vertical"
android:choiceMode="singleChoice"
android:divider="#null" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
In the section <!-- ** MY VIEW ELEMENTS **--> I insert the view Elements for the different views (that's the only thing who changes between all views).
I have many views which need this layout above. I know about includes, merges and compound views and read many articles about these. But I am not able to create a layout for this special case and I would like to avoid copy paste the whole layout throughout all my views.
In the end I want something like this for all my views:
<?xml version="1.0" encoding="utf-8"?>
<MyDrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drawer_layout"
android:fitsSystemWindows="true">
<!-- ** MY VIEW ELEMENTS **-->
</MyDrawerLayout>
Is that even possible? Or is there another way to achive this? Thanks in advance
Well, I would just use LayoutInflater.
First of all I would create FrameLayout as a container for my layouts.
Then I would inflate required layout.
And if I would need to inflate another layout, I would just do view_container.removeAllViews( ) and inflate that another layout.
Everything in action
main.xml
<!-- it's not full layout, I've just taken a fragment from your example -->
<LinearLayout
android:orientation="vertical" >
<android.support.v7.widget.Toolbar
android:id="#+id/my_awesome_toolbar"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
<!-- This is where you hold required layout -->
<FrameLayout
android:id="#+id/view_container"
android:layout_width = "match_parent"
android:layout_height = "match_parent" />
</LinearLayout>
Main.java
public class Main extends AppCompatActivity
{
#Override public void onCreate( Bundle savedInstaceBundle )
{
super.onCreate( );
setContentView( R.layout.main );
FrameLayout view_container = ( FrameLayout ) findViewById( R.id.view_container );
/** To inflate layout **/
LayoutInflater inflater = getApplicationContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
inflater.inflate( /*R.layout.my_layout */, view_container );
/** To remove layout in 'view_container' **/
view_container.removeAllViews( );
/** To change layout in 'view_container' **/
// 1. Remove all views
// 2. Inflate required layout
}
}
P.S. I wrote it all in here, so I may have made some mistakes but the idea is still the same!
You need to remove this. This is the wrong idea:
<LinearLayout
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/my_awesome_toolbar"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
<!-- ** MY VIEW ELEMENTS **-->
</LinearLayout>
Just use this:
<android.support.v7.widget.Toolbar
android:id="#+id/my_awesome_toolbar"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
Find the View in Java, and then add views you inflate into it.
Then you can have separate XML layouts for the different Toolbar configurations you have and inflate them dynamically.
E.G.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
orientation:horizontal>
<TextView
android:id="#+id/action_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:singleLine="true"
android:ellipsize="end"
android:layout_marginRight="#dimen/dp_fortytwo"
android:text="#string/app_name"
android:textColor="#color/primary_text"
android:textSize="#dimen/dp_eighteen"/>
</LinearLayout>
It's convenient to keep a Singleton class for your navigation that has a reference to a ViewGroup in the Toolbar. You could inflate an "actionbar" into the Toolbar:
toolBar = (Toolbar) drawerLayout.findViewById(R.id.toolbar);
context.setSupportActionBar(toolBar);
actionBar = (ViewGroup) LayoutInflater.from(context()).inflate(R.layout.actionbar, toolBar, false);
toolBar.addView(actionBar);
Then add views as needed dynamically:
LayoutInflater.from(context).inflate(R.layout.actionbar_home_icon, actionBar, true);
I am trying to design a 3D page curl.
In my mainActivity, I have a viewPager which contains pages to be curled. For each page I have a separate layout file.
In the layout file if i just add a text view like below it is showing up fine:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/PageTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/green"
android:text="VIEW 1" /> -->
But if I wrap the textview inside relative layout, it just shows a white blank screen and textview is not shown.
<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"
tools:context=".DemoActivity" >
<TextView
style="#style/PageTitle"
android:id="#+id/sampletextview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="VIEW 1" />
</RelativeLayout>
Can someone please help me to know why i am not able to use relative layout in my view layout file:
I inflate the view like this:
LayoutInflater inflater = (LayoutInflater) myAppContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view1 = inflater.inflate(mViewIds[0], null);
Try changing the second parameter of the inflate() call from null to the ViewGroup that view1 is added to. Or post the code where you add it (and include the part where you define the LayoutParams).