Switch view using intent (android studio) - java

I have been trying to switch view using Intent and onClick. When i click on the button my app crashes. I have browsed trough similiar problems asked here but none of them seem to apply to what i have. Anyway, I was wondering if anyone could give me a vague hint on what is wrong with my code.
The button XML:
<Button
android:id="#+id/shapes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SHAPES"
android:textSize="16dp"
android:paddingBottom="20dp"
android:paddingTop="20dp"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:layout_marginTop="30dp"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:onClick="onShapes"/>
When button is clicked start Shapes.class (from MainActivity):
public void onShapes(View view) {
Intent startShapes = new Intent(getApplicationContext(), Shapes.class);
startActivity(startShapes);
}
Shapes.class, sets view to content_shapes:
package com.example.******.****.buttonns;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.example.******.****.R;
public class Shapes extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_shapes);
}
}
content_shapes (the view i want to see when pressing the button):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.*******.*****.buttonns.Shapes">
<TextView
android:text="Helloooo"
android:gravity="center"
android:textSize="32dp"
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Shapes_text" />
</RelativeLayout>
And i also put this in the Manifest:
<activity
android:name=".buttonns.Shapes"
android:label="#string/title_activity_shapes"
android:parentActivityName=".MainActivity"
android:theme="#style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.********.*****.MainActivity" />
</activity>
I have been trying to figure out the fault for hours now.
By the way I am a total noob at Java, XML and Android Studio so dont hate me if I forgot something trivial.

use this :
Intent startShapes = new Intent(MainActivity.this, Shapes.class);
startActivity(startShapes);
instead of this :
Intent startShapes = new Intent(getApplicationContext(), Shapes.class);
startActivity(startShapes);

Related

Having more than one button crashes my app

I am currently creating an app on Android Studio.
I use different buttons (in the same activity) that go to different activities. The problem is that, with one button, it all works, but when I add another one the emulator crashes.
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
defineButtons();
}
public void defineButtons() {
findViewById(R.id.next1).setOnClickListener(buttonClickListener);
findViewById(R.id.next2).setOnClickListener(buttonClickListener);
findViewById(R.id.next3).setOnClickListener(buttonClickListener);
}
private View.OnClickListener buttonClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.next1:
Intent Next1 = new Intent (Start.this, Next1.class);
startActivity(Next1);
break;
case R.id.next2:
Intent Next2 = new Intent (Start.this, Next2.class);
startActivity(Next2);
break;
case R.id.next3:
Intent Next3 = new Intent (Start.this, Next3.class);
startActivity(Next3);
break;
}
}
};
And here the XML:
<RelativeLayout 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=".Start"
android:background="#drawable/background">
<TextView
android:layout_width="match_parent"
android:layout_height="700px"
android:id="#+id/statement"
android:text="TEXT1"
android:background="#drawable/button_border"
android:textColor="#000000"
android:alpha="0.95"
android:layout_marginHorizontal="70px"
android:layout_marginTop="180px"
android:gravity="center"
android:paddingHorizontal="50px"
android:textSize="16sp"
/>
<Button
android:id="#+id/next1"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="150px"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="160dp"
android:layout_marginHorizontal="70px"
android:alpha="0.9"
android:background="#drawable/button_border"
android:text="TEXT2"
android:textColor="#android:color/black"
android:textSize="16sp"
android:textAllCaps="false"/>
<Button
android:id="#+id/next2"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="150px"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="90dp"
android:layout_marginHorizontal="70px"
android:alpha="0.9"
android:background="#drawable/button_border"
android:text="TEXT3"
android:textColor="#android:color/black"
android:textSize="16sp"
android:textAllCaps="false"/>
<Button
android:id="#+id/next3"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="150px"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:layout_marginHorizontal="70px"
android:alpha="0.9"
android:background="#drawable/button_border"
android:text="TEXT4"
android:textColor="#android:color/black"
android:textSize="16sp"
android:textAllCaps="false"/>
</RelativeLayout>
I know this is messy, I'm new at programming.
Your code is perfectly fine you just need to declare your Next2, and Next3 activities in manifest file like this
<activity android:name=".Next2"
android:screenOrientation="portrait"/>
<activity android:name=".Next3"
android:screenOrientation="portrait"/>
Android reads your app components from manifest file, refer this link for more details https://developer.android.com/guide/topics/manifest/manifest-intro

Android Full-screen issue

i'm developing android App which is full-screen App,
in the main activity there are 2 buttons
The issue is when i click in the About button the pop-out activity appear
after click on dismiss the title bar appear in the main activity, see the screen capture:
The main activity
After clicking About button and clicking on dismiss
about_popout.xml
<?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="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="vertical"
android:weightSum="100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="1dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_weight="60"
android:background="#android:color/transparent"
android:gravity="center_vertical"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:background="#drawable/about_shape"
android:gravity="center_vertical"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12pt"
android:textColor="#FFFFFF"
android:text="It&apos;s a PopupWindow" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/common_ic_googleplayservices" />
<Button
android:id="#+id/btn_dismiss"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:text="Dismiss"
android:textColor="#FFFFFF" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
activity_main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#ff5722"
android:orientation="vertical"
tools:context="com.game.circle.thecirclegame.MainMenu">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="#+id/textView" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="To the Game"
android:id="#+id/startButton"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/btn_about"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20px"
android:layout_marginLeft="100px"
android:layout_marginRight="100px"
android:text="About"/>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.game.circle.thecirclegame">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainMenu">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".GamePanel"></activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
</manifest>
The class MainMenu.java where the title bar appears, after clicking btn_about
package com.game.circle.thecirclegame;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
import android.app.ActionBar.LayoutParams;
public class MainMenu extends AppCompatActivity {
Button btn_startGame, btn_about;
final Context context = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
setContentView(R.layout.activity_main_menu);
addListinerToButtons();
}
public void addListinerToButtons(){
btn_startGame = (Button)findViewById(R.id.startButton);
btn_about = (Button)findViewById(R.id.btn_about);
btn_startGame.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context,GamePanel.class);
startActivity(intent);
}
});
btn_about.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater layoutInflater = (LayoutInflater)
getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.about_popout, null);
final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, true);
popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
Button btn_dismiss = (Button)popupView.findViewById(R.id.btn_dismiss);
btn_dismiss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}});
popupWindow.showAsDropDown(btn_about, 50, -30);
}
});
}
}
i need to get red of the blue title bar.
You can remove the title bar forever by calling:
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
before the setContentView(R.layout.x) in your activity class.
To make the app fullscreen, you can do:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
Again, put the above code before the setContentView method.
And make sure, if you are doing this, you should extend your Activity class by Activity and not AppCompatActivity or any other class.
Problem solved.
Because i didn't continue using the popup window,
i create a new activity instead and make the button redirect from main menu to About activity

attempting multiple activites via button click

I am writing an app that will display a menu of juices at a vape shop.
I have the main screen which displays the name and a button that once clicked will send the user to a new screen displaying the menu.
I am having trouble setting up the button click and when i run the app it crashes after the button is clicked
any input would be appreciated!
MainActivity
package com.example.vitoriano_vaz.easybayvapes;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
public void sendMessage(View view){
Intent intent = new Intent(FromActivity.this, ToActivity.class);
startActivity(intent);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startJuiceMenu(View view) {
Log.d("MyApp", "button clicked");
}
}
Second activity once the button is clicked
package com.example.vitoriano_vaz.easybayvapes;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import java.util.ArrayList;
public class juiceMenu extends AppCompatActivity {
ArrayList<String> juiceMenu = new ArrayList<String>(50);
private static String VALUE = "myValue";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
juiceMenu.add("#1 Blueberry Bombshell");
juiceMenu.add("#2 Richie Rich");
juiceMenu.add("#3 Chiquita");
juiceMenu.add("#4 Afternoon Delight");
juiceMenu.add("#5 Poppin Otters");
juiceMenu.add("#6 Viva La Sangria");
juiceMenu.add("#7 Okole Maluna");
juiceMenu.add("#8 Carmen Miranda");
juiceMenu.add("#9 Pomalade");
juiceMenu.add("#10 Izual");
juiceMenu.add("#11 Butter Stotch ");
juiceMenu.add("#12 Blue Bulls");
juiceMenu.add("#13 Grape Ape");
juiceMenu.add("#14 Bruce Juice");
juiceMenu.add("#15 Doc Holiday");
juiceMenu.add("#16 Peachy Keen");
juiceMenu.add("#17 Hula");
juiceMenu.add("#18 New York");
juiceMenu.add("#19 Al Gore");
juiceMenu.add("#20 Lux Charms");
juiceMenu.add("#21 Sailor jack");
juiceMenu.add("#22 Get Him to the Greek");
juiceMenu.add("#23 Key We Lie Chi");
juiceMenu.add("#24 Spring Fling");
juiceMenu.add("#25 Gumby");
juiceMenu.add("#26 Chai-Milk");
juiceMenu.add("#27 Mr. Bean");
juiceMenu.add("#28 50 Shades of Orange");
juiceMenu.add("#29 Blue Waffles");
juiceMenu.add("#30 Enigma");
juiceMenu.add("#31 Mr. Freeze");
//juiceMenu.add("#32 New Flavor"); need to update to get newest flavor
}
}
Activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.vitoriano_vaz.easybayvapes.MainActivity"
android:id="#+id/main_view">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:text="Welcome to East Bay Vapes"
android:textAllCaps="true"
android:textColor="#000000"
android:textSize="20sp"
android:id="#+id/textView" />
<Button
android:id="#+id/juicemenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:text="#string/JuiceMenu"
android:layout_marginTop="42dp"
android:layout_alignParentTop="true" />
</RelativeLayout>
Activity_juice_menu
here i have a LinearLayout for the ArrayList I declared in juicemenu.java class
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.vitoriano_vaz.easybayvapes.juiceMenu">
<ListView
android:id="#+id/JuiceMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vitoriano_vaz.easybayvapes">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".juiceMenu"></activity>
</application>
</
The Intent that you are building needs to reference your own Activity classes.
The first parameter is a Context and the second is the Class of the Activity you want to start so it should be the following.
Intent intent = new Intent(this, juiceMenu.class);
Since your question has already been answered, I thought I'd suggest a more efficient means of populating your array to remove that blemish from your code. For example you can use a string-array resource. With that you simply add the values using a single code line. As an example:
String[] juiceArray = getResources().getStringArray(R.array.JuiceTextArray);
The array values themselves are populated in your arrays.xml file (found in values folder) as such:
<resources
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="MissingTranslation">
<string-array name="JuiceTextArray" tools:ignore="MissingTranslation">
<item name="Juice1">#1 Blueberry Bombshell.</item>
<item name="Juice2">#2 Richie Rich.</item>
[etc., etc.]
</string-array>
</resources>

How to fill page with overlay

I have a TRANSPARENT overlay in my android app that when user click on it,it fade but it can't fill all activity like below image
MainActivity :
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="This is Original View" />
</RelativeLayout>
OverlayActivity :
<?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" >
<ImageView
android:id="#+id/over_lay_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#50220000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="This is Overlay View" />
</RelativeLayout>
Java :
public class MainActivity extends Activity {
private ImageView mOverLayImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Dialog overlayInfo = new Dialog(MainActivity.this);
overlayInfo.requestWindowFeature(Window.FEATURE_NO_TITLE);
overlayInfo.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
overlayInfo.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
overlayInfo.setContentView(R.layout.overlay_view);
overlayInfo.show();
mOverLayImage = (ImageView) overlayInfo.findViewById(R.id.over_lay_image);
mOverLayImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
overlayInfo.cancel();
}
});
}
}
Use FrameLayout. Each item added to FrameLayout is on top of the previous one, like in this example the second TextView is on top of the frist one, but since it is not fully opaque, you can see them both!
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:text="Blablabla"
android:background="#FFFFFFFF"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#50220000"
android:textColor="#FFFFFF"
android:text="I am on top"
android:gravity="center"
/>
</FrameLayout>
Now all you need to do is show/hide the overlayed items and you are good to go.
Delete your overlay activity, and inside your main activity apply this code :
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="This is Original View" />
<!-- This is your overlay -->
<RelativeLayout android:id="#+id/over_lay_page"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/over_lay_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#50220000"
android:onClick="clickedOverlay" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="This is Overlay View" />
</RelativeLayout>
</RelativeLayout>
Note that I added a line on your ImageView which runs a function when clicked, now on your java file add this function:
//The onClick on xml requires a function of signature void(View) which is the clicked view (in this case the ImageView)
public void clickedOverlay(View view)
{
//ImageView is clicked
RelativeLayout rlLayout = (RelativeLayout) findViewById(R.id.over_lay_page);
rlLayout.setVisibility(View.GONE);
}
This will make the RelativeLayout that contains the overlay views (including the ImageView which is clicked) to not only be invisible but not to interfere with anything. It also ignores input to it.
In case I misunderstood anything about your question feel free to correct me (I'm not sure I understood that completely).
Also if you want it to fade in or out or something like that you can do it with an AlphaAnimation.

OnClickListener not working when i click on text

when click on text nothing happened. there is not exception in CatLog or showing any error. i checked Manifest file and c_programs_start_51_100.xml file but i found nothing wrong still struggling find out the problem. The function Onclick() doesn't executed at single when i check in CatLog. please help me resolve the problem.
Thanks in advance.
public class C_Programs_Start_51_100 extends Activity implements View.OnClickListener{
TextView textViewC51;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.c_programs_start_51_100);
textViewC51 = (TextView) findViewById(R.id.textView51);
textViewC51.setOnClickListener(this);
}
#Override
public void onClick(View view) {
Log.d("press", "second" + view.getId());
switch(view.getId()) {
Log.d("press", "first");
case R.id.textView51:
Log.d("press", "first");
Intent intent51 = new Intent(C_Programs_Start_51_100.this, C_Programs_P51.class);
startActivity(intent51);
Log.d("press", "second");
break;
}
}
}
// Here is my xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/relativeLayout1"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".First" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/table_vertical_margin"
android:paddingLeft="#dimen/table_horizontal_margin"
android:paddingRight="#dimen/table_horizontal_margin"
android:paddingTop="#dimen/table_vertical_margin"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/table_vertical_margin"
android:paddingLeft="#dimen/table_horizontal_margin"
android:paddingRight="#dimen/table_horizontal_margin"
android:paddingTop="#dimen/table_vertical_margin"
>
<TextView
android:id="#+id/textView51"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:linksClickable="true"
android:focusableInTouchMode="true"
android:text="#string/txtPn51" />
<TextView
android:id="#+id/textViewC51"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:linksClickable="true"
android:focusableInTouchMode="true"
android:textSize="15sp"
android:focusable="true"
android:fitsSystemWindows="true"
android:text="#string/txtC51" />
</TableRow>
</LinearLayout>
</TableLayout>
</ScrollView>
</RelativeLayout>
You seem to have two textViews one with
id = textView51
id = textViewC51
in your onCreate you are trying to find the first textview and pass it to another
textViewC51 = (TextView) findViewById(R.id.textView51);
Try to change the previous line with:
textViewC51 = (TextView) findViewById(R.id.textViewC51);
and see if it works for you.
Seems like you are trying to implement something when the user clicks the textview.If this is the case you can just use a spinner, Where you can implement the said functionality without much complexity
you implemented the View.OnClickListener and I'm assuming that you also include in the xml the android:onclick attribute in your xml file?..

Categories