Android: Enable click event on empty activity area - android

i set my activity theme to translucent so as to see through to the underneath activity window.
I want to know if it's possible to enable click event when user tap on empty area on this translucent activity?
Thanks,
dara kok

It is possible to add click event to your activity. You need to do as below:
You could have done setContentView(R.layout.main); in onCreate() of your activity.
In main.xml, give some id to the root layout. For eg.,
Lets consider you have root as LinearLayout with id set as below,
Then in onCreate() of your activity, you will have to do the following:
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
layout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});

Overriding this would work:
http://developer.android.com/reference/android/app/Activity.html#onTouchEvent(android.view.MotionEvent)
However i think it's your translucent activity that will get the taps, not the one visible under it.

You can add OnClickListener on parent view of your layout.
For example, add android:id="#+id/some_id" to your parent LinearLayout in main.xml.
Then add this code after setContentView in onCreate method:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.some_id);
FrameLayout frameLayout = (FrameLayout) linearLayout.getParent(); // Get parent FrameLayout
frameLayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed(); // Close activity, for example
}
});
linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// empty block for prevent frameLayout click event, if you need
}
});

Related

Android: Handle Onclick Listerner in included layout [duplicate]

I have two java class and two layout for both the class.
Each layout is having one button in it.
Both classes are extending Activity.
Now in first layout I used include tag like this
<include
android:id="#+id/clicked"
layout="#layout/activity_main" />
I can now see two buttons but the second button is not working.
First You have to declare and initialise the include view and then decalre and initialise both buttons using view.findViewById() method as follows:
View includeView = (View)findViewById(R.id.clicked);
Button button1 = (Button)includeView.findViewById(R.id.button1ID); //decalre button like this
Button button2 = (Button)includeView.findViewById(R.id.button2ID);
And then set their onClickListeners
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//code whatever you want to do here
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//code whatever you want to do here
}
});
** EDIT **
Fixed the typo. Should be includeView on the findViewById.
Good explanation though!

to add extra <Edittext> views on click of button

I used the following code but the editview layout appearing is in the bottom and i want it to get displayed on the top.
If anybody can please tell me what to do so that it get displayed on the top
private OnClickListener OnClick() {
// TODO Auto-generated method stub
return new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText t = new EditText(getApplicationContext());
t.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
mLayout.addView(t);
}
};
}
mLayout.addView(t) will add the new view at the end of the layout. Try to use mLayout.addView(t, 0) to add it as the first view in the layout.
You should add the EditText in the layout by default, but with it's visibility set to android:visibility="gone". This will cause the EditText to be present, but not visible. The rest of your layout will adjust for the View not being there.
Then in your onClick change the visibility on the EditText to visible. This will cause your UI to update with the now visible EditText.
mLayout.addView(t, 0);
where 0 indicates the position within the layout.
See this.

I want to start another activity when it is clicked?

Well I have an activity which contains nothing but a relative layout with brown background,
and I want to start another activity if the users clicks anywhere on the screen, how would i do that ??
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mainscreen);
}
First, give ID to your RelativeLayout by putting android:id="#+id/relativeLayout" in your layout file then.
RelativeLayout l = (RelativeLayout) findViewById(R.id.relativeLayout1);
l.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// startActivity here
}
});
or without using your RelativeLayout just implement the onTouchEvent(MotionEvent) of activity
public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_UP)
{
// start activity
}
return super.onTouchEvent(event);
}
Note: Not yet tried this code.
Add onClick attribute to your layout xml, and implement onClick method in your activity to start a new activity.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="onClick"
android:orientation="vertical" >
In your activity add an onClick method.
public void OnClick(View v) {
startActivity(new Intent(this,MyNewActivity.class));
}
I think your Mainscreen has Parent layout either Linear,Relative,Frame. Take reference of that and handle click listener.
Ex:
If your Parent layout is Linear.
LinearLayout linear=(LinearLayout)findViewById(R.id.linear);
linear.setOnClickListener(this);

Button state across different layouts in one activity?

I have 2 buttons to switch between 2 layouts in the same activity: clicking button1 on layout1, it goes to layout 2 (using setContentView). On layout2, clicking button2, it goes back to layout1. Then button1 is no longer responding OnClickListener. I looked into "Input Events" but still couldn't figure it out. What happened and how to fix it?
Thanks in advance!
Button submitBtn;
Button backBtn;
submitBtn = (Button)findViewById(R.id.button1); //on layout1
backButn = (Button)findViewById(R.id.button2); //on layout2
submitBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.layout2);
}
});
backBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.layout1);
}
});
You should re-assign listeners when you switch layout, cause when you're calling setContentView old view is destroyed, and new components are created.
You MUST set the contentview when it is time to change the layout, otherwise the views will be null.

passing click event to underlying view

I have a Layout with a TextView. The TextView has
android:autoLink="all"
How can I achieve the following:
if user clicks a link, an action associated with that link is executed (i.e., click on phone number invokes dialer, etc.)
if user clicks anywhere else within Layout boundaries, Layout's onClick is called.
Thanks.
Attach an oclicklistener to the TextBox layout, and handle it from there.
TextView txt = (TextView) findViewById(R.id.yourTextBoxId));
txt.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// USer Clicked the textBox
}
});
The same apply for the Layout, find it and ...
layout.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// USer Clicked the layout
}
});
I hope it helps.

Categories

Resources