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
}
});
Related
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);
}
});
I am making an app in which I want to design the options in 2*2 radio buttons.For this I am doing the following thing but getting "android.widget.RelativeLayout cannot be cast to android.widget.RadioButton" error
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TableRow>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</TableRow>
<TableRow>
<TextView android:id="#+id/textMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginTop="100dp"
android:gravity="center|left"/>
</TableRow>
<TableRow>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/group"
android:layout_marginLeft="25dp"
android:orientation="horizontal"
android:layout_span="2"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/btn1"/>
<RadioButton android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/btn1" />
<RadioButton android:id="#+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/btn2"
android:layout_toRightOf="#id/btn3"/>
</RelativeLayout>
</RadioGroup>
</TableRow>
<Button android:id="#+id/submitbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:visibility="gone"
android:layout_below="#id/group" />
<TextView android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/submitbtn"/>
</TableLayout>
Please help in this I am stuck since morning.
First, it depends on Your needs. But You can do it like this: Juste don´t use RadioGroup, instead use two LinearLayouts:
<LinearLayoutLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1">
<RadioButton android:id="#+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<RadioButton android:id="#+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayoutLayout>
<LinearLayoutLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1">
<RadioButton android:id="#+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<RadioButton android:id="#+id/btn4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayoutLayout>
Then You can refer it programatically and deselect all other buttons, if one is pressed:
yourFirstRadioButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view){
YourSecondRadioButton.setChecked(false);
YourThirdRadiobutton.setChecked(false);
YourFourthRadioButton.setChecked(false);
}
});
You have to do this for all Your radio buttons. This is just a simple example, You can do it in a better way, for example with a for loop. For example, after creating RadioButtons, initialize a RadioButtons Array:
private RadioButton[] radioButtons;
initialize after initialize all radioButtons:
radioButtons = {radioButton1,radioButton2,radioButton3,radioButton4};
And the, make a setSelected(RadioButton button) method:
private void setSelected(RadioButton button){
for(int i=0;i<radioButtons.length;i++){
if(radioButtons[i]!=button){
radioButtons[i].setChecked(false);
}
}
}
and call this in Your onClick():
yourFirstRadioButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view){
setSelected(yourFirstRadioButton);
}
});
It´s possible that I have some little wrong coding here, because I can´t test it at the moment, have no IDE here. But just give it a try and come back if it doesn´t work.
1.remove radiogroup.
2.take parent layout as relative
3.take 4 radio button as required
4.write onclicklistener for all radio button
5.In radio button-1 onclicklistener event disable other button and do that for other(set check for particular button and for other set unchecked)
Remove Radio Group, implements onClickListener for RadioButtons and maintain the active Radio Button Id. Like:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RadioButton
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickButton" />
<RadioButton
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/btn1"
android:onClick="onClickButton" />
<RadioButton
android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/btn1"
android:onClick="onClickButton" />
<RadioButton
android:id="#+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/btn2"
android:layout_toRightOf="#id/btn3"
android:onClick="onClickButton" />
</RelativeLayout>
Then, In ur activity implements "onClickButton" method mentioned to the OnClick attribute of RadioButtons:
private RadioButton checkedRadioButton;
// OnClick listener for Radio Buttons
public void onClickButton(View view) {
RadioButton radioButton = (RadioButton) view;
if (checkedRadioButton != null) {
checkedRadioButton.setChecked(false);
}
radioButton.setChecked(true);
checkedRadioButton = radioButton;
}
// Get the Active radio button ID
private int getCheckedRadiButtonId() {
if (checkedRadioButton != null) {
return checkedRadioButton.getId();
}
return 0;
}
U can get the current active radioButton Id using getCheckedRadiButtonId.
I have created radio group with custom layout i.e. custom button.
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup
android:id="#+id/radio_group_rating"
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="3" >
<RadioButton
android:id="#+id/radio_platinum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="#null"
android:clickable="true"
android:drawablePadding="10dp"
android:drawableTop="#drawable/star_small_dis"
android:gravity="center"
android:text="#string/platinum" />
<RadioButton
android:id="#+id/radio_gold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="#null"
android:clickable="true"
android:drawablePadding="10dp"
android:drawableTop="#drawable/star_small_dis"
android:gravity="center"
android:text="#string/gold" />
<RadioButton
android:id="#+id/radio_silver"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="#null"
android:clickable="true"
android:drawablePadding="10dp"
android:drawableTop="#drawable/star_small_dis"
android:gravity="center"
android:text="#string/silver" />
</LinearLayout>
</RadioGroup>
<Button
android:id="#+id/btn_submit"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#id/radio_group_rating"
android:text="#string/submit" />
And in Activity, I have below code,
final Dialog dialog = new Dialog(mActivity);
dialog.setContentView(R.layout.rating_cust_dialog_layout);
radioGroupRating = (RadioGroup) dialog.findViewById(R.id.radio_group_rating);
Button btnSubmit = (Button) dialog.findViewById(R.id.btn_submit);
btnSubmit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int selectedRadioId = radioGroupRating.getCheckedRadioButtonId();
View radioButton = radioGroupRating.findViewById(selectedRadioId);
Integer selctedPosition = radioGroupRating.indexOfChild(radioButton);
dialog.dismiss();
}
});
dialog.show();
My issue is radio buttons are not getting clicked. I thought it's because of android:button="#null" So I replaced it with android:button="#drawable/star_dis" but still not getting clicked.
Its the android:button="#null", please remove it from the xml file completely.
Try using this simple xml layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioGroup
android:id="#+id/radio_group_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radio_platinum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="platinum" />
<RadioButton
android:id="#+id/radio_gold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="gold" />
<RadioButton
android:id="#+id/radio_silver"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="silver" />
</RadioGroup>
<Button
android:id="#+id/btn_submit"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="submit" />
</LinearLayout>
Only difference to my code is that I don't put clickable="true" in xml, if RadioGroup handles touch event by itself to manage child radio buttons you should remove that attribute as clickable is generally set to true automatically when you set an OnClickListener.
I have a CustomListView, when I longClick on a row, the layout of that row changes to a different listRow Layout. So far, everything is working, however, the layout of the new view is compressed. Here are my xml files:
search_result_layout.xml --> this is the original xml file used by the customListView Adapter.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/search_result_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:orientation="horizontal"
android:padding="1dip" >
<TextView
android:id="#+id/dialogItemName"
android:layout_width="0dp"
android:layout_weight="0.54"
android:layout_marginRight="5dp"
android:layout_height="match_parent"
android:text="Item Name"
android:gravity="center"
android:textSize="10pt" />
<TextView
android:id="#+id/price"
android:layout_width="0dp"
android:layout_weight="0.14"
android:layout_height="match_parent"
android:text="Price"
android:gravity="center"
android:textSize="10pt" />
<TextView
android:id="#+id/discount"
android:layout_width="0dp"
android:layout_weight="0.10"
android:layout_height="match_parent"
android:text="Discount"
android:gravity="center"
android:textSize="10pt" />
<TextView
android:id="#+id/qtyInput"
android:layout_width="0dp"
android:layout_weight="0.14"
android:layout_height="match_parent"
android:text="qtyInput"
android:gravity="center"
android:textSize="10pt" />
<TextView
android:id="#+id/discInput"
android:layout_width="0dp"
android:layout_weight="0.11"
android:layout_height="match_parent"
android:text="discInput"
android:gravity="center"
android:textSize="10pt" />
</LinearLayout>
Here is my search_result_inflate.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:orientation="horizontal"
android:padding="1dip" >
<TextView
android:id="#+id/dialogItemName"
android:layout_width="0dp"
android:layout_weight="0.54"
android:layout_marginRight="5dp"
android:layout_height="match_parent"
android:text="Item Name"
android:gravity="center"
android:textSize="10pt" />
<TextView
android:id="#+id/price"
android:layout_width="0dp"
android:layout_weight="0.14"
android:layout_height="match_parent"
android:text="Price"
android:gravity="center"
android:textSize="10pt" />
<TextView
android:id="#+id/discount"
android:layout_width="0dp"
android:layout_weight="0.10"
android:layout_height="match_parent"
android:text="Discount"
android:gravity="center"
android:textSize="10pt" />
<EditText
android:id="#+id/qtyInput"
android:layout_width="0dp"
android:layout_weight="0.14"
android:layout_height="match_parent"
android:hint="qtyInput"
android:inputType="number"
android:gravity="center"
android:textSize="10pt" />
<EditText
android:id="#+id/discInput"
android:layout_width="0dp"
android:layout_weight="0.11"
android:layout_height="match_parent"
android:hint="discInput"
android:inputType="number|date"
android:gravity="center"
android:textSize="10pt" />
</LinearLayout>
The two files are almost identical, only that the last two TextViews become EditTexts instead so the user can input the values he or she wants.
Also, kindly notice that I placed weight in my textViews and EditTexts, so that the spaces are maximized when the user shifts the view from portrait to landscape.
resultListView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
TextView textViewQuantity = (TextView)view.findViewById(R.id.qtyInput);
TextView textViewDiscountReq = (TextView)view.findViewById(R.id.discInput);
TextView textViewName = (TextView)view.findViewById(R.id.dialogItemName);
TextView textViewPrice = (TextView)view.findViewById(R.id.price);
TextView textViewDiscount = (TextView)view.findViewById(R.id.discount);
textViewQuantity.setVisibility(View.GONE);
textViewDiscountReq.setVisibility(View.GONE);
textViewName.setVisibility(View.GONE);
textViewPrice.setVisibility(View.GONE);
textViewDiscount.setVisibility(View.GONE);
LinearLayout ll_inflate = (LinearLayout)view.findViewById(R.id.search_result_layout);
Log.d("Angelo", "original width = " + ll_inflate.getWidth());
View child = getLayoutInflater().inflate(R.layout.search_result_inflate, null);
TextView newText = (TextView)child.findViewById(R.id.qtyInput);
newText.setText(textViewQuantity.getText().toString());
EditText enter_txt = (EditText)child.findViewById(R.id.qtyInput);
ll_inflate.addView(child);
Log.d("Angelo", "not original width = " + ll_inflate.getWidth());
return false;
}
});
When the I longClick an item, the layout changes. However, the TextViews and the EditTexts are all next to one another, as if the weight property was ignored. At first I thought that the width might have changed before and after the view was changed, but the Log told me it had 1136 width (I'm using a 2nd Gen Nexus 7). Now I'm blank, the width of the parent didn't change but the weight value was ignored. How do I fix this? Would assigning the weight programmatically a viable solution?
Also, follow up question, I only want the last 2 TextViews to change to EditTexts. So what I try to do to achieve that effect is that from the original layout, I pull the values of the first 3 fields (Item Name, Price, Discount) by doing textView.getText().toString() and get the textviews from the new layout and use setText() but so far, it did not work, any suggestions?
UPDATE:
Upon reading this link, I think that the Weight property of the TextViews inside the LinearLayout is being ignored when I use the LinearLayout as my ListView Row View. Nesting another Linear Layout did not help much either.
I placed another LinearLayout inside as such:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:orientation="horizontal"
android:padding="1dip" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="1dip" >
<TextView
android:id="#+id/dialogItemName"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:layout_weight="0.70"
android:gravity="center"
android:text="Item Name"
android:textSize="23sp" />
<TextView
android:id="#+id/price"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.16"
android:gravity="center"
android:text="Price"
android:textSize="23sp" />
<TextView
android:id="#+id/discount"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.12"
android:gravity="center"
android:text="Discount"
android:textSize="23sp" />
<EditText
android:id="#+id/qtyInput"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.16"
android:gravity="center"
android:hint="qtyInput"
android:inputType="number"
android:textSize="23sp" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/discInput"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.13"
android:gravity="center"
android:hint="discInput"
android:inputType="number|date"
android:textSize="23sp" />
</LinearLayout>
</LinearLayout>
Any ideas?
Call instead:
ll_inflate.addView(child, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
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>