Android RelativeLayout Button above TextView - java

have 2 text views 1 next to another (using layout_toLeftOf attribute) and I'm trying to place a button above each one of the text views:
1) if I'm using above, alignLeft and alignRight attributes on the button, the button width and height enlarges to fit the size of the text views
2) so I thought of using linear layout so it will fit to the size of the textview , and then place a button with layout_gravity="center", but it's not centered, it's aligned to the left.
Here is the code(its all inside a RelativeLayout):
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/countdown_hours_tv"
android:layout_alignLeft="#id/countdown_hours_tv"
android:layout_alignRight="#id/countdown_hours_tv">
<Button
android:layout_width="#dimen/plus_minus_button_size"
android:layout_height="#dimen/plus_minus_button_size"
android:layout_gravity="center"
android:background="#drawable/plus_sign_button"
android:onClick="increaseHourClicked"/>
</LinearLayout>
<Button
android:layout_width="#dimen/plus_minus_button_size"
android:layout_height="#dimen/plus_minus_button_size"
android:layout_below="#id/countdown_hours_tv"
android:layout_alignLeft="#id/countdown_hours_tv"
android:layout_alignRight="#id/countdown_hours_tv"
android:background="#drawable/minus_sign_button"
android:onClick="decreaseHourClicked"/>

Try using FrameLayout for putting one view on top of another, which is in your case a button on top of textView.

Try adding the Button and the TextView inside a LinearLayout with vertical orientation. Something like this:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView1"
android:layout_gravity="center"/>
</LinearLayout>
<LinearLayout
android:id="#+id/layout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/layout1"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView1"
android:layout_gravity="center"/>
</LinearLayout>
</RelativeLayout>

Related

How to use ScrollView inside RelativeLayout for Android?

I am trying to add multiple TextViews dynamically to my RelativeLayout, and while this works, when the screen is filled with text, the scrolling does not kick in. Any ideas on how to make it scroll-able?
It looks like this: https://imgur.com/a/a7AnOQV (also, while we're at it, how can I make the fab buttons be 0% transparent? i dont understand why u can see through the buttons the text) :(
One way I tried to make it work was to make ScrollView the parent layout (but it would push my buttons on the top side of the screen), but scrolling worked (but idk how to push the buttons back on the bottom of the screen) and also tried other answers found on Stackoverflow, but none seemed to help me. I tried the whole day to fix this goddamn Scroll.
XML Code:
<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/relativeLayoutId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".PostDataFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:fillViewport="true"
android:scrollbars="vertical"
android:id="#+id/scrollViewId">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<TextView
android:id="#+id/row0"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="TextView" />
</RelativeLayout>
</ScrollView>
<android.support.design.widget.CoordinatorLayout
android:id="#+id/coordinatorLayoutId"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:clickable="true"
android:src="#drawable/baseline_add_white_24dp"
app:backgroundTint="#color/colorPrimary"
app:fabSize="normal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<LinearLayout
android:id="#+id/pictureLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginEnd="24dp"
android:layout_marginBottom="90dp"
android:orientation="horizontal">
<TextView
android:id="#+id/textView2"
style="#style/TextAppearance.AppCompat.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:text="New Picture" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabPicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:src="#drawable/baseline_insert_photo_white_24dp"
app:fabSize="mini" />
</LinearLayout>
<LinearLayout
android:id="#+id/textLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginEnd="24dp"
android:layout_marginBottom="150dp"
android:orientation="horizontal">
<TextView
android:id="#+id/textView"
style="#style/TextAppearance.AppCompat.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:text="New Paragraph" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:src="#drawable/baseline_notes_white_24dp"
app:fabSize="mini"
tools:layout_editor_absoluteX="144dp"
tools:layout_editor_absoluteY="320dp" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
Java Fragment File (inside onCreateView):
parentRLayout = (RelativeLayout) v.findViewById(R.id.relativeLayoutId);
Inside a button listener in the Java Fragment File -> When a button is
clicked, then text is added into a dialog, and when fabText button is
clicked and text is added on the screen -> following line of code:
parentRLayout.addView(createNewTextView(paragraphText.getText().toString(), 0, 5, 5));
//Function that returns a TextView (in the same Java Fragment Class)
TextView createNewTextView(String text, int alignment, int margin, int padding){
TextView newDynamicTextView = new TextView(getActivity());
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT
);
layoutParams.addRule(RelativeLayout.BELOW, currentId);
newDynamicTextView.setLayoutParams(layoutParams);
newDynamicTextView.setId(TextView.generateViewId());
currentId = newDynamicTextView.getId();
Log.d("currentId", ""+currentId);
newDynamicTextView.setText(text);
Log.d("newDynamic:", newDynamicTextView.getText().toString());
return newDynamicTextView;
}
EDIT:
I have managed to make it work. The idea is to have the CoordinatorLayout as the root(parent) layout and inside it a ScrollView that has inside a RelativeLayout. The buttons are outside the ScrollView (under it) so they don't scroll.
My one cent.
It looks like this: https://imgur.com/a/a7AnOQV (also, while we're at it, how can I make the fab buttons be 0% transparent? i dont understand why u can see through the buttons the text)
Please take note that views are drawn based on their order in your xml layout.
Because the tree is traversed pre-order, this means that parents will
be drawn before (i.e., behind) their children, with siblings drawn in
the order they appear in the tree.
https://developer.android.com/guide/topics/ui/how-android-draws

add single button on cardview page

created layout with radio button in a cardview.
needed a single button at bottom but button is repeating with radio button.
below is the code for card view and design
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_height="wrap_content">
<RadioGroup
android:id="#+id/radiogrp_surveyoptions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="#+id/rd_option1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#color/black"
android:gravity="left"
android:textSize="15dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center"
android:layout_weight="0.3">
<Button
android:id="#+id/btn_saveans"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="15dp"
android:background="#drawable/buttonclick"
android:text="Save"
android:textColor="#color/whitecolor"
android:textSize="15dp"
android:foregroundGravity="bottom"/>
</LinearLayout>
please guide how i can add only one button after repeated cardview
Instead of inflating layout containing both CardView and Button, for every element in the list, you should move Button to whatever layout you're inflating in onCreate() method. (or onCreateView() if you're using fragments)
So, assuming you're using RecyclerView, move Button to the layout containing your <RecyclerView> component.

How do I change alignment of a button in java?

So, i want to make an adding activity which is used for 3 different tables.
In the last one i want the date EditText to be gone.
I know how to make it disappear but i want the layout o change too and this is how it looks when date EditText is present
3 EditTexts and Button with alignbottom set to date EditText
And this is how it looks when I set date EditText to gone
2 EditTexts and Button with alignbottom set to the gone EditText
Try layout like that:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:visibility="visible"/>
</LinearLayout>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
The thing is, that you need another supporting layout, which is the number two in my hierarchy, which will have android:layout_height="wrap_content", so when you set the visibility to gone to one of the EditTexts, it is gonna change the height of the LinearLayout according to the other two EditTexts height, where Button's height is always android:layout_height="match_parent".
LinearLayout with orientation of vertical containing the EditText, wrapped in a horizontal LinearLayout containing the Button too. If both the LinearLayout and the Button have a height of wrap content, they will adapt regardless of how many EditText there are
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView3" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="New Button"
android:id="#+id/button"
android:layout_weight="1"
android:background="#3e3e3e" />
</LinearLayout>

Aligning my views to the right

Alright, what I'm trying to do is: setting an imageview to be on the right and the 3 textviews will be located on the left relative to the image. With the following XML code, the imageview is on the left and the 3 textviews are located in the right of the imageview.
Here's the look of it now:
As I said, I want the image to be aligned on the right side, and the 3 textviews on the left of the image.
So here's the current xml: http://pastebin.com/r68S1QKv
If I change the LinearLayout to RelativeLayout (so I can use alignParentRight = true), the textviews stack on each other and are not displayed vertically.
Suggestions, please?
EDIT: Looks like I haven't clarify the wanted look, here's an illustration of what I want:
Use This Code.Use RelativeLayout as parent & assign android:layout_alignParentRight="true" to his child this set your all view to right side.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_marginRight="20dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/row_title"
style="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hey1" />
<TextView
android:id="#+id/row_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hey2" />
<TextView
android:id="#+id/row_postcode_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hey3" />
</LinearLayout>
<ImageView
android:id="#+id/icon"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</RelativeLayout>
What you can do - is to make outer RelativeLayout, make you image alignParentRight=true, put text views in a separate linear layout (with vertical orientation), align your LinearLayout with text views to the left edge of the image view (layout_toLeftOf="#+id/img_id").
Here is what I manage to do following this approach:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toLeftOf="#+id/img"
android:orientation="vertical"
android:id="#+id/text_container">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="text1"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="text2"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="text3"/>
</LinearLayout>
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/icon"
android:id="#+id/img"/>
</RelativeLayout>

How to design buttons?

Does anyone know if there is a possible way to design the android buttons like this:
I'd say these are regular buttons with transparent background and icons set through drawableTop attribute. So an xml for such a button would look like:
<Button android:drawableTop="#drawable/icon" android:background="#android:color/transparent"/>
Try this
<Button android:drawableTop="#drawable/icon" android:background="#android:color/transparent"/>
ALso you can design this type of bottom bar using Tab in android
Click here
Also you can design this type of button using radio button
Click here
For that i think need to create the custom view for tab and call in tabhost
http://joshclemm.com/blog/?p=136
Thank you
okay
--take one layout and aligng it ALIGNPARENTBOTTOM = TRUE
now set background of that Layout to THIS BLACK
-- now take five button in that Layout Horizontal
now take images like these Transparent and set it to the buttons
-- also get Images for HOVER action
and set it on click of that particular button
(use layout_weight for setting equall sizes of button)
Have Fun!!
I have done same thing in one of my app. Here I am giving the xml code for this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="#000"
android:gravity="center_vertical">
<LinearLayout
android:layout_width="60dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/reviewLayout"
android:gravity="center_horizontal"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/reviews"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Review"
android:textColor="#FFF"/>
</LinearLayout>
<LinearLayout
android:layout_width="60dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/nearbyLayout"
android:layout_toRightOf="#id/reviewLayout"
android:gravity="center_horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:src="#drawable/nearby"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nearby"
android:textColor="#FFF"/>
</LinearLayout>
<LinearLayout
android:layout_width="60dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/searchLayout"
android:layout_toRightOf="#id/nearbyLayout"
android:gravity="center_horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:src="#drawable/search"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
android:textColor="#FFF"/>
</LinearLayout>
<LinearLayout
android:layout_width="75dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/bookmarkLayout"
android:layout_toRightOf="#id/searchLayout"
android:gravity="center_horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:src="#drawable/bookmarks"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bookmarks"
android:textColor="#FFF"/>
</LinearLayout>
<LinearLayout
android:layout_width="60dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/settingsLayout"
android:layout_toRightOf="#id/bookmarkLayout"
android:gravity="center_horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:src="#drawable/settings"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Settings"
android:textColor="#FFF"/>
</LinearLayout>
</RelativeLayout>
Use this layout where you want using include tag, and call it in your Activity.
Here I am giving the code to include this in Activity.
private LinearLayout reviewLay,nearbyLay, searchLay, bookmarkLay, settingLay;
reviewLay = (LinearLayout)findViewById(R.id.reviewLayout);
nearbyLay = (LinearLayout)findViewById(R.id.nearbyLayout);
searchLay = (LinearLayout)findViewById(R.id.searchLayout);
bookmarkLay = (LinearLayout)findViewById(R.id.bookmarkLayout);
settingLay = (LinearLayout)findViewById(R.id.settingsLayout);
reviewLay.setOnClickListener(this);
nearbyLay.setOnClickListener(this);
searchLay.setOnClickListener(this);
bookmarkLay.setOnClickListener(this);
settingLay.setOnClickListener(this);
btEdit.setOnClickListener(this);
Here I have used a layout instead of button and I am setting onclicklistener for each layout. Now override onclick method and do what you want with each layout

Categories

Resources