In my android app I want to display a HorizontalScrollView with a dynamic number of images. Currently I have to build a LinearLayout for each Image. Is there a way to do it dynamically with a list of URLs and appropriate the quantity of LinearLayouts of the length of this list?
enclosed my current code
activity_mail.xml:
<?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"
tools:context="com.example.post.scollagainandagain.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="178dp"
android:text="Fotos hinzufügen" />
<HorizontalScrollView
android:id="#+id/hz"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_below="#+id/textView"
android:layout_marginTop="208dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/ll1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/iv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="0.5dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:background="#color/colorSeperator" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/ll2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/iv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="0.5dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:background="#color/colorSeperator" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/ll3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/iv3"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="0.5dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:background="#color/colorSeperator" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/ll4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/iv4"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="0.5dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:background="#color/colorSeperator" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/ll5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/iv5"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="0.5dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:background="#color/colorSeperator" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/ll6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/iv6"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="0.5dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:background="#color/colorSeperator" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity {
ImageView iv1, iv2, iv3, iv4, iv5, iv6;
LinearLayout ll1, ll2, ll3, ll4, ll5, ll6;
HorizontalScrollView hz;
int scrollViewHeigth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv1 = (ImageView) findViewById(R.id.iv1);
iv2 = (ImageView) findViewById(R.id.iv2);
iv3 = (ImageView) findViewById(R.id.iv3);
iv4 = (ImageView) findViewById(R.id.iv4);
iv5 = (ImageView) findViewById(R.id.iv5);
iv6 = (ImageView) findViewById(R.id.iv6);
ll1 = (LinearLayout) findViewById(R.id.ll1);
ll2 = (LinearLayout) findViewById(R.id.ll2);
ll3 = (LinearLayout) findViewById(R.id.ll3);
ll4 = (LinearLayout) findViewById(R.id.ll4);
ll5 = (LinearLayout) findViewById(R.id.ll5);
ll6 = (LinearLayout) findViewById(R.id.ll6);
hz = (HorizontalScrollView) findViewById(R.id.hz);
ViewTreeObserver vto = hz.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
scrollViewHeigth = hz.getHeight();
}
});
GetImage getimage1 = new GetImage("1/1.png", iv1, ll1);
getimage1.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
GetImage getimage2 = new GetImage("1/2.png", iv2, ll2);
getimage2.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
GetImage getimage3 = new GetImage("1/3.png", iv3, ll3);
getimage3.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
GetImage getimage4 = new GetImage("1/4.png", iv4, ll4);
getimage4.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
GetImage getimage5 = new GetImage("1/5.png", iv5, ll5);
getimage5.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
GetImage getimage6 = new GetImage("1/6.png", iv6, ll6);
getimage6.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
private class GetImage extends AsyncTask<Void, Void, Boolean> {
String url;
ImageView iv;
LinearLayout ll;
Bitmap bitmap;
public GetImage (String url, ImageView iv, LinearLayout ll) {
this.iv = iv;
this.url = "https://url.url.com/app/images/" + url;
this.ll = ll;
}
#Override
protected Boolean doInBackground(Void... params) {
try {
bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
double srcollHeight = scrollViewHeigth;
double bitmapHeight = bitmap.getHeight();
double bitmapWidth = bitmap.getWidth();
double width = srcollHeight/bitmapHeight*bitmapWidth;
iv.setImageBitmap(bitmap);
ll.setLayoutParams(new LayoutParams((int) width, LayoutParams.WRAP_CONTENT));
}
});
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean result) {
}
}
}
Can you help me?
The correct way to solve this error would be using a RecyclerView along with an Adapter.
Recyclerview was built specifically for this task where you have a dynamic number of items which you want to be displayed on your screen.
If you want to get started with RecyclerView, check this link out :
http://www.theappguruz.com/blog/learn-recyclerview-with-an-example-in-android
Related
I designed two buttons b1,b2 inside "KeyboardService.java" which should update the value of PACK_LIB inside "Stickers.java".
Three files attached:
main_board_layout.xml
Stickers.java
KeyboardService.java
I assigned both buttons with IDs of Button1 = b1 and Button2 = b2.
The built is successful but when I click on the buttons both do not work.
I think it is maybe a problem with this line ->
final Button button1 = (Button) mainBoard.findViewById(R.id.b1);
Because the Buttons are inside a FrameLayout. But when I use ID of that it won't work either.
Any ideas?
-------main_board_layout.xml
<?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:id="#+id/main_board"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/black"
android:orientation="vertical">
<android.inputmethodservice.KeyboardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/keyboard_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black" />
<!--top bar-->
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#ffffff"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:padding="2dp"
android:id="#+id/buttons"
>
<Button
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button1" />
<Button
android:id="#+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="#string/button2"
/>
<TextView
android:id="#+id/packNameLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:text="#string/app_name"
android:textColor="#000000"
android:textSize="1sp"
android:visibility="gone"
/>
<ImageView
android:id="#+id/btShareLinkGP"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:src="#mipmap/bt_share"
tools:srcCompat="#mipmap/bt_share"
/>
</FrameLayout>
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#000000"/>
<ScrollView
android:id="#+id/gif_view"
android:layout_width="match_parent"
android:layout_height="190dp"
android:background="#color/black"
android:paddingLeft="0dp"
android:paddingRight="15dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black"
android:gravity="left|center"
android:orientation="horizontal"
android:padding="0dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/pack_recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#color/black"
android:orientation="horizontal"
app:layoutManager="android.support.v7.widget.LinearLayoutManager" />
</LinearLayout>
</LinearLayout>
-------Stickers.java
public static String PACK_LIB ="";
public void setDefaultStickerPack() {
checkVersion(true);
InputStream in = null;
String packList[]=new String[0];
final String PACK_APP="pack_app";
final String PACK_ICON="pack_on.png";
String curAssets="";
-------KeyboardService.java
#Override
public View onCreateInputView() {
mainBoard = (LinearLayout) getLayoutInflater().inflate(R.layout.main_board_layout, null);
packNameLabel = (TextView) mainBoard.findViewById(R.id.packNameLabel);
scrollView = (ScrollView) mainBoard.findViewById(R.id.gif_view);
stickerView = (RecyclerView) getLayoutInflater().inflate(R.layout.recycler_view, null);
stickerView.addItemDecoration(new MarginDecoration(this));
stickerView.setHasFixedSize(true);
stickerView.setLayoutManager(new GridLayoutManager(this, 4));
scrollView.addView(stickerView);
ImageView btShareLinkGP = (ImageView) mainBoard.findViewById(R.id.btShareLinkGP);
btShareLinkGP.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
shareLinkToGP();
}
});
// packs bar
packView = (RecyclerView) mainBoard.findViewById(R.id.pack_recycler_view);
// BUTTONS ACTIONS
final Button button1 = (Button) mainBoard.findViewById(R.id.b1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Stickers.PACK_LIB = "allstickers";
}
});
final Button button2 = (Button) mainBoard.findViewById(R.id.b2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Stickers.PACK_LIB = "teststickers";
}
});
showStickers();
return mainBoard;
}
I am experiencing a very weird issue where I am unable to view any text/data, but it clearly shows the number of items in the list.
I have confirmed that dataList has data, and I am able to get the exact same string that I set after debugging.
Parent Fragment:
ListView InboxListView = (ListView) view.findViewById(R.id.listViewInbox);
InboxAdapter adapter = new InboxAdapter(mActivity, R.layout.activity_inbox_item, dataList, NotificationInbox.this);
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/SwipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:padding="15dp"
android:id="#+id/searchBarInbox"
android:hint="Search"
android:layout_height="wrap_content" />
<ListView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_marginTop="49dp"
android:id="#+id/listViewInbox" />
</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>
Adapter: http://pastebin.com/mSUrdLcW << CLICK
public class InboxAdapter extends ArrayAdapter<InboxBO> {
NotificationInbox fragment;
Context context;
List<InboxBO> inboxForSharedPref;
List<InboxBO> inboxList;
int layoutResID;
SharedPreferences prefs;
private ArrayList<InboxBO> inboxarraylist;
private SparseBooleanArray mSelectedItemIds;
public InboxAdapter(Context context, int layoutResourceID, List<InboxBO> objs, NotificationInbox fragment) {
super(context, layoutResourceID, objs);
mSelectedItemIds = new SparseBooleanArray();
this.context = context;
this.inboxList = objs;
this.layoutResID = layoutResourceID;
this.inboxarraylist = new ArrayList<InboxBO>();
this.inboxarraylist.addAll(inboxList);
this.fragment = fragment;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
inboxHolder inboxholder;
View view = convertView;
if (view == null) {
view = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(layoutResID, parent, false);
//LayoutInflater inflater = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
//LayoutInflater inflater = LayoutInflater.from(getContext());
//inflater.inflate(R.layout.activity_inbox_item, null);
inboxholder = new inboxHolder();
//view = inflater.inflate(layoutResID, parent, false);
prefs = context.getSharedPreferences(
Constants.PREF_NAME, 0);
inboxholder.swipeLayout = (SwipeLayout)view.findViewById(R.id.swipe_layout);
inboxholder.senderNameText = (TextView) view.findViewById(R.id.senderNameText);
inboxholder.pushDateText = (TextView) view.findViewById(R.id.pushDateText);
inboxholder.pushTimeText = (TextView) view.findViewById(R.id.pushTimeText);
inboxholder.messageText = (TextView) view.findViewById(R.id.messageText);
inboxholder.delete = (TextView)view.findViewById(R.id.delete);
inboxholder.itemLayout = (RelativeLayout) view.findViewById(R.id.relativeViewInbox);
inboxholder.swipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut);
inboxholder.delete.setOnClickListener(onDeleteListener(position, inboxholder));
view.setTag(inboxholder);
}
else {
inboxholder = (inboxHolder) view.getTag();
}
InboxBO mItem = inboxList.get(position);
if(mItem!=null){
inboxholder.senderNameText.setText((String)mItem.getTitle());
inboxholder.pushDateText.setText(new Date().toString());
inboxholder.pushTimeText.setText(new Date().toString());
inboxholder.messageText.setText((String)mItem.getMessage());
}
return view;
}
// For swipe action
private View.OnClickListener onDeleteListener(final int position, final inboxHolder holder) {
return new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder alert = new AlertDialog.Builder(
context);
alert.setTitle("Delete Message");
alert.setMessage("Are you sure you wish to delete this message?");
alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
inboxList.remove(position);
Gson gson = new Gson();
inboxForSharedPref = fragment.getStoredMessages();
inboxForSharedPref = inboxList;
String jsonSavePref = gson.toJson(inboxForSharedPref);
fragment.commitToStoredList(jsonSavePref);
Toast.makeText(context, "Message Deleted!",
Toast.LENGTH_SHORT).show();
holder.swipeLayout.close();
fragment.retrieveMessage();
dialog.dismiss();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alert.show();
}
};
}
// Algorithm to filter out listview based on text changed in listview searchbox
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
inboxList.clear();
if (charText.length() == 0) {
inboxList.addAll(inboxarraylist);
}
else
{
for (InboxBO ilist : inboxarraylist)
{
/*if (ilist.getDate().toLowerCase(Locale.getDefault()).contains(charText) || ilist.getMessage().toLowerCase(Locale.getDefault()).contains(charText) || ilist.getSendername().toLowerCase(Locale.getDefault()).contains(charText))
{
inboxList.add(ilist);
}*/
if(ilist.getTitle().toLowerCase(Locale.getDefault()).contains(charText) || ilist.getMessage().toLowerCase(Locale.getDefault()).contains(charText) ){
inboxList.add(ilist);
}
}
}
notifyDataSetChanged();
}
public static class inboxHolder {
TextView senderNameText, pushDateText, pushTimeText, messageText, delete, title;
RelativeLayout itemLayout;
private SwipeLayout swipeLayout;
}
// Methods below are for multi-deletion
public void toggleSelection(int position) {
selectView(position, !mSelectedItemIds.get(position));
}
// Remove selection after unchecked
public void remove(InboxBO object) {
inboxList.remove(object);
Gson gson = new Gson();
inboxForSharedPref = fragment.getStoredMessages();
inboxForSharedPref = inboxList;
String jsonSavePref = gson.toJson(inboxForSharedPref);
fragment.commitToStoredList(jsonSavePref);
notifyDataSetChanged();
}
// Item checked on selection
public void selectView(int position, boolean value) {
if (value)
mSelectedItemIds.put(position, value);
else
mSelectedItemIds.delete(position);
notifyDataSetChanged();
}
// Get number of selected item
public int getSelectedCount() {
return mSelectedItemIds.size();
}
public SparseBooleanArray getSelectedIds() {
return mSelectedItemIds;
}
public void removeSelection() {
mSelectedItemIds = new SparseBooleanArray();
notifyDataSetChanged();
}
}
Adapter XML:
<?xml version="1.0" encoding="utf-8"?>
<com.daimajia.swipe.SwipeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/bottom_wrapper"
android:layout_width="100dp"
android:layout_gravity="start"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff0000">
<TextView
android:id="#+id/delete"
android:text="Delete"
android:textSize="18sp"
android:textColor="#color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:background="?android:attr/activatedBackgroundIndicator"
android:orientation="vertical"
android:layout_width="match_parent"
android:id="#+id/relativeViewInbox"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal"
android:id="#+id/linearLayout">
<TextView
android:layout_width="match_parent"
android:text="SenderName"
android:textColor="#color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="left"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="#+id/senderNameText"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:text="PushDate"
android:gravity="right"
android:textColor="#color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="#+id/pushDateText" />
</LinearLayout>
<TextView
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:text="Message..."
android:id="#+id/messageText"
android:textColor="#color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:ellipsize="end"
android:maxLines="1"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/linearLayout" />
<TextView
android:layout_width="match_parent"
android:text="PushTime"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="right"
android:layout_weight="1"
android:textColor="#color/black"
android:layout_height="match_parent"
android:id="#+id/pushTimeText"
android:layout_alignTop="#+id/messageText" />
</RelativeLayout>
</com.daimajia.swipe.SwipeLayout>
EDIT: Screenshot added
EDIT2: Clarified parent fragment calling the adapter.
EDIT3: I did a temporary fix. Apparently the RelativeLayout and the Linear Layout in the AdapterXML were the cause of my misery. I'm not exactly sure why, though. I think it's the RelativeLayout and the LinearLayout somehow not displaying the text. Removing those two tags will display the texts in a linear fashion.
Try this
Adapter XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/bottom_wrapper"
android:layout_width="100dp"
android:layout_gravity="start"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff0000">
<TextView
android:id="#+id/delete"
android:text="Delete"
android:textSize="18sp"
android:textColor="#color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:background="?android:attr/activatedBackgroundIndicator"
android:orientation="vertical"
android:layout_width="match_parent"
android:id="#+id/relativeViewInbox"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal"
android:id="#+id/linearLayout">
<TextView
android:layout_width="match_parent"
android:text="SenderName"
android:textColor="#color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="left"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="#+id/senderNameText"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:text="PushDate"
android:gravity="right"
android:textColor="#color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="#+id/pushDateText" />
</LinearLayout>
<TextView
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:text="Message..."
android:id="#+id/messageText"
android:textColor="#color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:ellipsize="end"
android:maxLines="1"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/linearLayout" />
<TextView
android:layout_width="match_parent"
android:text="PushTime"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="right"
android:layout_weight="1"
android:textColor="#color/black"
android:layout_height="match_parent"
android:id="#+id/pushTimeText"
android:layout_alignTop="#+id/messageText" />
</RelativeLayout>
I want to achieve the design like this using BottomSheetDialogFragment. But the problem is the bottom layout get scrolled when i drag. I want the bottom layout always in bottom until the BottomSheetDialogFragment gets dismissed.
Please find the screenshot
Here is my code
bottomsheet_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--The main content goes over here-->
</LinearLayout>
<!--The footer view-->
<LinearLayout
android:id="#+id/footer_purchase_layout"
android:layout_width="match_parent"
android:layout_height="52dp"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="#+id/txt_cancel_purchase"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="left|center_vertical"
android:text="CLOSE"
android:textSize="14sp" />
<TextView
android:id="#+id/txt_item_purchase_action"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="right|center_vertical"
android:text="ADD STICKERS"
android:textSize="14sp" />
</LinearLayout>
</FrameLayout>
ModalBottomSheetFragment.java
public class ModalBottomSheetFragment extends BottomSheetDialogFragment {
public ModalBottomSheetFragment() {}
private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onStateChanged(#NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
dismiss();
}
}
#Override
public void onSlide(#NonNull View bottomSheet, float slideOffset) {}
};
#TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
#Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
View mContentView = View.inflate(getContext(), R.layout.bottomsheet_layout, null);
dialog.setContentView(mContentView);
CoordinatorLayout.LayoutParams layoutParams =
(CoordinatorLayout.LayoutParams) ((View) mContentView.getParent()).getLayoutParams();
CoordinatorLayout.Behavior mBehavior = layoutParams.getBehavior();
if (mBehavior != null && mBehavior instanceof BottomSheetBehavior) {
((BottomSheetBehavior) mBehavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
int height = getScreenHeight(getActivity());
final double desiredHeight = 0.85 * height;
mContentView.getLayoutParams().height = height;
((BottomSheetBehavior) mBehavior).setPeekHeight((int) desiredHeight);
}
}
public static int getScreenHeight(Context ctx) {
DisplayMetrics metrics = ctx.getResources().getDisplayMetrics();
return metrics.heightPixels;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private Button mShowBottomSheetDialog;
private BottomSheetDialogFragment bottomSheetDialogFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mShowBottomSheetDialog = (Button) findViewById(R.id.showBottomSheet);
mShowBottomSheetDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
bottomSheetDialogFragment = new ModalBottomSheetFragment();
bottomSheetDialogFragment.show(getSupportFragmentManager(), bottomSheetDialogFragment.getTag());
}
});
}
}
use this layout instead of your layout ....
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:orientation="vertical">
<!--The main content goes over here-->
</LinearLayout>
</ScrollView>
<!--The footer view-->
<LinearLayout
android:id="#+id/footer_purchase_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="#+id/txt_cancel_purchase"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="left|center_vertical"
android:text="CLOSE"
android:textSize="14sp" />
<TextView
android:id="#+id/txt_item_purchase_action"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="right|center_vertical"
android:text="ADD STICKERS"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
Note:- Weight divide your Layout into different View which is constant in your layout ......
use this layout:
<?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:background="#android:color/white"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Title"
android:textSize="18sp" />
</FrameLayout>
</androidx.appcompat.widget.Toolbar>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp">
<!--your content here-->
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<FrameLayout
android:id="#+id/bottomView"
android:layout_width="match_parent"
android:layout_height="70dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Footer"
android:textSize="18sp"/>
</FrameLayout>
</LinearLayout>
I have a BottomSheet with a RecyclerView, and I want to add a title there as in the picture below ("Add new record"), I was trying to add TextView before the RecyclerView in the bottom_sheet LinearLayout, but this text doesn't appear on the screen, also I was trying to add an item in the RecyclerView (but I have a feeling that it's not a right approach), so my question is how to add the title to the BottomSheet?
This is the method where I'm adding items to the RecyclerView, which is in the BottomSheet:
private void showBottomSheetDialog() {
if (behavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
mBottomSheetDialog = new BottomSheetDialog(this);
View view = getLayoutInflater().inflate(R.layout.sheet, null);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new ItemAdapter(createItems(), new ItemAdapter.ItemListener() {
#Override
public void onItemClick(Item item) {
if (mBottomSheetDialog != null) {
mBottomSheetDialog.dismiss();
}
}
}));
mBottomSheetDialog.setContentView(view);
mBottomSheetDialog.show();
mBottomSheetDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
mBottomSheetDialog = null;
}
});
}
#Override
protected void onDestroy() {
super.onDestroy();
mAdapterItem.setListener(null);
}
public List<Item> createItems() {
ArrayList<Item> items = new ArrayList<>();
items.add(new Item(R.drawable.camera, "from new shoots"));
items.add(new Item(R.drawable.folder_multiple_image, "from ready images"));
return items;
}
xml of the screen:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#118b0a"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<android.support.v7.widget.RecyclerView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/my_toolbar" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/float_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_margin="16dp"
android:src="#drawable/add_white" />
<android.support.design.widget.CoordinatorLayout
android:layout_width="0dp"
android:layout_height="0dp">
<LinearLayout
android:id="#+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:gravity="center"
android:orientation="vertical"
app:layout_behavior="#string/bottom_sheet_behavior">
<!-- Here I was trying to add text view-->
<!--<TextView-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="Add new record 2"-->
<!--/>-->
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:background="#fff" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
xml of item in the RecyclerView which is in the BottomSheet:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="16dp"
android:src="#mipmap/ic_launcher" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:gravity="center_vertical"
android:textColor="#787878"
android:textSize="22sp" />
</LinearLayout>
I'm new to Android, and am currently using a ViewFlipper. I'd like to know if it's possible to use only one ImageView? That way I can just create an Arraylist of my images. I think using multiple ImageViews is not a good practice since I have 50+ images.
public void initContent() {
imageArrayList = new ArrayList<Integer>();
imageArrayList.add(R.drawable.pig2);
imageArrayList.add(R.drawable.pig3);
imageArrayList.add(R.drawable.pig4);
imageArrayList.add(R.drawable.pig5);
imageArrayList.add(R.drawable.pig6);
imageArrayList.add(R.drawable.pig7);
imageArrayList.add(R.drawable.pig8);
imageArrayList.add(R.drawable.pig9);
imageArrayList.add(R.drawable.pig10);
imageArrayList.add(R.drawable.pig11);
imageArrayList.add(R.drawable.pig12);
imageArrayList.add(R.drawable.pig13);
imageArrayList.add(R.drawable.pig14);
imageArrayList.add(R.drawable.pig24);
imageArrayList.add(R.drawable.pig25);
imageArrayList.add(R.drawable.theend);
MainActivity.java
public class MainActivity extends Activity {
ViewFlipper viewFlipper;
Button Next;
private Integer images[] = {R.drawable.ic_launcher,
R.drawable.ic_no_image, R.drawable.calendar52};
ImageView imageView1;
private int currImage = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewFlipper = (ViewFlipper) findViewById(R.id.ViewFlipper01);
Next = (Button) findViewById(R.id.Next);
imageView1 = (ImageView) findViewById(R.id.imageView1);
Next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
currImage++;
if (currImage == 3) {
currImage = 0;
}
final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageResource(images[currImage]);
viewFlipper.showNext();
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/RelativeLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ViewFlipper
android:id="#+id/ViewFlipper01"
android:layout_width="fill_parent"
android:layout_height="400dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#4B0082" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="117dp"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
</ViewFlipper>
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout03"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#000000"
android:gravity="center" >
<Button
android:id="#+id/Next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="20dp"
android:text="Next" />
</RelativeLayout>
</LinearLayout>
Yes. Use the frame animation, delete your arraylist. Here is some sample code: Link