Relative Layout All Childs Clickable - java

This Question Is Pretty Silly to ask but out, but i Would like to know how it Works?
I had a Relative Layout with 2 ImageViews as Child having separate clickListner instances. One above Another, of same Size and Attributes.
Overlaps Each other. Both having Different images.
Question is When i click on one image both ImageView Click listners are Called.
Or if i disable the Click on ImageView Top, The ImageView Below Still Works, I was Clicking on Image View Above though. How it is I'ts Getting callback from both.
I Just Want to know How it works? not The code, i do not have any issue writing code for clickListners Whether only one Working or Both.
<RelativeLayout
----
---
>
<ImageView
---
---<!--Child 1-->
<ImageView
---
---<!--Child 2-->
<RelativeLayout/>

Taken from here:
if you are working with just one clicklistener, you can do:
View.OnClickListener myOnlyhandler = new View.OnClickListener() {
public void onClick(View v) {
switch(v.getId()) {
case R.id.b1:
// it was the first button
break;
case R.id.b2:
// it was the second button
break;
}
}
}

Use ImageButton
Layout
<?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" >
<ImageButton
android:id="#+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
Activity class
public class MainActivity extends Activity {
ImageButton imgButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addButtonListener();
}
public void addButtonListener() {
imgButton = (ImageButton) findViewById(R.id.imageButton);
imgButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"ImageButton is working!", Toast.LENGTH_SHORT).show();
}
});
}
}

Give this to your parent layout
android:context="Yourclasshere"
then give this to your image view
android:onclick="onclick"
and then implement the on click listener or make the method like Vitly A make above

Related

TextView wont redirect me to my registration activity from login

When I click on the Sign up txt on my application it crashes I've tried almost everything i could find including the XML onClick
XML clickable
onClick
public class LoginActivity extends AppCompatActivity
{
TextView sign_up_text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
sign_up_text = (TextView) findViewById(R.id.sign_up);
sign_up_text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
}
});
}
}
xml:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/sign_up_text"
android:textSize="18dp"
android:gravity="center"
android:layout_alignParentBottom="true"
android:id="#+id/sign_up"
android:clickable="true"
android:onClick="onClick"
/>
Android app crashes with no failure
You have android:onClick="onClick" attribute in your xml. And you have also define sign_up_text.setOnClickListener in java.
You can do only one thing at a time.
1.
If you want to use sign_up_text.setOnClickListener , you simply need to remove android:onClick="onClick" from xml file.(Reference)
2.
If you want to use android:onClick="onClick", you should define new method in your activity like:(Reference)
public void onClick(View view) {
}
First solution is to remove android:onClick="onClick" from your XML, according to my perception the reason of crash is the function name called on onClick of your button click define in XML is "onClick" which is also override function of setOnClickListner
#Override
public void onClick(View v){
//todo your code
}
Second Solution is if you use onClick in XMl change function name other that override function onClick like:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/sign_up_text"
android:textSize="18dp"
android:gravity="center"
android:layout_alignParentBottom="true"
android:id="#+id/sign_up"
android:clickable="true"
android:onClick="myFunction"
/>
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
private myFunction(View view){
}

2 (or more) button views for the same onClick method

I'm a novice android developper and was wondering:
Could I have 2 buttons to be linked into the same, 1 onClick method (which i'll presumably override to accept 2 extra parameter, int btnId and View targetTextView for instance) in order to decide which button is calling the method and then which TextView text to update?
For Example:
btn1 will update the text on text_view_1
and btn2 will update text_view_2.
Except they we will be linked to the same method:
public void generalOnClick(View view, String btnId, String textViewId){...}
<?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">
<TextView
android:id="#+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="One"
android:onClick="btnClick"/>
<TextView
android:id="#+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Two"
android:onClick="btnClick"/>
</LinearLayout>
Button Click Function in your Activity
public void btnClick(View view) {
TextView tv = (TextView)view;
int id = tv.getId();
if(id==R.id.one) {
tv.setText("One Clicked");
} else if(id==R.id.two){
tv.setText("Two Clicked");
}
}
set an tag to button and verify it with onClick method that which buttons click event has been triggered , if it doesn't work then follows following trick,
define an common method for the functionality which you are going to execute on button click, make it as common function.
define independent onclick event for both button and then while calling the common function which created above pass some unique param and verify it.
use following code:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View.OnClickListener clickListener = new View.OnClickListener() {
public void onClick(View v) {
if(v.getId().Equals(btn1.getId())){
//do your work here
}
else{
//work for 2nd button
}
};
btn1 = (Button)findViewById(R.id.btn_1);
btn2 = (Button)findViewById(R.id.btn_2);
btn1.setOnClickListener(clickListener);
btn2.setOnClickListener(clickListener);
}

Android Studio: How to change the Background to jpg. images using OnclickListener

So far I know how to change the Background's color via html codes. Now I am Trying to change the background of the Layout of my main activity to different images using a button click. If possible using only one single button.
Thank you!
The following code is from this link Setting background image in java from Omi0301.
//assuming your Layout is named linearlayout1:
LinearLayout ll = (LinearLayout) findViewById(R.id.linearlayout1);
ll.setBackgroundResource(R.drawable.sample);
All you do is create a layout variable and set its background resource with the image that you would like it to be.
try to use "setImageResource" instead the "setBackgroundResourse"
Try below code
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/myLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="64dp"
android:layout_marginTop="71dp"
android:text="changeColor" />
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
/** Called when the activity is first created. */
Button button;
LinearLayout mainLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainLayout=(LinearLayout)findViewById(R.id.myLayout);
button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
mainLayout.setBackgroundResource(R.drawable.newImage);
}
});
}
}

How to make buttons to appear and disappear on the Onclick event

I have 2 button invite and share, if i click on invite linearlayout bar1 will appear which contains 4 imageviews, and for share button also same linearlayout bar2 within that 4 imageview options, if i click on invite and share button both the layout bar appear, but for me when i click on invite or share only one corresponding bar should appear at a time...
if I understand you correctly something like this will do the trick:
invite.setOnClickListener(new OnClickListener(){
public void onClick(View v){
linearlayoutbar1.setVisibility(View.VISIBLE);
linearlayoutbar2.setVisibility(View.GONE);
}
});
share.setOnClickListener(new OnClickListener(){
public void onClick(View v){
linearlayoutbar2.setVisibility(View.VISIBLE);
linearlayoutbar1.setVisibility(View.GONE);
}
});
Insert LinearyLayout upon your requirements
<merge>
<LinearLayout
android:id="#+id/main"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:visibility="gone"
/>
<LinearLayout
android:id="#+id/sub"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:visibility="gone"
/>
</merge>
depending upon your invite and share button you can put these code invite.setOnClickListener() or share.setOnClickListener()
Insert Visibility of LinearLayout according to your Logic
LinearLayout mainLayout=(LinearLayout)this.findViewById(R.id.main);
LinearLayout subLayout=(LinearLayout)this.findViewById(R.id.sub);
invite.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
mainLayout.setVisibility(View.VISIBLE);
}
});
share.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
subLayout.setVisibility(View.VISIBLE);
}
});

Android app and java activities, issues with onCreate() method and XML

I am trying use the onClick() function in an activity for an android app. So far I have:
public class Activity2 extends Activity implements OnClickListener {
private ImageButton closeButton;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
Button myButton = (Button) findViewById(R.id.wowButton);
myButton.setOnClickListener(this);
this.closeButton = (ImageButton)this.findViewById(R.id.close);
this.closeButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
finish();
}
});
}
public void onClick(View v) {
// TODO Auto-generated method stub
TextView lowerText = (TextView) findViewById(R.id.textView2);
EditText boxText = (EditText) findViewById(R.id.editText1);
lowerText.setText(boxText.getText());
}
}
This is just a stripped down version of my project. When I enter my app as usual in the emualtor, everything works fine. When I click the button to open up this particular activity, everything crashes. I assume the issue lies within the onClick() method. This code was sent to me by my project partner without the XML files so I think thats where the problem lies.
here is my basic XML file that I thought should work:
<?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">
<Button android:id="#+id/wowButton" android:layout_width="fill_parent"
android:layout_height="200dp" android:text="WoW" android:typeface="sans"
android:background="#drawable/btn_default_normal_red" />
<EditText
android:id="#+id/editText1"
/>
<TextView
android:id="#+id/textView2"
/>
</LinearLayout>
While very basic, I though it should work. Thanks for your help.
you instantiate an ImageButton through this.closeButton = (ImageButton)this.findViewById(R.id.close);
But you don't declare it in the xml file. That's why it's null and throws a NullPointer Exception.

Categories

Resources