add custom layout into DraggableGridView - android

I use DraggableGridView to create a ViewGroup, and I want to add my custom view into it.
Below is my custom layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/Green"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="#+id/item_img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:padding="10dp"
android:scaleType="fitCenter"
android:src="#drawable/default_avatar" />
<TextView
android:id="#+id/item_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/Gray"
android:gravity="center"
android:text="Name" />
</LinearLayout>
As you can see, the background color of LinearLayout should be green, which is displayed correctly in the ViewGroup.
However, the ImageView and the TextView aren't displayed on the screen, neither even the background color of the TextView.
And this is my code to add the custom view:
LayoutInflater vi = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.item, null);
TextView textView = (TextView) v
.findViewById(R.id.item_name);
textView.setText(contact.getName());
textView.setTextColor(Color.WHITE);
gridView.addView(v);
What's the problem? Thank you.

Related

ListView scroll in marshmallow getting slow with images

I have a ListView with a ImageView covering the row. (I'm loading the image with Picasso. I had tried glide first but Picasso is slightly faster) It works smoothly enough on lollipop and kitkat, but on marshmallow the scroll is getting extremely slow and sometimes it is getting ANR messages.
Here's my xml for the list item:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="false"
android:clipToPadding="false">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="170dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/img_genre_bg"
android:scaleType="fitXY"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--android:background="#color/trans_black"-->
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/img_play"
android:layout_centerHorizontal="true"
android:src="#drawable/play_button"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_genre_name"
android:textSize="20sp"
android:textAllCaps="true"
android:layout_centerHorizontal="true"
android:textColor="#color/white"
android:layout_below="#+id/img_play"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_genre_id"
android:visibility="gone"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#color/colorAccent"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
</RelativeLayout>
Listview:
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lv_genre"
android:scrollbars="none"
android:divider="#null"
android:cacheColorHint="#android:color/transparent"
android:fastScrollEnabled="true"
android:persistentDrawingCache="scrolling"
android:scrollingCache="false">
</ListView>
getView():
public View getView(int position, View view, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View rowview=view;
if(rowview==null){
rowview=inflater.inflate(R.layout.item_genre_list,viewGroup,false);
}
id = (TextView) rowview.findViewById(R.id.tv_genre_id);
TextView name = (TextView) rowview.findViewById(R.id.tv_genre_name);
ImageView img_genre_bg = (ImageView) rowview.findViewById(R.id.img_genre_bg);
ImageView img_play = (ImageView) rowview.findViewById(R.id.img_play);
opensans = Typeface.createFromAsset(context.getAssets(), "font/OpenSans-Light.ttf");
name.setTypeface(opensans);
GENRE genre=new GENRE();
genre=genres.get(position);
id.setText(genre.getGenre_id());
name.setText(genre.getGenre_name());
Picasso
.with(context)
.load(genre.getGenre_image())
.placeholder(R.drawable.loading)
.into(img_genre_bg);
return rowview;
}
I had tried loading the image on separate AsyncTask thread but still it got stuck on scrolling. Changing it to RecyclerView also didn't helped.
It's because you are creating your Typeface everytime a row is created. The overhead of creating an Asset everytime a row is created probably slows the execution.
Move this piece of code to the Constructor of your class;
opensans = Typeface.createFromAsset(context.getAssets(), "font/OpenSans-Light.ttf");
Then you can use that already created type as:
name.setTypeface(opensans);
It is better to move to RecyclerView which is more suitable since it manages memory more efficiently than ListView.

TextView in customtitle view of alert dialog is not displayed

I make setting customtitle of alertdialog builder with a view that contains two webviews for animated gif and textview between them. the webviews are shown in the title of the alert dialog but the textview is missing.
here the view XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="75dp"
android:weightSum="1"
android:background="#android:color/background_dark">
<WebView
android:layout_width="wrap_content"
android:layout_height="75dp"
android:id="#+id/Title_webView1"
android:layout_weight="0.15"
android:layout_gravity="left" />
<TextView
android:id="#+id/titletextView"
android:layout_width="match_parent"
android:layout_height="75dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_gravity="center"
android:layout_weight="0.7"
android:textAlignment="center"
android:textIsSelectable="false"
android:visibility="visible"
android:gravity="center_vertical|center_horizontal"
android:inputType="none"
android:textColor="#d0d2d116"
android:text="test123"
android:singleLine="true"
android:textSize="32dp" />
<WebView
android:layout_width="wrap_content"
android:layout_height="75dp"
android:id="#+id/Title_webView2"
android:layout_weight="0.15"
android:layout_gravity="right" />
here the view function:
private View setCustomDialogTitle(String Title)
{
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.embark_title, null);
TextView TitleText = (TextView) view.findViewById(R.id.titletextView);
TitleText.setText(Title);
WebView flashingLight1 = (WebView) view.findViewById(R.id.Title_webView1);
flashingLight1.loadUrl("file:///android_asset/flashlight751.gif");
WebView flashingLight2 = (WebView) view.findViewById(R.id.Title_webView2);
flashingLight2.loadUrl("file:///android_asset/flashlight752.gif");
return view;
}
and the function call:
AlertDialog.Builder alrtBuilder= new AlertDialog.Builder(this);
alrtBuilder.setCustomTitle(setCustomDialogTitle("Test123"));
the issue is with setting layout_width for the components and again setting them with weight.
so a small change in xml can help you out.The confusion in rendering will be the cause.
set all the layout_width attribute to "0dp"
android:layout_width="0dp"
for the three components, then it will bring you the exact look as you want. for some reference read this article, it really helps you to understand.
NB: tested and worked for your code

Layout inflater - for only part of a layout in another layout

In my application, I have a layout list_item.xml with 3 textviews which are filled from an array and I want to inflate this 3 textviews alone in another layout. The layout in which I want to inflate is a separate xml file layout_result.xml which has a single button and android search widget.
layout file for list_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px"
android:background="#F0F8FF"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:id="#+id/ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/ProductName"
android:layout_alignBottom="#+id/ProductName"
android:layout_alignParentLeft="true"
android:text="New Text" />
<TextView
android:id="#+id/Status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="17dp"
android:layout_marginTop="42dp"
android:text="New Text" />
<TextView
android:id="#+id/Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/Status"
android:layout_alignBottom="#+id/Status"
android:layout_centerHorizontal="true"
android:text="New Text" />
</RelativeLayout>
Layout for list_resut.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px"
android:background="#F0F8FF"
android:orientation="horizontal"
android:layout_weight="1">
<SearchView
android:id="#+id/searchView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="118dp"
android:layout_marginTop="16dp" >
</SearchView>
<Button
android:id="#+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/searchView1"
android:layout_marginLeft="22dp"
android:text="Button" />
</RelativeLayout>
Problem I faced when I tried to include search widget and button within list_item.xml and inflating them was that the search widget and button were being inserted in each of the 3 textview element.
So I created this separate layout list_result.xml which holds only the search widget and the button.
Question, is there anyway where I can place this list_item.xml with the textview results within list_result.xml to over come this error?
My layout inflater for list_item looks like this,
public View getView(int i, View view, ViewGroup viewGroup) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.list_item, viewGroup, false);
TextView txtName = (TextView) itemView.findViewById(R.id.Name);
TextView txtID= (TextView) itemView.findViewById(R.id.ID);
TextView txtStatus = (TextView) itemView.findViewById(R.id.Status);
txtName.setText(list.get(i).getProductName());
txtID.setText(list.get(i).getProductID());
txtStatus.setText(list.get(i).getStatus());
return itemView;
}
}
You should be able to just create a single layout XML. Not sure how you did it previously but you could do something like this:
<RelativeLayout>
<RelativeLayout>
<TextView/>
<TextView/>
<TextView/>
</RelativeLayout/>
<SearchView/>
<Button/>
</RelativeView>
If you want to keep the XML separate and then combine them at runtime, you still need a "container" for the inflated text views. You could use a FrameLayout as the container. Something like this for example:
results.xml:
<RelativeLayout>
<FrameLayout/>
<SearchView/>
<Button/>
</RelativeLayout>
items.xml
<RelativeLayout>
<TextView/>
<TextView/>
<TextView/>
</RelativeView/>
And then you can insert the textviews into the other layout like this:
ViewGroup container = (ViewGroup) findViewById(R.id.FrameLayoutId);
container.addChild(yourInflaterItemsView);
Hope this helps

cant get layout inflater to work

Having trouble getting layout inflater to work - wondering if someone get shed some light on why the text view is not working..
this is main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:background="#A9BCF5"
android:orientation="vertical"
tools:context=".MainActivity"
android:weightSum="1">
<ImageView
android:layout_width="639dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:id="#+id/imageView"
android:background="#drawable/retreatgoddess"
android:scaleType="centerCrop"
android:clickable="true"
android:cropToPadding="false"
android:longClickable="false"
android:layout_weight="0.80" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/affirmation"
android:layout_weight="0.23"
android:textAlignment="center"
android:textColor="#1728ff"
android:textStyle="normal"
android:typeface="sans"/>
</LinearLayout>
this is the method
public void fillTextView(Context context, int layoutId, int resId, String text)
{
View view= LayoutInflater.from(context).inflate(layoutId, null);
TextView textElement = (TextView) view.findViewById(resId);
textElement.setText(text);
}
i am calling it via
fillTextView(context, R.layout.main, R.id.affirmation, "i love you");
i am getting the background but no text
As you're using layout weight, you should set android:layout_height to 0px on both imageView and affirmation. Then the layout will be calculated properly.
Also, the total weight in your case is 1.03, yet you set the sum to 1.0. Change the sum or one of the layout weights.

Increasing The Size Of View In ListView in Android

I have a list View . I want to show the following field in each row .
1. An Image
2. An TextView For Book Name
3. An TextView For Author Name
4. An TextView For File Type
All thing is successfully done by me . But the size of each row in the list view is not sufficient for showing all this feature as explained in the image .
The code for creating this :
if( row_view == null)
{
LayoutInflater inflater = context.getLayoutInflater();
row_view = inflater.inflate(R.layout.row_layout, null,true) ;
holder = new ViewHolder();
holder.textView_for_BookName = (TextView) row_view.findViewById(R.id.book_text_view_book_tab);
holder.textView_For_fileType = (TextView) row_view.findViewById(R.id.type_for_book_tab);
holder.textView_Forauthor = (TextView) row_view.findViewById(R.id.author_text_view_book_tab);
holder.imageview = (ImageView) row_view.findViewById(R.id.image_sd_list_view);
row_view.setTag(holder);
}
The Code of rowLayout.xml is as follows :
<?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:background="#FFFFFF"
android:orientation="horizontal">
<ImageView
android:src="#drawable/icon"
android:layout_width="wrap_content"
android:id="#+id/image_sd_list_view"
android:layout_height="wrap_content">
</ImageView>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dip"
android:paddingBottom="5dip"
android:textColor="#000000"
android:textSize="20dip"
android:paddingLeft="10dip"
android:text="Book Name"
android:id="#+id/book_text_view_book_tab">
</TextView>
<TextView
android:id="#+id/author_text_view_book_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Author Name"
android:textColor="#000000"
android:textSize="13dip"
android:layout_marginLeft="15dip">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="File Type :"
android:textColor="#000000"
android:layout_marginLeft="15dip"
android:textSize="10dip"
android:id="#+id/type_for_book_tab">
</TextView>
</LinearLayout>
</LinearLayout>
I want to show the above field (book_name,author_name,file_type) in each textView . For that , I have to increase the size of each row in the listView . What to do for increasing the size of row in list view ?? Please hlep ....
You simply need to set the root layout's height in row_layout.xml to wrap_content (or a larger static value).
android:height="wrap_content"
(If you need more specific help, you need to post row_layout.xml.)
Try changing both of your LinearLayout's height attributes:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
/>

Categories

Resources