Android Button loses its Text Alignment - android

I have some buttons and a Spinner in my Layout. I Play Music on button click. When Button is pressed then button text gets aligned to left. I don't know why its happening. If I comment button click listener and then if I change items in Spinner , then also it gets Left Aligned.
Here is the part of my code.
<?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="fill_parent" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<Spinner
android:id="#+id/spnList"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:padding="5dp" >
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/btnCall"
android:layout_weight="1"
android:text="C" />
<Button
android:id="#+id/btnCut"
android:layout_weight="1"
android:text="X" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/btn1"
android:layout_weight="1"
android:text="1" />
<Button
android:id="#+id/btn2"
android:layout_weight="1"
android:text="2" />
<Button
android:id="#+id/btn3"
android:layout_weight="1"
android:text="3" />
</TableRow>
</TableLayout>
</RelativeLayout>
Here is Activity Code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
spnList = (Spinner) findViewById(R.id.spnList);
List<String> list = new ArrayList<String>();
list.add("First");
list.add("Second");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
BabyMobile1.this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnList.setAdapter(adapter);
spnList.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
activeItem = (String) arg0.getItemAtPosition(arg2);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});

You are not defining width and height attributes on your buttons. Try with adding below attributes.
android:layout_width="0dp"
android:layout_height="wrap_content"
Example:
<Button
android:id="#+id/btnCall"
android:layout_weight="1"
android:text="C"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
Edit: Answer above gives a solution to keep texts of buttons center horizantal but their texts are shifted up when I tested. I searched this a while and found similar problem (not answered yet) at stack. I guess there is a small bug when using Spinners(or dialogs) inside a RelativeLayout. I tried to change your layout below(changed root to LinearLayout) and it worked as expected. If you have a chance to replace your layout with mine(below) your problem will be solveld. But I have no idea why using RelativeLayout causes a problem like this.
<?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="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Spinner
android:id="#+id/spnList"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btnCall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="C" />
<Button
android:id="#+id/btnCut"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="X" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<Button
android:id="#+id/btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<Button
android:id="#+id/btn3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />
</TableRow>
</TableLayout>
</LinearLayout>

Check the alignments of the button and spinner, if you are using Custom items in spinner then you should check the custom item alignment also.
better understanding purpose give me the code what u are writeing and where you are getting problem?

check this link it may helps you.
http://developer.android.com/reference/android/widget/Spinner.html#attr_android:gravity
here there is a tag in Spinner
android:gravity=" "
using this align the selected item in required position.

Related

How to insert a Whole LinearLayout in the same Activity on a Button click in android Programming?

this is my xml code and i want to insert the whole LinearLayout id(l1) as i click the Mainbutton defined with id(b1). Thing to note is that the linearLayout l1 also contains a textview and a Button.
I know how to insert a View on a button click but i can't find how to insert a Whole Layout on a button Click.
Hope there is no need for .java file. please Define your own onClick method for doing so.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.akash.courtcounter.MainActivity"
android:background="#e5e2e2">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Main Button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="15dp"
android:id="#+id/b1"
android:layout_marginBottom="10dp" />
<LinearLayout
android:layout_width="match_parent"
android:background="#c5ffffff"
android:id="#+id/l1"
android:layout_height="70dp"
android:layout_below="#+id/b1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:text="TextView Here"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/t1"
android:paddingLeft="10dp"
android:textStyle="normal"
android:textSize="20dp"
android:layout_weight="1" />
<Button
android:layout_width="40dp"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/b2"
android:textColor="#747070"
android:textSize="35dp"
android:background="#d3d6d8"
android:onClick="increase"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
You should inflate a new layout dynamically and add it to your parent Linear Layout
LinearLayout myLayout = (LinearLayout)findViewById(R.id.linearLayout2);
View newLayout = getLayoutInflater().inflate(R.layout.new_layout, null, false);
myLayout.addView(newLayout);
new_layout is your layout what you want to add dynamically.
By default make LinearLayout invisible from your .xml file
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#c5ffffff"
android:id="#+id/l1"
android:visibility="gone" //it hides the entire linear layout
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
//Add your views here
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textColor="#color/white" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
//Add your views here
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:textColor="#color/white" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
//Add your views here
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:textColor="#color/white" />
</LinearLayout>
then in your .java file
public class MainActivity extends AppCompatActivity {
LinearLayout l1;
#Override
protected void onCreate(Bundle savedInstanceState) {
l1 = (LinearLayout) this.findViewById(R.id.l1);
l1.setVisibility(View.GONE);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
l1.setVisibility(View.VISIBLE); //on button click Linear Layout will be visible
}
});
}
}

EditText can't edit in android ListView

Currently working with listview which contains edittext in each row.
The row item xml is below
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/result_solution_list_row_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/demo_product_background"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/artist_name_textview"
android:layout_width="#dimen/twohundred"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/home_dark_blue"
android:textSize="#dimen/list_text_size"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="#+id/contentLinearLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:descendantFocusability="afterDescendants"
android:orientation="vertical" >
</LinearLayout>
<TextView
android:id="#+id/add_product"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="2dp"
android:textColor="#color/home_dark_blue"
android:textSize="#dimen/list_text_size"
android:textStyle="bold" />
<ImageView
android:id="#+id/img_camera"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_margin="2dp"
android:background="#drawable/ic_pre_camera"
android:contentDescription="#string/app_name" />
<ImageView
android:id="#+id/drag_handle"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_gravity="center_vertical"
android:layout_margin="2dp"
android:background="#drawable/drag_icon"
android:contentDescription="#string/app_name" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="#color/cus_container" >
</LinearLayout>
In this xml, LinearLayout - contentLinearLayout can be inflated a view dynamically.
The inflating view (xml) contains the EditText views.
The following is the inflated xml .
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingLeft="#dimen/five"
android:paddingRight="#dimen/five"
>
<TextView
android:id="#+id/artist_name_textview"
android:layout_width="#dimen/hundreadfifty"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/home_dark_blue" />
<LinearLayout
android:layout_width="#dimen/home_hundred"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/home_five"
android:layout_marginRight="#dimen/home_five"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/txt_name_quan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:singleLine="true"
android:text="QUANTITY"
android:textSize="#dimen/list_text_size"
android:textColor="#color/home_dark_blue" />
<EditText
android:id="#+id/txt_quan"
android:layout_width="#dimen/home_eighty"
android:layout_height="match_parent"
android:background="#drawable/edt_blue_background"
android:inputType="numberDecimal"
android:singleLine="true"
android:textColor="#color/home_dark_blue" />
</LinearLayout>
<LinearLayout
android:layout_width="#dimen/nav_drawer_uf_image_width"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/txt_name_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:singleLine="true"
android:text="PRICE"
android:textStyle="bold"
android:textSize="#dimen/list_text_size"
android:textColor="#color/home_dark_blue" />
<EditText
android:id="#+id/txt_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/edt_blue_background"
android:inputType="numberDecimal"
android:singleLine="true"
android:textColor="#color/home_dark_blue" />
</LinearLayout>
My problem is , I want both events. 1. ListView's OnItemClickListener 2. EditText should be editable.
I have tried the solutions posted in below links
Trying to catch a click on android ListView item: android:descendantFocusability="blocksDescendants" not working
Android EditText in ListView - keyboard
Focusable EditText inside ListView
http://androidtechnicalblog.blogspot.in/2014/04/quick-trick-of-week-edittext-inside.html
Most of the solutions stressed on android:descendantFocusability attribute.I have tried with different values and set to the listview and listview's root layout alone.
Still the EditTexts are not editable.
Any solutions ?
I have done this with the Imageview earlier.
At that time i put the click listner of Imageview at the Adapter.
I am not sure but may be this will help you.
set this at your adapter
viewHolder.txt_price.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Your Code
}
});
your ListView click event
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// your code
}
});
Not Sure might be help You. Good Luck

android custom radio button not getting checked

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.

Android - TextView's Weight Property in LinearLayout is being ignored when used as ListView row View

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));

How to make listView item and view out of it appear at the same time?

my wish is, to have a listView and when i choose delete in the contextmenu appearing when I do a longclick on a listview item, there should appear checkboxes in each listview element and a buttonbar on the top and one on the bottom...and all at the same time. So i wrote the code following below. But when I click on delete, only the buttonbars appear and I have to do a second click on delete to make the checkboxes appear. By testing with different "configurations" of my code (adding or removing some parts) and checking everything step by step with the debugger I found out, that the checkboxes appear immediately, when I remove the buttonbars and all code is reached.
So now my question is: Does anyone know, why the checkboxes and the bars don't appear on the same click and what to make, so they do?
public boolean onContextItemSelected(MenuItem menu)
{
AdapterView.AdapterContextMenuInfo info = (AdapterView
.AdapterContextMenuInfo) menu.getMenuInfo();
ListView tripList = (ListView) tripTracker.findViewById(R.id.tripList);
tripTracker.findViewById(R.id.buttonBar).setVisibility(View.VISIBLE);
tripTracker.findViewById(R.id.buttonBarCB).setVisibility(View.VISIBLE);
checkBox(false, View.VISIBLE);
((CheckBox) tripList.findViewById(info.targetView.getId())
.findViewById(R.id.checkBox)).setChecked(true);
return true;
}
private void checkBox(boolean checked, int visibility)
{
ListView tripList = (ListView) tripTracker.findViewById(R.id.tripList);
for (int i = 0; i < tripList.getAdapter().getCount(); i++)
{
int id = ((Trip) tripList.getAdapter().getItem(i)).getId();
CheckBox cb = (CheckBox) tripList.findViewById(id).findViewById(
R.id.checkBox);
cb.setVisibility(visibility);
cb.setChecked(checked);
}
}
XML with ListView
<LinearLayout
android:id="#+id/buttonBarCB"
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" >
<Button
android:id="#+id/allCB"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/all" />
<Button
android:id="#+id/noneCB"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/none" />
</LinearLayout>
<TextView
android:id="#+id/noTrip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:text="#string/noTrip" />
<ListView
android:id="#+id/tripList"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
<LinearLayout
android:id="#+id/buttonBar"
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" >
<Button
android:id="#+id/cancelTr"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="#android:string/cancel" />
<Button
android:id="#+id/okTr"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="#android:string/ok" />
</LinearLayout>
</LinearLayout>
XML for rows
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:visibility="gone" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/h1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="?android:attr/textColorPrimaryInverse" />
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/h1"
android:text="date"
android:textColor="?android:attr/textColorSecondaryInverse" />
<TextView
android:id="#+id/h2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/h1"
android:layout_below="#+id/h1"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorSecondaryInverse" />
</RelativeLayout>
</LinearLayout>
I realy suggest you do not reinvent the wheel and use Contexual Action Bar. Here you can find my example how to select rows in listview and work with them.
And of cause, do not forget to call adapter.notifyDataSetChanged(), after changes of data that is under adapter.

Categories

Resources