gridview layout center(android) - android

Sorry for my english. I want locate gridview in center layout. But i have this:
.
I use this properties android:layout_gravity="center_horizontal" and android:gravity="center" but its not help
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<service.ExpandableHeightGridView
android:layout_gravity="center_horizontal"
android:id="#+id/gridView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:columnWidth="150dp"
android:drawSelectorOnTop="true"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="1dp"
android:focusable="true"
android:clickable="true"/>
</RelativeLayout>
</LinearLayout>
ExpandableHeightGridView
public class ExpandableHeightGridView extends GridView
{
boolean expanded = false;
public ExpandableHeightGridView(Context context)
{
super(context);
}
public ExpandableHeightGridView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public ExpandableHeightGridView(Context context, AttributeSet attrs,
int defStyle)
{
super(context, attrs, defStyle);
}
public boolean isExpanded()
{
return expanded;
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// HACK! TAKE THAT ANDROID!
if (isExpanded())
{
// Calculate entire height by providing a very large height hint.
// View.MEASURED_SIZE_MASK represents the largest height possible.
int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
else
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded)
{
this.expanded = expanded;
}
}
Item gridview
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="1dp"
android:paddingLeft="1dp"
>
<ImageView
android:id="#+id/imageRoom"
android:layout_width="200dp"
android:layout_height="200dp"
android:scaleType="centerCrop" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text="Large Text"
android:textColor="#fff"
android:id="#+id/textView30"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>

Related

Linear Layout can not type cast to Keyword Layout

I am having one LinnearLayout and one Keyword layout. I want to type cast Linearlayout to Keyword layout but It gives exception.
java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to com.codiant.prortc.rtc.KeywordView
I want to use the object reference of Linearlayout. I am posting the code of activity where I implement code .
try {
mKeywordView = (KeywordView) getContnetView();
mKeywordView.setListner(new KeywordView.KeyboardListner() {
#Override
public void onVisbile(boolean isVisibile) {
Toast.makeText(getApplicationContext(), "" + isVisibile, Toast.LENGTH_LONG).show();
}
});
}catch (ClassCastException e)
{
Log.e(TAG, "onCreate: "+e);
}
keywordChatView.java
This is KeywordChatView class
public class KeywordView extends LinearLayout {
private KeyboardListner mListner;
public void setListner(KeyboardListner listner) {
mListner = listner;
}
public KeywordView(Context context) {
super(context);
}
public KeywordView(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
}
public KeywordView(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public KeywordView(Context context, #Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
#Override
protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Log.d("Search Layout", "Handling Keyboard Window shown");
final int proposedheight = MeasureSpec.getSize(heightMeasureSpec);
final int actualHeight = getHeight();
if (actualHeight > proposedheight){
// Keyboard is shown
mListner.onVisbile(true);
} else {
// Keyboard is hidden
mListner.onVisbile(false);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public interface KeyboardListner
{
public void onVisbile(boolean isVisibile);
}
}
this is Linearlayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/main_layout1"
android:weightSum="1"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.codiant.prortc.PrivateChatActivity"
tools:showIn="#layout/private_activity_chat">
<FrameLayout
android:id="#+id/close_fl"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".3"></FrameLayout>
<FrameLayout
android:id="#+id/swipe_fl"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".7"
android:background="#800b0b0b">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="#dimen/dp_30"
android:background="#color/colorAccent">
<TextView
android:id="#+id/title_tv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
android:layout_marginLeft="#dimen/dp_5"
android:gravity="center"
android:text="Private Chat" />
<ImageButton
android:id="#+id/close_iv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="top|right"
android:background="#android:color/transparent"
android:src="#drawable/ic_close" />
</FrameLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/chat_rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/dp_5"
android:layout_marginRight="#dimen/dp_5"
android:layout_marginTop="#dimen/dp_40" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="#dimen/dp_5"
android:layout_marginLeft="#dimen/dp_5"
android:layout_marginRight="#dimen/dp_5"
android:orientation="horizontal"
android:weightSum="1">
<EditText
android:id="#+id/chat_et"
android:layout_width="0dp"
android:layout_height="#dimen/dp_40"
android:layout_marginRight="#dimen/dp_5"
android:layout_weight=".9"
android:background="#drawable/circluar_border" />
<RelativeLayout
android:layout_width="0dp"
android:layout_height="#dimen/dp_40"
android:layout_alignParentRight="true"
android:layout_weight=".1">
<android.support.design.widget.FloatingActionButton
android:id="#+id/send_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:src="#drawable/ic_send"
app:backgroundTint="#color/blue_500"
app:borderWidth="0dp"
app:elevation="6dp" />
</RelativeLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
Inheritance works in the opposite direction than you have written. You can set KeywordView to the variable of LinearLayout, but not vice-versa.
You have object of type of LinearLayout ( as specified in XML ) and it can not be cast to its Subclass. Specify proper layout in your XML
So you need to convert your XML linear layout to your KeywordView then you can do what you task.
<?xml version="1.0" encoding="utf-8"?>
<packagename.KeywordView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/main_layout1"
android:weightSum="1"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.codiant.prortc.PrivateChatActivity"
tools:showIn="#layout/private_activity_chat">
</packagename.KeywordView >

add Scrollview inside Layout+Viewpager + listview it is possible?

i am trying to add Layout+View pager +list view,Layout+View pager working fine but View pager inside recycler view not scrolling.
it is possible...guide me
-Advance thanks
requirement Screenshot
How Can i set viewpager an recyclear view height dynamically
my xml code ...
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView3"
android:background="#drawable/profile"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="27dp" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:tabMode="fixed"
app:tabGravity="fill"
app:tabTextColor="#color/your_unselected_text_color"
app:tabSelectedTextColor="#color/your_selected_text_color"
android:layout_below="#+id/imageView3"
android:layout_alignParentStart="true" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="#+id/tabs"
android:layout_alignParentStart="true">
</android.support.v4.view.ViewPager>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/android.support.v7.widget.RecyclerView"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
First of all copy this class file in your project
import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ListView;
public class ExpandableHeightListView extends ListView {
boolean expanded = false;
public ExpandableHeightListView(Context context) {
super(context);
}
public ExpandableHeightListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ExpandableHeightListView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public boolean isExpanded() {
return expanded;
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (isExpanded()) {
// Calculate entire height by providing a very large height hint.
// View.MEASURED_SIZE_MASK represents the largest height possible.
int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded) {
this.expanded = expanded;
}
}
Then use the ExpandableHeightlistview in scrollview like this
<ScrollView ...>
<RelativeLayout ...>
<viewpager></viewpager>
<com.example.ExpandableHeightListView ... />
<other view items />
</RelativeLayout>
Then setadpater of listview
and lastly and very important listview.setExpandable(true)

Add more ListView inside one page layout Android

I dont know to add more listview inside one page layout (attach picture)
I want Each ListView is not scrolling. Page layout is scrolling only.
Help me!!
Build same home layout app: https://play.google.com/store/apps/details?id=com.google.android.youtube
Answer
I had resolve this problem
Step1: Make new file java:
public class ExpandableHeightListview extends ListView {
boolean expanded = false;
public ExpandableHeightListview(Context context)
{
super(context);
}
public ExpandableHeightListview(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public ExpandableHeightListview(Context context, AttributeSet attrs,int defStyle)
{
super(context, attrs, defStyle);
}
public boolean isExpanded()
{
return expanded;
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// HACK! TAKE THAT ANDROID!
if (isExpanded())
{
// Calculate entire height by providing a very large height hint.
// But do not use the highest 2 bits of this integer; those are
// reserved for the MeasureSpec mode.
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
else
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded)
{
this.expanded = expanded;
}
}
Step2: Java: Replace all ListView ->ExpandableHeightListview, and set listview.setExpanded(true);
Step3: layout xml: Replace
<yourpackage.ExpandableHeightListview
android:id="#+id/etResponse"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="300dp"
android:scrollbars="none"
android:orientation="vertical"
android:fadingEdge="none">
</yourpackage.ExpandableHeightListview>
Step4: Done!
You can calculate height of listview belong to number item in this. Here is example :
public class ExpandedListView extends ListView {
private android.view.ViewGroup.LayoutParams params;
private int old_count = 0;
public ExpandedListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onDraw(Canvas canvas) {
if (getCount() != old_count) {
old_count = getCount();
params = getLayoutParams();
if (getChildAt(0)!= null){
params.height = getCount() * (old_count > 0 ? (getChildAt(0).getHeight()) : 0) ;
}else
params.height = 0;
setLayoutParams(params);
requestLayout();
}
super.onDraw(canvas);
}
}
And use it in xml like this :
.......
<your package.ExpandedListView
android:id="#+id/lv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none" />
<your package.ExpandedListView
android:id="#+id/lv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none" />
....
Note This solution will fix the height of listView.
You need to set the height of each ListView. Lets say 30dp also give layout_weight="1" Here is example LinearLayout nested inside ScrollView
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/lastest"
android:layout_marginTop="10sp"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="30dp" />
<ListView
android:id="#+id/featured"
android:layout_marginTop="10sp"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="30dp" />
<ListView
android:id="#+id/startup"
android:layout_marginTop="10sp"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="30dp" />
</LinearLayout>
</ScrollView>
Hope it helps
Please use custom Listview:
public class ExpandableHeightListview extends ListView
{
boolean expanded = false;
public ExpandableHeightListview(Context context)
{
super(context);
}
public ExpandableHeightListview(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public ExpandableHeightListview(Context context, AttributeSet attrs,int defStyle)
{
super(context, attrs, defStyle);
}
public boolean isExpanded()
{
return expanded;
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// HACK! TAKE THAT ANDROID!
if (isExpanded())
{
// Calculate entire height by providing a very large height hint.
// But do not use the highest 2 bits of this integer; those are
// reserved for the MeasureSpec mode.
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
else
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded)
{
this.expanded = expanded;
}
}
in .xml file:
<yourpackagename.ExpandableHeightListview
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/White"
android:scrollbars="none"
android:orientation="vertical"
android:fadingEdge="none">
</cyourpackagename.ExpandableHeightListview>

Android Compound View: cannot make child view match parent

I have created a custom view (compound view) that is inherited from FrameLayout and contains several child views in it:
MediaComponentView.java:
final public class MediaComponentView extends FrameLayout {
private int ratio = 1;
private ImageView imageView;
private CircularProgressView progressView;
private View downloadButton;
private View cancelButton;
private View playButton;
public MediaComponentView(Context context) {
super(context);
initializeViews();
}
public MediaComponentView(Context context, AttributeSet attrs) {
super(context, attrs);
initializeViews();
}
public MediaComponentView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initializeViews();
}
private void initializeViews() {
inflate(getContext(), R.layout.view_media_component, this);
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
imageView = (ImageView) this.findViewById(R.id.image_view);
progressView = (CircularProgressView) this.findViewById(R.id.progress_view);
downloadButton = this.findViewById(R.id.download_button);
cancelButton = this.findViewById(R.id.cancel_button);
playButton = this.findViewById(R.id.play_button);
}
public void setRatio(int ratio) {
this.ratio = ratio;
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth() / ratio);
}
}
view_media_component.xml:
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<com.makeramen.roundedimageview.RoundedImageView
android:id="#+id/image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
app:riv_border_color="#eee"
app:riv_border_width="1px"
app:riv_corner_radius="3dp"
/>
<com.github.rahatarmanahmed.cpv.CircularProgressView
android:id="#+id/progress_view"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:visibility="gone"
/>
<ImageView
android:id="#+id/download_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/icon_chat_media_download"
android:visibility="gone"
/>
<ImageView
android:id="#+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#android:drawable/ic_menu_close_clear_cancel"
android:visibility="gone"
/>
<ImageView
android:id="#+id/play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/icon_chat_media_play"
android:visibility="gone"
/>
</merge>
Usage of the compound view:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginTop="4dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
>
<some.hidden.package.MediaComponentView
android:id="#+id/media_0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="#00ff00"
/>
<some.hidden.package.MediaComponentView
android:id="#+id/media_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="#ff0000"
/>
</LinearLayout>
The issue is that the "image_view" is not layouted correctly. It does not match parent.
I have checked that the MediaComponentView itself has correct size by setting background to it. It HAS correct size.
But the child view has incorrect size. I guess it happens because of the overriden onMeasure() method in the MediaComponentView.
Any help or explanation would be appreciated.
You need to call measureChildren(int widthMeasureSpec, int heightMeasureSpec) at the end of your onMeasure() override. The child views don't know that you changed the height.

Android, when GridView nested in a ListView, it width will fill_parent even it's child is just one

Android, when GridView nested in ScrollView or ListView, it's width will fill_parent even it's child is just one, i want it's width is wrap_content, any sulotion ? thanks.
the listview item xml like below :
<?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:orientation="vertical" >
<TextView
android:id="#+id/my_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp" >
</TextView>
<com.umeng.example.communitydemo.MGridView
android:id="#+id/my_gv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="#aaaaaa"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:padding="10dp"
android:verticalSpacing="5dp" />
</LinearLayout>
the MGridView is :
public class MGridView extends GridView {
public boolean hasScrollBar = true;
/**
* #param context
*/
public MGridView(Context context) {
this(context, null);
}
public MGridView(Context context, AttributeSet attrs) {
super(context, attrs, 0);
}
public MGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = heightMeasureSpec;
if (hasScrollBar) {
expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
two screen shot : http://pan.baidu.com/s/1c06Gfny。

Categories

Resources