how to visible nested layout when click root layout? - android

I am developing an android application in that i have a linear layout that contains one root layout(invitation_single)and two nested layouts namely(hidden,hidden1).In "hidden1" layout i have two textviews and "hidden" layout contains three buttons(yes,no,maybe),now my need is when click root layout i am need to visible two nested layouts at same time,after visible sub layout when i am click anyone of these buttons(yes,no,maybe) the nested layout hide and again to visble root layout.
here is my layout code,
<ImageButton
android:id="#+id/image"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/ic_action_event" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:clickable="false"
android:focusable="true"
android:orientation="vertical">
<TextView
android:id="#+id/invitation_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="0dp"
android:paddingTop="3dp"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:id="#+id/invitation_place"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="0dp"
android:textColor="#color/black"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/hidden1"
android:layout_width="1000dp"
android:layout_height="50dp"
android:layout_weight="1"
android:clickable="false"
android:focusable="true"
android:orientation="vertical"
android:visibility="visible">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:paddingLeft="0dp"
android:paddingRight="10dp"
android:paddingTop="3dp"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:id="#+id/place"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:paddingLeft="0dp"
android:paddingRight="10dp"
android:textColor="#color/black"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/hidden"
android:layout_width="310dp"
android:layout_height="60dp"
android:layout_marginTop="50dp"
android:layout_weight="1"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal"
android:paddingTop="1dp"
android:visibility="gone"
android:weightSum="3">
<Button
android:id="#+id/yesbutton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="7dp"
android:layout_weight="1"
android:background="#color/blue"
android:text="Yes"
android:textColor="#color/black"></Button>
<Button
android:id="#+id/nobutton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="30dp"
android:layout_weight="1"
android:background="#color/blue"
android:text="No"
android:textColor="#color/black"></Button>
<Button
android:id="#+id/buttonmaybe"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="30dp"
android:layout_weight="1"
android:background="#color/blue"
android:text="Maybe"
android:textColor="#color/black"></Button>
</LinearLayout>
</LinearLayout>
In below code, layout first is root layout and layout second & third is nested layouts,in coding i am try to visible the nested layouts when on click the root layout but nested layout "hidden" is only visible,but i am need to visible both sub layouts at the same time when i am click layout hidden the nested layouts are need to gone and show root layout.how can i achieve this...
final LinearLayout first = (LinearLayout)convertView.findViewById(R.id.invitation_single);
final LinearLayout second =
(LinearLayout) convertView.findViewById(R.id.hidden);
final LinearLayout third =
(LinearLayout) convertView.findViewById(R.id.hidden1);
first.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
second.setVisibility(View.VISIBLE);
third.setVisibility(View.VISIBLE);
}
});

Try this way,
final LinearLayout rootLayout= (LinearLayout)convertView.findViewById(R.id.invitation_single);
rootLayout.setOnClickListener(this);
//And then add onClickListener method and do what you want.
#Override
public void onClick(View v) {
//do what you want to do when button is clicked
switch (v.getId()) {
case R.id.invitation_single:
// Do your staff
break;
}
}

first of all you need to set visiblity gone in your both child layout..
final LinearLayout rootLayout= (LinearLayout)convertView.findViewById(R.id.invitation_single);
final LinearLayout ChildLayout1= (LinearLayout)convertView.findViewById(R.id.ChildLayout1);
final LinearLayout ChildLayout2= (LinearLayout)convertView.findViewById(R.id.ChildLayout2);
rootLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//set visibility for your child layout
ChildLayout1.setVisibility(View.Visible);
ChildLayout2.setVisibility(View.Visible);
}
});

Related

Align horizontally 5 text views in Android

I need your help to solve this problem:
I have a linear layout with horizontal orientation. Inside this layout I have 5 textviews with visibility:gone. Above this layout I have a radio group with 5 radio button aligned horizontally.
When I check a radio button the corresponding textviews changes his visibility in visible. I want to show a text view below every radio button. Actually they are alle in the same position.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView />
<TextView />
<TextView />
<TextView />
<TextView />
</LinearLayout>
How can I do that?
Set LinearLayout weigthSum to 1.0. Make each textview weight to 0.2 & visibility to invisible. View with gone visibility doesn't take any space.
I made second textview visible. But textview with text "1" will occupy space before textview with text of "2".
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="net.techdesire.HomeActivity"
tools:showIn="#layout/app_bar_home">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/radio_layout">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="#+id/value_group">
<RadioButton
android:text="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/radioButton1"
android:layout_weight="0.2"/>
<RadioButton
android:text="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/radioButton2"
android:layout_weight="0.2"/>
<RadioButton
android:text="3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/radioButton3"
android:layout_weight="0.2"/>
<RadioButton
android:text="4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/radioButton4"
android:layout_weight="0.2"/>
<RadioButton
android:text="5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/radioButton5"
android:layout_weight="0.2"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/fragment_container"
android:orientation="horizontal"
android:layout_below="#+id/radio_layout"
android:weightSum="1.0">
<TextView
android:id="#+id/text1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:visibility="invisible"
android:text="1"/>
<TextView
android:id="#+id/text2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:visibility="visible"
android:text="22345678dsada"
android:maxLines="1"
android:ellipsize="marquee"/>
<TextView
android:id="#+id/text3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:visibility="invisible"
android:text="3"/>
<TextView
android:id="#+id/text4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:visibility="invisible"
android:text="4"/>
<TextView
android:id="#+id/text5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:visibility="invisible"
android:text="5"/>
</LinearLayout>
</RelativeLayout>
On Radio group set onCheckChangeListener then compare id of checked radio button and according to it hide/show textview.See following
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
/*hide all*/
TextView tv;
if(checkedID==R.id.radioButton1)
tv=(RadioButton)findViewById(R.id.text1);
elseif(checkedID==R.id.radioButton2)
tv=(RadioButton)findViewById(R.id.text2);
/* Similarly other else conditions*/
tv.setVisibility(View.VISIBLE);
}
});
Use visibility:Invisible instead of gone, because with gone they don't use any screen so the rest are not really there when the selected one appears, and it will appear at the first position.
Hope this helps.
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
// set visibility in visible according to Id by using if condition Or switch
}
});

How to close nested layout when open new layout in android?

I am developing an android application,In that I am using a Layout that contains a root layout(invitation_single) and nested layout(hidden). When I am on click root layout the nested layout display along with root layout that works fine.
now my output is display like below image
but i am expecting my output as,
that means if am click "event2" then simultaneously nested layout(yes,no,maybe buttons) of first "event" needs to hide,but if i have a single event then the nested layout of that event needs to be visible,else if i have more than one event means i want to display my output as second image(nested layout of first"event" hide when click second event). How can i achieve that.
my layout code is looking below,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/invitation_single"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="?android:dividerVertical"
android:dividerPadding="5dp"
android:showDividers="middle"
tools:context=".MainActivity">
<ImageButton
android:id="#+id/image"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/ic_action_event" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:clickable="false"
android:focusable="true"
android:orientation="vertical">
<TextView
android:id="#+id/invitation_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="0dp"
android:paddingTop="3dp"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:id="#+id/invitation_place"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="0dp"
android:textColor="#color/black"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/hidden"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginLeft="-270dp"
android:layout_marginTop="60dp"
android:layout_weight="1"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal"
android:paddingTop="1dp"
android:visibility="gone"
android:weightSum="3">
<Button
android:id="#+id/yesbutton"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="7dp"
android:layout_weight="1"
android:background="#color/blue"
android:text="Yes"
android:textColor="#color/black"></Button>
<Button
android:id="#+id/nobutton"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="25dp"
android:layout_weight="1"
android:background="#color/blue"
android:text="No"
android:textColor="#color/black"></Button>
<Button
android:id="#+id/buttonmaybe"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="#color/blue"
android:text="Maybe"
android:textColor="#color/black"></Button>
</LinearLayout>
</LinearLayout>
my programming code is looking below,
final LinearLayout first = (LinearLayout) convertView.findViewById(R.id.invitation_single);
final LinearLayout second = (LinearLayout) convertView.findViewById(R.id.hidden);
first.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.invitation_single:
second.setVisibility(View.VISIBLE);
break;
}
}
});
In above code I am used to display nested layout(hidden) immediately below root layout(invitation single).
Do you have a small set of events or the number of events could get pretty long? If the second choice is true then I would suggest looking at ExpandableListView. Check out this link particularly on how to collapse the previously open items upon expanding a new one: ExpandabeListView auto collapse

setVisibility(view.GONE) is not working Android

i need to disable the view programmatically so i used setVisibility(view.GONE) in activity. My xml have two buttons named lower and upper and two relative layouts named lower_lay and upper_lay.when i click the lower button i need enable lower_lay and i click the upper button i need to enable upper_lay. Both in upeer_lay and lower_lay having images and performing onTouch event. Now my problem is when i am in lower_lay the images of upper_lay are disabled but when i touch the empty space in lower_lay, upper_lay images are coming...and in upper_lay i am having this issue. Why the view is not completelt gone?
i am trying this from 3 days....please any one help me out.
xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/r1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1e356a">
<TextView
android:id="#+id/placce_head"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="Hyderabad to banglore"
android:textColor="#ffffff"
android:textSize="20dp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#78869c"
android:weightSum="2"
android:orientation="horizontal">
<TextView
android:id="#+id/seats"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="Selected Seats"
android:textColor="#ffffff"
android:textSize="16dp" />
<TextView
android:id="#+id/totalamount"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="Total Amount"
android:textColor="#ffffff"
android:textSize="16dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#78869c"
android:weightSum="2"
android:orientation="horizontal">
<TextView
android:id="#+id/seat_num"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:textColor="#ffffff"
android:textSize="16dp" />
<TextView
android:id="#+id/total_amount"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="#ffffff"
android:textSize="16dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#1e356a"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1e356a"
android:layout_marginTop="5dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:src="#drawable/bluesmall"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:text="Available"
android:textSize="12dp"
android:textColor="#ffffff"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:src="#drawable/greensmall"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:textSize="12dp"
android:textColor="#ffffff"
android:text="Selected"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:src="#drawable/pinksmall"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ladies"
android:textSize="12dp"
android:textColor="#ffffff"
android:layout_marginLeft="6dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:src="#drawable/redsmall"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:textSize="12dp"
android:textColor="#ffffff"
android:text="Booked"/>
</LinearLayout>
<LinearLayout
android:id="#+id/sleeper_lay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#78869c"
android:weightSum="2"
android:padding="7dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<Button
android:id="#+id/lower"
android:layout_width="0dp"
android:layout_weight="1"
android:text="Lower"
android:textColor="#F93249"
android:gravity="center"
android:textSize="18dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/upper"
android:layout_width="0dp"
android:layout_weight="1"
android:text="Upper"
android:textSize="18dp"
android:textColor="#ffffff"
android:gravity="center"
android:layout_height="wrap_content" />
</LinearLayout>
<RelativeLayout
android:id="#+id/relative_layout"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:background="#drawable/bg_border"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
</RelativeLayout>
<RelativeLayout
android:id="#+id/relative_layout_two"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:background="#drawable/bg_border"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
</RelativeLayout>
<Button
android:id="#+id/done_btn"
android:background="#F93249"
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="done"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Activitiy:
lower_lay = (RelativeLayout) findViewById(R.id.relative_layout);//lower layout
upper_lay= (RelativeLayout) findViewById(R.id.relative_layout_two);//upperlayout
upper.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
lower_lay.setVisibility(View.GONE);
upper.setTextColor(Color.parseColor("#F93249"));
lower.setTextColor(Color.parseColor("#ffffff"));
upper_lay.setVisibility(View.VISIBLE);
sheetdetails.clear();
}
});
lower.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
upper_lay.setVisibility(View.GONE);
upper.setTextColor(Color.parseColor("#ffffff"));
lower.setTextColor(Color.parseColor("#F93249"));
lower_lay.setVisibility(View.VISIBLE);
}
});
Try using
View.GONE
not
view.GONE
and lower.setOnClickListener not lower.setOnTouchListener
View is the class, so should starts with capital letter.
Kindly see my updates.
upper.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
lower_lay.setVisibility(View.GONE);
upper.setTextColor(Color.parseColor("#F93249"));
lower.setTextColor(Color.parseColor("#ffffff"));
upper_lay.setVisibility(View.VISIBLE);
}
});
lower.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
lower_lay.setVisibility(View.VISIBLE);
upper.setTextColor(Color.parseColor("#ffffff"));
lower.setTextColor(Color.parseColor("#F93249"));
upper_lay.setVisibility(View.GONE);
}
});
I will give you a good advice about your layout:
From Chat Haase blog:
RelativeLayout is a very convenient layout to use, because it allows
developers to specify how content should be placed relative to other
content. In many situations, this is necessary and may be the best
solution for the job. However, it is important to understand that
RelativeLayout is an expensive solution, because it requires two
measurement passes to ensure that it has handled all of the layout
relationships correctly. Moreover, this problem compounds with every
additional RelativeLayout throughout the hierarchy. Imagine a
RelativeLayout at the top of your view hierarchy; this essentially
doubles the measurement work on your entire view hierarchy. Now
imagine another RelativeLayout as one of the children of that first
one — this doubles again the measurement passes that happen under it,
requiring four measurement passes for all of the views in its
sub-hierarchy.
Use a different type of layout for situations that do not require the
capabilities of RelativeLayout, such as LinearLayout or even a custom
layout. Or for situations in which relative alignment of child views
is necessary, consider the more optimized GridLayout, which
pre-processes the child view relationships and avoids the
double-measurement problem.

Aligning buttons+images+text in a row (android)

I'm trying to align 3 buttons in a row in the center of the screen. Each button will have a background color, an image, and some text under the image. I've tried using LinearLayout but I can't put the image over the button. I've tried RelativeLayout, and I managed to get them in a row, but they are not in the center of the screen. Any advices?
<Button
android:id="#+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="85dp"
android:paddingBottom="10dp"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:background="#drawable/light"
android:text="#string/start"
android:textColor="#color/text"
android:textSize="8pt"
android:textStyle="bold"
android:layout_marginTop="0dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_alignParentTop="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/image"
android:src="#drawable/hstart"
android:layout_alignLeft="#+id/start"
android:layout_alignStart="#+id/start"
android:layout_alignRight="#+id/start"
android:layout_alignEnd="#+id/start"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="0dp" />
Here is your Button view (your_btn_view.xml in the layout folder):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:clickable="true"
android:background="#android:drawable/btn_default"
android:gravity="center" >
<ImageView
android:id="#+id/btn_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_add" />
<TextView
android:id="#+id/btn_tv"
android:text="text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Here is your layout view containing three buttons:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal" >
<include
android:id="#+id/btn1"
layout="#layout/your_btn_view" />
<include
android:id="#+id/btn2"
layout="#layout/your_btn_view" />
<include
android:id="#+id/btn3"
layout="#layout/your_btn_view" />
</LinearLayout>
Then, access and fill in the appropriate TextView and ImageView in your onCreate() like so:
#Override
public void onCreate(Bundle sIS){
super.onCreate(sIS);
setContentView(R.layout.that_layout_above, this, false);
LinearLayout btn1 = (LinearLayout)findViewById(R.id.btn1);
((TextView)btn1.findViewById(R.id.btn_tv)).setText("my text");
...
Use a LinearLayout to hold a RelativeLayout for each button/image combination. "Nested" layouts is pretty common to get this type of visual effect.

Android RelativeLayout with multiple elements set ClickListener

I am trying to set an onClickListener for a RelativeLayout with multiple elements. I have placed the listener on the RelativeLayout but it will not register if you click on top of any of the children elements, only if you click around them. I have tried a few different ways setting the children to clickable="true" or all focusable="false" but cant seem to get the entire layout to act like one button. Is this possible or do I need clickListeners for each of the children essentially doing the same thing?
<RelativeLayout
android:id="#+id/nearbyLikeGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="1">
<ImageView
android:id="#+id/nearbyLikeIconImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_centerInParent="true"
android:focusable="false"
android:contentDescription="#string/content_description"
android:src="#drawable/like_icon"/>
<Button
android:id="#+id/nearbyFeedsLikeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:layout_toRightOf="#+id/nearbyLikeIconImageView"
android:focusable="false"
android:paddingBottom="15dp"
android:paddingTop="15dp"
android:text="#string/like"
android:textColor="#color/white"
android:textSize="12sp" />
</RelativeLayout>
Adapter Class :
RelativeLayout likeGroup = (RelativeLayout)v.findViewById(R.id.nearbyLikeGroup);
likeGroup.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Log.d("test", "layout group clicked");
}
});
To prevent the child views from consuming the click event you need to set android:clickable="false" on them. I assume only the Button is making problems since ImageViews are not clickable by default.
Try this layout:
<RelativeLayout
android:id="#+id/nearbyLikeIconGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="1">
<ImageView
android:id="#+id/nearbyLikeIconImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_centerInParent="true"
android:focusable="false"
android:contentDescription="#string/content_description"
android:src="#drawable/like_icon"/>
<Button
android:id="#+id/nearbyFeedsLikeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:layout_toRightOf="#+id/nearbyLikeIconImageView"
android:focusable="false"
android:clickable="false"
android:paddingBottom="15dp"
android:paddingTop="15dp"
android:text="#string/like"
android:textColor="#color/white"
android:textSize="12sp" />
</RelativeLayout>

Categories

Resources