How could I add a line just above of first item (10) a line?
I'm using RecyclerView and LinearLayoutManager to implement my design.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".MainActivity">
<SeekBar
android:id="#+id/timesTablesSeekBar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="32dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
list_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:showDividers="beginning">
<TextView
android:id="#+id/timeTables"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text=""
android:textSize="70px"
android:textStyle="bold|italic" />
</LinearLayout>
First of all, using LinearLayout's android:showDividers to divide items is not a best practice. First, I would remove outer LinearLayout from your list_layout.xml like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/timeTables"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text=""
android:textSize="70px"
android:textStyle="bold|italic" />
To show dividers, the best way is to use RecyclerView.ItemDecoration. Please see DividerItemDecoration to draw divider:
https://developer.android.com/reference/androidx/recyclerview/widget/DividerItemDecoration
dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
layoutManager.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);
Then the only thing missing would be drawing top line. You can implement a custom ItemDecoration like this:
public class TopLineDecoration extends RecyclerView.ItemDecoration {
private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
private final Drawable divider;
public TopLineDecoration(Context context) {
TypedArray a = context.obtainStyledAttributes(ATTRS);
divider = a.getDrawable(0);
a.recycle();
}
#Override
public void onDraw(#NonNull Canvas c, #NonNull RecyclerView parent, #NonNull RecyclerView.State state) {
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
if (parent.getChildAdapterPosition(child) == 0) {
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int top = child.getTop() - params.topMargin;
int bottom = top + divider.getIntrinsicHeight();
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
divider.setBounds(left, top, right, bottom);
divider.draw(c);
}
}
}
}
And your final implementation would be like:
dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
layoutManager.getOrientation());
topLineDecoration = new TopLineDecoration(recyclerView.getContext())
recyclerView.addItemDecoration(dividerItemDecoration);
recyclerView.addItemDecoration(topLineDecoration)
Related
I have 100+ item in RecyclerView. these days I have tried to create simple code/effective code to create Checkbox in BottomSheetDialog Programmatically but no success.
My goal :
Every item click in RecyclerView will show BottomSheetDialog with different amount checkbox. example :
when click item[0] in RecyclerView will open BottomSheetDialog with 7 checkboxes, if click item[1] in RecyclerView will open BottomSheetDialog with 286 checkboxes, or if click item [2] will open BottomSheetDialog with 200 checkboxes, and so on.
This image will describe, what I meant.
I think my goal is possible instead I create 100+ xml for every item in RecyclerView. Using for looping, array and other... if no simple/effective code for programmatically create checkbox, never mind I will create 100+ xml for every itemListener at RecyclerView
What I have tried :
I successed add single checkbox, but failed to add other checkbox. Here is my code :
Note : I commented for looping because it didn't work. Oke, thanks all :)
#Override
public void onClick(View v) {
int itemPosition = recyclerViewSurat.getChildLayoutPosition(v);
String namaSurat = suratList.get(itemPosition).getNamaSurat();
// Toast.makeText(context, namaSurat, Toast.LENGTH_SHORT).show();
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(context, R.style.BottomSheetDialogTheme);
bottomSheetDialog.setContentView(R.layout.layout_bottom_sheet_ayat);
ConstraintLayout constraintLayout = bottomSheetDialog.findViewById(R.id.constraintLayoutCheckBox);
ConstraintSet constraintSet = new ConstraintSet();
ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.MATCH_PARENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT
);
int margin = (int) convertDpToPixel(30F, context);
params.setMargins(margin,0,0,0);
// for (int i = 0; i < 10; i++) {
// AppCompatCheckBox compatCheckBox = new AppCompatCheckBox(context);
// compatCheckBox.setId(i + 1);
// compatCheckBox.setText("Ayat " + i);
// compatCheckBox.setPadding(25, 0,0,0);
// constraintLayout.addView(compatCheckBox, params);
//
// constraintSet.clone(constraintLayout);
// constraintSet.connect(compatCheckBox.getId(), ConstraintSet.TOP, compatCheckBox.getId() + i, ConstraintSet.BOTTOM, 0);
// constraintSet.applyTo(constraintLayout);
// }
AppCompatCheckBox compatCheckBox = new AppCompatCheckBox(context);
compatCheckBox.setId(R.id.checkbox2);
compatCheckBox.setText("Ayat 1");
compatCheckBox.setPadding(25, 0,0,0);
constraintLayout.addView(compatCheckBox, params);
constraintSet.clone(constraintLayout);
constraintSet.connect(compatCheckBox.getId(), ConstraintSet.TOP, R.id.checkbox1, ConstraintSet.BOTTOM, 0);
constraintSet.applyTo(constraintLayout);
// constraintLayout.addView(checkBox);
bottomSheetDialog.show();
}
private float convertDpToPixel(float dp, Context context) {
return dp * ((float) context.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT);
}
}
Here my layout_bottom_sheet_ayat.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/bottomSheetLinearLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bottom_sheet_ayat"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="15dp"
android:text="Pilih Ayat"
android:textColor="#color/black"
android:textSize="20sp"
android:textStyle="bold"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="5dp"
android:background="#color/white"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayoutCheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatCheckBox
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/checkbox1"
android:layout_marginStart="15dp"
android:layout_marginTop="24dp"
android:padding="10dp"
android:checked="false"
android:enabled="true"
android:text="Semua Ayat"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="16dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.appcompat.widget.LinearLayoutCompat>
Thanks very much for any sugesstion and help :)
Finally I have simple code to create Checkbox Programmatically in BottomSheetDialog. So far I satisfied with this code.
I got clues:
Checkbox programmatically using LinearLayout from this tutorial on Youtube
and Convert dp like in layout_bottom_sheet_ayat.xml programmatically using this Stackoverflow answer
So base on those clues, I made some changes on my code below :
private class MyOnClickListener implements View.OnClickListener {
#Override
public void onClick(View v) {
int itemPosition = recyclerViewSurat.getChildLayoutPosition(v);
String jumlahAyat = suratList.get(itemPosition).getJumlahAyat();
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(context, R.style.BottomSheetDialogTheme);
bottomSheetDialog.setContentView(R.layout.layout_bottom_sheet_ayat);
LinearLayoutCompat linearLayoutCheckBox = bottomSheetDialog.findViewById(R.id.linearLayoutCheckBox);
LinearLayoutCompat.LayoutParams params = new LinearLayoutCompat.LayoutParams(
LinearLayoutCompat.LayoutParams.MATCH_PARENT,
LinearLayoutCompat.LayoutParams.WRAP_CONTENT
);
int margin = (int) convertDpToPixel(30F, context);
params.setMargins(margin, 0,0,0);
AppCompatCheckBox compatCheckBox;
int banyakAyat = Integer.parseInt(jumlahAyat);
for (int i = 1; i <= banyakAyat; i++) {
compatCheckBox = new AppCompatCheckBox(context);
compatCheckBox.setId(i);
compatCheckBox.setText("Ayat " + i);
int margin1 = (int) convertDpToPixel(10F, context);
compatCheckBox.setPadding(margin1,0,0,0);
linearLayoutCheckBox.addView(compatCheckBox, params);
}
bottomSheetDialog.show();
}
private float convertDpToPixel(float dp, Context context) {
return dp * ((float) context.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT);
}
}
and my layout_bottom_sheet_ayat.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/bottomSheetLinearLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="550dp"
android:background="#drawable/bottom_sheet_ayat"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="15dp"
android:text="Pilih Ayat"
android:textColor="#color/black"
android:textSize="20sp"
android:textStyle="bold"/>
<View
android:id="#+id/viewId"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="5dp"
android:background="#color/white"/>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.LinearLayoutCompat
android:id="#+id/linearLayoutCheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatCheckBox
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/checkbox1"
android:layout_marginTop="24dp"
android:padding="10dp"
android:checked="false"
android:enabled="true"
android:text="Semua Ayat"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.core.widget.NestedScrollView>
</androidx.appcompat.widget.LinearLayoutCompat>
So thanks all :)
i would like to add to a LinearLayout some imageViews dynamically in my app.
My following code doesn't show errors and prints what it is supposed to on logcat each time an ImageView is added from java code.
My problem is that when i run the app the images don't appear on screen....any idea where i am doing wrong ?
thanks a lot.
public class MesListesFragment extends Fragment {
private RecyclerView mes_listes_recyclerView;
private ArrayList<ListItem> liste_items;
private LinearLayout list_card_categories;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.mes_listes_fragment, container, false);
View view2 = inflater.inflate(R.layout.list_item, container, false);
mes_listes_recyclerView = view.findViewById(R.id.mes_listes_recyclerView);
list_card_categories = view2.findViewById(R.id.list_card_recyclerView);
liste_items = new ArrayList<>();
for (int i = 0; i < 10; i ++){
liste_items.add(new ListItem("Liste des ploucs", "18 Août 198" + i));
}
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
mes_listes_recyclerView.setLayoutManager(linearLayoutManager);
mes_listes_recyclerView.setAdapter(new ListViewAdapter(getContext(), liste_items));
for (int i = 0; i < 8; i++){
ImageView image = new ImageView(getContext());
image.setImageResource(R.drawable.user);
list_card_categories.addView(image);
Log.d("view created", "...");
}
return view;
}
}
Here is the xml, the file's called list_item.xml :
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginHorizontal="10dp"
android:layout_marginTop="10dp"
app:cardCornerRadius="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/list_card_first_container"
android:layout_width="match_parent"
android:layout_height="100dp"
android:baselineAligned="false"
android:orientation="horizontal"
android:weightSum="100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="50"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/list_card_textView_nom_liste"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nom de la liste"></TextView>
<TextView
android:id="#+id/list_card_textView_date_liste"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date liste"></TextView>
</LinearLayout>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="50">
<LinearLayout
android:id="#+id/list_card_recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="50"
android:gravity="center"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
<LinearLayout
android:id="#+id/list_card_second_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/list_card_first_container"
android:gravity="center"
android:orientation="horizontal"
android:paddingVertical="5dp">
<Button
android:id="#+id/list_card_button_editer"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginEnd="20dp"
android:background="#color/bleu_fb"
android:text="#string/editer"></Button>
<Button
android:id="#+id/list_card_button_supprimer"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:background="#android:color/holo_red_light"
android:text="#string/supprimer"></Button>
</LinearLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
I'm trying to add a divider on top of my element in a RecyclerView.
Following google i've succeded to add a DividerItemDecoration, the code is as below:
DividerItemDecoration:
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private static final int[] ATTRS = new int[]{
android.R.attr.listDivider
};
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
private Drawable mDivider;
private int mOrientation;
public DividerItemDecoration(Context context, int orientation) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
setOrientation(orientation);
}
public void setOrientation(int orientation) {
if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
throw new IllegalArgumentException("invalid orientation");
}
mOrientation = orientation;
}
#Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
if (mOrientation == VERTICAL_LIST) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
public void drawVertical(Canvas c, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
public void drawHorizontal(Canvas c, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getHeight() - parent.getPaddingBottom();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int left = child.getRight() + params.rightMargin;
final int right = left + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
#Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (mOrientation == VERTICAL_LIST) {
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
}
The result:
What i'm trying to accomplish:
I've tried to play with the offset and the index but didn't get expected result.
How can I add the same item divider on top of my elements?
Also in case of necessary i've leave my layout of the row, here is the code:
Row:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tvDescrizione"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textStyle="italic"
android:textColor="#000"
android:textSize="18sp"/>
<TextView
android:id="#+id/tvBarcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvDescrizione"
android:textColor="#000"
android:textSize="18sp"/>
<TextView
android:id="#+id/tvData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvDescrizione"
android:layout_alignParentEnd="true"
android:textColor="#000"
android:textSize="18sp"/>
</RelativeLayout>
Explanation : In this answer i have added a divider above every item in your RecyclerView and after the RecyclerView itself. This way you wont have "overdraw", where 2 lines are painted on top of each other.I have used View tag for this purpose which draws a simple line from the xml.
Row file :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<View android:layout_height="2dp"
android:layout_width="match_parent"
android:layout_marginBottom="5dp"
android:background="#color/colorPrimary"/>
<TextView
android:id="#+id/tvDescrizione"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textStyle="italic"
android:textColor="#000"
android:textSize="18sp"/>
<TextView
android:id="#+id/tvBarcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvDescrizione"
android:textColor="#000"
android:textSize="18sp"/>
<TextView
android:id="#+id/tvData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvDescrizione"
android:layout_alignParentEnd="true"
android:textColor="#000"
android:textSize="18sp"/>
</RelativeLayout>
Main file :
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/repository_rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="#layout/tmp3"
android:layout_marginTop="30dp"/>
<View
android:id="#+id/bottomLine"
android:layout_height="2dp"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:background="#color/colorPrimary"/>
</LinearLayout>
Copy and paste DividerItemDecoration and change the following two lines. I only tested for vertical orientation.
//Change - getItemOffsets
//old
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight())
//new
outRect.set(0, mDivider.getIntrinsicHeight(), 0, 0)
//Change - drawVertical
//old
final int bottom = mBounds.bottom + Math.round(child.getTranslationY())
//new
final int bottom = mBounds.bottom + Math.round(child.getTranslationY()) - child.getHeight()
Don't add divider via Item Decoration. Instead put Two vies above and below of your row code like below.
<?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">
<View
android:layout_height="1px"
android:layout_width="match_parent"
android:background="#000000"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tvDescrizione"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textStyle="italic"
android:textColor="#000"
android:textSize="18sp"/>
<TextView
android:id="#+id/tvBarcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvDescrizione"
android:textColor="#000"
android:textSize="18sp"/>
<TextView
android:id="#+id/tvData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvDescrizione"
android:layout_alignParentEnd="true"
android:textColor="#000"
android:textSize="18sp"/>
</RelativeLayout>
<View android:layout_height="1px"
android:layout_width="match_parent"
android:background="#000000"
/>
</LinearLayout>
I am working with Android Studio and designing a layout as below. However, my ListView lv_comics only displays enough space for 1 item. I have tried to set layout_height to fill_parent, but it still does not work. However, when I set layout_height to a specific size, it is OK, but this is not good when I switch to another screen size. Anyone can help me?
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/layout_swiperefresh"
android:background="#color/grey">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Latest Update"
android:textColor="#color/white"
android:background="#color/indigo_main"
android:textSize="10dp"/>
<Gallery
android:id="#+id/gallery1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Most Popular"
android:textColor="#color/white"
android:background="#color/indigo_main"
android:textSize="10dp"/>
<Gallery
android:id="#+id/gallery2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="New Manga"
android:textColor="#color/white"
android:background="#color/indigo_main"
android:textSize="10dp"/>
<ListView
android:drawSelectorOnTop="true"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="#+id/lv_comics" />
</LinearLayout>
</ScrollView>
Here is my layout show.
http://i.stack.imgur.com/KFIJy.jpg
try to this to calculate height dynamically
public static void setListViewHeight(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0) {
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));
}
view.measure(desiredWidth, MeasureSpec.AT_MOST);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
I am creating tabs inside the fragment class. Right now i am showing two tabs in the fragment class. Everything works fine and tabs are shown properly. Only cache is that, the tabs shown only take half of the screen width, It doesn't take the full screen width.
So anyone tell me what i need to change in my code to achieve that
My Code
tabsinfo_fragment.xml file
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#EFEFEF" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</HorizontalScrollView>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:id="#+id/tab_1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="#+id/tab_2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
</LinearLayout>
</TabHost>
tab.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="20dp"
android:background="#drawable/tab_selector">
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#drawable/tab_text_selector"
android:textIsSelectable="false" />
</LinearLayout>
code inside fragment class
public class TabsInfoFragment extends Fragment implements OnTabChangeListener
{
private View m_Root;
private TabHost m_TabHost;
private int m_CurrentTab;
public String m_VisitorTabText;
public String m_FeedTabText;
public TabsInfoFragment()
{
}
#Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
m_VisitorTabText = Integer.toString(R.string.tab_visitor_text);
m_FeedTabText = Integer.toString(R.string.tab_feed_text);
m_Root = inflater.inflate(R.layout.tabsinfo_fragment, null);
m_TabHost = (TabHost) m_Root.findViewById(android.R.id.tabhost);
setupTabs();
return m_Root;
}
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
m_TabHost.setOnTabChangedListener(this);
m_TabHost.setCurrentTab(m_CurrentTab);
// Manually start loading stuff in the first tab
updateTab(m_VisitorTabText, R.id.tab_1, new ProfileInfoFragment());
}
private void setupTabs()
{
m_TabHost.setup();
m_TabHost.addTab(newTab(m_VisitorTabText, R.string.tab_visitor_text, R.id.tab_1));
m_TabHost.addTab(newTab(m_FeedTabText, R.string.tab_feed_text, R.id.tab_2));
}
private TabSpec newTab(String tag, int labelId, int tabContentId)
{
View indicator = LayoutInflater.from(getActivity()).inflate(R.layout.tab, (ViewGroup) m_Root.findViewById(android.R.id.tabs), false);
((TextView) indicator.findViewById(R.id.text)).setText(labelId);
TabSpec tabSpec = m_TabHost.newTabSpec(tag);
tabSpec.setIndicator(indicator);
tabSpec.setContent(tabContentId);
return tabSpec;
}
#Override
public void onTabChanged(String tabId)
{
if (m_VisitorTabText.equals(tabId))
{
updateTab(tabId, R.id.tab_1, new ProfileInfoFragment());
m_CurrentTab = 0;
return;
}
if (m_FeedTabText.equals(tabId))
{
updateTab(tabId, R.id.tab_2, new ProfileInfoFragment());
m_CurrentTab = 1;
return;
}
}
private void updateTab(String tabId, int placeholder, Fragment fragmentClass)
{
FragmentManager fm = getFragmentManager();
if (fm.findFragmentByTag(tabId) == null)
{
fm.beginTransaction().replace(placeholder, fragmentClass, tabId).commit();
}
}
}
ScreenShot
The TabWidget class is a subclass of LinearLayout, so in the tab.xml file for the root xml element, in your case a LinearLayout, set the width to 0dp and the weight to 1, then all the tabs will be equal width. Like so:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:gravity="center">
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="This is a tab" />
</LinearLayout>
Try changing your TabWidget to this instead;
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
try this
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="#+android:id/tab_1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<FrameLayout
android:id="#+android:id/tab_2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</TabHost>
Try removing HorizontalScrollView and adding android:layout_width="fill_parent" to TabWidget
Need to add one single line
tabs.setDistributeEvenly(true);