in android, I write a code for ask questions from user.answers show in RadioGroup that have multiple RadioButton, but when user click on a RadioButton, that RadioButton does not fill in the graphic.
code of radioGroup.setOnCheckedChangeListener is here:
holder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(final RadioGroup group, final int checkedId) {
rb = group.findViewById(checkedId);
try {
//set time in mili
Thread.sleep(500);
}catch (Exception e){
e.printStackTrace();
}
if (correctAns.equals(rb.getText())) {
if (itemList.get(position).getLevel().equals("el"))
sumResultEl++;
else if (itemList.get(position).getLevel().equals("pre"))
sumResultPre++;
else
sumResultInter++;
}
there is RecyclerView.my questions show by RecyclerView.
xml of questions is here:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="center">
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_gravity="center"
android:orientation="vertical">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true">
<TextView
android:id="#+id/question_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:textSize="20dp"
android:paddingLeft="10dp"/>
<RadioGroup
android:id="#+id/radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
>
<RadioButton
android:id="#+id/radio_button_answer1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:checked="false"
android:padding="10dp"
android:background="#android:color/white"/>
<RadioButton
android:id="#+id/radio_button_answer2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:checked="false"
android:padding="10dp"
android:background="#android:color/white"/>
<RadioButton
android:id="#+id/radio_button_answer3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:checked="false"
android:padding="10dp"
android:background="#android:color/white"/>
<RadioButton
android:id="#+id/radio_button_answer4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:checked="false"
android:padding="10dp"
android:background="#android:color/white"/>
</RadioGroup>
</LinearLayout>
</ScrollView>
</LinearLayout>
Please check your app theme.
Especially the colorAccent. If it is same as colorPrimary then that might be the issue for the graphics
It might be due to the same color of background as well as of radio button's tint
Try setting RadioGroup background to white
Add below line to your RadioGroup
android:background="#android:color/white"
edit: Everything works fine for me here with your provided xml code.
But Thread.sleep(500) pauses your main thread for half second.
Instead use
`handler.postDelayed(runnable, delayInMillis);`
Also I suspect that your style is modified (colorPrimary, colorAccent etc.) please check that
Related
I am using recyclerview for showing data from web service. Based on particular condition i need to remove the particular recycler view item. So i am using View.Gone. But it is showing empty space. I googled about this issue and set the height to wrapcontent, but it is not working. Please any one help me.
my code:
xml: item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/businfo"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="4dp"
app:contentPaddingBottom="10dp"
app:contentPaddingTop="16dp"
android:elevation="10dp"
android:layout_margin="5dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:orientation="horizontal">
<TextView
android:id="#+id/travel_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="14sp"
android:layout_marginLeft="5dp"
android:textColor="#color/holo_blue_light"
android:text="CGR Travels"/>
<TextView
android:id="#+id/bus_price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:textSize="14sp"
android:textColor="#F04C3B"
android:layout_marginRight="30dp"
android:text="Rs.349"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginTop="5dp"
android:background="#b6b6b6"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<TextView
android:id="#+id/arrtime_desttime"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:textSize="15sp"
android:textColor="#1e356a"
android:text="9.00P.M - 7.00AM"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:textSize="12sp"
android:layout_marginRight="30dp"
android:text="38 seats"
android:id="#+id/seatcount" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="12sp"
android:layout_marginLeft="5dp"
android:text="2+1 semi sleeeper"
android:id="#+id/bustype" />
<TextView
android:id="#+id/cancellationpolicy"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:textSize="12sp"
android:textColor="#color/holo_blue_light"
android:layout_marginRight="30dp"
android:text="Cancellation policy"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:id="#+id/routeid"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:id="#+id/dropdate"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
java:
#Override
public void onBindViewHolder(final BusrouteAdapter.ViewHolder viewHolder, final int position) {
if ((bus_routes.get(position).getAvailableSeats() == 0)) {
viewHolder.businfo.setVisibility(View.GONE);
} else {
viewHolder.businfo.setVisibility(View.VISIBLE);
}
}
Although removing the item from list and then updating list (notifydatasetchanged()) is a good practice but in some cases like when you have multiple viewHolders in a signle recyclerView it may be complicated, so in order to hide an specific item in your recyclerView you should do as below:
- Dont hide the root view of your layout, instead add a child to rootView of your layout which contains all other views and hide that. for example in your case you can set your cardView visibity to View.GONE and dont make any change to its parent linearLayout (businfo). i had similar case and this worked for me.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/rootView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- don't change visibility of rootView view -->
<LinearLayout
android:id="#+id/child_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Place all child views inside here -->
<!-- change visibility of child_container view -->
</LinearLayout>
</LinearLayout>
You should remove this item from your bus_routes:
bus_routes.remove(position);
And then:
notifyItemRemoved(position);
notifyItemRangeChanged(position, bus_routes.size());
You can not directly hide view this way. you have to remove unwanted items from your ArrayList or don't add unwanted item before setting adapter. If your items are updated then again remove unwanted items and call notifydataset changed
Remove this element from ArrayList and use notifyDataSetChanged();
Example :
holder.imageDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
paths.remove(position);
notifyDataSetChanged();
}
});
You dont need to set visible or gone just remove item from list and than update list.
#Override
public void onBindViewHolder(final BusrouteAdapter.ViewHolder viewHolder, final int position) {
if ((bus_routes.get(position).getAvailableSeats() == 0)) {
bus_routes.remove(position);
}
}
try to call youradapter.notifydatasetchanged() after set adapter to recyclerview from activity.
I want to change the background color of my RecyclerView item. I think I should be able to do it in onBindViewHolder method but I am not able to do this. I only get bottom border color of the item changed but I want to change the full background color
Here is what I want
public void onBindViewHolder(InstalledFontViewRecyclerAdapter.ViewHolder holder, int position) {
if (//Some Condition) {
holder.itemView.setBackgroundColor(Color.GREY);
}
else {
holder.itemView.setBackgroundColor(Color.RED);
}
}
I think this should produce something like
What I get is this
Here is my RecyclerView Fragment layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:id="#+id/installed_recyclerView"
android:paddingTop="1dp"
></android.support.v7.widget.RecyclerView>
</LinearLayout>
Here is my item layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="3dp"
android:paddingBottom="1dp"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/post_card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="1dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Arial New"
android:id="#+id/installed_font_name"
android:textSize="16dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Monotype Solutions"
android:textSize="12dp"
android:id="#+id/installed_preview_company_name"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Preview Test"
android:id="#+id/installed_preview_textview"
android:textSize="30dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingRight="10dp"
android:gravity="center"
android:id="#+id/installed_preview_Unnstall">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_remove_circle_24dp"
android:background="#android:color/transparent"
android:tint="#android:color/holo_red_light"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Uninstall"
android:textSize="12dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingRight="10dp"
android:gravity="center"
android:id="#+id/installed_preview_flip_layout"
>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_autorenew_24dp"
android:background="#android:color/transparent"
android:tint="#android:color/holo_blue_dark"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Flip This"
android:textSize="12dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
Please try to change the code in onBindViewHolder as below
if(// Any Condition) {
((CardView)holder.post_card_view).setCardBackgroundColor(Color.GREY);
}
else
{
((CardView)holder.post_card_view).setCardBackgroundColor(Color.RED);
}
I hope this will help you , Feel free to comment.
I think that holder.itemView is not CardView but LinearLayout which holds CardView. Try something like this:
public void onBindViewHolder(InstalledFontViewRecyclerAdapter.ViewHolder holder, int position) {
if (//Some Condition) {
holder.yourCardView.setCardBackgroundColor(Color.GREY);
}
else {
holder.yourCardView.setCardBackgroundColor(Color.RED);
}
}
#pankaj as #pawelo said casting can be costly sometimes so the best solution would be to give transparent color to CardView in xml like this
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/post_card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
card_view:cardBackgroundColor="#android:color/transparent"
card_view:cardCornerRadius="1dp">
and then set background color to holder itemView like this.
public void onBindViewHolder(InstalledFontViewRecyclerAdapter.ViewHolder holder, int position) {
if (//Some Condition) {
holder.itemView.setBackgroundColor(Color.GREY);
}
else {
holder.itemView.setBackgroundColor(Color.RED);
}
}
now it should work as intended.
I tried the above answers in my project, it worked but Android studio gave me errors.
So I used holder.text_container.setBackgroundResource(R.color.category_numbers); instead and the red lines were gone. I used color from my custom resource.
I hope this helps someone.
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 an alert dialog with a positive and negative button added programatically. In the dialog layout there are also two buttons, above the AlertDialog's native buttons.
When these two are right next to each other, I realized that in this dialog, the native positive/negative/neutral buttons are not equally weighted. The content of their text determines their horizontal weight, rather than each taking up 50% (or 33% if 3 buttons) of the dialog. So in my case, where the negative button's text is longer than that positive button's text, we get something that looks like the image I posted above.
I can't find a way to fix this, does anyone have an idea?
Thanks!
XML as requested:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="#+id/rcd_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="8dp"
android:text="#string/rating_dialog_text"
android:textSize="14dp"
android:textColor="#android:color/white"
android:gravity="center"/>
<RatingBar android:id="#+id/rcd_rating_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/rcd_msg"
android:layout_margin="8dp"
android:paddingBottom="10dp"
android:numStars="5"
android:rating="0"
android:layout_centerHorizontal="true"/>
<RelativeLayout android:layout_height="56dp"
android:layout_width="match_parent"
android:layout_below="#id/rcd_rating_bar">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout android:id="#+id/rcd_image"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:background="#color/selectable_transparent"
android:layout_weight="1">
<TextView android:id="#+id/rcd_image_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:paddingLeft="0dp"
android:gravity="center"
android:text="#string/add_image"
android:textColor="#android:color/white"
android:textSize="16sp"
android:clickable="true"/>
<ImageView android:id="#+id/rcd_image_img"
android:layout_height="32dp"
android:layout_width="32dp"
android:layout_centerVertical="true"
android:layout_margin="8dp"
android:layout_toLeftOf="#id/rcd_image_text"
android:scaleType="centerInside"
android:src="#android:drawable/ic_menu_camera"/>
</RelativeLayout>
<RelativeLayout android:id="#+id/rcd_comment"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:background="#color/selectable_transparent"
android:layout_weight="1">
<TextView android:id="#+id/rcd_comment_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:paddingRight="6dp"
android:gravity="center"
android:text="#string/add_comment"
android:textColor="#android:color/white"
android:textSize="16sp"
android:clickable="true"/>
<ImageView android:id="#+id/rcd_comment_img"
android:layout_height="32dp"
android:layout_width="32dp"
android:layout_centerVertical="true"
android:layout_margin="4dp"
android:layout_toRightOf="#id/rcd_comment_text"
android:scaleType="centerInside"
android:src="#drawable/ic_menu_comment"/>
</RelativeLayout>
</LinearLayout>
<LinearLayout android:layout_width="1dp"
android:layout_height="38dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:layout_margin="10dp"
android:background="#color/transparent_white" />
</RelativeLayout>
</RelativeLayout>
Figured out a solution... don't know why I didn't think of it before.
I do the following:
mRateConcertDialog.setOnShowListener(new OnShowListener() {
#Override
public void onShow(DialogInterface d) {
Button posButton = mRateConcertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
Button negButton = mRateConcertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
LayoutParams posParams = (LayoutParams) posButton.getLayoutParams();
posParams.weight = 1;
posParams.width = LayoutParams.MATCH_PARENT;
LayoutParams negParams = (LayoutParams) negButton.getLayoutParams();
negParams.weight = 1;
negParams.width = LayoutParams.MATCH_PARENT;
posButton.setLayoutParams(posParams);
negButton.setLayoutParams(negParams);
}
});
Basically, you have to manipulate the size of the buttons AFTER the dialog is shown. So create an onShowListener and you can just set weights and widths that way. Thanks for the comments, I hope this can help someone in the future.
I have a listview with some text and a checkbox. I can click the checkbox, and the check changes, but when I try to listen for the check being changed, or the click, the listener never triggers.
I have the listview in an Activity (not a ListActivity)
schedule.xml layout:
<?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="fill_parent"
android:orientation="vertical"
android:id="#+id/schedulerLayout">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:paddingBottom="10dp">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/currentHours"
android:textSize="50dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50dp"
android:text=":"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/currentMinutes"
android:textSize="50dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical">
<!--vertical filler for am/pm display -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ampm"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/currentDayOfWeek"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/currentDate"/>
</LinearLayout>
</LinearLayout>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/scheduleButton"
android:text="#string/newSchedule"/>
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#FFFFFFFF"/>
<ListView
android:id="#+id/scheduler_list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"/>
</LinearLayout>
schedulerow.xml:
<?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="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
<TextView
android:id="#+id/scheduleListTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:text="Title"/>
<TextView
android:id="#+id/scheduleListDays"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/scheduleListTitle"
android:text="Days"/>
<TextView
android:id="#+id/scheduleListMonths"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/scheduleListDays"
android:text="Months"/>
<View
android:id="#+id/schedulerSeparator"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#FFFFFFFF"
android:layout_below="#id/scheduleListMonths"/>
<CheckBox
android:id="#+id/scheduleCheckBox"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="true"
android:layout_above="#id/schedulerSeparator"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
<TextView
android:id="#+id/scheduleListTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/scheduleCheckBox"
android:text="Time"/>
</RelativeLayout>
I'm filling in the data like this:
SimpleAdapter adapter = new SimpleAdapter(this,
data,
R.layout.schedulerow,
new String[] {"Title","Time","Day", "Month"},
new int[] { R.id.scheduleListTitle, R.id.scheduleListTime,R.id.scheduleListDays, R.id.scheduleListMonths});
listView.setAdapter(adapter);
If I try to add a listener to the checkbox, it does nothing. Any suggestions? I've looked through a TON of sample code and similar questions, and none of it seems to work.
One solution is to forget the list item click listener and use another listener for the rest of the item other than the checkbox.. In item layout xml, put the other views in single layout and set the click listener on this and set the check listener on checkbox as usual. Both will work without issues. Dont use the list item click listener.
class MySimpleAdapter extends SimpleAdapter {
...
#Override
void getView(.....) {
View view = super.getView(.....);
view.findViewById(R.id.checkboxid).setOnCheckChangedListner(checklistener);
view.findViewById(R.id.restOfitemview).setOnClickListner(clickListner);
return view;
}
}
Define the two listeners and set as shown above.
Edit : First try your method(blocking focus on checkbox) with the MySimpleAdapter setting only the checkchangelistener.. If that does not work then use above solution of dividing the item layout.