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);
Related
I want to know how to get the same activity to show 2 layouts. I explain, I have an activity with a login, and within this same are 2 buttons (one called "login" and another "SingUp") the idea is that when you click on one of these 2 shows a different layout for each button, but without having to change all activity in one. I achieved it partially by adding the SetContentView to the Onclic of each button, and it works but only the first time, without the activity it starts with the login layout and then I click on the singUp button, if it changes layout but if later I want to return to the login layout I can not anymore I have to restart the app.What I want to achieve is to change layout in the same activity using buttons, whatever method they mention or they help me to know which part of my code is wrong :(Thank you.
public class MainActivity extends AppCompatActivity {
private Button btnLogin,btnSignup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnLogin=(Button)findViewById(R.id.btnLogin);
btnSignup=(Button)findViewById(R.id.btnSignup);
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.activity_main);
}
});
btnSignup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.activity_sign_up);
}
});
}
}
with the previous code and managed to alternate between 2 layouts in the same activity, but the buttons only work the first time if I try to go to a layout by pressing a button for the second time, this simply does not work until I restart the app.
Option 1
Try recreating the Activity. Save which view to load then recreate.
private Button btnLogin,btnSignup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(preference.getInt(ACTIVITY)==1)
setContentView(R.layout.activity_main);
else
setContentView(R.layout.activity_sign_up);
btnLogin=(Button)findViewById(R.id.btnLogin);
btnSignup=(Button)findViewById(R.id.btnSignup);
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
preference.setInt(ACTIVITY, 1);
recreate();
}
});
btnSignup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
preference.setInt(ACTIVITY, 2);
recreate();
}
});
}
Option 2
Use fragments. Create 2 fragments and inflate them into activity on onclick of button
Option 3
Use VISIBLE/GONE with predefined layouts inside activity_main
Create one layout with two separate views that match_parent's height and width.
Hide / show each one when switching views
<LinearLayout
android:id="#+id/parent"
android:width="match_parent"
android:height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/main"
android:width="match_parent"
android:height="match_parent"
android:orientation="vertical"
android:visibility="visibile">
<!-- Add child views here -->
</LinearLayout>
<LinearLayout
android:id="#+id/sign_up"
android:width="match_parent"
android:height="match_parent"
android:orientation="vertical"
android:visibility="gone">
<!-- Add child views here -->
</LinearLayout>
</LinearLayout>
Then programmatically...
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mainView.setVisibility(View.VISIBLE);
loginView.setVisibility(View.GONE);
}
});
btnSignup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mainView.setVisibility(View.GONE);
loginView.setVisibility(View.VISIBLE);
}
});
You can approach it in other ways using Fragments, which one could argue that's the more appropriate way.
Switch between two fragments
I have a simple LinearLayout that consists of a TextView and an EditText. The behaviour that I'd like to achieve is to be able to click on the EditText and handle it like normal, but treat the encompassing LinearLayout as a button that launches a new activity.
So for example, if the user clicks the space around the button in the view, a new activity is launched. If the user clicks on the EditText, then the keyboard appears and the user can populate the EditText.
Here is the simple onClickListener for the layout, which simply states that it has been clicked:
LinearLayout test = (LinearLayout)findViewById(R.id.linearLayout1);
test.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
System.out.println("layout clicked");
}
});
And the EditText has an OnFocusChangeListener that will simply state when it has gotten focus:
#Override
public void onFocusChange(View v, boolean hasFocus) {
System.out.println("EditText clicked");
}
Results:
-When the user clicks on the layout, the result "layout clicked" is correct
-When the user clicks on the edittext, the result is "layout clicked" followed by "EditText clicked", which is not correct. I'd like to ignore the linear layout's onClick event for this case.
Try creating a FrameLayout that contains a LinearLayout containing the TextView and place the EditText above the LinearLayout. This way you will not need to change anything about the listeners.
So it would be like this:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</LinearLayout>
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="200dp" />
</FrameLayout>
Note: use margins to adjust the position of the EditText
I guess something like this should work, although it isn't the cleanest solution.
Declare a runnable that should be executed when the layout is clicked.
final Handler handler = new Handler();
Runnable layoutPressed = new Runnable() {
public void run() {
// LAYOUT CLICKED
}
};
Then start this runnable in your layout onClickListener.
LinearLayout test = (LinearLayout)findViewById(R.id.linearLayout1);
test.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
handler.postDelayed(layoutPressed, 100);
}
});
Cancel the runnable in onFocusChange.
#Override
public void onFocusChange(View v, boolean hasFocus) {
handler.removeCallbacks(layoutPressed);
// EditText clicked
}
So whenever you click the editText, the onClick of your layout will get called first, but the actions will get cancelled when onFocusChange of the editText is called.
When you click the layout, onClick will get called and will execute its actions with a 100 msec delay.
You might have to modify the delay of the runnable.
I have a button which is called Check, I want it to be invisible and visible as I click each time on it, as If its visible and I clicked it will become invisible and verse vies !
But my code doesn't work ! any ideas ?
Button Check ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
Check = (Button)findViewById(R.id.checkButton);
Check.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View View) {
if (View.getVisibility() == android.view.View.VISIBLE)
View.setVisibility(android.view.View.INVISIBLE);
else if (View.getVisibility() == android.view.View.INVISIBLE)
View.setVisibility(android.view.View.VISIBLE);
}
});
In my activity its visible at the beginning and when I click on it, it become invisible, BUT when I click again it stays invisible !
Change your code to this,
Check.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (v.isShown())
v.setVisibility(View.INVISIBLE);
else
v.setVisibility(View.VISIBLE);
}
But i think problem is, when button goes invisible, you are not getting any click event on it. First make sure that onClick method get call when button is invisible.
An invisible button will not dispatch any interaction event. So instead of setting button's visibility to the invisible, you can set a transparent or blank background or something like that.
But i personally believe, you should change your use-case because why one will click on the invisible button.
Try This:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="abcd" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:onClick="abc"
android:text="Button" />
</LinearLayout>
public void abc(View v) {
v.setVisibility(View.INVISIBLE);
}
public void abcd(View v) {
v.findViewById(R.id.button1).setVisibility(View.VISIBLE);
}
Invisible Items don't receive on-click event. So the only way you can receive a click on invisible is by receiving on some other view in place of the invisible view. The above solution wraps the button in a layout, so when button is invisible the on-click is passed on to layout, which handles the event and do accordingly. If you have a high usage of such layout you can also create a custom button with above mechanism.
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
}
});
Can anyone tell me what's wrong with this implementation? All I want to do here is have two overlapping views that swap places when you tap the screen. Unless I'm just using it wrong, View.bringToFront() does nothing?
Below is all the code in my app. Note that I added padding to the 'backView' just to make sure the two were actually overlapping. Indeed I could see both on the screen. While tapping the top view does indeed trigger the onClick method, nothing visibly changes in response to the calls to bringToFront.
public class MainActivity extends Activity implements OnClickListener {
private ImageView frontView;
private ImageView backView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
frontView = (ImageView) findViewById(com.example.R.id.FrontView);
backView = (ImageView) findViewById(com.example.R.id.BackView);
frontView.setOnClickListener(this);
backView.setOnClickListener(this);
backView.setPadding(10,0,0,0);
}
private boolean flag;
public void onClick(View v) {
if (!flag) {
backView.bringToFront();
}
else {
frontView.bringToFront();
}
flag = !flag;
}
}
and the corresponding layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/FrontView"
android:src="#drawable/front"
/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/BackView"
android:src="#drawable/back"
/>
</RelativeLayout>
Maybe it's the layout I'm using? I'm not sure... I've tried FrameLayout and LinearLayout as well.
I would try swapping content views instead of ImageViews.
Put each imageView in a different layout and then it is easy:
public void onClick(View v) {
if (!flag) {
setContentView(R.layout.main_front);
frontView = (ImageView) findViewById(com.example.R.id.FrontView);
frontView.setOnClickListener(this);
}
else {
setContentView(R.layout.main_back);
backView = (ImageView) findViewById(com.example.R.id.BackView);
backView.setOnClickListener(this);
}
flag = !flag;
}
There are a couple of Components that you can use that do this for you.
ViewAnimator, ViewFlipper and ViewSwitcher. You can set the animations you require etc and they hand the rest.
here's one example.
http://www.androidpeople.com/android-viewflipper-example/
Given your example, do you have to call invalidate() on the parent after you've called bringToFront() ?