Android: How can I disable touchability? - android

I'm using android 2.0. I have a view (just a sipmle linearlayout with an image as background), which contains 3 imagebuttons. When I touch any part of this view, my buttons are not touchable any more. As they are disabled! How can I disable the touchability of the background? I just want to be able to touch the buttons even if I put my finger on the other parts of the view. Is that possible?
The code sample:
public class myView extends LinearView {
public myView(Context context) {
super(context);
}
public setView(Context context) {
setBackgroundDrawable(backImg);
setOrientation(LinearLayout.HORIZONTAL);
setGravity(Gravity.CENTER);
setScrollContainer(false);
ImageButton imgBtn0 = new ImageButton(context);
imgBtn0.setBackgroundColor(Color.TRANSPARENT);
imgBtn0.setBackgroundDrawable(imgBtnD0);
imgBtn0.setDrawingCacheEnabled(true);
imgBtn0.setOnTouchListener(new TouchListener(context, id0));
...
addView(imgBtn0, 0);
addView(imgBtn1, 1);
addView(imgBtn2, 2);
}
}

you can disable the touch of the linearlayout using android:clicable="false" but how will you figure out which image button to be clicked if the user clicks in the background?

Related

How can I style Android tabs to get 3d look?

I want to create an Android tab view to look like this image:
I guess there are many ways to Rome, but I think I still haven't found the ideal one. My idea was to cut out a divider and an active divider and place them between the buttons. However, I don't know if this would be such a good solution because I still would need different styling for the first and last button. I already have a 9 patch for the surrounding (grey) container.
I've also thought about making a red 9 patch for the red bar, and than just style the selected button. The problem with this solution is that I'd still have to place the top diagonal white lines according to the number of buttons.
Does anyone have a better solution for me?
Here's another approach: to separate the header from the tabs. A bit complicated, yes, but the benefits are:
It allows you to define common tabs style;
Supports any number of buttons.
On this picture the buttons are of different width, so in reality an additional ImageView may be needed to the left of the header.
Let's create our header view as a LinearLayout. We can put upper dividers and stretchable gaps with the same layout_weight.
public class HeaderLayout extends LinearLayout {
public HeaderLayout(Context context) {
super(context);
initView();
}
public HeaderLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public void setNumberOfColumns(int number) {
removeAllViews();
for (int i = 0; i < number; i++) {
addView(getColumnView(), getColumnLayoutParams());
// We don't need a divider after the last item
if (i < number - 1) {
addView(getDividerView(), getDividerLayoutParams());
}
}
}
private void initView() {
setBackgroundResource(R.drawable.header_bg);
}
private View getColumnView() {
return new View(getContext());
}
private View getDividerView() {
ImageView dividerView = new ImageView(getContext());
dividerView.setImageResource(R.drawable.header_divider);
dividerView.setScaleType(ImageView.ScaleType.FIT_XY);
return dividerView;
}
private LayoutParams getColumnLayoutParams() {
return new LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f);
}
private LayoutParams getDividerLayoutParams() {
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
}
}
Where R.drawable.header_bg is a 9patch:
And R.drawable.header_divider is a simple (optionally transparent) bitmap:
For me personally, making different background for the first and the last button is the least difficult solution, but it depends on the actual task.

Set onClickListener for custom view

I have created a custom view called Cell that draws a square. In my activity I create a 2d array of cells, hence giving a grid structure. Now the reason for this is, I want individual cells to respond to my clicks based on some value that they have. Say for eg, each cell has a boolean, and based on true of false I will color the cell.I tried doing this with one cell first. But the strange part is, the click event is triggered even when I click outside the cell.
More Information : I am creating all view in the onCreate method of the activity.
Let me know if you need more information.
Thanks in advance for your help !
Thought I will edit my original question for other references :) .
Activity
onCreate() {
super.onCreate(savedInstanceState);
LinearLayout masterLayout = new LinearLayout(this);
LinearLayout.LayoutParams params;
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
masterLayout.setLayoutParams(params);
addCells();
setContentView(masterLayout);
}
function addCells() {
for(int i =0; i<2;i++) {
Cell cell = new Cell(this,i);
LinearLayout.LayoutParams viewParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
cell.setLayoutParams(viewParams);
masterLayout.addView(cell);
cell.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Do Something here
}
});}}
Custom View :
Cell extends View {
boolean flag=false;
int cellNumber;
float xCoordinate=50;
float yCoordinate=50;
Cell(Context context, AttributeSet attrs) {
super(context, attrs);
}
Cell (Context context, int i) {
super(context);
cellNumber = i;
}
onDraw(Canvas c) {
paint.setColor(Color.RED);
paint.setStrokeWidth(3);
paint.setStyle(Paint.Style.STROKE);
xCoordinate = xCoordinate + 40*cellNumber;
c.drawRect(xCoordinate,yCoordinate,xCoordinate+40,yCoordinate+40, paint);
}}
This is a much simpler version that I was trying out. Now the strange part is, though I am adding two different instances of my custom view, the onDraw() is called just once (on exiting the onCreate() of the activity). From what I read, the onDraw() is called for every new view render. Please enlighten me on that front !
Thanks a lot!
I suppose you are not setting the onClickListener to the proper layout. Its some how being set to the parent of the element you want. Please share your code for us to zero in on the exact problem.
the click event is triggered even when I click outside the cell.
Probably you somehow forward the touch events to your cell, at least it is receiving them. In the onTouchEvent() method of your view is decided whether it is a click (and fire the click listener) or long click or drag, etc.
However, please post some code if this didn't help you identify the problem.

Can I do addView in onDraw of MyLayout?

I am new for android. I want to add image button in onDraw. I wonder if I can do like this. I don't have compile error. But simulator says, "Unfortunately MyApp has stopped."
My second question is how can I add button at x, y location in screen?
There is no location parameters in addView.
protected class MyLayout extends LinearLayout {
ImageButton button;
public MyLayout(Context context) {
super(context);
setWillNotDraw(false);
button = new ImageButton(context);
button.setImageBitmap(buttonBitmap); // buttonBitmap is loaded in onCreate
}
public void onDraw(Canvas canvas) {
addView(button);
}
}
I would not be adding a button to a custom linearlayout in the onDraw() override function. onDraw is called periodically (for example when the element is resized). So you will be adding the same button EACH time onDraw() is called. And you will get an error if you add the same item more than once.
What exactly are you trying to do? If you want to just add a button to a linear layout, you can do so without extending a layout.

How do I position ZoomButtonsController in custom ImageView?

Layouting in Android is getting me rather perplexed.
I'm slowly implementing a custom ImageView where I'd like to make use of the ZoomButtonsController.
However, I would like to decide where the zoom buttons go in the layout and I can't figure out how to move them from the default bottom center position.
I have been experimenting with layouting simple views such as buttons in the main activity and this seems to be working as I would guess and expect.
In the case of the ZoomButtonsController I would however like to reposition them. I'm using a RelativeLayout as the mail layout and add the ZoomButtonsController within the custom ImageView.
The Activity code
public class ImageViewActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
CustomImageView imageView = new CustomImageView(this);
relativeLayout.addView(imageView);
}
}
The CustomImageView code
public class CustomImageView extends ImageView {
private ZoomButtonsController mZoomButtons;
public CustomImageView(Context context) {
super(context);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
mZoomButtons = new ZoomButtonsController(this);
mZoomButtons.getZoomControls();
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
setLayoutParams(layoutParams);
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
Log.d("TAG", "touch");
mZoomButtons.setVisible(true);
return true;
}
}
I've tested with WRAP_CONTENT in the parameters, but this only makes the zoom buttons disappear.
As a matter of fact, I couldn't position the ZoomButtonsController in any way and in the end had to accept the default placement.

android Create a button with something above it

I wanna create a button class which extends Button.And when I click my custom button ,it will show a circle shape above it and then the circle shape dispears.The code likes:
public class MyButton extends Button {
public GlowButton(Context context) {
super(context);
}
public GlowButton(Context context,AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean performClick() {
//Add the code for show the shape
return super.performClick();
}
}
Is this possible ? And How can i create the shape above the button?
thanks in advance!!
I believe what you can do is load an image from a drawable, where your shape has already been created. Put the shape above the button in the layout (XML) file using an imageview, and then change the visibility of that imageview to invisible on default. When the button is clicked, change the visibility to visible.

Categories

Resources