Views within custom layout won't show - android

I'm creating a custom linear layout to hold two image views.
I first thought that the layout is not showing at all but I then set its background to black to check if the layout is being inflated and it did. Then understood that the problem is the image views, they simply not showing.
Thanks in advance :-)
This is the class:
public class LoginIcons extends LinearLayout {
private ImageView mImageViewLogo;
private ImageView mImageViewIcons;
private View mView;
boolean test = false;
public LoginIcons(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public LoginIcons(Context context) {
super(context);
init();
}
private void init() {
setOrientation(LinearLayout.VERTICAL);
mImageViewLogo = new ImageView(this.getContext());
mImageViewIcons = new ImageView(this.getContext());
mView = new View(getContext());
LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mImageViewLogo.setLayoutParams(params);
params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.topMargin = (int)LocalSettingsHelper.dpToPx(getContext(), 20);
mImageViewIcons.setLayoutParams(params);
params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.topMargin = (int)LocalSettingsHelper.dpToPx(getContext(), 10);
mView.setLayoutParams(params);
mImageViewLogo.setImageResource(R.drawable.splash_logo_placeholder);
mImageViewIcons.setImageResource(R.drawable.login_icons);
mImageViewIcons.setBackgroundColor(Color.BLACK);
mImageViewLogo.setBackgroundColor(Color.BLACK);
addView(mView);
addView(mImageViewLogo);
addView(mImageViewIcons);
}
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
setViewsDimensions();
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
}
private void setViewsDimensions() {
LinearLayout parent = (LinearLayout) mImageViewLogo.getParent();
int screenWidth = LocalSettingsHelper.getScreenWidth(getContext());
// 0.375 percent
int imgDim = (int) ((double) screenWidth * 0.32);
int iconsWidth = (int) ((double) screenWidth * 0.5);
LinearLayout.LayoutParams params = (LayoutParams) mImageViewLogo
.getLayoutParams();
params.width = imgDim;
params.height = imgDim;
mImageViewLogo.setLayoutParams(params);
params = (LayoutParams) mImageViewIcons.getLayoutParams();
params.width = iconsWidth;
mImageViewIcons.setLayoutParams(params);
}
}
And this is the 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"
android:background="#color/HeaderBackground"
android:orientation="vertical" >
<Button
android:id="#+id/button_ok"
style="#style/LoginOkButtonStyle"
android:background="#drawable/blue_button_background_selector"
android:text="#string/ok" />
<com.example.me.entities.views.LoginIcons
android:id="#+id/loginIcons"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</com.example.me.entities.views.LoginIcons>
</LinearLayout>

It seems that the View object acted like match_parent and had height of nearly 400 pixels. This caused the image views to drop outside the boundaries of the screen.
Once I removed the View the image views turned up.

Related

add view to customview and resize that

i have a customview that extend from FrameLayout:
public class FanView extends FrameLayout {
private static final String TAG = FanView.class.getSimpleName();
private List<FanItem> fanItems = new ArrayList<>();
private float openRatio = 0f;
public FanView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
setStaticTransformationsEnabled(true);
}
#Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Log.d(TAG, "onLayout(" + changed + ", " + left + ", " + top + ", " + right + ", " + bottom + ")");
super.onLayout(changed, left, top, right, bottom);
Log.d(TAG, "End onLayout");
}
#Override
protected boolean getChildStaticTransformation(View child, Transformation t) {
final float index = getChildCount() - indexOfChild(child) - 1;
final float height = child.getHeight();
Matrix matrix = t.getMatrix();
matrix.setTranslate((float) (-index * 1.1 * (height/2) * openRatio), (float) (index * height * openRatio * 1.2));
return true;
}
public void setOpenRatio(float r) {
this.openRatio = r;
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View view = getChildAt(i);
view.invalidate();
}
invalidate();
}
public void setFanItems(List<FanItem> fanItems) {
this.fanItems = fanItems;
removeAllViewsInLayout();
for (int i = 0; i < fanItems.size(); i++) {
int fanItemIndex = fanItems.size() - i - 1;
FanItem fanItem = fanItems.get(fanItemIndex);
View fanView = inflate(getContext(),
fanItemIndex == 0 ? R.layout.fan_item_header : R.layout.fan_item, null);
TextView textView = (TextView) fanView.findViewById(R.id.fan_view_item_title);
textView.setText(fanItem.getTitle());
addViewInLayout(fanView, i,
new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
}
invalidate();
}
}
in xml layout i use this custom view with width and height wrap_content.
when i add view to that custom view not show this views,
because custom layout not change it's size.
how to say to custom view that change it's size
Your current code probably needs another constructor
public FanView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
Please provide valid size to inflate the items at first. Give at least one element to acquire size after layout inflation. You can change the size of items using layout params.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="schemas.android.com/apk/res/android"
android:background="#drawable/fan_item_background"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textDirection="rtl">
<TextView
android:id="#+id/fan_view_item_title"
android:layout_height="200dp"
android:layout_width="200dp"
android:text="ersfsdf"
android:textAlignment="textStart"
android:textColor="#ffffff"/>
</FrameLayout>
To change the size dynamically try something like this .
// Gets linearlayout
FrameLayout layout = findViewById(R.id.framelaut);// or any layout
// Gets the layout params that will allow you to resize the layout
LayoutParams params = layout.getLayoutParams();
// Changes the height and width to the specified *pixels*
params.height = 100;
params.width = 100;
layout.setLayoutParams(params);

slowly scroll in page

I want to make a view slowly scrolls into mobile page, first like this:
then like this:
the view with red background slowly scrolls into mobile page.
Here is the xml file:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/iv_clean_complete"
android:layout_width="140dp"
android:layout_height="140dp"
android:layout_marginTop="80dp"
android:src="#mipmap/img_clean_completed_ok"
android:layout_centerHorizontal="true"/>
<RelativeLayout
android:id="#+id/layout_btm"
android:background="#color/colorAccent"
android:layout_width="match_parent"
android:layout_height="288dp">
<TextView
android:text="Hello World"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
Here is the code:
public class MainActivity extends AppCompatActivity {
private RelativeLayout layoutBtm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layoutBtm = findViewById(R.id.layout_btm);
int topMargin = getHeightPixels();
ViewGroup.MarginLayoutParams marginLayoutParams =
new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dip2px(this, 288));
marginLayoutParams.topMargin = topMargin;
RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(marginLayoutParams);
layoutParams2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layoutBtm.setLayoutParams(layoutParams2);
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(layoutBtm, "translationY", -dip2px(this, 288));
objectAnimator.setDuration(1000);
objectAnimator.start();
}
private int getHeightPixels(){
DisplayMetrics dm = new DisplayMetrics();
dm = getResources().getDisplayMetrics();
return dm.heightPixels;
}
private int getStatusBarHeight(Activity activity){
try {
Class<?> clazz = Class.forName("com.android.internal.R$dimen");
Object object = clazz.newInstance();
Field field = clazz.getField("status_bar_height");
int dpHeight = Integer.parseInt(field.get(object).toString());
return activity.getResources().getDimensionPixelSize(dpHeight);
} catch (Exception e) {
return 0;
}
}
private int dip2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
}
It seems to be fine. But after the red view scrolls in, there is some space left at the bottom.The page doesn't change its height automatically
Finally I know how to solve this problem. I edit the code of change the translationY of the view to change the top margin of the view.Here is the code after change:
public class MainActivity extends AppCompatActivity {
private RelativeLayout layoutBtm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layoutBtm = findViewById(R.id.layout_btm);
int topMargin = getHeightPixels();
ViewGroup.MarginLayoutParams marginLayoutParams =
new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dip2px(this, 288));
marginLayoutParams.topMargin = topMargin;
RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(marginLayoutParams);
layoutParams2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layoutBtm.setLayoutParams(layoutParams2);
ValueAnimator animator = ValueAnimator.ofInt(getHeightPixels(), getHeightPixels() - getStatusBarHeight(this) -dip2px(this, 288));
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animator) {
int currentValue = (int) animator.getAnimatedValue();
ViewGroup.MarginLayoutParams marginLayoutParams =
new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dip2px(MainActivity.this, 288));
marginLayoutParams.topMargin = currentValue;
RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(marginLayoutParams);
layoutParams2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layoutBtm.setLayoutParams(layoutParams2);
layoutBtm.requestLayout();
}
});
animator.setDuration(1000);
animator.start();
}
private int getHeightPixels(){
DisplayMetrics dm = new DisplayMetrics();
dm = getResources().getDisplayMetrics();
return dm.heightPixels;
}
private int getStatusBarHeight(Activity activity){
try {
Class<?> clazz = Class.forName("com.android.internal.R$dimen");
Object object = clazz.newInstance();
Field field = clazz.getField("status_bar_height");
int dpHeight = Integer.parseInt(field.get(object).toString());
return activity.getResources().getDimensionPixelSize(dpHeight);
} catch (Exception e) {
return 0;
}
}
private int dip2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
}

Is it possible create view without xml in Android?

I am trying to create a custom view without xml
public class MyView extends ViewGroup {
public MyView (Context context) {
super(context);
setBackgroundColor(0xffff0000);
RelativeLayout base = new RelativeLayout(context);
RelativeLayout.LayoutParams rP = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
base.setBackgroundColor(0xff00ff00);
base.setLayoutParams(rP);
RelativeLayout.LayoutParams iP = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ImageView icon = new ImageView(context);
icon.setBackgroundResource(android.R.drawable.ic_menu_add);
icon.setLayoutParams(iP);
addView(icon);
addView(base);
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
}
And use it in actitvy
MyView myview = new MyView(this);
WindowManager.LayoutParams baseParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
addContentView(myview, baseParams);
But the LayoutParams does not work, it display nothing
I must use this onLayout to make it display, and the base layout is not MATCH_PARENT and icon is not WRAP_CONTENT
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final View child = getChildAt(0);
final View child2 = getChildAt(1);
child.layout(0, 0, 300, 300);
child2.layout(0, 0, 300, 300);
}
I also tried to add icon to layout, but the app will crash
base.addView(icon);
Is it possible to create this layout without hardcode the size and position?
I checked RelativeLayout.LayoutParams, but I cannot find any method to set it centerInParent to true.
http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/ic_menu_add"
android:layout_centerInParent="true"
/>
</RelativeLayout>
Please give this code a try.
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create parent RelativeLayout
RelativeLayout relParent = new RelativeLayout(this);
RelativeLayout.LayoutParams relParentParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
relParent.setLayoutParams(relParentParam);
// Create child ImageView
ImageView imgView = new ImageView(this);
RelativeLayout.LayoutParams imgViewParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
imgView.setLayoutParams(imgViewParams);
imgView.setImageResource(android.R.drawable.ic_menu_add);
imgViewParams.addRule(RelativeLayout.CENTER_IN_PARENT);
// Add child ImageView to parent RelativeLayout
relParent.addView(imgView);
// Set parent RelativeLayout to your screen
setContentView(relParent, relParentParam);
}
}

Is it possible to implement a FlowLayout with RelativeLayout properties?

I would like to create a custom RelativeLayout that has two views in one row: one on the left side of the screen (android:layout_alignParentStart="true") and one on the right (android:layout_alignParentEnd="true"). The view on the right will grow toward the left view until it takes up all the space between the two views. Then it will move to a new line under the view on the left.
I have implemented a slightly modified version of Romain Guy's FlowLayout that extends RelativeLayout. However, this class seems to ignore the RelativeLayout's align properties and just sticks the views right next to each other. Is there a way to implement a such a layout that will anchor the views to the left and right?
FlowLayout class:
public class FlowLayout extends RelativeLayout {
private int mHorizontalSpacing;
private int mVerticalSpacing;
public FlowLayout(Context context) {
super(context);
}
public FlowLayout(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.FlowLayout);
mHorizontalSpacing = attributes.getDimensionPixelSize(R.styleable
.FlowLayout_horizontalSpacing, 0);
mVerticalSpacing = attributes.getDimensionPixelSize(R.styleable
.FlowLayout_verticalSpacing, 0);
attributes.recycle();
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int width = 0;
int height = getPaddingTop();
int currentWidth = getPaddingStart();
int currentHeight = 0;
final int count = getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
LayoutParams lp = (LayoutParams) child.getLayoutParams();
measureChild(child, widthMeasureSpec, heightMeasureSpec);
if (currentWidth + child.getMeasuredWidth() > widthSize) {
height += currentHeight + mVerticalSpacing;
currentHeight = 0;
width = Math.max(width, currentWidth);
currentWidth = getPaddingEnd();
}
int spacing = mHorizontalSpacing;
if (lp.spacing > -1) {
spacing = lp.spacing;
}
lp.x = currentWidth + spacing;
lp.y = currentHeight;
currentWidth += child.getMeasuredWidth();
currentHeight = Math.max(currentHeight, child.getMeasuredHeight());
}
width += getPaddingEnd();
height += getPaddingBottom();
setMeasuredDimension(resolveSize(width, widthMeasureSpec), resolveSize(height,
heightMeasureSpec));
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
LayoutParams lp = (LayoutParams) child.getLayoutParams();
child.layout(lp.x, lp.y, lp.x + child.getMeasuredWidth(), lp.y + child
.getMeasuredHeight());
}
}
#Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
return p instanceof LayoutParams;
}
#Override
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout
.LayoutParams.WRAP_CONTENT);
}
#Override
protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
return new LayoutParams(p.width, p.height);
}
#Override
public RelativeLayout.LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(getContext(), attrs);
}
public static class LayoutParams extends RelativeLayout.LayoutParams {
public int spacing;
public int x;
public int y;
public LayoutParams(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable
.FlowLayout_LayoutParams);
spacing = attributes.getDimensionPixelSize(R.styleable
.FlowLayout_LayoutParams_layoutSpacing, -1);
attributes.recycle();
}
public LayoutParams(int width, int height) {
super(width, height);
}
}
}
It turns out that rather than calculating the right view's new position yourself, you can change its LayoutParams and have the OS handle positioning for you. I created a custom layout that extends RelativeLayout and overrides the onMeasure() method. This will adjust the LayoutParams accordingly.
More specifically:
Call the super method then find the widths of the two views and their parent in onMeasure(). Use these to figure out if the right view will overlap the left view. If so, change the right view's layout_alignParentEnd="true" property to be layout_alignParentStart="true" and give it the layout_below="#id/left_view" property. Do the opposite when there will be no overlap. Call the super method again to have the OS remeasure the views for you.
The layout class:
public class WrappingLayout extends RelativeLayout {
private TextView leftView;
private EditText rightView;
//Use this to prevent unnecessarily adjusting the LayoutParams
//when the right view is already in the correct position
private boolean isMultiline = false;
public WrappingLayout(Context context) {
super(context);
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.wrapping_layout, this);
leftView = (TextView) findViewById(R.id.left_view);
rightView = (EditText) findViewById(R.id.right_view);
}
public WrappingLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.wrapping_layout, this);
leftView = (TextView) findViewById(R.id.left_view);
rightView = (EditText) findViewById(R.id.right_view);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//Call first to make sure the views' initial widths have been set
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int screenWidth = getMeasuredWidth();
int leftViewWidth = getPaddingStart() + leftView.getMeasuredWidth() + leftView.getPaddingEnd();
int rightViewWidth = getPaddingEnd() + rightView.getMeasuredWidth() + rightView.getPaddingStart();
LayoutParams rightViewParams = (LayoutParams) rightView.getLayoutParams();
if (!isMultiline && rightViewWidth + leftViewWidth > screenWidth) {
isMultiline = true;
rightViewParams.addRule(BELOW, R.id.left_view);
rightViewParams.removeRule(ALIGN_PARENT_END);
rightViewParams.addRule(ALIGN_PARENT_START);
//Call again here to adjust dimensions for new params
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
} else if (isMultiline && rightViewWidth + leftViewWidth < screenWidth) {
isMultiline = false;
rightViewParams.removeRule(BELOW);
rightViewParams.addRule(ALIGN_PARENT_END);
rightViewParams.removeRule(ALIGN_PARENT_START);
//Call again here to adjust dimensions for new params
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
The layout XML:
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#id/left_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"/>
<EditText
android:id="#id/right_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:gravity="center"
android:text="#string/hello"/>
</merge>

Add multiple views in ScrollView?

I am working on an Android App. I have some problem..
Here is my app :
The pink rectangle is a ScrollView. I want to add TextView to the same ScrollView. I know I need a LinearLayout and add my Views inside this layout. After I need to add the LinearLayout to my ScrollView. Here is my ScrollView class with my methods setClassicLabel to implement TextViewand setClassicButton to implement Button in my ScrollView.
Here is my ScrollView.java :
public class MyScrollView extends ScrollView {
private JSONParser jParser;
private JSONObject contenuScrollView;
private Context context;
#SuppressLint("NewApi")
public MyScrollView(Context context, JSONArray listScrollView) throws JSONException {
super(context);
this.context = context;
}
public void onCreate(Bundle savedInstanceState) throws JSONException {
}
/* Création de TextView */
public void setClassicLabel(JSONArray listLabel, LinearLayout ll) throws JSONException {
if (listLabel.length() > 0)
{
DisplayMetrics metrics = new DisplayMetrics();
ll = new LinearLayout(getContext());
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams paramsll = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for (int i = 0; i < listLabel.length(); i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) (getContext().getResources().getDisplayMetrics().widthPixels * listLabel.getJSONObject(i).getDouble("size_x")),
(int) (getContext().getResources().getDisplayMetrics().heightPixels * listLabel.getJSONObject(i).getDouble("size_y")));
params.leftMargin = (int) (getContext().getResources().getDisplayMetrics().widthPixels * listLabel.getJSONObject(i).getDouble("position_x"));
params.topMargin = (int) (getContext().getResources().getDisplayMetrics().heightPixels * listLabel.getJSONObject(i).getDouble("position_y"));
TextView tv = new TextView(context);
tv.setText(listLabel.getJSONObject(i).getString("text"));
tv.setTextSize(listLabel.getJSONObject(i).getInt("police_size"));
tv.setTextColor(Color.parseColor(listLabel.getJSONObject(i).getString("colortext")));
tv.setBackgroundColor(Color.parseColor(listLabel.getJSONObject(i).getString("color")));
ll.addView(tv, params);
}
this.addView(ll, paramsll);
}
}
#SuppressWarnings("deprecation")
public void setClassicButton(JSONArray listButton, LinearLayout ll) throws JSONException {
if (listButton.length() > 0)
{
DisplayMetrics metrics = new DisplayMetrics();
ll = new LinearLayout(getContext());
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams paramsll = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for (int i = 0; i < listButton.length(); i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) (getContext().getResources().getDisplayMetrics().widthPixels * listButton.getJSONObject(i).getDouble("size_x")),
(int) (getContext().getResources().getDisplayMetrics().heightPixels * listButton.getJSONObject(i).getDouble("size_y")));
params.leftMargin = (int) (getContext().getResources().getDisplayMetrics().widthPixels * listButton.getJSONObject(i).getDouble("position_x"));
params.topMargin = (int) (getContext().getResources().getDisplayMetrics().heightPixels * listButton.getJSONObject(i).getDouble("position_y"));
int dest = listButton.getJSONObject(i).getInt("dest");
MyButton myButton = new MyButton(context, dest);
RoundRectShape rect = new RoundRectShape(
new float[] {50,50, 50,50, 50,50, 50,50},
null,
null);
ShapeDrawable bg = new ShapeDrawable(rect);
bg.getPaint().setColor(Color.parseColor(listButton.getJSONObject(i).getString("color")));
myButton.setBackgroundDrawable(bg);
myButton.setTextColor(Color.parseColor(listButton.getJSONObject(i).getString("colortext")));
//BitmapDrawable bdra = new BitmapDrawable(downloadBitmap(listButton.getJSONObject(i).getString("http")));
myButton.setText(listButton.getJSONObject(i).getString("text"));
myButton.getBackground().setAlpha(30);
myButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
MyButton button = (MyButton)view;
Intent intent = new Intent(context, ClassicView.class);
Content content = new Content(button.getDestination());
intent.putExtra("CONTENT", content);
context.startActivity(intent);
}
});
ll.addView(myButton, params);
}
this.addView(ll, paramsll);
}
}
}
MyScrollView heritates from ScrollView. The problem is the next :
When I create my ScrollView with the method setClassicScrollView, i call my methods setClassicLabel and setClassicButton to implements theses two views in my ScrollView. But i can only set Button or Label. I can't set one label and one button. I must have a problem who comes from a layout.. I am on it for two days
Here is my method setClassicScrollView :
public void setClassicScrollView(JSONArray listScrollView, RelativeLayout rl) throws JSONException {
if (listScrollView.length() > 0) {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
for (int i = 0; i < listScrollView.length(); i++) {
MyScrollView myScrollView = new MyScrollView(this, listScrollView);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams paramsll = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) (metrics.widthPixels * listScrollView.getJSONObject(i).getDouble("size_x")), (int) (metrics.heightPixels * listScrollView.getJSONObject(i).getDouble("size_y")));
params.leftMargin = (int) (metrics.widthPixels * listScrollView.getJSONObject(i).getDouble("position_x"));
params.topMargin = (int) (metrics.heightPixels * listScrollView.getJSONObject(i).getDouble("position_y"));
myScrollView.setBackgroundColor(Color.RED);
myScrollView.setLayoutParams(params);
myScrollView.setHorizontalScrollBarEnabled(true);
myScrollView.getMaxScrollAmount();
myScrollView.getBackground().setAlpha(50);
//myScrollView.setClassicLabel(listScrollView.getJSONObject(i).getJSONArray("Label"), ll);
myScrollView.setClassicButton(listScrollView.getJSONObject(i).getJSONArray("Button"), ll);
rl.addView(myScrollView);
}
}
}
Someone has an idea of how I can set TextViews and Buttons in the same ScrollView ?
Here's the layout for your scroll view.
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Spinner
android:id="#+id/spending_report_cycle_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:id="#+id/SomeView2"
android:layout_width="wrap_content"
android:layout_height="5dp"
android:layout_gravity="center_horizontal"
android:background="#008080"
android:orientation="vertical" />
<fragment
android:id="#+id/fragment_content_1"
android:name="com.mike.passintents.Fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:id="#+id/SomeView2"
android:layout_width="wrap_content"
android:layout_height="5dp"
android:layout_gravity="center_horizontal"
android:background="#008080"
android:orientation="vertical" />
<ListView
android:id="#+id/spending_report_listview"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:background="#333333" >
</ListView>
</LinearLayout>
</ScrollView>
Here.. In the scroll view, I have one linear layout as one child and after that I have other views inside the linear layout.

Categories

Resources