onClick Method not working Android - android

I have a LandingPage with two Buttons, each pointing to a different activity.. None of the onClick Methods is functioning.. I know this is supposed to be really basic stuff but I cannot find the solution..
My code is the following:
public class LandingPage extends AppCompatActivity {
Button log_in, sign_up;
Typeface tfc_button;
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_landing_page);
log_in = (Button) findViewById(R.id.LogIn_Button);
sign_up = (Button) findViewById(R.id.SignUp_Button);
setFontType();
addSoundtoButtons();
}
public void addSoundtoButtons(){
//Add Sound to the Buttons
final MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.button_click_sound);
log_in.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mediaPlayer.start();
}
});
sign_up.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mediaPlayer.start();
}
});
}
public void setFontType(){
//Set Font Type for Buttons
tfc_button = Typeface.createFromAsset(getAssets(), "fonts/TEMPSITC.TTF");
log_in = (Button) findViewById(R.id.LogIn_Button);
sign_up = (Button) findViewById(R.id.SignUp_Button);
log_in.setTypeface(tfc_button);
sign_up.setTypeface(tfc_button);
}
public void OnClickButtonLogin(View view){
Intent intent = new Intent(this, SignInPage.class);
startActivity(intent);}
public void OnClickButtonSignUp(View view){
Intent intent = new Intent(this, SignUpPage.class);
startActivity(intent);
}
}
My Activity Layout is the Following:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:background="#drawable/landingpagenormal"
tools:context="com.example.gebruiker.prototype1oneplayerpurevisualisation.Activities.LandingPage">
<Button
android:id="#+id/LogIn_Button"
android:layout_width="171dp"
android:layout_height="57dp"
android:layout_marginBottom="17dp"
android:layout_marginStart="18dp"
android:background="#drawable/login_button"
android:onClick="OnClickButtonLogin"
android:text="Log In"
android:textColor="#FFEAEA"
android:textSize="35sp"
app:layout_constraintBottom_toTopOf="#+id/SignUp_Button"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/SignUp_Button"
android:layout_width="171dp"
android:layout_height="57dp"
android:onClick="OnClickButtonSignUp"
android:layout_marginBottom="38dp"
android:layout_marginStart="18dp"
android:background="#drawable/signup_button"
android:text="Sign Up"
android:textColor="#FFEAEA"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
The Layout of the Landscape Mode of my Activity is the following:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:background="#drawable/landingpagelandscape"
tools:context="com.example.gebruiker.prototype1oneplayerpurevisualisation.Activities.LandingPage">
<Button
android:id="#+id/LogIn_Button"
android:layout_width="171dp"
android:layout_height="57dp"
android:layout_marginBottom="25dp"
android:layout_marginStart="100dp"
android:background="#drawable/login_button"
android:onClick="OnClickButtonLogin"
android:text="Log In"
android:textColor="#FFEAEA"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/SignUp_Button"
android:layout_width="171dp"
android:layout_height="57dp"
android:layout_marginBottom="25dp"
android:onClick="OnClickButtonSignUp"
android:layout_marginEnd="100dp"
android:background="#drawable/signup_button"
android:text="Sign Up"
android:textColor="#FFEAEA"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>

Ok, so, you have two conflicting things. First, the onClick attribute in your layout:
android:onClick="OnClickButtonLogin"
Second, the setOnClickListener() in your Activity:
log_in.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mediaPlayer.start();
}
});
The second one will override the first. You should still see/hear the effects of mediaPlayer.start(), but you won't ever get a call to OnClickButtonLogin(). If you wanted to have both, you could remove the setOnClickListener() call and just add mediaPlayer.start() to the OnClickButtonLogin() method:
public void OnClickButtonLogin(View view) {
mediaPlayer.start();
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}

Related

What function should i use to open the activities of each button on android studio?

I'm having small problem on my project. I can't open the activities of each button after clicking it. I don't know if the codes I used are correct. please help me fix it. Thank you.
activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity2"
android:background="#drawable/backgroundimg">
<Button
android:id="#+id/button"
android:layout_width="270dp"
android:layout_height="60dp"
android:backgroundTint="#android:color/holo_blue_dark"
android:text="Sleep"
android:textAllCaps="false"
android:textColor="#FFFFFF"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.299" />
<Button
android:id="#+id/button1"
android:layout_width="270dp"
android:layout_height="60dp"
android:backgroundTint="#android:color/holo_blue_dark"
android:text="Stress Reliever"
android:textAllCaps="false"
android:textColor="#FFFFFF"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.433" />
<Button
android:id="#+id/button2"
android:layout_width="270dp"
android:layout_height="60dp"
android:backgroundTint="#android:color/holo_blue_dark"
android:text="Calm"
android:textAllCaps="false"
android:textColor="#FFFFFF"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.57" />
<Button
android:id="#+id/button3"
android:layout_width="270dp"
android:layout_height="60dp"
android:backgroundTint="#android:color/holo_blue_dark"
android:text="Motivational Quotes"
android:textAllCaps="false"
android:textColor="#FFFFFF"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.701" />
<TextView
android:id="#+id/goodday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Good Day,"
android:textColor="#color/white"
android:textSize="20pt"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.078"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.04" />
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Melana!"
android:textColor="#color/white"
android:textSize="17pt"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.059"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.112" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity2.java
package com.example.serenityapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity2 extends AppCompatActivity {
Button butt1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
butt1=(Button) findViewById(R.id.button);
butt1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
setContentView(R.layout.activity_stress);
}
});
}
}
This is what the app looks like and the four buttons should open their respective activities
This is just an example. As someone answered here, you could just search how to start a new activity.
butt1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Intent navigateToAnotherActivity = new Intent(this, SleepActivity.class);
startActivity(navigateToAnotherActivity);
}
});
to navigate from one activity to another intent is used
Intent intent = new Intent(this, TargetActivity.class)
startActivity(intent)
As I can see in the screenshot you provided. You have created 4 different activities. You just need to open them using the below code.
This code goes inside the onCreate() method of MainActivity2
public class MainActivity2 extends AppCompatActivity {
private Button bt_Sleep, bt_Stress_Reliever, bt_Calm, bt_Motivational_Quotes;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
bt_Sleep = findViewById(R.id.button);
bt_Stress_Reliever = findViewById(R.id.button1);
bt_Calm = findViewById(R.id.button2);
bt_Motivational_Quotes = findViewById(R.id.button3);
bt_Sleep.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent sleep = new Intent(MainActivity2.this, sleep.class);
startActivity(sleep);
}
});
bt_Stress_Reliever.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent stressReliever = new Intent(MainActivity2.this, stress.class);
startActivity(stressReliever);
}
});
bt_Calm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//calm change with your actual activity name
Intent calmIntent = new Intent(MainActivity2.this, calm.class);
startActivity(calmIntent);
}
});
bt_Motivational_Quotes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//motivational_quotes change with your actual activity name
Intent mq = new Intent(MainActivity2.this, motivational_quotes.class);
startActivity(mq);
}
});
}
}
This code should work.
Still facing problem comment here.

very slow performance when i click a button on my android app

when i testing it on my mobile (huawei G8) in vertical view it takes like 5 second to click on any of this 4 buttons in my app
also it leave dark spot in the button for second or 2
in horizontal view it works faster
in emulator it also works fine
java file
package com.example.kemo.videoplayer;
import android.content.Context;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
import android.widget.Button;
import android.widget.ImageButton;
public class Home extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
final ImageButton facebook1 = (ImageButton) findViewById(R.id.imageButton);
facebook1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// your handler code here image 1
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com"));
startActivity(browserIntent);
}
});
final ImageButton youtube1 = (ImageButton) findViewById(R.id.imageButton2);
youtube1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// your handler code here image 1
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com"));
startActivity(browserIntent);
}
});
final Button button = (Button) findViewById(R.id.btnabout);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// your handler code here
Intent intent = new Intent(getBaseContext(), About.class);
startActivity(intent);
}
});
final Button button1 = (Button) findViewById(R.id.btnaikido);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// your handler code here
Intent intent = new Intent(getBaseContext(), Aikido.class);
startActivity(intent);
}
});
final Button button2 = (Button) findViewById(R.id.btnv);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// your handler code here
Intent intent = new Intent(getBaseContext(), MainActivity.class);
startActivity(intent);
}
});
final Button button3 = (Button) findViewById(R.id.btnpic);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// your handler code here
Intent intent = new Intent(getBaseContext(), Gview.class);
startActivity(intent);
}
});
}
}
XML file
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<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:id="#+id/activity_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="com.example.kemo.videoplayer.Home">
<ImageView
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/imageView3"
android:layout_height="140dp"
android:scaleType="fitXY"
android:background="#drawable/aikidobanner" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnabout"
android:layout_margin="5dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/btnaikido"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="#string/aikido" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnaikido"
android:layout_margin="5dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/btnv"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="#string/videos" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnv"
android:layout_margin="5dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/btnpic"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="#string/gallery" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/btnabout"
android:text="#string/about"
android:layout_below="#+id/imageView3"
android:layout_margin="5dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:id="#+id/imageButton"
app:srcCompat="#drawable/facebook"
android:layout_width="120dp"
android:layout_height="60dp"
android:scaleType="fitXY"
android:layout_marginTop="10dp"
android:layout_marginRight="25dp"
android:layout_marginLeft="50dp"
android:background="#android:color/transparent" />
<ImageButton
android:id="#+id/imageButton2"
app:srcCompat="#drawable/youtube"
android:layout_width="120dp"
android:layout_height="60dp"
android:scaleType="fitXY"
android:layout_marginTop="10dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="50dp"
android:background="#android:color/transparent" />
</LinearLayout>
</LinearLayout>
</ScrollView>
It's hard to know for sure without seeing the rest of the app, but this sounds like the new Activities that you are launching are probably doing too much work in their onCreate methods, or doing something heavy-weight in some other method on the UI thread during startup.
From the code you've shown, I think that it's not very likely that it's anything in your Home activity, unless that Activity has something intensive going on in it's UI thread.
Cannot find any obvious wrong from the code you have shown. So If there's no more other code there and problem still exist, I suggest that you should find it through a process of elimination. For example, delete all listener in onCreate and add just one, then keep trying.

OnClick crashes my application

I have a textview in dialog and I want when I click this textview to do something.
I want it when clicking on the textview to do something.
I have added clickable and onClick in my layout
Here are my codes
public class Activitytwo extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//WORKING CODES
//WORKING CODES
//WORKING CODES
//WORKING CODES
}
// This one to show the dialog
//This one works fine!
public void share(View view){
sharedialog = new Dialog(Activitytwo.this);
sharedialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
sharedialog.setContentView(R.layout.postdialog);
sharedialog.setCanceledOnTouchOutside(true);
sharedialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
sharedialog.show();
}
public void facebookp(View view){
//This one for the TextView (onClick)
//This one doesn't work..
}
}
This is my layout (postdialog.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/postdialogl"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_height="150dp"
android:background="#drawable/postdialog" >
<TextView
android:id="#+id/postdtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:textSize="18sp"
android:textStyle="bold"
android:text="#string/post"
android:textColor="#a7b8d6"/>
<ImageView
android:id="#+id/poststrokea"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#202e4c"
android:layout_below="#+id/postdtitle"
android:layout_marginTop="5dp"/>
<ImageView
android:id="#+id/poststrokeb"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#803e5482"
android:layout_below="#+id/poststrokea"/>
<TextView
android:id="#+id/facebookpost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="60dp"
android:layout_marginLeft="30dp"
android:drawablePadding="5dp"
android:text="#string/facebook"
android:textColor="#a7b8d6"
android:textSize="12sp"
android:drawableTop="#drawable/facebook"
android:clickable="true"
android:onClick="facebookp"/>
</RelativeLayout>
One solution is to declare your textview inside your dialog.
public void share(View view){
//your code.
sharedialog = new Dialog(Activitytwo.this);
sharedialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
sharedialog.setContentView(R.layout.postdialog);
sharedialog.setCanceledOnTouchOutside(true);
sharedialog.getWindow().setBackgroundDrawable(newColorDrawable(android.graphics.Color.TRANSPARENT));
final TextView tviPost = (TextView) sharedialog.findViewById(R.id.sharedialog);
tviPost.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
//Do something
}
});
sharedialog.show();
}
and delete the next lines from your layout.
android:clickable="true"
android:onClick="facebookp"

Button click event not fire properly in <include> tag in ANDROID

I have three XML files are
1) Header
2) FirstActivity
3) SecondActivity.
header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="Button 1"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="first1"
>
</Button>
<Button
android:text="Button 2"
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="second"
>
</Button>
</LinearLayout>
firstactivity.xml
<?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="fill_parent"
>
<include layout="#layout/headerfile"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="100dip"
>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone Detail"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/simid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/imeino"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/phoneno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</RelativeLayout>
HeaderActivity.java
public class HeaderActivity extends Activity {
Button b1,b2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.headerfile);
b1 = (Button)findViewById(R.id.button1) ;
b2 = (Button)findViewById(R.id.button2) ;
} //onCreate over
public void first1(View v)
{
Intent i =new Intent(this,firstactivity.class);
startActivity(i);
finish();
}
public void second(View v)
{
Intent i1 =new Intent(this, secondactivity.class);
startActivity(i1);
finish();
}
}
But I got ERROR every time …
Log-cat :
ERROR/AndroidRuntime(328): java.lang.IllegalStateException: Could not find a method first1
(View) in the activity class com.contactDetails.ContactDetailsActivity for onClick handler on view class
android.widget.Button with id 'button1'
In ContactDetailsActivity.java class you should add below method
public void first1(View v)
{
// onlick code
}
<include> tags include all the elements in your root layout. So you can always access them using findViewById on your Activity.
You could use another way to handle onClickListener of your Button..
You don't have to write separate Activity for handling include layout views....
you can directly access that buttons from activity where you have used view (i.e. firstactivity.xml).
Try This..
Button b1 = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i =new Intent(this,firstactivity.class);
startActivity(i);
finish();
}
});
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i1 =new Intent(this,secondactivity.class);
startActivity(i1);
finish();
}
});
and Remove onClick property from both Button

Show/hide text with button

I am trying to show text with a button click.
Here is my onClick code:
public class Uteliv extends Activity {
public void onCrate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_uteliv);
TextView tidiTekst = (TextView) findViewById(R.id.tidiTekst);
tidiTekst.setVisibility(View.GONE);
Button tidiKnapp= (Button) findViewById(R.id.tidiKnapp);
tidiKnapp.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
tidiTekst.setVisibility(View.VISIBLE);
}
});
}
}
What is wrong? When I test it on my phone I only get a blank page.
Unless this was a typo in your post, your problem is you haven't declared the proper Activity method
public void onCrate(Bundle savedInstanceState) {
should be
public void onCreate(Bundle savedInstanceState) {
Also, you should probably make your Views member variables (declare them before onCreate() and initialize them inside of onCreate())
Edit
To show/hide your TextView you can use getVisibility to determine what to do. So it would be something like
public void onClick(View v) {
tidiTekst.setVisibility((tidiTekst.getVisibility() == View.Visible)
? View.GONE : View.VISIBLE);
}
activity_Main.xml
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:padding="10dip" >
<Button
android:id="#+id/mybtn"
style="?buttonBarButtonStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:layout_weight="1"
android:background="#c4c5c7"
android:shadowColor="#959597"
android:shadowDx="0"
android:shadowDy="1"
android:shadowRadius="1"
android:text="Button"
android:textColor="#ffffff"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:id="#+id/myTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="hidden"
android:visibility="gone"/>
</LinearLayout>
code for MainActivity.java
private TextView mytxtvw;
private Button myButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mytxtvw=(TextView)findViewById(R.id.myTextView);
myButton=(Button)findViewById(R.id.mybtn);
onBtnClick();
}
public void onBtnClick()
{
myButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mytxtvw.setVisibility((mytxtvw.getVisibility() == View.VISIBLE)
? View.GONE : View.VISIBLE);
}
});
}
}
Define your button and textView variables outside of the on-create method. Until you post the error you are getting, I'm going to assume that you are getting a null pointer because they are going out of scope once onCreate is finished.
TextView tidiTekst;
Button tidiKnapp;
public void onCrate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_uteliv);
tidiTekst = (TextView) findViewById(R.id.tidiTekst);
tidiTekst.setVisibility(View.GONE);
tidiKnapp= (Button) findViewById(R.id.tidiKnapp);
tidiKnapp.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
tidiTekst.setVisibility(View.VISIBLE);
}
});
}
Is it working if you decalre your textView as final ?
final TextView tidiTekst = (TextView) findViewById(R.id.tidiTekst);
Please check your R.layout.activity_uteliv in a WYSIWYG IDE such as Eclipse or IntelliJ IDEA, edit it in graphic mode, it must be something wrong with the layout(xml).
In xml :
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_marginTop="#dimen/_6sdp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MORE"
android:textStyle="bold"
android:textColor="#color/darkgray"
android:textSize="#dimen/_10sdp"
android:gravity="left"
android:id="#+id/moreone"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/darkgray"
android:layout_marginTop="#dimen/_6sdp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_marginTop="#dimen/_9sdp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="16) I will not indulge in any way in any act that is against the spirit of law and society. "
android:textStyle="bold"
android:textColor="#color/darkgray"
android:textSize="#dimen/_10sdp"
android:gravity="left"
android:id="#+id/tandcone"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_marginTop="#dimen/_6sdp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="17) I will treat all with dignity irrespective and provide them with high level of service and remedies for their dissatisfaction any time during the course of my business as an Independent Associate."
android:textStyle="bold"
android:textColor="#color/darkgray"
android:textSize="#dimen/_10sdp"
android:gravity="left"
android:id="#+id/tandctwo"/>
</LinearLayout>
In MainActivity :
moreone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tandcone.setVisibility((tandcone.getVisibility() == View.VISIBLE)
? View.GONE : View.VISIBLE);
tandctwo.setVisibility((tandctwo.getVisibility() == View.VISIBLE)
? View.GONE : View.VISIBLE);
}
});

Categories

Resources