Toast String display object instead of Text - android

In my code,I want to display TextView text name as Toast message.
I do following coding for that .I didn't get the proper text only object of that text I got.
In Toast I want to display the Text please guide me if possible.
Here is my code :
package com.Viewflipper;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ViewFlipper;
public class Viewflipper extends Activity implements OnClickListener {
/** Called when the activity is first created. */
Button next;
Button previous;
ViewFlipper vf;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vf = (ViewFlipper) findViewById(R.id.ViewFlipper01);
next = (Button) findViewById(R.id.Button01);
previous = (Button) findViewById(R.id.Button02);
next.setOnClickListener(this);
previous.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == next) {
vf.showNext();
Toast.makeText(this,vf.toString(), Toast.LENGTH_SHORT).show();
**//HERE I DIDN'T GET vf.getText().toString();**
}
if (v == previous) {
vf.showPrevious();
}
}
}
**EDITED**
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<LinearLayout android:id="#+id/LinearLayout03"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:id="#+id/Button01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Next"></Button>
<Button android:id="#+id/Button02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Previous"></Button>
</LinearLayout>
<LinearLayout android:id="#+id/LinearLayout02"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<ViewFlipper android:id="#+id/ViewFlipper01"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<!--adding views to ViewFlipper-->
<TextView android:id="#+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/a10"
android:text="one"></TextView>
<TextView android:id="#+id/TextView02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/a11"
android:text="two"></TextView>
<TextView android:id="#+id/TextView03" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/a12"
android:text="three">
</TextView>
<TextView android:id="#+id/TextView04" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/a13"></TextView>
<TextView android:id="#+id/TextView05" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/a14"></TextView>
<TextView android:id="#+id/TextView06" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/a15"></TextView>
</ViewFlipper>
</LinearLayout>
</LinearLayout>

Try This:-
package com.Viewflipper;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ViewFlipper;
public class Viewflipper extends Activity implements OnClickListener {
/** Called when the activity is first created. */
Button next;
Button previous;
ViewFlipper vf;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vf = (ViewFlipper) findViewById(R.id.ViewFlipper01);
next = (Button) findViewById(R.id.Button01);
previous = (Button) findViewById(R.id.Button02);
next.setOnClickListener(this);
previous.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == next) {
vf.showNext();
Toast.makeText(getApplicationContext(),((TextView) vf.getCurrentView()).getText(), Toast.LENGTH_SHORT).show();
}
if (v == previous) {
vf.showPrevious();
}
}
}

A ViewFliper is an adapter.. you can't just grab it like that, you have to interact with the adapter and get the view and THEN grab the textview in that view.

Related

How to debug an Android app that says "unfortunately <app> stopped working"?

This is my java class, MainActivity.java.
Basically I'm trying to create a button performing addition on variable i. Everything seems to be correct as far as I learnt until now. I am getting an error message on my emulator saying:
unfortunately myfirstandroidapp stopped working.
Code:
package com.example.android.myfirstandroidapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = (Button) findViewById(R.id.addButton);
final TextView txtView = (TextView) findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
i++;
txtView.setText("i value is:"+i);
}
});
setContentView(R.layout.activity_main);
}
}
This is my layout.xml
main_activity.xml:
<?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:id="#+id/activity_main"
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.android.myfirstandroidapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your value is:0"
android:id="#+id/textView"
android:textColor="#android:color/black"
android:textStyle="normal|bold"
android:textSize="14dp" />
<Button
android:text="Add One"
android:layout_width="100dp"
android:layout_height="20dp"
android:layout_marginTop="25dp"
android:id="#+id/addButton"
android:background="#android:color/holo_green_dark"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
You need to call setContentView() before initializing any View.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.addButton);
final TextView txtView = (TextView) findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
i++;
txtView.setText("i value is:"+i);
}
});
}

How can store Data text view data?

In this file i have two Edit text and one textview.I want to sum two number.then result are show in text view.Then i want to store Textview data. please help.i am trying but i am fail.
Activity_main.xml//
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Edit"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Edit1"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Add"
android:id="#+id/add"
android:onClick="add"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sub"
android:id="#+id/sub"
android:onClick="sub"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:paddingBottom="30dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/pr"
android:text="Previoues Data"
/>
</LinearLayout>
</LinearLayout>
the java file is
MainActivity.java
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText Edit,Edit1;
Button add, sub, pr;
TextView view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Edit = (EditText) findViewById(R.id.Edit);
Edit1 = (EditText) findViewById(R.id.Edit1);
add = (Button) findViewById(R.id.add);
sub = (Button) findViewById(R.id.sub);
pr = (Button) findViewById(R.id.pr);
view = (TextView) findViewById(R.id.view);
Edit.setText("0");
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int value1 = Integer.parseInt(Edit.getText().toString());
int value2 = Integer.parseInt(Edit1.getText().toString());
int result = value1 + value2;
view.setText(Integer.toString(result));
// Edit.setText(Integer.toString(result));
SharedPreferences sharedPreferences=getSharedPreferences("Mydata",Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putInt("value",result);
editor.commit();
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int value1 = Integer.parseInt(Edit.getText().toString());
int value2 = Integer.parseInt(Edit1.getText().toString());
int result = value1 - value2;
view.setText(Integer.toString(result));
Edit.setText(Integer.toString(result));
}
});
pr.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences sharedPreferences=getSharedPreferences("Mydata",Context.MODE_PRIVATE);
// int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
String v=sharedPreferences.getInt("value","");
view.setText(v);
}
});
}
}
update your xml file remove onClick from your button
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Add"
android:id="#+id/add"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sub"
android:id="#+id/sub"
/>
Problem is with your getting value from sharedPreference
String v=sharedPreferences.getInt("value","");
why are you passing string as default value and your
try this String
v = Integer.toString(sharedPreferences.getInt("value",0));
in the layout you´ve got android:onClick="add" there should be a public void add(View v) to handle the event. Do the sum inside that handler.
Please, follow the documentation
http://developer.android.com/reference/android/widget/Button.html and see the :onCLick handling.

TimePicker + CheckBox

I've got a TimePicker in my app and i want to add above it a CheckBox that if it checked the TimePicker won't work. This is my TimePicker: http://developer.android.com/guide/topics/ui/controls/pickers.html#TimePicker.
How can I do that? I want the timePicker to look the same as the example.
I will put here the entire project also for others to use it if they ever need it:
Short description: A program that let's you get the time set by user and show it in the bottom of the page, after you press Done button.Used intent to get from one activity to another.
--------Main Activity-------
package com.example.chxboxtest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
private Button start_intent_button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start_intent_button = (Button) findViewById(R.id.start_intent_button);
start_intent_button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start_intent_button: {
Intent intent = new Intent(this,TimePickerTest.class);
startActivity(intent);
}
break;
}
}
}
-------------Main Activity XML-----------------
<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"
>
<Button
android:id="#+id/start_intent_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Intent Time Picker"
android:layout_centerInParent="true"
/>
</RelativeLayout>
-------------TimePicker class ---------------
package com.example.chxboxtest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
public class TimePickerTest extends Activity implements OnClickListener{
private CheckBox cBox;
private TimePicker tPicker;
private TextView showTime;
private Button done;
private String hour;
private String minute;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.time_picker_layout);
initButtons();
}
private void initButtons() {
tPicker = (TimePicker) findViewById(R.id.time_picker);
showTime = (TextView) findViewById(R.id.get_time);
done = (Button)findViewById(R.id.done);
cBox = (CheckBox) findViewById(R.id.time_picker_checkbox);
cBox.setOnClickListener(this);
done.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
//If check enable or disble timePicker
case R.id.time_picker_checkbox: {
if (((CheckBox) v).isChecked()) {
Toast.makeText(this, "CHECKED", Toast.LENGTH_SHORT).show();
tPicker.setEnabled(false);
} else {
Toast.makeText(this, "NOT CHECKED", Toast.LENGTH_SHORT).show();
tPicker.setEnabled(true);
}
}
break;
//If Done button pressed get time selected by user
case R.id.done:{
tPicker.clearFocus();
// re-read the values, in my case i put them in a Time object.
hour = tPicker.getCurrentHour().toString();
minute = tPicker.getCurrentMinute().toString();
if(tPicker.getCurrentMinute().intValue() < 10){
String setTimeText = hour+ " : " + "0" + minute;
showTime.setText(setTimeText);
}else{
String setTimeText = hour+ " : " + minute;
showTime.setText(setTimeText);
}
}
break;
default:
break;
}
}
}
-------------TimerPicker XML------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/timer_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/time_picker"
android:layout_toRightOf="#+id/time_picker_checkbox"
android:text="Check this to Cancel Alarm" />
<CheckBox
android:id="#+id/time_picker_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/time_picker"
android:layout_centerHorizontal="true" />
<TimePicker
android:id="#+id/time_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<Button
android:id="#+id/done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/time_picker"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="Done" />
<TextView
android:id="#+id/get_time"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:text="Time"
android:textColor="#FF0000"
android:textSize="25sp" />
</RelativeLayout>
-------------Manifest.xml-----------
**<activity
android:name="com.example.chxboxtest.TimePickerTest"
android:configChanges="orientation|keyboardHidden">
</activity>**
Dont forget to add TimePicker class as activity.
BestPracice add all xml text in strings.xml
Cheers
Here's an functional example with all your requirements.
Tested and 100% functional :)
Put this in xml:
<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"
>
<TextView
android:id="#+id/timer_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check this to Cancel Alarm"
android:layout_above="#+id/time_picker"
android:layout_toRightOf="#+id/time_picker_checkbox"
/>
<CheckBox
android:id="#+id/time_picker_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/time_picker"
/>
<TimePicker
android:id="#+id/time_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
And Here is your java code:
package com.example.chxboxtest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.TimePicker;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private CheckBox cBox;
private TimePicker tPicker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initButtons();
}
private void initButtons() {
tPicker = (TimePicker) findViewById(R.id.time_picker);
cBox = (CheckBox) findViewById(R.id.time_picker_checkbox);
cBox.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.time_picker_checkbox: {
if (((CheckBox) v).isChecked()) {
Toast.makeText(this, "Deactivate",Toast.LENGTH_SHORT).show();
tPicker.setEnabled(false);
} else {
Toast.makeText(this, "Activate",Toast.LENGTH_SHORT).show();
tPicker.setEnabled(true);
}
}
break;
default:
break;
}
}
}
If you want this example in a custom dialog please let me know.
Cheers,

displaying a string on the textview when clicking a button in android

I'm very new to Android development and just started to study.
What I'm trying is to add a button and when that button is pressed a text "my first project" to get displayed in the text view.
With the help of some experts I created the button and text view.
So the button is showing in the simulator but when I click that button nothing happens.
Can anyone please help me with how can I display the text when pressing the button?
.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/txtView"
android:layout_width="150dp"
android:layout_height="150dp"
android:text="#string/hello" />
<Button
android:id="#+id/mybtn"
android:layout_width="50dp"
android:layout_height="30dp" />
<TextView
android:id="#+id/viewwidth"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/viewheight"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.widget.Toast;
public class NameonbuttonclickActivity extends Activity implements View.OnClickListener {
Button mybtn;
TextView txtView;
String hello;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
super.onCreate(savedInstanceState);
mybtn= new Button(this);
txtView=new TextView(this);
mybtn.setOnClickListener(this);
printmyname();
mybtn = (Button)findViewById(R.id.mybtn);
txtView=(TextView)findViewById(R.id.txtView);
txtView = (TextView)findViewById(R.id.viewwidth);
txtView = (TextView)findViewById(R.id.viewheight);
hello="This is my first project";
//setContentView(mybtn);
// setContentView(R.layout.main);
}
public void onClick(View view){
txtView.setText(hello);
//printmyname();
Toast.makeText(NameonbuttonclickActivity.this, hello, Toast.LENGTH_LONG).show();
}
private void printmyname(){
System.out.println("coming");
}
}
You can do like this:
String hello;
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
super.onCreate(savedInstanceState);
mybtn = (Button)findViewById(R.id.mybtn);
txtView=(TextView)findViewById(R.id.txtView);
txtwidth = (TextView)findViewById(R.id.viewwidth);
hello="This is my first project";
mybtn.setOnClickListener(this);
}
public void onClick(View view){
txtView.setText(hello);
}
Check your textview names. Both are same . You must use different object names and you have mentioned that textview object which is not available in your xml layout file.
Hope this will help you.
First create xml file as follows. Create one textview and a button:
main.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="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<Button
android:id="#+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
The first TextView is created by default. You can leave or remove it if you want.
Next one is to create a button
The next one is TextView where you want to display text.
Now coming to the main activity code...
package com.android.example.simple;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class SimpleActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textView=(TextView)findViewById(R.id.textView1);
final Button button1 = (Button)findViewById(R.id.mybutton);
//Implement listener for your button so that when you click the
//button, android will listen to it.
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
textView.setText("You clicked the button");
} });
}
}
This is because you DON'T associated the OnClickListener() on the button retrieve from the XML layout.
You don't need to create object because they are already created by the Android system when you inflate XML file (with the Activity.setContentLayout( int resourceLayoutId ) method).
Just retrieve them with the findViewById(...) method.
Just check your code in .java class
You had written below line
mybtn.setOnClickListener(this);
before initializing the mybtn object I mean
mybtn = (Button)findViewById(R.id.mybtn);
just switch this two line or put that line "mybtn.setOnClickListener(this)" after initializing your mybtn object and you will get the answer what you want..
Try this
public void onClick(View view){
txtView.setText("hello");
//printmyname();
Toast.makeText(NameonbuttonclickActivity.this, "hello", Toast.LENGTH_LONG).show();
}
Also in toast use "Hello"
you use this as txtView.setText("hello");
MainActivity.java:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button1;
TextView textView1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
textView1=(TextView)findViewById(R.id.textView1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView1.setText("TextView displayed Successfully");
}
});
}
}
activity_main.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:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click here" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
public void onCreate(Bundle savedInstanceState)
{
setContentView(R.layout.main);
super.onCreate(savedInstanceState);
mybtn = (Button)findViewById(R.id.mybtn);
txtView=(TextView)findViewById(R.id.txtView);
mybtn .setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
txtView.SetText("Your Message");
}
});
}
Check this:
hello.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View paramView) {
text.setText("hello");
}
});

force down error in app

I have a problem on running my app but i cant find out where is the problem! I have created a first screen that has two buttons. This "home" screen is chordsApp.java. I want to go to the track1.java if I press the first button and to track2.java if I press the second! The program runs but when I'm trying to press the first or the second button it pops up an error and tells me to force down!! Here is the code:
ChordApp.java
package com.example.ChordsApp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ChordsApp extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.button);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), track1.class);
startActivityForResult(myIntent,0);
}
});
Button next1 = (Button) findViewById(R.id.button1);
next1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), track2.class);
startActivityForResult(myIntent, 0);
}
});
}
}
track1.java
package com.example.ChordsApp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class track1 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Button next = (Button) findViewById(R.id.button3);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
}
track2.java
package com.example.ChordsApp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class track2 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
Button next1 = (Button) findViewById(R.id.button4);
next1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/pic"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/guitar"
android:layout_gravity="center"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<Button
android:text="track1"
android:id="#+id/button"
android:layout_width="250px"
android:textSize="18px"
android:layout_height="55px" />
<Button
android:text="track2"
android:id="#+id/button1"
android:layout_width="250px"
android:textSize="18px"
android:layout_height="55px" />
</LinearLayout>
</LinearLayout>
main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="hi"/>
<Button android:text="Home"
android:id="#+id/button3"
android:layout_width="250px"
android:textSize="18px"
android:layout_height="55px"
android:layout_gravity="center_horizontal"
>
</Button>
</LinearLayout>
main3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="hi"/>
<Button android:text="Home"
android:id="#+id/button4"
android:layout_width="250px"
android:textSize="18px"
android:layout_height="55px"
android:layout_gravity="center_horizontal"
>
</Button>
</LinearLayout>

Categories

Resources