I have ListView with custom adapter. Listview is not clickable but setOnItemLongClickListener worked properly. ListView row.xml is below:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1" >
<ImageView
android:id="#+id/ivTourImage"
android:layout_width="120dp"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="#+id/lblTourName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Tour Name" />
<RatingBar
android:id="#+id/ratingBarTourStarCount"
style="#style/hotelRatingBarSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:focusable="false"
android:isIndicator="true"
android:numStars="5"
android:stepSize="1.0" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="#+id/lblPlace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Place" />
<TextView
android:id="#+id/lblPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#b6fffa12"
android:text="Price"
android:textColor="#ff4770ff" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:weightSum="1">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="16dp"
android:src="#drawable/adult_icon_16x16" />
<TextView
android:id="#+id/lblAdultCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Adult" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="16dp"
android:layout_marginLeft="15dp"
android:src="#drawable/children_icon_16x16" />
<TextView
android:id="#+id/lblChildCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Child" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="16dp"
android:layout_marginLeft="15dp"
android:src="#drawable/meal_icon_16x16" />
<TextView
android:id="#+id/lblMealCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Meal" />
</LinearLayout>
</LinearLayout>
ListView usage:
<LinearLayout
android:id="#+id/tabTourList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listViewTourAnounce"
android:layout_gravity="center"/>
</LinearLayout>
How to make my listview clickable? Please help me
Try by this code It may Help you.
ListView roomsList
List<String> roomlistdata;
roomlistdata = new ArrayList<String>();
roomsList = (ListView) findViewById(R.id.roomsList);
roomlistdata.add("your data");
ArrayAdapter<String> str = new ArrayAdapter<String>(
getBaseContext(),
android.R.layout.simple_list_item_1,
roomlistdata);
roomsList.setAdapter(str);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long arg3) {
view.setSelected(true);
String UserSelected = ((TextView) view).getText().toString();
...//contests_listView is a array list
}
}
Related
I'm trying to make a visualization of some data in Android using a vertical bar chart, but when I increase or decrease the height of a View programatically by clicking a button, it changes from top to bottom, as in the image below (default value is 300).
How do I change the height from bottom to top, ie keeping the base of the view fixed?
Expected result:
My code in Java:
public final class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
LinearLayout layoutXAxis = (LinearLayout)this.findViewById(R.id.layoutX);
LinearLayout layoutYAxis = (LinearLayout)this.findViewById(R.id.layoutY);
LinearLayout layoutZAxis = (LinearLayout)this.findViewById(R.id.layoutZ);
TextView txtXAxis = (TextView)layoutXAxis.findViewById(R.id.txtXAxis);
TextView txtYAxis = (TextView)layoutYAxis.findViewById(R.id.txtYAxis);
TextView txtZAxis = (TextView)layoutZAxis.findViewById(R.id.txtZAxis);
View graphXAxis = (View)layoutXAxis.findViewById(R.id.XAxis);
View graphYAxis = (View)layoutYAxis.findViewById(R.id.YAxis);
View graphZAxis = (View)layoutZAxis.findViewById(R.id.ZAxis);
LinearLayout layoutBtnAdd = (LinearLayout)this.findViewById(R.id.layAdd);
Button btnAddX = (Button)layoutBtnAdd.findViewById(R.id.btnXAdd);
Button btnAddY = (Button)layoutBtnAdd.findViewById(R.id.btnYAdd);
Button btnAddZ = (Button)layoutBtnAdd.findViewById(R.id.btnZAdd);
LinearLayout layoutBtnSub = (LinearLayout)this.findViewById(R.id.laySub);
Button btnSubX = (Button)layoutBtnSub.findViewById(R.id.btnXSub);
Button btnSubY = (Button)layoutBtnSub.findViewById(R.id.btnYSub);
Button btnSubZ = (Button)layoutBtnSub.findViewById(R.id.btnZSub);
txtXAxis.setText(String.valueOf(graphXAxis.getLayoutParams().height));
txtYAxis.setText(String.valueOf(graphYAxis.getLayoutParams().height));
txtZAxis.setText(String.valueOf(graphZAxis.getLayoutParams().height));
btnAddX.setOnClickListener((OnClickListener)(new OnClickListener() {
public final void onClick(View it) {
View graph = graphXAxis;
LayoutParams params = graph.getLayoutParams();
params.height += 10;
graph = graphXAxis;
graph.setLayoutParams(params);
TextView text = txtXAxis;
View graph = graphXAxis;
text.setText(String.valueOf(graph.getLayoutParams().height));
}
}));
... // onClickListener of the other buttons
}
My code in XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="#+id/layoutY">
<TextView
android:id="#+id/txtYAxis"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textAlignment="center"
android:text="Y AXIS"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#android:color/holo_blue_light"/>
...
<View android:id="#+id/YAxis"
android:layout_width="75dp"
android:layout_height="200dp"
android:background="#android:color/holo_blue_dark"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/layoutX"
android:layout_toLeftOf="#id/layoutY"
android:layout_toStartOf="#id/layoutY"
android:layout_marginRight="50dp"
android:layout_marginEnd="50dp">
<TextView
android:id="#+id/txtXAxis"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textAlignment="center"
android:text="X AXIS"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#android:color/holo_red_light"/>
...
<View android:id="#+id/XAxis"
android:layout_width="75dp"
android:layout_height="200dp"
android:background="#android:color/holo_red_dark"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/layoutZ"
android:layout_toRightOf="#id/layoutY"
android:layout_toEndOf="#id/layoutY"
android:layout_marginLeft="50dp"
android:layout_marginStart="50dp">
<TextView
android:id="#+id/txtZAxis"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textAlignment="center"
android:text="Z AXIS"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#android:color/holo_green_light"/>
...
<View android:id="#+id/ZAxis"
android:layout_width="75dp"
android:layout_height="200dp"
android:background="#android:color/holo_green_dark"/>
</LinearLayout>
...
</RelativeLayout>
You might consider constructing your layout like the following.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/button_layout"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/layoutY"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/txtYAxis"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Y AXIS"
android:textColor="#android:color/holo_blue_light"
android:textSize="18sp"
android:textStyle="bold" />
<View
android:id="#+id/YAxis"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="#android:color/holo_blue_dark"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:id="#+id/layoutX"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|bottom"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/txtXAxis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="X AXIS"
android:textAlignment="center"
android:textColor="#android:color/holo_red_light"
android:textSize="18sp"
android:textStyle="bold" />
<View
android:id="#+id/XAxis"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="#android:color/holo_red_dark" />
</LinearLayout>
<LinearLayout
android:id="#+id/layoutZ"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|bottom"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/txtZAxis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Z AXIS"
android:textAlignment="center"
android:textColor="#android:color/holo_green_light"
android:textSize="18sp"
android:textStyle="bold" />
<View
android:id="#+id/ZAxis"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="#android:color/holo_green_dark" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/button_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Here is the output.
Hope that helps!
I am having a strange issue where my app automatically logs me out when I open a certain fragment. I have even tried commenting out all the code to stop this from happening but the issue still occurs. I have included my empty class and XML for the fragment. I can only assume it is a silly issue I have overlooked but I have been trying to find the answer for a good few days.
Appreciate any help
fragment_edit_profile
<merge 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">
<ScrollView android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/profileImage"
android:layout_marginTop="25dp"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/profileImage"
android:layout_centerHorizontal="true"
android:text="Change profile picture"
android:textSize="18sp"
android:id="#+id/changeProfilePictureTextView"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="50dp"
android:layout_below="#id/changeProfilePictureTextView"
android:background="#drawable/white_and_grey_border_bottom"
android:id="#+id/relativeLayout1">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/ic_action_compose"
android:layout_centerVertical="true"/>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="45dp"
android:id="#+id/usernameEditTextEditProfile"
android:hint="#string/change_username"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="50dp"
android:layout_below="#id/relativeLayout1"
android:background="#drawable/white_and_grey_border_bottom"
android:id="#+id/relativeLayout2">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/ic_action_compose"
android:layout_centerVertical="true"/>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="45dp"
android:hint="Change email address"
android:id="#+id/emailEditTextEditProfile"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="50dp"
android:layout_below="#id/relativeLayout2"
android:background="#drawable/white_and_grey_border_bottom"
android:id="#+id/relativeLayout3">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/ic_action_compose"
android:layout_centerVertical="true"
android:id="#+id/imageView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="45dp"
android:layout_centerVertical="true"
android:text="Click here to reset password"
android:textStyle="italic"
android:textSize="18sp"
android:id="#+id/changePassword"/>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
EditPrfileFragment
public class EditProfileFragment extends Fragment implements View.OnClickListener {
private static final String TAG = "EditProfileFragment";
String password;
String passwordConfirm;
private EditText passwordEditText;
private EditText passwordConfirmEditText;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_edit_profile, container, false);
return view;
}
#Override
public void onClick(View v) {
}
Replace your code with below code.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<ImageView
android:id="#+id/profileImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp" />
<TextView
android:id="#+id/changeProfilePictureTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/profileImage"
android:layout_centerHorizontal="true"
android:text="Change profile picture"
android:textSize="18sp" />
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_below="#id/changeProfilePictureTextView"
android:layout_marginTop="50dp"
android:background="#drawable/white_and_grey_border_bottom">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:background="#drawable/ic_action_compose" />
<EditText
android:id="#+id/usernameEditTextEditProfile"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="45dp"
android:hint="#string/change_username"
tools:ignore="RtlCompat" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_below="#id/relativeLayout1"
android:layout_marginTop="50dp"
android:background="#drawable/white_and_grey_border_bottom">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:background="#drawable/ic_action_compose" />
<EditText
android:id="#+id/emailEditTextEditProfile"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="45dp"
android:hint="Change email address"
tools:ignore="RtlCompat" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout3"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_below="#id/relativeLayout2"
android:layout_marginTop="50dp"
android:background="#drawable/white_and_grey_border_bottom">
<ImageView
android:id="#+id/imageView"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:background="#drawable/ic_action_compose" />
<TextView
android:id="#+id/changePassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="45dp"
android:text="Click here to reset password"
android:textSize="18sp"
android:textStyle="italic"
tools:ignore="RtlCompat" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>
I have created a Custom Adapter to show ImageView and TextView together in ListView. But the ImageView is showing on the top of TextView in every row of Listview. I have attached a screenshot of app below:
Custom Adapter Class:
public class CustomeAdapter extends BaseAdapter {
private Context context;
private ListView listView;
private ArrayList<PlayersModel> playersModelArrayList;
public CustomeAdapter(Context context, ArrayList<PlayersModel> playersModelArrayList) {
this.context = context;
this.playersModelArrayList = playersModelArrayList;
}
#Override
public int getViewTypeCount() {
return getCount();
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getCount() {
return playersModelArrayList.size();
}
#Override
public Object getItem(int position) {
return playersModelArrayList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position,View convertView,ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.lv_item, null, true);
holder.icon = (TextView) convertView.findViewById(R.id.TextID);
holder.tvname = (TextView) convertView.findViewById(R.id.name);
holder.tvdescr = (TextView) convertView.findViewById(R.id.descr);
holder.tvlink = (TextView) convertView.findViewById(R.id.link);
holder.ImageIcon = (ImageView) convertView.findViewById(R.id.logoImageView);
convertView.setTag(holder);
}else {
holder = (ViewHolder)convertView.getTag();
}
holder.ImageIcon.setImageResource(playersModelArrayList.get(position).getImageId());
holder.icon.setText(playersModelArrayList.get(position).getICON());
holder.tvname.setText(playersModelArrayList.get(position).getName());
holder.tvdescr.setText(playersModelArrayList.get(position).getDescr());
holder.tvlink.setText(playersModelArrayList.get(position).getLink());
Glide.with(context)
.load("http://csalabs.in/wp-content/uploads/2017/10/CSA-Final-PNG-300x350.png")
.override(200,200)
.into(holder.ImageIcon);
Log.d("Icon URL",holder.icon.toString());
return convertView;
}
private class ViewHolder {
protected TextView icon, tvname, tvdescr, tvlink;
protected ImageView ImageIcon;
}
}
Layout File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:padding="10dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:onClick="buttonClickNew">
<ImageView
android:id="#+id/logoImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
android:adjustViewBounds="true"
tools:srcCompat="#tools:sample/avatars[0]" />
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#383838"
android:layout_marginTop="10dp"
android:textSize="15sp"
android:textStyle="bold"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingLeft="10dp"
android:text="Name" />
<TextView
android:id="#+id/descr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#313131"
android:textSize="12sp"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingLeft="10dp"
android:text="Country" />
<TextView
android:id="#+id/link"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#383838"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingLeft="10dp"
android:text="#string/city"
android:visibility="gone"/>
<TextView
android:id="#+id/TextID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#313131"
android:textSize="12sp"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingLeft="10dp"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#color/colorAccent"/>
</LinearLayout>
How to align ImageView on the left side of TextView? Thanks in advance.
BTW I am newbie in android and java.
Try this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/logoImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitStart"
tools:srcCompat="#tools:sample/avatars[0]" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="buttonClickNew"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#383838"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/descr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="Country"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#313131"
android:textSize="12sp" />
<TextView
android:id="#+id/link"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="city"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#383838"
android:visibility="visible" />
<TextView
android:id="#+id/TextID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#313131"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#color/colorAccent" />
</LinearLayout>
Use like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:padding="10dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:onClick="buttonClickNew">
<ImageView
android:id="#+id/logoImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
android:adjustViewBounds="true"
tools:srcCompat="#tools:sample/avatars[0]" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:onClick="buttonClickNew">
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#383838"
android:layout_marginTop="10dp"
android:textSize="15sp"
android:textStyle="bold"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingLeft="10dp"
android:text="Name" />
<TextView
android:id="#+id/descr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#313131"
android:textSize="12sp"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingLeft="10dp"
android:text="Country" />
<TextView
android:id="#+id/link"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#383838"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingLeft="10dp"
android:text="#string/city"
android:visibility="gone"/>
<TextView
android:id="#+id/TextID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#313131"
android:textSize="12sp"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingLeft="10dp"/>`enter code here`
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#color/colorAccent"/>
</LinearLayout>
Try to wrap Your text to separated LinearLayout with vertical orientation, and than put this LinearLayout with text on same level as ImageView inside another LinearLayout with orientation="horizontal"
But also, you should remember about render and measure performance.
You can read about this in https://android.jlelse.eu/constraint-layout-performance-870e5f238100
<LinearLayout
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_weight="1" // try to read about this parameter more
/>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="4"
android:orientation="vertical">
<TextView ... />
<TextView ... />
<TextView ... />
</LinearLayout>
</LinearLayout>
Try to create two linear layouts with orientation vertical under parent linear layout which has the horizontal orientation.
Set the second linear layout's width to match_parent and the image layout should be a fixed width value.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="buttonClickNew"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:id="#+id/logoImageView"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
tools:srcCompat="#tools:sample/avatars[0]" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#383838"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/descr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="Country"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#313131"
android:textSize="12sp" />
<TextView
android:id="#+id/link"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="City"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#383838"
android:visibility="gone" />
<TextView
android:id="#+id/TextID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#313131"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#color/colorAccent" />
</LinearLayout>
In order to simplify your xml, you should take a look to ConstraintLayout
To use it, make the import in your app gradle by adding implementation 'com.android.support.constraint:constraint-layout:1.1.3'
Then, refactor your xml as below:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorAccent" android:id="#+id/view"
tools:layout_editor_absoluteX="0dp" app:layout_constraintTop_toBottomOf="#+id/logoImageView"
android:layout_marginTop="8dp"/>
<ImageView
android:layout_width="64dp"
android:layout_height="64dp" tools:srcCompat="#tools:sample/avatars"
android:id="#+id/logoImageView"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp"
android:layout_marginTop="8dp" app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:id="#+id/name"
tools:text="Name"
android:textAppearance="#style/TextAppearance.AppCompat" android:textStyle="bold"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/descr" app:layout_constraintStart_toEndOf="#+id/logoImageView"
android:layout_marginStart="8dp"/>
<TextView
tools:text="Description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/descr" android:textColor="#313131"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="#+id/logoImageView"
app:layout_constraintStart_toEndOf="#+id/logoImageView" android:layout_marginStart="8dp"/>
</android.support.constraint.ConstraintLayout>
I have a ListView and it has a Header and Footer. Their layout in CardView. And Content list in which one must be background. Here is a picture which can be clearly seen: I now this is how it turns out:
And how to do:
I did so, ScrollView> first container>listView in second container > third container. But, unfortunately, at the bottom of the container 2 it was a huge empty space!
Is it possible to implement a so-in ListView? if so how, if not then how to implement?
UPDATE
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Details.Book.BookDetails"
android:background="#color/fragment_bg">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>
<View android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#drawable/toolbar_shadow"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/toolbar"
android:id="#+id/view5" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/scrollView6"
android:layout_below="#+id/toolbar">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="#+id/toolbar"
android:id="#+id/linearLayout16">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#drawable/rounded_top_layout"
android:paddingBottom="#dimen/leftPadding"
android:paddingLeft="#dimen/rightPadding"
android:paddingRight="#dimen/rightPadding"
android:paddingTop="#dimen/leftPadding">
<TextView
android:id="#+id/textviewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="false"
android:layout_centerVertical="true"
android:text="#string/buy_det_pass_txt"
android:textColor="#color/background_color"
android:textSize="#dimen/mainLargeSize" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/relativeLayout9"
android:paddingBottom="#dimen/leftPadding"
android:paddingLeft="#dimen/rightPadding"
android:paddingRight="#dimen/rightPadding"
android:paddingTop="#dimen/leftPadding">
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/relativeLayout9"
android:layout_marginBottom="#dimen/textMargin"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/buy_det_fullname"
android:textColor="#color/mainGreyColor"
android:textSize="#dimen/mainSmallSize" />
<TextView
android:id="#+id/tvFullName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="Medium Text"
android:textColor="#color/mainGreyColor"
android:textSize="#dimen/mainMiddleSize" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/linearLayout4"
android:layout_marginBottom="#dimen/textMargin"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/buy_det_gender"
android:textColor="#color/mainGreyColor"
android:textSize="#dimen/mainSmallSize" />
<TextView
android:id="#+id/tvGender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="Medium Text"
android:textColor="#color/mainGreyColor"
android:textSize="#dimen/mainMiddleSize" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/linearLayout3"
android:layout_marginBottom="#dimen/textMargin"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/buy_det_ticknumber"
android:textColor="#color/mainGreyColor"
android:textSize="#dimen/mainSmallSize" />
<TextView
android:id="#+id/tvTickNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="Medium Text"
android:textColor="#color/mainGreyColor"
android:textSize="#dimen/mainMiddleSize" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/linearLayout6"
android:layout_marginBottom="#dimen/textMargin"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/buy_det_issdate"
android:textColor="#color/mainGreyColor"
android:textSize="#dimen/mainSmallSize" />
<TextView
android:id="#+id/tvIssDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="Medium Text"
android:textColor="#color/mainGreyColor"
android:textSize="#dimen/mainMiddleSize" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
<kz.tem_tour.Primer.MainActivity.NonScrollListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_centerHorizontal="true"
android:paddingRight="#dimen/textMargin"
android:paddingLeft="#dimen/textMargin"
android:layout_below="#+id/linearLayout16" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="#+id/listView">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#drawable/rounded_top_layout"
android:paddingTop="#dimen/leftPadding"
android:paddingBottom="#dimen/leftPadding"
android:paddingLeft="#dimen/rightPadding"
android:paddingRight="#dimen/rightPadding">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buy_det_cost_txt"
android:id="#+id/tvFromTo"
android:textColor="#color/background_color"
android:textSize="#dimen/mainLargeSize"
android:layout_alignParentTop="false"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/relativeLayout9"
android:paddingRight="#dimen/rightPadding"
android:paddingLeft="#dimen/rightPadding"
android:paddingTop="#dimen/leftPadding"
android:paddingBottom="#dimen/leftPadding">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/relativeLayout9"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout4"
android:gravity="center_vertical"
android:layout_marginBottom="#dimen/textMargin" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/buy_det_fare"
android:layout_weight="1"
android:textColor="#color/mainGreyColor"
android:textSize="#dimen/mainSmallSize" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Medium Text"
android:id="#+id/tvFare"
android:layout_weight="1"
android:textColor="#color/mainGreyColor"
android:gravity="right"
android:textSize="#dimen/mainMiddleSize" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout3"
android:gravity="center_vertical"
android:layout_marginBottom="#dimen/textMargin">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/buy_det_taxes"
android:layout_weight="1"
android:textColor="#color/mainGreyColor"
android:textSize="#dimen/mainSmallSize" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Medium Text"
android:id="#+id/tvTaxes"
android:layout_weight="1"
android:textColor="#color/mainGreyColor"
android:gravity="right"
android:textSize="#dimen/mainMiddleSize" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout6"
android:gravity="center_vertical"
android:layout_marginBottom="#dimen/textMargin">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/buy_det_service_fees"
android:layout_weight="1"
android:textColor="#color/mainGreyColor"
android:textSize="#dimen/mainSmallSize" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Medium Text"
android:id="#+id/tvFees"
android:layout_weight="1"
android:textColor="#color/mainGreyColor"
android:gravity="right"
android:textSize="#dimen/mainMiddleSize" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout6"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout7"
android:gravity="center_vertical"
android:layout_marginBottom="#dimen/textMargin">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/buy_det_total_price"
android:layout_weight="1"
android:textColor="#color/mainGreyColor"
android:textSize="#dimen/mainSmallSize" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Medium Text"
android:id="#+id/tvTotPrice"
android:layout_weight="1"
android:textColor="#color/mainGreyColor"
android:gravity="right"
android:textSize="#dimen/mainMiddleSize" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout15"
android:gravity="center_vertical"
android:layout_marginBottom="#dimen/textMargin" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/buy_det_form_of_pay"
android:layout_weight="1"
android:textColor="#color/mainGreyColor"
android:textSize="#dimen/mainSmallSize" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Medium Text"
android:id="#+id/tvPayment"
android:layout_weight="1"
android:textColor="#color/mainGreyColor"
android:gravity="right"
android:textSize="#dimen/mainMiddleSize" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
I have solution for that.. not healthy one but it should work for you.
Rather then adding header and footer wrap all three layout inside ScrollView.
<ScrollView ... >
<CardView ... /> <!-- this will be header -->
<CardView ...>
<yourpackagename.NonScrollListView ... /> <!-- non scrollable listview -->
</CardView>
<CardView ... /> <!-- this will footer -->
</ScrollView>
Here is the class for NonScrollListView
public class NonScrollListView extends ListView {
public NonScrollListView(Context context) {
super(context);
}
public NonScrollListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
}
Add listview in CardView and above that cardview add header in different card view and same add footer in cardview under ListView cardview. And add whole layout is scrollview. Calculate height of listview at runtime and add height to listview. Your custom look is ready.
public static void setTotalHeightofListView(ListView listView) {
ListAdapter mAdapter = listView.getAdapter();
int totalHeight = 0;
for (int i = 0; i < mAdapter.getCount(); i++) {
View mView = mAdapter.getView(i, null, listView);
mView.measure(
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
totalHeight += mView.getMeasuredHeight();
Log.w("HEIGHT" + i, String.valueOf(totalHeight));
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (mAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
I nedd to click on items in list activity to do something, but its not working
i search for this problem and saw some answers about this such as :
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
or
android:descendantFocusability="afterDescendants"
android:descendantFocusability="beforeDescendants"
android:descendantFocusability="blocksDescendants"
but those are not worked
this is my listActivity :
public class TrainListActivity extends SherlockListActivity
{
public static String varStart = "com.example.traininfo.startcity";
public static String varDestination = "com.example.traininfo.destinationcity";
private String start;
private String destination;
ArrayList<TrainType> trains;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trainlist);
Log.i("SearchTrain", "try to get extras...");
start = getIntent().getExtras().getString(varStart);
destination = getIntent().getExtras().getString(varDestination);
TrainController tc = new TrainController(this);
trains = new ArrayList<TrainType>();
trains = tc.getTrainList(start, destination).getTrain();
Log.i("SearchTrain", "got the train list...");
TrainListAdapter adapter = new TrainListAdapter(this, trains);
Log.i("SearchTrain", "adapter initialized successfully!!");
setListAdapter(adapter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
// I want to do something here :(
super.onListItemClick(l, v, position, id);
Log.i("trainList", "on click");
}
}
row xml file :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#27ae60"
android:paddingBottom="5dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true" >
<TextView
android:id="#+id/tvTrainName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="#string/resultTrainName"
android:textColor="#android:color/white"
android:textSize="20sp"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:textStyle="bold" />
<TextView
android:id="#+id/tvStartLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/tvTrainName"
android:gravity="right"
android:text="#string/resultTimeOut"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:textColor="#android:color/white"
android:textSize="15sp" />
<TextView
android:id="#+id/tvTimeOut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tvTrainName"
android:layout_marginRight="30sp"
android:layout_toLeftOf="#id/tvStartLabel"
android:text="#string/resultTimeOutEx"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:textColor="#android:color/white"
android:textSize="15sp" />
<TextView
android:id="#+id/tvFinishLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/tvStartLabel"
android:gravity="right"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:text="#string/resultTimeIn"
android:textColor="#android:color/white"
android:textSize="15sp" />
<TextView
android:id="#+id/tvTimeIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/tvTimeOut"
android:layout_below="#id/tvTimeOut"
android:text="#string/resultTimeInEx"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:textColor="#android:color/white"
android:textSize="15sp" />
<TextView
android:id="#+id/tvPrices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/tvTimeIn"
android:gravity="right"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:textColor="#android:color/white"
android:textSize="15sp" />
<ImageButton
android:id="#+id/btnMoreInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="23dp"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:background="#android:color/transparent"
android:contentDescription="#string/resultMoreInfo"
android:src="#drawable/action_info" />
and ListActivity xml File :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:clickable="true"
android:descendantFocusability="afterDescendants" />
</LinearLayout>
please help me :(
Use normal activity and put a ListView in xml. Use inflator to use your custom row design. Just put the focusable=false for your imagebutton. It will allow you to click on the row and you can apply a separate click listener for your imagebutton to handle its click separately.
I faced the same problem not too long ago. It seems that the ImageButton takes focus regardless what you do. What I did was to replace the ImageButton with and ImageView and set the listener to it.
This is how your xml files should look like.
row xml file :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#27ae60"
android:paddingBottom="5dp" >
<TextView
android:id="#+id/tvTrainName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="#string/resultTrainName"
android:textColor="#android:color/white"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvStartLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/tvTrainName"
android:gravity="right"
android:text="#string/resultTimeOut"
android:textColor="#android:color/white"
android:textSize="15sp" />
<TextView
android:id="#+id/tvTimeOut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tvTrainName"
android:layout_marginRight="30sp"
android:layout_toLeftOf="#id/tvStartLabel"
android:text="#string/resultTimeOutEx"
android:textColor="#android:color/white"
android:textSize="15sp" />
<TextView
android:id="#+id/tvFinishLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/tvStartLabel"
android:gravity="right"
android:text="#string/resultTimeIn"
android:textColor="#android:color/white"
android:textSize="15sp" />
<TextView
android:id="#+id/tvTimeIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/tvTimeOut"
android:layout_below="#id/tvTimeOut"
android:text="#string/resultTimeInEx"
android:textColor="#android:color/white"
android:textSize="15sp" />
<TextView
android:id="#+id/tvPrices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/tvTimeIn"
android:gravity="right"
android:textColor="#android:color/white"
android:textSize="15sp" />
<ImageView
android:id="#+id/btnMoreInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="23dp"
android:background="#android:color/transparent"
android:contentDescription="#string/resultMoreInfo"
android:src="#drawable/action_info" />
Activity xml.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"/>
</LinearLayout>