how to anchor a view in front of the toolbar?
please help me like this.
indeed I wish to place this half circle above the toolbar as shown below
Try this way
<?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"
android:background="#ffaa">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000"
android:id="#+id/toolbar">
</android.support.v7.widget.Toolbar>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/semicircle"
android:layout_marginTop="-170dp"/>
</LinearLayout>
output:
Keep layout_marginTop for image view as per your convenience
Related
I'm currently using a Card View inside a recycler and the text is left-alligned despite my efforts.
Here is the Book Item Layout.
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:cardCornerRadius="4dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
app:cardBackgroundColor="#color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/bookTitleTextView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
Here is the recycler itself.
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/bookRecycler" />
This is the current output.
enter image description here
As you can see the text is not centered.
I tried using layout gravity to center the text which didn't work.
I also tried using a constraint layout on the text view and using constraints to center the text without any luck.
android:layout_gravity = "center" has already been attempted.
Any advice on what I'm overlooking would be great.
Thank you.
add android:gravity="center" for LinearLayout and you are good to go
I see your LinearLayout have only one child and no special attributes, so you can even remove whole LinearLayout - place TextView straight inside CardView, which extends FrameLayout. For centering childs inside FrameLayout you may also use android:gravity="center" for parent and/or android:layout_gravity="center" for child
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center"
app:cardCornerRadius="4dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
app:cardBackgroundColor="#color/white">
<TextView
android:layout_gravity="center"
... rest of code
android:gravity="center" is the correct code, instead of android:layout_gravity="center_horizontal"
This is resizing when the keyboard is showing instead of scrolling. Why is that?
I have adjustResize set in the Manifest but it still isnt scrolling. What else do I need to change? I have tried removing isScrollableView also.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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:background="#color/night"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:isScrollContainer="true">
<ImageView
android:id="#+id/iv_login_logo"
android:layout_width="#dimen/login_logo_width"
android:layout_height="#dimen/login_logo_height"
android:src="#drawable/travelbank_logo_white"
android:layout_marginTop="#dimen/login_logo_margin"
android:layout_marginBottom="#dimen/login_logo_margin"/>
<!--Tab Layout-->
<android.support.design.widget.TabLayout
android:id="#+id/tl_login_tabs"
android:layout_width="match_parent"
android:layout_height="#dimen/tab_layout_height"
app:tabGravity="fill"
tabMode="fixed"
app:tabTextAppearance="#style/NavigationTabTextAppeareance"
app:tabIndicatorHeight="0dp"
app:tabPadding="0dp"/>
<!--Login Page-->
<android.support.v4.view.ViewPager
android:id="#+id/vp_login_view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</ScrollView>
Turns out you cant have a View Pager inside a Scroll View. I removed the View Pager and everything works fine
Problem I want view-layout like the following photo, on top I want image view, in the middle a text view and bottom list of items. All this I want to be scrollable (black arrow in the picture). in this way, the user for first see image-text-and part of list items, if scroll down, see the list of item.
For the bottom I thought to use listView, but in this way I think I'll have 2 scrollable view (???) and then I want to make the list in 2 columns, and I don't know if it is possible with ListView
Question
I didn't write code, because I want to know what kind of view or somethings else I've to study.
I sketchy something like this, but the result is really horrible
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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">
<RelativeLayout
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
app:srcCompat="#drawable/nope"
android:id="#+id/imageView4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<View
android:id="#+id/centerShim5"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_centerInParent="true"
android:visibility="invisible" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/list"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/textView2"
android:textColor="#color/ame_default_cluster_circle_color_medium"
android:textSize="26dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</ScrollView>
I can't use listview in Scrollview, and inspired by the matryoshka I found the solution.
I define xml1 file in which I have only ScrollView and LinearLayout inside
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/linear">
</LinearLayout>
</ScrollView>
after, for each kind of items that I want to add inside, I define another xml2 and after I add element:
<?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"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/ame_default_cluster_text_color">
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="5"
android:textSize="32sp"
android:textColor="#color/ame_default_cluster_circle_shadow_color"
android:id="#+id/text1"
android:textAlignment="center" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/nope"
android:id="#+id/photo" />
<TextView
android:text=" "
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:id="#+id/descrtiption"
android:textColor="#color/ame_default_cluster_circle_shadow_color"
android:layout_margin="10sp" />
</LinearLayout>
with code:
layoutInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view=layoutInflater.inflate(R.layout.xml1,container,false);
LinearLayout linearLayout=(LinearLayout)item_view.findViewById(R.id.linear);
LayoutInflater inflater= LayoutInflater.from(ctx);
View view1 = inflater.inflate(R.layout.xml2, linearLayout, false);
TextView text1=(TextView)view1.findViewById(R.id.text1);
text1.setText();
ImageView imageview=(ImageView)view1.findViewById(R.id.photo);
and obviously I can define XML for element to add like listview and add it with normal foreach ,Hope this will help someone.
I trying to create a relative layout that look like toolbar but I keep on having such height problem as shown below:
What I want is:
Here's my code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/Searchtoolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:background="#color/Toolbar_default_color"
android:minHeight="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
<RelativeLayout
android:layout_below="#+id/Searchtoolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/FilterLayout"
android:layout_width="match_parent"
android:layout_height="40dp">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/search_filter_icon"
android:background="#drawable/search_filter_bgn_btn"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
<ListView
android:id="#+id/feed_listview"
android:layout_below="#+id/FilterLayout"
android:layout_marginTop="?attr/actionBarSize"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#null" />
</RelativeLayout>
</RelativeLayout>
In your ImageButton use: android:layout_height="match_parent"
instead of android:layout_height="wrap_content"
Or resize your Image using photoshop or any other free tool to fit 40dp exactly.
WRAP_CONTENT: which means that the view wants to be just big enough to enclose its content (plus padding)
What you need:
MATCH_PARENT: which means that the view wants to be as big as its parent (minus padding)
Documentation.
I found my bad and silly mistake. It was the "android:layout_marginTop="?attr/actionBarSize" under the listview that causes the white layout to be extended. After I removed that line, I got what I wanted. =)
So I'm having some trouble with a RecyclerView in a layout file
Here it is:
<android.support.v7.widget.RecyclerView
android:id="#+id/chat_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="#dimen/abc_action_bar_default_padding_material"
android:paddingLeft="#dimen/abc_action_bar_default_padding_material"
/>
<LinearLayout
android:layout_below="#id/chat_listview"
android:layout_width="match_parent"
android:layout_height="#dimen/bottom_bar_height"
android:orientation="horizontal"
>
<EditText
android:id="#+id/chat_input_edittext"
android:layout_weight="7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:inputType="textAutoCorrect"
/>
<Button
android:id="#+id/chat_send_button"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#drawable/ic_action_send_now"
/>
</LinearLayout>
The RecyclerView is visible and scrollable but the LinearLayout and the Views inside are not visible... I tried quite a few things but nothing has worked so far.
Can any of you please point me into the right direction?
Many thanks in advance!
When RecyclerView is drawn, it calculates all the remaining size on screen to itself before drawing the next elements and don't recalculate after the other elements are drawn, leaving them outside the screen.
The trick is to draw all other elements first, and leave RecyclerView for last. FrameLayout does not work anymore so use a relative layout and put the RecyclerView last on the XML layout file.
Example to add a bar with buttons below the RecyclerView:
<?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"
android:padding="16dp"
tools:context=".MainActivity"
>
<LinearLayout
android:id="#+id/pagination_btns"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"> //HERE YOU ALIGN THIS ELEMENT TO THE BOTTOM OF THE PARENT
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/previous_btn_label"/>
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/next_btn_label"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/items_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layout_above="#id/pagination_btns"/> //HERE YOU ALIGN THE RECYCLERVIEW ABOVE THE PAGINATION BAR
</RelativeLayout>
Change the root view to a FrameLayout. Set the gravity of the LinearLayout to bottom
<FrameLayout 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.support.v7.widget.RecyclerView
android:id="#+id/chat_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingRight="#dimen/abc_action_bar_default_padding_material"
android:paddingLeft="#dimen/abc_action_bar_default_padding_material"
/>
<LinearLayout
android:layout_below="#id/chat_listview"
android:layout_width="match_parent"
android:layout_height="#dimen/bottom_bar_height"
android:layout_gravity="bottom"
android:orientation="horizontal">
<EditText
android:id="#+id/chat_input_edittext"
android:layout_weight="7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:inputType="textAutoCorrect"/>
<Button
android:id="#+id/chat_send_button"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#drawable/ic_action_send_now"/>
</LinearLayout>
</FrameLayout>
Alternatively, you can wrap the linear layout in a FrameLayout
and set it to align_parentBottom = true if it is a relative layout or a linear layout at your root and you have other things in your view.