Using Picasso in GridView: ImageView is there but with no image - java

I'm trying to implement Picasso to help load images into a GridView. Currently, the code below runs without any problems, but the GridView item is just blank. I know the particular ImageView is actually there, because if I select it and hold down, the selected_state drawable is activated, as you can see in this screenshot.
Can you please help me find the problem? Thanks!
GalleryFragment.java:
//in onCreateView()
v = inflater.inflate(R.layout.fragment_gallery, parent, false);
GridView gridView = (GridView) v.findViewById(R.id.fragmentGalleryGridview);
gridView.setAdapter(new GalleryAdapter(getActivity()));
gridView.setOnScrollListener(new GalleryScrollListener(getActivity()));
return v;
GalleryAdapter.java extends BaseAdapter:
public GalleryAdapter(Context mContext)
{
this.mContext = mContext;
inflater = LayoutInflater.from(mContext);
//get ArrayList<String> mPaths from SQLite database
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
SquareImageView view = (SquareImageView) convertView;
if (view == null)
{
view = new SquareImageView(mContext);
view.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
Picasso.with(mContext)
.load(mPaths.get(position))
.fit()
.into(view);
return view;
}
SquareImageView.java:
public class SquareImageView extends ImageView
{
//three constructors
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());
}
}
fragment_gallery.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eeeeee"
android:layout_marginTop="?android:attr/actionBarSize">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragmentGalleryGridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:columnWidth="100dp"
android:numColumns="auto_fit"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:drawSelectorOnTop="true"
android:stretchMode="columnWidth"/>
</FrameLayout>

This is how I've successfully implemented the Picasso library with a GridLayout...GridView requires you to use a ListAdapter for the items and I had the same problem but solved it using a GridLayout instead:
public class ImageHandler {
private static Picasso instance;
public static Picasso getSharedInstance(Context context)
{
if(instance == null)
{
instance = new Picasso.Builder(context).executor(Executors.newSingleThreadExecutor()).memoryCache(Cache.NONE).indicatorsEnabled(true).build();
return instance;
}
else
{
return instance;
}
}
}
After I implement the class here is how I access in code and load the image into my ImageView which is part of a GridLayout.
// imString = "http://filepath.file.jpg" as example
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
GridLayout grid = (GridLayout) findViewById(R.id.viewAllGrid);
grid.canScrollVertically(1);
ImageButton image;
for(int i = 0; i < itemsList.size(); i++){
aContainer= (LinearLayout) inflater.inflate(R.layout.layout_container, null);
image = (ImageButton) aContainer.findViewById(R.id.imageViewOrButton);
ImageHandler.getSharedInstance(getApplicationContext()).load(imString).skipMemoryCache().fit().into(image);
grid.addView(aContainer);
}
Here is the layout_container
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:id="#+id/Container"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageButton
android:layout_width="100dp"
android:layout_height="130dp"
android:background="#null"
android:id="#+id/seriesThumbnail"/>
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold"
android:textColor="#ffffffff"
android:id="#+id/title"
android:autoText="false"
android:ellipsize="end"
android:maxLines="1" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/publisher"
android:text="Francis Chan"
android:maxLines="1"
android:ellipsize="end" />
</LinearLayout>
And here is the viewAllGrid layout schema
<GridLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="20dp"
android:id="#+id/viewAllGrid"
android:layout_gravity="center_horizontal"
android:columnCount="3"
android:layout_weight=".8"
android:orientation="horizontal">
</GridLayout>

I had the same problem and it was because I was missing the permissions
<!-- This is required for Picasso to work. -->
<uses-permission android:name="android.permission.INTERNET"/>
<!-- The following permissions are OPTIONAL. -->
<!-- Used to adjust the work load depending on the type of network the device is using. -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Related

Remove Excess Space in Gridview

Current Gridview
I want the 3rd image to take up the white space. The gridview looks good when the height of the images are equal but when its unequal it shows blank to match up with the next images height. i just want the below image to come up and fill up the space.
Adapter
public class Adapter extends BaseAdapter {
Context context;
int logos[];
LayoutInflater inflter;
public Adapter(Context applicationContext, int[] logos) {
this.context = applicationContext;
this.logos = logos;
inflter = (LayoutInflater.from(applicationContext));
}
#Override
public int getCount() {
return logos.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.custom_grid, null); // inflate the layout
ImageView icon = view.findViewById(R.id.icon); // get the reference of ImageView
icon.setImageResource(logos[i]); // set logo images
return view;
}
}
Custom View
<?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:padding="1dp"
android:orientation="vertical">
<ImageView
android:id="#+id/icon"
android:scaleType="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/logo" />
</LinearLayout>
XML
<LinearLayout
android:padding="5dp"
android:orientation="vertical"
android:background="#color/white"
android:layout_below="#id/appbar"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:stretchMode="columnWidth"
android:numColumns="2"
android:id="#+id/grid"
android:gravity="center"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Java
GridView mGridView = findViewById(R.id.grid);
Adapter customAdapter = new Adapter(getApplicationContext(), logos);
mGridView.setAdapter(customAdapter);
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),position,Toast.LENGTH_LONG).show();
}
});
Usually use recycler view but needed columns so switched to gridview. I found sources on making the images equal height. But i dont want that. I dont want it to keep aspect ratio but just fill up all the available space
Solution:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="1dp"
android:orientation="vertical">
<ImageView
android:id="#+id/icon"
android:scaleType="center"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:src="#drawable/logo" />
</LinearLayout>

How to use GridView or GridLayout?

I'm creating a seat selection screen but I'm confused, how can I achieve this view. I want to access all selected seats into one single Java file. Plz, help me
EDIT: I've tried this code but with this code, I'm not able to create a view like this into single gridview. Now I'm using 2 separate gridviews for that and two Java files with the separate adapter with them.
Seat_Select.java
public class Seat_Select extends AppCompatActivity {
GridView androidgridview;
int[] image = {
R.drawable.rect_select, R.drawable.rect_select,
R.drawable.rect_select, R.drawable.rect_select,
R.drawable.rect_select, R.drawable.rect_select,
R.drawable.rect_select, R.drawable.rect_select,
R.drawable.rect_select, R.drawable.rect_select,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seat__select);
androidgridview = findViewById(R.id.grid);
SeatAdapter seatAdapter=new SeatAdapter(Seat_Select.this,image);
androidgridview.setAdapter(seatAdapter);
androidgridview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), "Grid Item " + (position + 1) + " Selected", Toast.LENGTH_LONG).show();
}
});
}
SeatAdapter.java
public class SeatAdapter extends BaseAdapter {
Context context;
LayoutInflater layoutInflater;
int image[];
public SeatAdapter(Context context,int[] image) {
this.context = context;
this.image=image;
layoutInflater=(LayoutInflater.from(context));
}
#Override
public int getCount() {
return image.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView =layoutInflater.inflate(R.layout.grid2col,null);
ImageView imageView= convertView.findViewById(R.id.grid_image);
imageView.setImageResource(image[position]);
return convertView;
}
}
grid2col.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/grid_image"
android:layout_width="30dp"
android:layout_height="70dp">
</ImageView>
</LinearLayout>
activity_seat_select.xml
<RelativeLayout>
<android.support.v7.widget.CardView
android:layout_width="190dp"
android:layout_height="390dp"
android:layout_below="#+id/relativeLayout"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp">
<GridView
android:id="#+id/grid"
android:layout_width="70dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:columnWidth="30dp"
android:gravity="center"
android:numColumns="2"
android:stretchMode="columnWidth"
android:horizontalSpacing="1dp"
android:verticalSpacing="8dp"
/>
<GridView
android:id="#+id/grid2"
android:layout_width="35dp"
android:numColumns="1"
android:layout_height="match_parent"
android:layout_gravity="start"
android:gravity="center">
</GridView>
</android.support.v7.widget.CardView>
</RelativeLayout>
Please check SeatBookingRecylerView. This example has full source code. It may be helpful for you. This is using RecyclerView.
GridView
GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid. The grid items are automatically inserted to the layout using a ListAdapter. you can GridView in your layout file like below.
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="80dp"
android:numColumns="auto_fit"
android:verticalSpacing="16dp"
android:horizontalSpacing="16dp"
android:padding="16dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
GridLayout
Grid layout is mainly used to align its child views in cells which are the horizontal and vertical intercepts of an invisible line. Each view child is place in a cell and the cells are numbered with the horizontal and vertical indexes.
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/GridLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="3"
android:orientation="horizontal">
<Space />
<Button
android:id="#+id/button1"
android:layout_gravity="left|top"
android:text="#string/button_name" />
<Button
android:id="#+id/button3"
android:layout_gravity="left|top"
android:text="#string/button_name" />
<Button
android:id="#+id/button2"
android:layout_column="0"
android:layout_gravity="left|top"
android:layout_row="0"
android:text="#string/button_name" />
<Button
android:id="#+id/button4"
android:layout_column="0"
android:layout_gravity="left|top"
android:layout_row="2"
android:text="#string/button_name" />
<Button
android:id="#+id/button5"
android:layout_column="1"
android:layout_row="2"
android:layout_columnSpan="2"
android:layout_gravity="fill"
android:text="#string/button_name" />
</GridLayout>

Screen turns background colour when clicking on EditText inside ExpandableListView

I've been struggling to figure out this weird bug for a day and a half. Whenever I click on an EditText that is a child of an ExpandableListView, the screen gets obscured such that the user is unable to see what is being typed, as shown in the gif below: \n
https://giant.gfycat.com/GenuineUnluckyHyrax.mp4
I started digging into the Hierarchy View to see what could be doing this, here is a screenshot of it:
https://i.imgur.com/B4kkHv7.png
To me it looks like the Toolbar up top is extending downwards to cover the screen, which is odd because it doesnt do that on other activities that contain EditText widgets. I've checked that parent views are using "wrap_content" for height instead of "match_parent" as well, except for my DrawerLayout which must match_parent. Below is the layout code for the activity:
<!-- This DrawerLayout has two children at the root -->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- This LinearLayout represents the contents of the screen -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- The ActionBar displayed at the top -->
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- The main content view where fragments are loaded -->
<FrameLayout
android:id="#+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/rlcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true">
<!--SideBar views -->
<RelativeLayout
android:layout_width="350dp"
android:layout_height="match_parent"
android:background="#color/sidebarColor">
<ExpandableListView
android:id="#+id/qwedit_sidebar_elv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:indicatorLeft="?
android:attr/expandableListPreferredItemIndicatorLeft"
android:divider="#color/colorPrimary"
android:dividerHeight="0.5dp">
</ExpandableListView>
</RelativeLayout>
<!--MainView views -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ExpandableListView
android:id="#+id/qwedit_main_elv"
android:layout_width="930dp"
android:layout_height="wrap_content"
android:indicatorLeft="?
android:attr/expandableListPreferredItemIndicatorLeft"
android:divider="#color/colorPrimary"
android:dividerHeight="0.5dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="25dp">
</ExpandableListView>
</RelativeLayout>
</RelativeLayout>
</FrameLayout>
</LinearLayout>
<!-- The navigation drawer that comes from the left -->
<!-- Note that `android:layout_gravity` needs to be set to 'start' -->
<android.support.design.widget.NavigationView
android:id="#+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
android:layout_marginTop="25dp"
app:menu="#menu/drawer_view" />
</android.support.v4.widget.DrawerLayout>
Here is the EVL's group layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/qwedit_sidebar_elv_parent_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:padding="3dp"
android:layout_marginLeft="40dp"
android:gravity="center"
android:textSize="20sp"
android:text="Category"/>
<ImageView
android:id="#+id/qwedit_elv_parent_status"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="35dp"
android:src="#drawable/ic_warning"/>
<TextView
android:id="#+id/qwedit_elv_parent_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="75dp"
android:layout_gravity="left"
android:gravity="right"
android:textColor="#color/colorPrimary"
android:textSize="20sp"
android:text="Score"/>
</LinearLayout>
and the item layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/qwedit_elv_item_score_container"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/qwedit_main_badView"
android:layout_width="232.5dp"
android:layout_height="145.3dp"
android:background="#color/badScore">
<TextView
android:id="#+id/qwedit_main_badTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#color/white"
android:textSize="20sp"
android:text="Bad"/>
</LinearLayout>
<LinearLayout
android:id="#+id/qwedit_main_mediocreView"
android:layout_width="232.5dp"
android:layout_height="145.3dp"
android:background="#color/mediocreScore">
<TextView
android:id="#+id/qwedit_main_mediumTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#color/white"
android:textSize="20sp"
android:text="Mediocre"/>
</LinearLayout>
<LinearLayout
android:id="#+id/qwedit_main_goodView"
android:layout_width="232.5dp"
android:layout_height="145.3dp"
android:background="#color/goodScore">
<TextView
android:id="#+id/qwedit_main_goodTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#color/white"
android:textSize="20sp"
android:text="Good"/>
</LinearLayout>
<LinearLayout
android:id="#+id/qwedit_main_wcView"
android:layout_width="232.5dp"
android:layout_height="145.3dp"
android:background="#color/worldclassScore">
<TextView
android:id="#+id/qwedit_main_worldclassTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#color/white"
android:textSize="20sp"
android:text="World Class"/>
</LinearLayout>
</LinearLayout>
<EditText
android:layout_width="930dp"
android:layout_height="wrap_content"
android:layout_below="#+id/qwedit_elv_item_score_container"
android:focusable="true"
android:focusableInTouchMode="true"
android:hint="Comments"/>
</RelativeLayout>
I'm assuming that this is all due to the layouts, but here is the CustomExpandableListAdapter that I wrote for it just in case the answer lies in how the views are being inflated:
/**
* Created by Tadhg on 9/8/2017.
*/
public class QwEditMainCustomExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;
//Values inits
private int categoryScore;
private int subCategoryScore;
private boolean completedCategory;
private boolean completedSubCategory;
// ChildView views
private View badScoreView;
private View mediocreScoreView;
private View goodScoreView;
private View worldclassScoreView;
TextView badScoreTextView;
TextView mediocreScoreTextView;
TextView goodScoreTextView;
TextView worldClassScoreTextView;
EditText questionCommentEditText;
public QwEditMainCustomExpandableListAdapter(Context context, List<String> expandableListTitle,
HashMap<String, List<String>> expandableListDetail) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
}
#Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail
.get(this.expandableListTitle.get(listPosition))
.get(expandedListPosition);
}
#Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
#Override
public View getChildView(int listPosition, final int expandedListPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
final String expandedListText = (String) getChild(listPosition, expandedListPosition);
if(convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.qwedit_main_elv_item, null);
}
// ChildView views
badScoreView = convertView.findViewById(R.id.qwedit_main_badView);
mediocreScoreView = convertView.findViewById(R.id.qwedit_main_mediocreView);
goodScoreView = convertView.findViewById(R.id.qwedit_main_goodView);
worldclassScoreView = convertView.findViewById(R.id.qwedit_main_wcView);
badScoreTextView = (TextView) convertView.findViewById(R.id.qwedit_main_badTextView);
mediocreScoreTextView = (TextView) convertView.findViewById(R.id.qwedit_main_mediumTextView);
goodScoreTextView = (TextView) convertView.findViewById(R.id.qwedit_main_goodTextView);
worldClassScoreTextView = (TextView) convertView.findViewById(R.id.qwedit_main_worldclassTextView);
// TODO: TextView score subcategory setters
badScoreView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO: add logic to change score
subCategoryScore = 1;
badScoreView.setBackgroundColor(Color.parseColor("#eca9a7"));
mediocreScoreView.setBackgroundColor(Color.parseColor("#f0ad4e"));
goodScoreView.setBackgroundColor(Color.parseColor("#5cb85c"));
worldclassScoreView.setBackgroundColor(Color.parseColor("#0275d8"));
}
});
mediocreScoreView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO: add logic to change score
subCategoryScore = 2;
badScoreView.setBackgroundColor(Color.parseColor("#d9534f"));
mediocreScoreView.setBackgroundColor(Color.parseColor("#f7d6a6"));
goodScoreView.setBackgroundColor(Color.parseColor("#5cb85c"));
worldclassScoreView.setBackgroundColor(Color.parseColor("#0275d8"));
}
});
goodScoreView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO: add logic to change score
subCategoryScore = 3;
badScoreView.setBackgroundColor(Color.parseColor("#d9534f"));
mediocreScoreView.setBackgroundColor(Color.parseColor("#f0ad4e"));
goodScoreView.setBackgroundColor(Color.parseColor("#addbad"));
worldclassScoreView.setBackgroundColor(Color.parseColor("#0275d8"));
}
});
worldclassScoreView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO: add logic to change score
subCategoryScore = 4;
badScoreView.setBackgroundColor(Color.parseColor("#d9534f"));
mediocreScoreView.setBackgroundColor(Color.parseColor("#f0ad4e"));
goodScoreView.setBackgroundColor(Color.parseColor("#5cb85c"));
worldclassScoreView.setBackgroundColor(Color.parseColor("#80baeb"));
}
});
/**
* Use variables categoryScore and subCategoryScore to update scores
*/
return convertView;
}
#Override
public int getChildrenCount(int listPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition)).size();
}
#Override
public Object getGroup(int listPosition) {
return this.expandableListTitle.get(listPosition);
}
#Override
public int getGroupCount() {
return this.expandableListTitle.size();
}
#Override
public long getGroupId(int listPosition) {
return listPosition;
}
#Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String listTitle = (String) getGroup(listPosition);
if(convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.qwedit_main_elv_group, null);
}
TextView listTitleTextView = (TextView) convertView
.findViewById(R.id.qwedit_main_elv_parent_title);
listTitleTextView.setText(listTitle);
return convertView;
}
#Override
public boolean hasStableIds() { return false; }
#Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) { return true; }
}
Any points or tips would be great. I think that this may be due to the number of views in my hierarchy (or at least a subissue of it), but I'm not sure and am relatively new to Android Development.
Thanks!
I solved the issue, simply adding android:windowSoftInputMode="adjustNothing|stateHidden" caused the ActionBar to not expand to 996px when the EditText gained focus

Android : Creating a vertical space for ImageView and tab

I am working on an Android project in which I am creating UI for editing a Note object in the app. For that, I will be replicating the UI which is designed with JS,HTML,CSS. Unfortunately, android has discrete elements like ListView, TextView and all. As you can see in the screenshot, the top horizontal bar is just a representation and inside it contains a photo.
Also, there is a tab like option in the activity itself. I don't know how to create both these things. The choice of colour I am planning to create with a List and get the element clicked in the List. I have basic functionality working, and working on UI part now.
I don't know what all is required for creating such fancy UI's. Any help would be nice.
edit_note.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:weightSum="1">
<EditText
android:id="#+id/noteTagEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical|center_horizontal" />
<EditText
android:id="#+id/noteTextEdit"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_weight="0.97"
android:ems="10"
android:gravity="top"
android:inputType="textMultiLine"
android:scrollbarAlwaysDrawVerticalTrack="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/noteDate"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/noteNumber"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/attachCount"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginStart="50dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<GridView
android:id="#+id/attachmentDisplay"
android:layout_width="match_parent"
android:layout_height="132dp"
android:layout_gravity="center_horizontal"
android:numColumns="2"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
>
<Button
android:id="#+id/saveNoteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:text="#string/saveNote" />
<Button
android:id="#+id/cancelEditButton"
android:layout_width="98dp"
android:layout_height="wrap_content"
android:text="#string/cancel"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
/>
</RelativeLayout>
</LinearLayout>
Java code :
public class EditNoteActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_note);
}
public class getNoteByItsId extends AsyncTask<Integer, Void, ResponseEntity<RestNote>> {
#Override
protected void onPostExecute(ResponseEntity<RestNote> params) {
restNote = params.getBody();
restAttachmentList = restNote.getNotesAttachments();
final ArrayList<HashMap<String, String>> restSectionArrayList = new ArrayList<>();
for (RestAttachment restAttachment : restAttachmentList) {
HashMap<String, String> restAttachmentDisplay = new HashMap<>();
restAttachmentDisplay.put(fileName, restAttachment.getFileName());
restAttachmentDisplay.put(fileSize, readableFileSize(restAttachment.getFileSize()));
restSectionArrayList.add(restAttachmentDisplay);
}
attachmentsGridView = (GridView) findViewById(R.id.attachmentDisplay);
noteDate = (TextView) findViewById(R.id.noteDate);
noteDate.setText(restNote.getNoteDate());
noteNumber = (TextView) findViewById(R.id.noteNumber);
String noteNos = "#" + String.valueOf(restNote.getNoteNumber());
noteNumber.setText(noteNos);
attachCount = (TextView) findViewById(R.id.attachCount);
attachCount.setText(String.valueOf(restNote.getAttachCount()));
editNoteAttachments = new EditNoteAttachments(editNoteActivity, restSectionArrayList);
attachmentsGridView.setAdapter(editNoteAttachments);
}
public class EditNoteAttachments extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private LayoutInflater inflater = null;
public EditNoteAttachments(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.attachment_rows, parent, false);
TextView fileName = (TextView) vi.findViewById(R.id.fileName);
TextView fileSize = (TextView) vi.findViewById(R.id.fileSize);
HashMap<String, String> conversationList;
conversationList = data.get(position);
fileName.setText(conversationList.get(EditNoteActivity.fileName));
fileSize.setText(conversationList.get(EditNoteActivity.fileSize));
return vi;
}
}
Kindly let me know. Thank you.
For the top horizontal bar you definitely need a compound custom view like this:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<ImageView
android:id="#+id/background
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_centerVertical="true"
/>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/profileImageView"
android:layout_marginLeft="16dp"
/>
<ImageView
android:id="#+id/profileImageView"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:layout_centerVertical="true"
/>
</merge>
With code like this:
public class TopBarView extends RelativeLayout {
private TextView title;
private ImageView profileImage;
public TopBarView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.TopBarView, 0, 0);
String titleText = a.getString(R.styleable.TopBarView_titleText);
a.recycle();
setGravity(Gravity.CENTER_VERTICAL);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.view_top_bar, this, true);
title = (TextView) findViewById(R.id.title);
title.setText(titleText);
profileImage = (ImageView) findViewById(R.id.profileImageView);
profileImage.setBackgroundColor(R.color.yellow);
profileImage.setImageResource(R.drawable.myProfileDrawable);
setBackgroundColor(Color.TRANSPARENT);
}
For tab options you could use android.support.design.widget.TabLayout from design support library.

Can't get Admob to show on multiple layouts?

I am trying to get Admob to display on my layout_one.xml and layout_two and can't get it to show. This app has a lot of swipe left and right and I want to display one ad over both layouts
Here are all my xml layouts:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/pager">
</android.support.v4.view.ViewPager>
layout_one.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/relative_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Copy"
android:id="#+id/Copy"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_alignParentEnd="false"
android:layout_alignParentStart="false"
android:textSize="30sp"
android:onClick="onClickCopy"/>
<EditText
android:layout_width="300dp"
android:layout_height="50dp"
android:id="#+id/stringCopy"
android:layout_above="#+id/Copy"
android:layout_centerHorizontal="true"
android:layout_marginBottom="70dp"
android:textSize="30sp"
android:inputType="text"
android:gravity="left|top"
android:background="#ffffff" />
<com.google.android.gms.ads.AdView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id"/>
layout_two.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/relative_one"
android:background="#000000"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Paste"
android:id="#+id/Paste"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_alignParentEnd="false"
android:layout_alignParentStart="false"
android:textSize="30sp"
android:onClick="onClickPaste"/>
<EditText
android:layout_width="300dp"
android:layout_height="50dp"
android:id="#+id/stringCopy"
android:layout_marginBottom="71dp"
android:textSize="30sp"
android:inputType="text"
android:gravity="left|top"
android:background="#ffffff"
android:layout_above="#+id/Paste"
android:layout_centerHorizontal="true" />
<!-- SHOULD I PUT ANOTHER ADVIEW IN THIS XML -->
</RelativeLayout>
Here is all my java classes:
MainActivity.java
public class MainActivity extends FragmentActivity {
//View Pager
ViewPager viewpager;
//AdView
AdView adView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpager = (ViewPager)findViewById(R.id.pager);
PagerAdapter pAdapter = new PagerAdapter(getSupportFragmentManager());
viewpager.setAdapter(pAdapter);
}
public void onClickCopy(View v)
{
//Copy Text From layout_one to Clipboard
}
public void onClickPaste(View v)
{
//Paste Text From Clipboard
}
}
PagerAdapter.java
public class PagerAdapter extends FragmentPagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int arg0) {
switch (arg0) {
case 0:
return new FragmentOne();
case 1:
return new FragmentTwo();
default:
break;
}
return null;
}
#Override
public int getCount() {
return 2;
}
}
FragmentOne.java
public class FragmentOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.layout_one, container, false);
}
}
FragmentTwo.java
public class FragmentTwo extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.layout_two, container, false);
}
}
Where would I put this code so that when they swipe between layout_one and layout_two that the same ad will be displayed and not make a new ad request every time they change views?
//Display test Ads
adView = new AdView(this);
AdRequest request = new AdRequest.Builder()
.addTestDevice("DEVICE_ID").build();
adView.loadAd(request);
The standard pattern for doing this is to put your AdView either above or below your ViewPager and keep it static. That way you are not constantly requesting ads that you never get time to display.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/pager">
</android.support.v4.view.ViewPager>
<com.google.android.gms.ads.AdView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:adUnitId="yourAdUnitId"
android:id="#+id/adView">
</LinearLayout>
This way your Adview is outside your ViewPager and remains constant across all pages.
I use the following code to cache Admob in scroll Grid View:
public View getView(int position, View convertView, ViewGroup parent) {
if (position != adNum) {
...
} else {
if (mAdView == null) {
frameLayout = new FrameLayout(mContext);
mAdView = new AdView(mContext);
...
frameLayout.addView(mAdView);
} else {
frameLayout.removeView(mAdView);
frameLayout = new FrameLayout(mContext);
frameLayout.addView(mAdView);
}
return frameLayout;
}
}

Categories