I'm trying to swap out Layouts in my Fragment, by setting one View invisivle and the other one visible. However I want to use the same Button in both cases, which works fine on the UI-Side of the App, but I can't get the onClick-Event in the Fragment from the button in the second View.
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/id_subfragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/id_tv"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textSize="20sp"
android:text="SomeText" />
<RelativeLayout
android:id="#+id/id_relLayout_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/id_tv">
<Button
android:id="#+id/id_button_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
app:backgroundTint="#color/colorPrimaryDark"
android:text="Button" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/id_relLayout_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/id_tv">
<Button
android:id="#id/id_button_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
app:backgroundTint="#color/colorPrimaryDark"
android:text="Button" />
</RelativeLayout>
</RelativeLayout>
As you see, the buttons have the same ID, so in my Fragment-class (which implements View.onClickListener I should be able to get the onClick-Event for both of them.
Code from Fragment in onCreateView:
Button button = v.findViewById(R.id.id_button_next);
button.setOnClickListener(this);
I 'change' the layouts with
v.findViewById(R.id.id_relLayout_1).setVisibility(View.GONE);
v.findViewById(R.id.id_relLayout_2).setVisibility(View.VISIBLE);
Thanks for any help!
findViewById returns first view with the given ID, but if I'm not wrong you should be able to reference those buttons using their parents.
So instead of:
Button button = v.findViewById(R.id.id_button_next);
button.setOnClickListener(this);
you could use parent containers:
RelativeLayout parent1 = v.findViewById(R.id.id_relLayout_1)
Button buttonInParent1 = parent1.findViewById(R.id.id_button_next)
buttonInParent1.setOnClickListener(this);
RelativeLayout parent2 = v.findViewById(R.id.id_relLayout_2)
Button buttonInParent2 = parent2.findViewById(R.id.id_button_next)
buttonInParent2.setOnClickListener(this);
But I want mention that having the same ids within a single view is considered as a bad practice.
Related
I know the code might not be well formatted but I want the button (last tag) to always appear in front
of all other views if the description is long the button disappears.
I'm using scrollView and if any long text appears but this makes the button go down the screen.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center|bottom"
android:orientation="vertical">
<Button
android:layout_gravity="bottom|end"
android:id="#+id/cart_button"
android:layout_width="250dp"
android:layout_height="55dp"
android:layout_marginBottom="10dp"
android:iconTint="#color/black"
android:elevation="6dp"
android:text="ADD TO CART"
android:textAppearance="?attr/textAppearanceButton"
android:textColor="#28022E"
android:textSize="20sp"
app:backgroundTint="#F6F2FA"
app:elevation="10dp"
app:rippleColor="#FFF"
app:shapeAppearance="?attr/shapeAppearanceSmallComponent"
app:strokeColor="#0000"
app:strokeWidth="10dp" />
</RelativeLayout>
Take your button out of scrollview.
something like :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
// content that you wants to scroll
</ScrollView>
<Button alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
i have a task_layout.xmland i wanted to add Programmatically it to card view layout file that already adapted with recyclerView which works so good with <include>command in xml file
Cardview_layout.xml that adapted with recyclerView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:id="#+id/CARD_VIEW"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="8dp"
android:layout_marginHorizontal="25dp"
android:elevation="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<TextView
android:id="#+id/LIST_NAME_TEXT_VIEW"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Groceries"
android:fontFamily="#font/segoeuib"
android:textSize="21sp"
android:textColor="#000000" />
<!-- Here where i want to add the child layout -->
</RelativeLayout>
</android.support.v7.widget.CardView>
the layout task_layout.xml i want to add programmatically and i tried a lot of things and it always crash
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<CheckBox
android:id="#+id/CHECK_B0X_TASK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:allowUndo="true"
android:text="Task1"
android:textSize="17sp"
android:fontFamily="#font/segoeui"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"/>
<TextView
android:id="#+id/TEXT_CLOCK_TASK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9:00 AM"
android:textSize="17sp"
android:fontFamily="#font/segoeui"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
in the main activity i typed that but still not working it always crash the application even the <include> tag in the xml file is working
CardView cardView = findViewById(R.id.CARD_VIEW);
View child1 = LayoutInflater.from(this).inflate(R.layout.task_layout,null);
cardView.addView(child1);
Try this...
CardView cardView = findViewById(R.id.id_cardview);
Create an object to your layout
View child1 = LayoutInflater.from(this).inflate(
R.layout.extra, null);
cardView.addView(child1);
Basically what I need is overlap the views
View to achieve
Each cardview is an item and will be contained within a recyclerview
this is how my card view is defined
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginTop="-11dp">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none"
android:layout_centerInParent="true"
android:layout_alignParentBottom="true"
>
<android.support.v7.widget.CardView...>
</ScrollView>
<ImageView
android:layout_width="70dp"
android:layout_height="40dp"
android:src="#drawable/img_food_little"
android:layout_alignParentEnd="true"
android:layout_marginEnd="40dp"
/>
</RelativeLayout>
And looks like (The blue one is the RelativeLayout):
How my item card view
Please Help
Try to replace the linear layout in the following with cardview and use the resultant layout as the layout for the recyclerview items .
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#android:color/transparent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_gravity="top"
android:layout_marginTop="20dp"
android:background="#cccccc"
android:layout_height="100dp">
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:src="#mipmap/ic_launcher"
android:layout_gravity="right"
android:layout_marginRight="20dp"
android:layout_height="wrap_content" />
</FrameLayout>
Hope this helps.
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.
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