click event of button not getting raised - android

I am trying to make something like customised quick badge with some buttons on it. I have successfully desgined it but when i am clicking on those buttons it does nothing. Can someone tell me where am i going wrong.
demo_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/likemenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="30dp"
android:text="Button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
</TextView>
<Button
android:id="#+id/likequickaction"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="30dp"
android:text="Button" />
</LinearLayout>
popup_grid_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<FrameLayout
android:id="#+id/header2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10.0dip"
android:background="#drawable/quickaction_top_frame" />
<ImageView
android:id="#+id/arrow_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/quickaction_arrow_up" />
<HorizontalScrollView
android:id="#+id/scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header2"
android:background="#drawable/quickaction_slider_background"
android:fadingEdgeLength="0.0dip"
android:paddingLeft="1.0dip"
android:scrollbars="none" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/quickaction_slider_grip_left" />
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/api_buttons" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/quickaction_slider_grip_right" />
</LinearLayout>
</HorizontalScrollView>
<FrameLayout
android:id="#+id/footer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/scroll"
android:background="#drawable/quickaction_bottom_frame" />
</RelativeLayout>
api_buttons.xml
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/quickaction_slider_background" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/one"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="One" />
<Button
android:id="#+id/two"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="Two" />
<Button
android:id="#+id/three"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="Three" />
<Button
android:id="#+id/four"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="Four" />
<Button
android:id="#+id/five"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="Five" />
<Button
android:id="#+id/six"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="Six" />
</TableRow>
</HorizontalScrollView>
BetterPopupWindow.java
public class BetterPopupWindow {
protected final View anchor;
private final PopupWindow window;
private View root;
private Drawable background = null;
private final WindowManager windowManager;
public BetterPopupWindow(View anchor) {
this.anchor = anchor;
this.window = new PopupWindow(anchor.getContext());
// when a touch even happens outside of the window
// make the window go away
this.window.setTouchInterceptor(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
BetterPopupWindow.this.window.dismiss();
return true;
}
return false;
}
});
this.windowManager = (WindowManager) this.anchor.getContext()
.getSystemService(Context.WINDOW_SERVICE);
onCreate();
}
protected void onCreate() {
}
protected void onShow() {
}
private void preShow() {
if (this.root == null) {
throw new IllegalStateException(
"setContentView was not called with a view to display.");
}
onShow();
if (this.background == null) {
this.window.setBackgroundDrawable(new BitmapDrawable());
} else {
this.window.setBackgroundDrawable(this.background);
}
this.window.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
this.window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
this.window.setTouchable(true);
this.window.setFocusable(true);
this.window.setOutsideTouchable(true);
this.window.setContentView(this.root);
}
public void setBackgroundDrawable(Drawable background) {
this.background = background;
}
public void setContentView(View root) {
this.root = root;
this.window.setContentView(root);
}
public void setContentView(int layoutResID) {
LayoutInflater inflator = (LayoutInflater) this.anchor.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.setContentView(inflator.inflate(layoutResID, null));
}
public void setOnDismissListener(PopupWindow.OnDismissListener listener) {
this.window.setOnDismissListener(listener);
}
public void showLikePopDownMenu() {
this.showLikePopDownMenu(0, 0);
}
public void showLikePopDownMenu(int xOffset, int yOffset) {
this.preShow();
this.window.setAnimationStyle(R.style.Animations_PopDownMenu);
this.window.showAsDropDown(this.anchor, xOffset, yOffset);
}
public void showLikeQuickAction() {
this.showLikeQuickAction(0, 0);
}
public void showLikeQuickAction(int xOffset, int yOffset) {
this.preShow();
this.window.setAnimationStyle(R.style.Animations_GrowFromBottom);
int[] location = new int[2];
this.anchor.getLocationOnScreen(location);
Rect anchorRect = new Rect(location[0], location[1], location[0]
+ this.anchor.getWidth(), location[1] + this.anchor.getHeight());
this.root.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
int rootWidth = this.root.getMeasuredWidth();
int rootHeight = this.root.getMeasuredHeight();
int screenWidth = this.windowManager.getDefaultDisplay().getWidth();
int screenHeight = this.windowManager.getDefaultDisplay().getHeight();
int xPos = ((screenWidth - rootWidth) / 2) + xOffset;
int yPos = anchorRect.top - rootHeight + yOffset;
// display on bottom
if (rootHeight > anchorRect.top) {
yPos = anchorRect.bottom + yOffset;
this.window.setAnimationStyle(R.style.Animations_GrowFromTop);
}
this.window.showAtLocation(this.anchor, Gravity.NO_GRAVITY, xPos, yPos);
}
public void dismiss() {
this.window.dismiss();
}
}
LikeQuickActionDemo.java
public class LikeQuickActionsDemo extends Activity {
private Button likemenuButton;
private Button likequickactionButton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.demo_layout);
this.likemenuButton = (Button) this.findViewById(R.id.likemenu);
this.likemenuButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
DemoPopupWindow dw = new DemoPopupWindow(v);
dw.showLikePopDownMenu();
}
});
this.likequickactionButton = (Button) this
.findViewById(R.id.likequickaction);
this.likequickactionButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
DemoPopupWindow dw = new DemoPopupWindow(v);
dw.showLikePopDownMenu(0, 200);
}
});
}
private static class DemoPopupWindow extends BetterPopupWindow implements
OnClickListener {
public DemoPopupWindow(View anchor) {
super(anchor);
}
protected void onCreate() {
// inflate layout
LayoutInflater inflater = (LayoutInflater) this.anchor.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup root = (ViewGroup) inflater.inflate(
R.layout.popup_grid_layout, null);
// setup button events
for (int i = 0, icount = root.getChildCount(); i < icount; i++) {
View v = root.getChildAt(i);
if (v instanceof TableRow) {
TableRow row = (TableRow) v;
for (int j = 0, jcount = row.getChildCount(); j < jcount; j++) {
View item = row.getChildAt(j);
if (item instanceof Button) {
Button b = (Button) item;
b.setOnClickListener(this);
}
}
}
}
// set the inflated view as what we want to display
this.setContentView(root);
}
public void onClick(View v) {
// we'll just display a simple toast on a button click
Button b = (Button) v;
Toast.makeText(this.anchor.getContext(), b.getText(),
Toast.LENGTH_SHORT).show();
this.dismiss();
}
}
}

The problem is in DemoPopupWindow.onCreate method:
ViewGroup root = (ViewGroup) inflater.inflate(
R.layout.popup_grid_layout, null);
for (int i = 0, icount = root.getChildCount(); i < icount; i++) {
View v = root.getChildAt(i);
if (v instanceof TableRow) {
root will be RelativeLayout defined in popup_grid_layout.xml. Later you iterate through all its children and set onclick listeners only if child view is TableRow, which is never the case. I guess what you want there is to find horizontalScrollView1 view and do the same with its children instead, something like:
View horizontalScrollView1 = findViewById(R.id.horizontalScrollView1);
for (int i = 0, icount = horizontalScrollView1.getChildCount(); i < icount; i++) {
View v = horizontalScrollView1.getChildAt(i);
if (v instanceof TableRow) {
...

Related

Unable to Draw lines between two listviews or list of buttons in horizontal

I am trying to draw lines between two columns consists of 10 buttons in each column like in image showing below. I tried using linear layout.
private void createCHTDesign(int scenarios, int numberOfPlots, ArrayList<Plots> plotsArrayList) {
headingLayout.clear();
Log.d(TAG, "scenarios:" + scenarios);
Log.d(TAG, "headingsize:" + headingLayout.size());
for (int i = 0; i < scenarios; i++) {
LinearLayout linearLayout = new LinearLayout(mContext);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if (i == 0) {
params.setMargins(0, 0, 0, 0);
} else {
params.setMargins(250, 0, 0, 0);
}
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(params);
mainLayout.addView(linearLayout);
headingLayout.add(linearLayout);
Log.d(TAG, "count:" + mainLayout.getChildCount());
}
for (int i = 0; i < headingLayout.size(); i++) {
addButtons(headingLayout.get(i), i, numberOfPlots, plotsArrayList);
}
}
#SuppressLint("ClickableViewAccessibility")
private void addButtons(LinearLayout layout, int index, int numberOfPlots, ArrayList<Plots> plotsArrayList) {
TextView textView = new TextView(mContext);
LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(tvParams);
textView.setText(combinationNames.get(index));
textView.setTextSize(16);
layout.addView(textView);
for (int i = 0; i < numberOfPlots; i++) {
Button button = new Button(mContext);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(400, 150);
button.setLayoutParams(params);
button.setId(View.generateViewId());
button.setText(plotsArrayList.get(i).getMpValue() + "\n" + plotsArrayList.get(i).getYield());
button.setGravity(Gravity.CENTER_VERTICAL);
button.setPadding(15, 0, 0, 0);
button.setTextColor(Color.parseColor("#FFFFFF"));
button.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.right2x, 0);
GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setColor(Color.parseColor(colors[i]));
gradientDrawable.setCornerRadius(10);
gradientDrawable.setStroke(20, Color.parseColor(colors[i]));
clickonArrow(button.getText().toString(), button);
button.setBackground(gradientDrawable);
layout.addView(button);
}
}
XML file:
<?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"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1"
android:layout_margin="10dp">
<androidx.appcompat.widget.AppCompatSpinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_weight="0.3"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"
android:gravity="center"
style="#style/SpinnerTheme"
/>
<androidx.appcompat.widget.AppCompatSpinner
android:id="#+id/spinner2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:spinnerMode="dropdown"
android:gravity="center"
style="#style/SpinnerTheme"/>
<androidx.appcompat.widget.AppCompatSpinner
android:id="#+id/spinner3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:gravity="center"
android:spinnerMode="dropdown"
style="#style/SpinnerTheme" />
</LinearLayout>
<EditText
android:id="#+id/searchCht"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/ic_search"
android:layout_marginLeft="10dp"
android:hint="Search CHT"
android:backgroundTint="#android:color/black"
android:layout_marginRight="10dp"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:fillViewport="true"
android:scrollbars="horizontal">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- <com.sma.cht.chtcharacterization.view.MatchTheFollowingAttempted
android:id="#+id/match"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbarThumbHorizontal="#color/colorPrimary"
android:scrollbars="horizontal">
</com.sma.cht.chtcharacterization.view.MatchTheFollowingAttempted>
-->
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
Presently I am getting buttons in both columns but unable to draw lines either you can do this buttons stuff by using listview as well. can you please help me with this. Thanks in advance.
divide your cross line in two parts.
try the below code,
MainActivity.java
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerview=findViewById(R.id.recyclerview);
recyclerview.setLayoutManager(new LinearLayoutManager(this));
recyclerview.setAdapter(new SampleAdapter());
}
}
SampleAdapter.java
public class SampleAdapter extends RecyclerView.Adapter<SampleAdapter.MyViewHolder> {
ArrayList<String> list1;
ArrayList<String> list2;
public SampleAdapter() {
list1 = new ArrayList<>();
list2 = new ArrayList<>();
for (int i = 1; i <= 20; i++) {
list1.add("Left " + i);
list2.add("Right " + i);
}
}
#NonNull
#Override
public SampleAdapter.MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycle_item, viewGroup, false);
return new SampleAdapter.MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull SampleAdapter.MyViewHolder myViewHolder, int position) {
myViewHolder.buttonLeft.setText(list1.get(position));
myViewHolder.buttonRight.setText(list2.get(position));
//show partA image
if (position == 8) {
myViewHolder.imageview1.setVisibility(View.GONE);
myViewHolder.imageview2.setVisibility(View.VISIBLE);
}
//show partB image
if (position == 9) {
myViewHolder.imageview1.setVisibility(View.GONE);
myViewHolder.imageview3.setVisibility(View.VISIBLE);
}
}
#Override
public int getItemCount() {
return list1.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
Button buttonLeft, buttonRight;
ImageView imageview1, imageview2, imageview3;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
buttonLeft = itemView.findViewById(R.id.buttonLeft);
buttonRight = itemView.findViewById(R.id.buttonRight);
imageview1 = itemView.findViewById(R.id.imageview1);
imageview2 = itemView.findViewById(R.id.imageview2);
imageview3 = itemView.findViewById(R.id.imageview3);
}
}}
recycle_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="5">
<Button
android:id="#+id/buttonLeft"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="text" />
<ImageView
android:id="#+id/imageview1"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#B3D4F3"
android:visibility="visible" />
<ImageView
android:id="#+id/imageview2"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#drawable/image_a" android:visibility="gone" />
<ImageView
android:id="#+id/imageview3"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#drawable/image_b android:visibility="gone" />
<Button
android:id="#+id/buttonRight"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="text" />
</LinearLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
image_a.png
image_b.png
output

Android radiogroup with nested linearlayouts not deselecting radiobuttons [duplicate]

This question already has answers here:
How to group RadioButton from different LinearLayouts?
(21 answers)
Closed 5 years ago.
I have a group of radiobuttons in a radiogroup that refuses to behave the way I'd like. The radiobuttons need to be two lines of two, and one line of one, with the last button having an edittext instead of a label. The layout looks correct, but the radiobuttons allow more than one to be selected and won't deselect any.
Below is my XML:
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rgDesignation">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/llVT1">
<RadioButton
android:text="#string/pressure_equipment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rbPE"
android:layout_weight="1" />
<RadioButton
android:text="#string/assemblies"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rbAss"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/llVT2">
<RadioButton
android:text="#string/unheated"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rbUnh"
android:layout_weight="1" />
<RadioButton
android:text="#string/heated"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rbH"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/llVT3">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rbOwnSel"
android:layout_weight="0" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:id="#+id/etOwnChoice"
android:layout_weight="1"
android:textColor="#android:color/black"
android:background="#android:color/white"
android:elevation="5dp" />
</LinearLayout>
</RadioGroup>
Make custom RadioGroup class like this:
public class RadioGroup extends LinearLayout {
private int mCheckedId = -1;
private CompoundButton.OnCheckedChangeListener mChildOnCheckedChangeListener;
private boolean mProtectFromCheckedChange = false;
private OnCheckedChangeListener mOnCheckedChangeListener;
private PassThroughHierarchyChangeListener mPassThroughListener;
public RadioGroup(Context context) {
super(context);
setOrientation(VERTICAL);
init();
}
public RadioGroup(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
mChildOnCheckedChangeListener = new CheckedStateTracker();
mPassThroughListener = new PassThroughHierarchyChangeListener();
super.setOnHierarchyChangeListener(mPassThroughListener);
}
#Override
public void setOnHierarchyChangeListener(OnHierarchyChangeListener listener) {
mPassThroughListener.mOnHierarchyChangeListener = listener;
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
if (mCheckedId != -1) {
mProtectFromCheckedChange = true;
setCheckedStateForView(mCheckedId, true);
mProtectFromCheckedChange = false;
setCheckedId(mCheckedId);
}
}
#Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
if (child instanceof RadioButton) {
final RadioButton button = (RadioButton) child;
if (button.isChecked()) {
mProtectFromCheckedChange = true;
if (mCheckedId != -1) {
setCheckedStateForView(mCheckedId, false);
}
mProtectFromCheckedChange = false;
setCheckedId(button.getId());
}
}
super.addView(child, index, params);
}
public void check(#IdRes int id) {
// don't even bother
if (id != -1 && (id == mCheckedId)) {
return;
}
if (mCheckedId != -1) {
setCheckedStateForView(mCheckedId, false);
}
if (id != -1) {
setCheckedStateForView(id, true);
}
setCheckedId(id);
}
private void setCheckedId(#IdRes int id) {
mCheckedId = id;
if (mOnCheckedChangeListener != null) {
mOnCheckedChangeListener.onCheckedChanged(this, mCheckedId);
}
}
private void setCheckedStateForView(int viewId, boolean checked) {
View checkedView = findViewById(viewId);
if (checkedView != null && checkedView instanceof RadioButton) {
((RadioButton) checkedView).setChecked(checked);
}
}
#IdRes
public int getCheckedRadioButtonId() {
return mCheckedId;
}
public void clearCheck() {
check(-1);
}
public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
mOnCheckedChangeListener = listener;
}
#Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(getContext(), attrs);
}
#Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
return p instanceof RadioGroup.LayoutParams;
}
#Override
protected LinearLayout.LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}
#Override
public CharSequence getAccessibilityClassName() {
return RadioGroup.class.getName();
}
public static class LayoutParams extends LinearLayout.LayoutParams {
public LayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
}
public LayoutParams(int w, int h) {
super(w, h);
}
public LayoutParams(int w, int h, float initWeight) {
super(w, h, initWeight);
}
public LayoutParams(ViewGroup.LayoutParams p) {
super(p);
}
public LayoutParams(MarginLayoutParams source) {
super(source);
}
#Override
protected void setBaseAttributes(TypedArray a,
int widthAttr, int heightAttr) {
if (a.hasValue(widthAttr)) {
width = a.getLayoutDimension(widthAttr, "layout_width");
} else {
width = WRAP_CONTENT;
}
if (a.hasValue(heightAttr)) {
height = a.getLayoutDimension(heightAttr, "layout_height");
} else {
height = WRAP_CONTENT;
}
}
}
public void onCheckedChanged(RadioGroup group, #IdRes int checkedId);
}
private class CheckedStateTracker implements CompoundButton.OnCheckedChangeListener {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// prevents from infinite recursion
if (mProtectFromCheckedChange) {
return;
}
mProtectFromCheckedChange = true;
if (mCheckedId != -1) {
setCheckedStateForView(mCheckedId, false);
}
mProtectFromCheckedChange = false;
int id = buttonView.getId();
setCheckedId(id);
}
}
private class PassThroughHierarchyChangeListener implements
ViewGroup.OnHierarchyChangeListener {
private ViewGroup.OnHierarchyChangeListener mOnHierarchyChangeListener;
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public void traverseTree(View view) {
if (view instanceof RadioButton) {
int id = view.getId();
// generates an id if it's missing
if (id == View.NO_ID) {
id = View.generateViewId();
view.setId(id);
}
((RadioButton) view).setOnCheckedChangeListener(
mChildOnCheckedChangeListener);
}
if (!(view instanceof ViewGroup)) {
return;
}
ViewGroup viewGroup = (ViewGroup) view;
if (viewGroup.getChildCount() == 0) {
return;
}
for (int i = 0; i < viewGroup.getChildCount(); i++) {
traverseTree(viewGroup.getChildAt(i));
}
}
/**
* {#inheritDoc}
*/
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public void onChildViewAdded(View parent, View child) {
traverseTree(child);
if (parent == RadioGroup.this && child instanceof RadioButton) {
int id = child.getId();
// generates an id if it's missing
if (id == View.NO_ID) {
id = View.generateViewId();
child.setId(id);
}
((RadioButton) child).setOnCheckedChangeListener(
mChildOnCheckedChangeListener);
}
if (mOnHierarchyChangeListener != null) {
mOnHierarchyChangeListener.onChildViewAdded(parent, child);
}
}
/**
* {#inheritDoc}
*/
public void onChildViewRemoved(View parent, View child) {
if (parent == RadioGroup.this && child instanceof RadioButton) {
((RadioButton) child).setOnCheckedChangeListener(null);
}
if (mOnHierarchyChangeListener != null) {
mOnHierarchyChangeListener.onChildViewRemoved(parent, child);
}
}
}
}
and add in your layout like this:
<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:padding="#dimen/activity_vertical_margin"
>
<com.example.admin.myapplication.RadioGroup
android:id="#+id/radio_group_plus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<RadioButton
android:id="#+id/rb_latte"
android:text="radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<RadioButton
android:text="radio2"
android:id="#+id/rb_latte1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#E0E0E0" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<ImageView
android:id="#+id/iv_mocha"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/iv_mocha"
android:paddingLeft="20dp"
android:text="Mocha" />
<RadioButton
android:id="#+id/rb_mocha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#E0E0E0" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<ImageView
android:id="#+id/iv_americano"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/iv_americano"
android:paddingLeft="20dp"
android:text="Americano" />
<RadioButton
android:id="#+id/rb_americano"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#E0E0E0" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<ImageView
android:id="#+id/iv_espresso"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/iv_espresso"
android:paddingLeft="20dp"
android:text="Espresso" />
<RadioButton
android:id="#+id/rb_espresso"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#E0E0E0" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1">
<ImageView
android:id="#+id/iv_orange"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/iv_orange"
android:paddingLeft="20dp"
android:text="Orange" />
<RadioButton
android:id="#+id/rb_orange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<View
android:layout_width="2dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="#E0E0E0" />
<RelativeLayout
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:paddingLeft="10dp">
<ImageView
android:id="#+id/iv_butter"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/iv_butter"
android:paddingLeft="20dp"
android:text="Butter" />
<RadioButton
android:id="#+id/rb_butter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
</LinearLayout>
</com.example.admin.myapplication.RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="#138BE8"
android:gravity="center"
android:onClick="onOrderClicked"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="Order Drink"
android:textColor="#android:color/white" />

First image appearing small in size and on top left corner in a viewpager when rotate the device

Hi in my application i am using the Viewpager with PagerAdapter. it contains n number of images. but when i rotate the device when viewpager is on first image then the image shift to top left corner and appears very small. please have a look in the attached image.
code which i am using
public class CustomViewPager extends ViewPager {
private final long SWITCH_TIME_INTERVAL = 5000;
public CustomViewPager(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public CustomViewPager(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
postDelayed(mSwither, SWITCH_TIME_INTERVAL);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int height = 0;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
if (h > height)
height = h;
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
/**
* this Runnable is use for automatically switching the view pager item.
* #author
*/
private Runnable mSwither = new Runnable() {
#Override
public void run() {
if( CustomViewPager.this.getAdapter() != null )
{
int count = CustomViewPager.this.getCurrentItem();
if( count == (CustomViewPager.this.getAdapter().getCount() - 1) )
{
count = 0;
}else
{
count++;
}
//Log.d(this.getClass().getName(), "Curent Page " + count + "");
CustomViewPager.this.setCurrentItem(count, true);
}
CustomViewPager.this.postDelayed(this, SWITCH_TIME_INTERVAL);
}
};
#Override
public boolean onTouchEvent(MotionEvent arg0) {
switch (arg0.getAction()) {
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP :
postDelayed(mSwither, SWITCH_TIME_INTERVAL);
break;
default:
removeCallbacks(mSwither);
break;
}
return super.onTouchEvent(arg0);
} }
//PagerAdapter onInstantiateItem code..
#Override
public Object instantiateItem(ViewGroup container, final int position) {
mImageFetcher.setLoadingImage(R.drawable.placeholder_tabportrait);
inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.test_fragment1, container, false);
description = (TextView) itemView.findViewById(R.id.description);
title = (TextView) itemView.findViewById(R.id.car_title);
published = (TextView) itemView.findViewById(R.id.date_data);
author = (TextView) itemView.findViewById(R.id.author);
jumbo_tron_image = (ImageView) itemView.findViewById(R.id.jumbo_imgae);
TrayItem trayItem = getData(position);
if (null != trayItem) {
if (trayItem.getType() != null) {
if (trayItem.getImages() != null) {
mImageFetcher.loadImage(trayItem.getImages().getWidget(),
jumbo_tron_image);
}
return itemView;
}
//xml code.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="500dp"
android:background="#color/white">
<FrameLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_gravity="center_horizontal|center_vertical"
android:orientation="vertical"
>
<ImageView
android:id="#+id/jumbo_imgae"
android:layout_width="match_parent"
android:scaleType="fitXY"
android:layout_height="300dp"
/>
</FrameLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="#dimen/container_height"
android:layout_below="#+id/main_container">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal"
android:id="#+id/linearLayout2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_alignParentLeft="true"
android:textStyle="bold"
android:layout_weight="0.5"
android:textColor="#color/black"
android:id="#+id/car_title"
android:maxLines="2"
android:ellipsize="end"
android:textSize="20sp"
android:text=" "/>
<ImageView
android:id="#+id/iv_add_test1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:paddingTop="10dp"
android:src="#drawable/add_icon"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/car_title"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout2"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:id="#+id/linearLayout3"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:id="#+id/date_data"
android:layout_width="90dp"
android:layout_height="18dp"
android:text=""
android:textSize="13sp"
android:layout_gravity="left"
android:gravity="center"
android:textColor="#color/white"
android:background="#drawable/date_bg"/>
<TextView
android:id="#+id/author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="15sp"
android:layout_marginLeft="10dp"
android:textColor="#color/black"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout> </RelativeLayout>

Overlying View with Opacity on top of ImageView

Currently I have a layout xml file where I bind many views. Here I am facing a problem. The problem is I can not overlay a view on an (heart image)Imageview. In layout xml file I can set but I can not overlay the view on that imageview.Please help me to figure our.I want the Black Opacity view on the Heart Imageview.
Here is the xmlsource codes. I have marked the important codes with *.
<LinearLayout
android:id="#+id/listlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="top"
android:orientation="vertical" >
<ListView
android:id="#+id/datalist"
style="#style/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
<RelativeLayout
android:id="#+id/layout2"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#F18EA4"
android:gravity="bottom" >
<TextView
android:id="#+id/income_entry_container"
style="#style/LightYellow"
android:layout_width="70dp"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="left|center"
android:text="#string/confirm_income" />
<TextView
android:id="#+id/expense"
style="#style/LightYellow"
android:layout_width="70dp"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/income_entry_container"
android:gravity="left|center"
android:text="#string/confirm_expense" />
<TextView
android:id="#+id/confirm_income_item_TextView"
style="#style/LightYellow"
android:layout_width="70dp"
android:layout_height="20dp"
android:layout_above="#+id/expense"
android:layout_toRightOf="#+id/income_entry_container"
android:gravity="right|center"
android:text="#string/confrim_income_item" />
<TextView
android:id="#+id/confirm_expense_item_TextView"
style="#style/LightYellow"
android:layout_width="70dp"
android:layout_height="20dp"
android:layout_alignLeft="#+id/confirm_income_item_TextView"
android:layout_below="#+id/confirm_income_item_TextView"
android:gravity="right|center"
android:text="#string/confirm_expense_item" />
<TextView
android:id="#+id/confirm_income_amount_TextView"
style="#style/LightYellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="right|center"
android:text="#string/confirm_income_amount" />
<TextView
android:id="#+id/confrim_expnese_amount_TextView"
style="#style/LightYellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/confirm_income_item_TextView"
android:gravity="right|center"
android:text="#string/confirm_expnese_amount" />
<ImageButton
android:id="#+id/up_down_imageButton"
style="#style/UpDownButtonSelector"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="170dp"
android:layout_marginTop="10dp"
android:contentDescription="#string/up_down_imageButton" />
<TextView
android:id="#+id/save_percentage"
style="#style/LightYellow"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_below="#+id/balance_amount_TextView"
android:layout_toRightOf="#+id/heart_ImageView"
android:gravity="left|center"
android:textSize="14sp" />
<TextView
android:id="#+id/lbl_balance"
style="#style/LightYellow"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/confrim_expnese_amount_TextView"
android:gravity="left|center"
android:text="#string/usable_amount"
android:textSize="12sp" />
**<ImageView
android:id="#+id/heart_ImageView"
style="#style/LightYellow"
android:layout_width="260dp"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/lbl_balance"
android:contentDescription="#string/heart_content" />**
<TextView
android:id="#+id/balance_amount_TextView"
style="#style/LightYellow"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_alignBaseline="#+id/lbl_balance_amount"
android:layout_alignBottom="#+id/lbl_balance_amount"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/lbl_balance_amount"
android:gravity="right|center"
android:text="#string/balance_amount"
android:textSize="14sp" />
<TextView
android:id="#+id/lbl_balance_amount"
style="#style/LightYellow"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_below="#+id/confrim_expnese_amount_TextView"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/lbl_balance"
android:gravity="right|center"
android:text="#string/lbl_balance_amount"
android:textSize="14sp" />
**<View
android:id="#+id/overlay"
android:layout_width="260dp"
android:layout_height="20dp"
android:layout_below="#+id/lbl_balance" />**
</RelativeLayout>
</RelativeLayout>
Here is the source codes. I have marked the important codes with *.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_buta_confirm, container, false);
listRelativeLayout = (RelativeLayout) v
.findViewById(R.id.fragment_list);
int top = ((ButaColleTabBarActivity) getActivity())
.getActionBarHeight() + 2;
listRelativeLayout.setPadding(0, top, 0, 120);
listLinearLayout = (LinearLayout) v.findViewById(R.id.listlayout);
listLinearLayout.setPadding(0, 0, 0, 160);
ie_adapter = new Income_Expense_Adapter(getActivity(),
android.R.layout.simple_list_item_1, butaColleList);
view = (ListView) v.findViewById(R.id.datalist);
view.setAdapter(ie_adapter);
view.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ButaColle butaColle = butaColleList.get(position);
FragmentManager fragmentManager = getFragmentManager();
ButaColleDetailIncomeExpenseFragment butaDetail = ButaColleDetailIncomeExpenseFragment
.newInstance(butaColle);
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.realTabContent, butaDetail);
fragmentTransaction.addToBackStack("SearchFragment");
fragmentTransaction.commit();
}
});
view.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
view.setMultiChoiceModeListener(new MultiChoiceModeListener() {
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
ie_adapter.removeSelection();
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.menu_delete_context, menu);
return true;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_delete_data:
SparseBooleanArray selected = ie_adapter.getSelectedIds();
for (int i = (selected.size() - 1); i >= 0; i--) {
if (selected.valueAt(i)) {
ButaColle butaitem = ie_adapter.getItem(selected
.keyAt(i));
dbhelper.deleteHistoryRecords(butaitem);
ie_adapter.remove(butaitem);
}
}
mode.finish();
return true;
default:
return false;
}
}
#Override
public void onItemCheckedStateChanged(ActionMode mode,
int position, long id, boolean checked) {
final int checkedCount = view.getCheckedItemCount();
mode.setTitle(checkedCount + " Selected");
ie_adapter.toggleSelection(position);
}
});
updownImageButton = (ImageButton) v
.findViewById(R.id.up_down_imageButton);
updownImageButton.setBackgroundResource(R.drawable.close01);
updownImageButton.setTag(R.drawable.close01);
updownImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (updownImageButton.getTag().equals((R.drawable.close01))) {
updownImageButton.setBackgroundResource(R.drawable.open01);
updownImageButton.setTag(R.drawable.open01);
int top = ((ButaColleTabBarActivity) getActivity())
.getActionBarHeight() + 2;
listRelativeLayout.setPadding(0, top, 0, 50);
listLinearLayout.setPadding(0, 0, 0, 160);
} else if (updownImageButton.getTag().equals(R.drawable.open01)) {
updownImageButton.setBackgroundResource(R.drawable.close01);
updownImageButton.setTag(R.drawable.close01);
int top = ((ButaColleTabBarActivity) getActivity())
.getActionBarHeight() + 2;
listRelativeLayout.setPadding(0, top, 0, 120);
listLinearLayout.setPadding(0, 0, 0, 160);
}
}
});
int itemCount = 0;
String itemlbl = "item";
incomeItemcount_TextView = (TextView) v
.findViewById(R.id.confirm_income_item_TextView);
itemCount = dbhelper.getItemCount(1);
if (itemCount > 1)
itemlbl = "items";
incomeItemcount_TextView.setText(String.valueOf(itemCount) + itemlbl);
itemCount = 0;
itemlbl = "item";
expenseItemcount_TextView = (TextView) v
.findViewById(R.id.confirm_expense_item_TextView);
itemCount = dbhelper.getItemCount(0);
if (itemCount > 1)
itemlbl = "items";
expenseItemcount_TextView.setText(String.valueOf(itemCount) + itemlbl);
double balance = 0.0;
double incomeAmount = 0.0;
double expenseAmount = 0.0;
incomeAmount_TextView = (TextView) v
.findViewById(R.id.confirm_income_amount_TextView);
incomeAmount = dbhelper.getTotalAmount(1);
incomeAmount_TextView.setText(String.valueOf(incomeAmount) + "$");
expenseAmount_TextView = (TextView) v
.findViewById(R.id.confrim_expnese_amount_TextView);
expenseAmount = dbhelper.getTotalAmount(0);
expenseAmount_TextView.setText(String.valueOf(expenseAmount) + "$");
balance = incomeAmount - expenseAmount;
balance_TextView = (TextView) v
.findViewById(R.id.balance_amount_TextView);
balance_TextView.setText(String.valueOf(balance) + "$");
int percentage = 0;
percentage = (int) ((balance / incomeAmount) * 100);
if (balance > 0) {
heartImageView = (ImageView) v.findViewById(R.id.heart_ImageView);
heartImageView.setImageResource(R.drawable.graph);
percentage_TextView = (TextView) v
.findViewById(R.id.save_percentage);
percentage_TextView.setText(String.valueOf(percentage) + "%");
} else {
heartImageView = (ImageView) v.findViewById(R.id.heart_ImageView);
heartImageView.setImageResource(0);
percentage_TextView = (TextView) v.findViewById(0);
percentage = 0;
}
**View overlay = (View) v.findViewById(R.id.overlay);
int opacity = 200; // from 0 to 255
overlay.setBackgroundColor(opacity * 0x1000000); // black with a variable alpha
RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(300, 36);
overlay.setLayoutParams(params);
overlay.invalidate();**
return v;
}
Please Help me to figure our.Thank you!
Try Changing
**<View
android:id="#+id/overlay"
android:layout_width="260dp"
android:layout_height="20dp"
android:layout_below="#+id/lbl_balance" />**
to
**<View
android:id="#+id/overlay"
android:layout_width="260dp"
android:layout_height="20dp"
android:layout_alignBottom="#+id/heart_ImageView" />**
Changes made: layout alignment and the id of to whom it was aligned
You can try adding rules to the params with the addRule method
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
I'm not near my PC to test, but you may be able to set the view below lbl_balance with the addRule method
Or, try removing the layout params in the code all together. The xml inflates the view, so it is already created. All you need to do in code is set the background color. Which you can also do in the xml with
android:background="#80000000"

Android expand and contract animation on Layout

I have a normal Relative Layout with text view and progress bar.
Now, since i have a fixed width and height of the layout the text is properly placed in the center and looks good, onclick of layout we are changing the visibility of progress bar to "Visible", but since i have a fixed width the progress bar is on top of the text.
What i am trying to achieve is , onclick increase the right end width of the layout along with animation.
Here is my code :
<RelativeLayout
android:id="#+id/rellyt"
android:layout_width="150dp"
android:layout_height="35dp"
android:layout_margin="5dp"
android:background="#B7E4FF"
android:clickable="true" >
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:text="click on this button"
android:textColor="#000000"
android:textSize="14sp" />
<ProgressBar
android:id="#+id/prgbar"
style="#android:style/Widget.ProgressBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="8dp"
android:visibility="visible" />
</RelativeLayout>
Screen shot of the layout :
Animation
public class ResizeWidthAnimation extends Animation
{
private int mWidth;
private int mStartWidth;
private View mView;
public ResizeWidthAnimation(View view, int width)
{
mView = view;
mWidth = width;
mStartWidth = view.getWidth();
}
#Override
protected void applyTransformation(float interpolatedTime, Transformation t)
{
int newWidth = mStartWidth + (int) ((mWidth - mStartWidth) * interpolatedTime);
mView.getLayoutParams().width = newWidth;
mView.requestLayout();
}
#Override
public void initialize(int width, int height, int parentWidth, int parentHeight)
{
super.initialize(width, height, parentWidth, parentHeight);
}
#Override
public boolean willChangeBounds()
{
return true;
}
}
Usage
if(animate)
{
ResizeWidthAnimation anim = new ResizeWidthAnimation(leftFrame, leftFragmentWidthPx);
anim.setDuration(500);
leftFrame.startAnimation(anim);
}
else
{
this.leftFragmentWidthPx = leftFragmentWidthPx;
LayoutParams lp = (LayoutParams) leftFrame.getLayoutParams();
lp.width = leftFragmentWidthPx;
leftFrame.setLayoutParams(lp);
}
Try this way,hope this will help you to solve your problem.
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<LinearLayout
android:id="#+id/customLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#B7E4FF"
android:gravity="center"
android:padding="5dp">
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click on this button"
android:textColor="#000000"
android:textSize="14sp" />
<ProgressBar
android:id="#+id/prgbar"
style="#android:style/Widget.ProgressBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:visibility="invisible" />
</LinearLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
private LinearLayout customLayout;
private ProgressBar prgbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
customLayout = (LinearLayout) findViewById(R.id.customLayout);
prgbar = (ProgressBar) findViewById(R.id.prgbar);
customLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(prgbar.getVisibility() == View.INVISIBLE){
prgbar.setVisibility(View.VISIBLE);
}else{
prgbar.setVisibility(View.INVISIBLE);
}
}
});
}
}

Categories

Resources