Filling 4 Items in a listview on any device - android

I'm developing an application that have to show exactly 4 items at a time in a listview, independent of device.
How do I get the size of the list view on the device and set list items height to fit 4 at a time on the screen?

1- extend ListView class
public class CustomListView extends ListView {
public CustomListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomListView(Context context) {
super(context);
}
private Runnable afterLayoutRunnable;
public void setAfterLayoutRunnable(Runnable afterLayoutRunnable) {
this.afterLayoutRunnable = afterLayoutRunnable;
}
#Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (afterLayoutRunnable != null) {
afterLayoutRunnable.run();
}
}
}
2- create your own ArrayAdapter
public class CustomListAdapter extends ArrayAdapter<MyObject> {
public CustomListAdapter(Context context, int textViewResourceId, List<MyObject> objects) {
super(context, textViewResourceId, objects);
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.my_item_layout, null);
int height = TestActivity.listItemsMinimumHieght;
if (height > 0) {
convertView.setMinimumHeight(height);
}
}
}
}
3- in your Activity
public class TestActivity extends Activity {
public static int listItemsMinimumHieght = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final CustomListView customListView = (CustomListView) findViewById(R.id.conversation_activity_button_send);
customListView.setAfterLayoutRunnable(new Runnable() {
#Override
public void run() {
listItemsMinimumHieght = customListView.getHeight() / 4;
ArrayList<MyObject> objects=getListContent();
customListView.setAdapter(new ListViewConversationAdapter(TestActivity.this, R.id.sample, objects));
}
});
}
}
i think this should work, good luck

Related

Android Studio: Create Object in custom SwipeButton with attributes from XML

public class SwipeButton extends RelativeLayout {
private ImageView swipeButtonInner;
private float initialX;
private boolean active;
private TextView centerText;
private ViewGroup background;
private Drawable disabledDrawable;
private Drawable enabledDrawable;
private OnStateChangeListener onStateChangeListener;
private OnActiveListener onActiveListener;
private static final int ENABLED = 0;
private static final int DISABLED = 1;
private int collapsedWidth;
private int collapsedHeight;
private LinearLayout layer;
private boolean trailEnabled = false;
private boolean hasActivationState;
public SwipeButton(Context context) {
super(context);
init(context, null, -1, -1);
}
public SwipeButton(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, -1, -1);
}
public SwipeButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr, -1);
}
#TargetApi(21)
public SwipeButton(Context context, AttributeSet attrs, int defStyleAttr, int
defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs, defStyleAttr, defStyleRes);
}
public boolean isActive() {
return active;
}
public void setText(String text) {
centerText.setText(text);
}
public void setBackground(Drawable drawable) {
background.setBackground(drawable);
}
public void setSlidingButtonBackground(Drawable drawable) {
background.setBackground(drawable);
}
public void setDisabledDrawable(Drawable drawable) {
disabledDrawable = drawable;
if (!active) {
swipeButtonInner.setImageDrawable(drawable);
}
}
public void setButtonBackground(Drawable buttonBackground) {
if (buttonBackground != null) {
swipeButtonInner.setBackground(buttonBackground);
}
}
public void setEnabledDrawable(Drawable drawable) {
enabledDrawable = drawable;
if (active) {
swipeButtonInner.setImageDrawable(drawable);
}
}
public void setOnStateChangeListener(OnStateChangeListener
onStateChangeListener) {
this.onStateChangeListener = onStateChangeListener;
}
public void setOnActiveListener(OnActiveListener onActiveListener) {
this.onActiveListener = onActiveListener;
}
public void setInnerTextPadding(int left, int top, int right, int bottom) {
centerText.setPadding(left, top, right, bottom);
}
public void setSwipeButtonPadding(int left, int top, int right, int bottom) {
swipeButtonInner.setPadding(left, top, right, bottom);
}
public void setHasActivationState(boolean hasActivationState) {
this.hasActivationState = hasActivationState;
}
My 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:layout_width="match_parent"
android:id="#+id/AnswerRelativeLayout"
android:layout_height="70dp"
android:orientation="vertical"
tools:context="com.example.dynamicobj.FragmentMain">
<com.example.dynamicobj.SwipeButton
android:id="#+id/test_btn"
android:layout_width="match_parent"
android:layout_height="70dp"
app:button_background="#drawable/shape_button"
app:button_image_disabled="#drawable/ic_launcher_background"
app:button_image_height="60dp"
app:button_image_width="100dp"
app:has_activate_state="true"
app:initial_state="disabled"
app:inner_text="Termine buchen"
app:inner_text_background="#drawable/shape_rounded"
app:inner_text_bottom_padding="18dp"
app:inner_text_right_padding="200dp"
app:inner_text_color="#android:color/black"
app:inner_text_size="16sp"
app:inner_text_top_padding="18dp" />
</LinearLayout>`
F
ragmentMain:
public class FragmentMain extends Fragment {
Context context;
View rootView;
public FragmentMain() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(false);
}
#Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_main, container, false);
LinearLayout layout = (LinearLayout)
rootView.findViewById(R.id.AnswerRelativeLayout);
SwipeButton button = new SwipeButton(getContext());
layout.addView(button);
return rootView;
}
}
So I got this custom SwipeButton class from Git (com.ebanx.swipebtn.SwipeButton). Now I want to create an Object from SwipeButton with a layout looking like the one from the xml file. Is there any method, where I just can give the new button the prefinished layout without having to use all these Methods in the SwipeButton class? I am going to create the buttons dynamically later on, but all being the same layout. Help pls?
you forgot to add LayoutParams to your button
use below code before adding button into layout
button.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT))
and set your LinearLayout height wrap_content not 70dp

ListView dynamically load contents

Good day.
What I need:
I need to create a ListView, with custom rows, that will Async-load infos into rows when user scrolls over those rows.
Like -
We open layout, there's 5 rows shown.
I do async-load for those, for example - load photos.
User scrolls down, opens 3 more.
I load data for those 3 items.
Question:
Can anyone please describe a pattern, and elements that I should use to achieve that?
Tried:
Create a custom View, based on ....Layout (FrameLayout f.e.)
Put this custom View as a root element in Row's layout
Override onDraw event of that view.
What I get:
This event is shot every time, when ListView is rendered, and IS SHOT FOR EVERY ITEM from the start...
Any advices would be appreciated, thanks.
Some code
Activity, onResume event.
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
tvl = (TextView) findViewById(R.id.tvLog);
LinearLayout lv = (LinearLayout) findViewById(R.id.Ll1);
LinkedList<String> ll = new LinkedList<>();
for (int i = 0; i < 30; i++) {
ll.add("s"+i);
}
CommonCheckRowAdapter cma = new CommonCheckRowAdapter(getApplication(), ll);
int adapterCount = cma.getCount();
for (int i = 0; i < adapterCount; i++) {
View item = cma.getView(i, null, null);
lv.addView(item);
}
}
Custom View class body
package com.example.testscroll;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnDrawListener;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;
public class CView extends FrameLayout{
private LayoutInflater inflater;
public CView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.setWillNotDraw(false);
}
public CView(Context context, AttributeSet attrs) {
super(context, attrs);
this.setWillNotDraw(false);
}
public CView(Context context) {
super(context);
this.setWillNotDraw(false);
}
private void initView(Context context){
View.inflate(context, R.layout.common_row, this);
}
#Override
protected void onDraw(Canvas canvas) {
//super.onDraw(canvas);
//Do what you want.
//Toast.makeText(getContext(), "Showing.", Toast.LENGTH_SHORT).show();
//Toast.makeText(getContext(), "Showing " + ((TextView)findViewById(R.id.tv1)).getText().toString(), Toast.LENGTH_SHORT).show();
MainActivity.tvl.setText(MainActivity.tvl.getText().toString() + " "+ ((TextView)findViewById(R.id.tv1)).getText().toString());
}
#Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
}
Adapter's getView(rest - is by the book)
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (vi == null)
vi = inflater.inflate(R.layout.common_row, null);
//Fill
((TextView)vi.findViewById(R.id.tv1)).setText(data.get(position));
return vi;
}
And commoon_row layout
<?xml version="1.0" encoding="utf-8"?>
<com.example.testscroll.CView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_margin="10dp"
android:gravity="center_horizontal|center_vertical"
android:minLines="2"
android:text="www"
android:textSize="50sp" >
</TextView>
</com.example.testscroll.CView>
private String text = "";
public CView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.setWillNotDraw(false);
}
public CView(Context context, AttributeSet attrs) {
super(context, attrs);
this.setWillNotDraw(false);
}
public CView(Context context) {
super(context);
this.setWillNotDraw(false);
}
private void initView(Context context){
View.inflate(context, R.layout.common_row, this);
}
#Override
protected void onDraw(Canvas canvas) {
//super.onDraw(canvas);
//Do what you want.
//Toast.makeText(getContext(), "Showing.", Toast.LENGTH_SHORT).show();
//Toast.makeText(getContext(), "Showing " + ((TextView)findViewById(R.id.tv1)).getText().toString(), Toast.LENGTH_SHORT).show();
MainActivity.tvl.setText(text);
}
#Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
public void update(){
text = MainActivity.tvl.getText().toString() + " "+ ((TextView)findViewById(R.id.tv1)).getText().toString();
}
Adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (vi == null)
vi = inflater.inflate(R.layout.common_row, null);
//Fill
((TextView)vi.findViewById(R.id.tv1)).setText(data.get(position));
((vi)vi).update();
return vi;
}

Adding a Custom View as header of ListView

I have a custom view (a class extending View) that I'd like to add as a header of List View. Here's the code snippet:
public class MyActivity extends RoboListActivity {
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View header = getLayoutInflater().inflate(R.layout.myactivity_apps_header, null);
getListView().addHeaderView(header);
...more code
}
But, I can't see anything. But, when I tried to add a non-custom view, it works. Am I missing something, please guide
Providing Complete Source Code
Custom View
public class SpaceCustomView extends View {
private Paint mPaint;
private Paint mTextPaint;
private final String mMessage = "Foo Bar";
private Rect mBounds;
public StorageSpaceCustomView(Context context) {
super(context);
initInput();
}
public StorageSpaceCustomView(Context context, AttributeSet attrs) {
super(context, attrs, 0);
initInput();
}
public StorageSpaceCustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initInput();
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
}
#Override
public void onDraw(android.graphics.Canvas canvas) {
canvas.drawRect(30, 30, 800, 80, mPaint);
canvas.drawText(mMessage, 30, 60, mTextPaint);
}
private void initInput() {
mBounds = new Rect();
mPaint = new Paint();
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mTextPaint = new Paint();
mTextPaint.setColor(Color.BLACK);
mTextPaint.setTextAlign(Paint.Align.LEFT);
mTextPaint.setTextSize(20);
mTextPaint.getTextBounds(mMessage, 0, mMessage.length(), mBounds);
}
}
Header Layout XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.mycompany.app.view.custom.SpaceCustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Activity Class
public class AppsActivity extends RoboListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//add header and footer views
View header = getLayoutInflater().inflate(R.layout.activity_apps_header, null);
getListView().addHeaderView(header);
View footer = getLayoutInflater().inflate(R.layout.activity_pps_footer, null);
getListView().addFooterView(footer);
List<AppInfo> applicationList = Mycatalog.getPromotions();
AppListAdapter adapter = new AppListAdapter(this, applicationList);
setListAdapter(adapter);
}
private class AppListAdapter extends ArrayAdapter<AppInfo> {
public AppListAdapter(Activity activity, List<AppInfo> apps) {
super(activity, android.R.layout.simple_list_item_1, apps);
}
#Override
public boolean isEmpty(){
return false;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// if we weren't given a view, inflate one
if (null == convertView) {
convertView = getLayoutInflater()
.inflate(R.layout.activity_uninstall_apps, null);
}
return convertView;
}
}
}
Conclusion: I can see the footer but not the header.
ListView shows headers and footers only when it's Adapter's isEmpty() returns false.
So try setting an adapter that does that...

Android: inflated view do not show in customed ViewGroup

I made a customed view by extending ViewGroup, my problems is:
when adding a child( addView(chileView) ) view into ViewGroup, if the child view is a inflated view, the child view will not show. but when the child view is hard-code view( like, btn = new Button(ctx) ), it will show!
The Code, not show:
public class EdgeSwiper extends ViewGroup{
public EdgeSwiper(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public EdgeSwiper(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public EdgeSwiper(Context context) {
super(context);
init(context);
}
View v;
private void init(Context context) {
v = View.inflate(context,R.layout.aview,null);
this.addView(v);
}
#Override
protected void onLayout(boolean arg0, int a, int b, int c, int d) {
v.layout(a, b, c, d);
}
}

custom navigation bar android

I want to do a custom horizontal navigation bar, which consists of as many dots as i have pages.
My thoughts are:
Create a Custom ListView
Create Class Dot extends from View
Add all these dots to the custom listview dynamically..
Is this right to do so?
EDIT:
public class NavigationBarLesson extends LinearLayout {
private LessonConfig config = LessonConfig.getInstance();
private ArrayList<NavigationCircle> navigationCircles;
private int pageCount;
public NavigationBarLesson(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public NavigationBarLesson(Context context) {
super(context);
init(context);
}
private void init(Context context) {
Log.i("init","yes");
pageCount = config.getLektionCount();
navigationCircles = new ArrayList<NavigationCircle>();
for(int i=0; i < pageCount; i++){
this.addView(new NavigationCircle(context));
}
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = View.MeasureSpec.getSize(widthMeasureSpec);
int height = View.MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(width, height);
}
}
public class NavigationCircle extends ImageView{
private static Bitmap img;
private Bitmap activeImg;
public NavigationCircle(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public NavigationCircle(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public NavigationCircle(Context context) {
super(context);
init();
}
public void init() {
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 0, 5, 0);
this.setLayoutParams(lp);
this.setBackgroundResource(R.drawable.upcoming_pages);
}
public Bitmap getImg() {
return img;
}
public Bitmap getActiveImg() {
return this.activeImg;
}
}
Well, I do not see why you would need a ListView. Create a horizontal LinearLayout with some simple views like TextView's or ImageView's etc added dynamically to the LinearLayout.

Categories

Resources