Defining text view to the corresponding id in android? - android

Found the solution : Declared textview in the oncreateOptionMenu instead of Oncreate method.
Problem : It generates an Multiple marker error when i define text view for the corresponding id.
TwitterSearchActivity.java
package com.example.twittersearchactivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class TwitterSearchActivity extends Activity {
TextView tweet;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_twitter_search);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.twitter_search, menu);
return true;
}
tweet=(TextView)findViewById(R.id.tweet_txt);
}
activity_twitter_search.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"
tools:context=".TwitterSearchActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world"
/>
<TextView
android:id="#+id/intro_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textStyle="italic"
android:text="#string/intro"
/>
<EditText
android:id="#+id/search_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="#string/hint"
android:layout_below="#+id/intro_txt"
android:padding="10dp"
android:background="#ffff66"
android:layout_margin="5dp"
/>
<Button
android:id="#+id/search_btn"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/search_label"
android:layout_below="#+id/search_edit"
android:onClick="searchTwitter"
android:layout_margin="5dp"
/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/search_btn">
<TextView
android:id="#+id/tweet_txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
android:padding="10dp"
android:background="#330000"
android:textColor="#ffffff"
android:layout_margin="5dp"
android:text="#string/placeholder"
android:freezesText="true"
/>
</ScrollView>
</RelativeLayout>
Error
Multiple markers at this line
- Syntax error on token ".", ... expected
- Syntax error, insert ";" to complete FieldDeclaration
- Syntax error on token "tweet", VariableDeclaratorId expected after this token
- Return type for the method is missing
- Syntax error, insert "}" to complete ClassBody

Put
tweet=(TextView)findViewById(R.id.tweet_txt);
within your onCreate() method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_twitter_search);
tweet=(TextView)findViewById(R.id.tweet_txt);
}

Your Button and Your ScrollView got the same id : search_btn. Don´t know if this is the issue for Your problem, but should be fixed

Related

Remove and add layout dynamically by click button in Android

I'm working on a big project and I can not solve the problem of a dynamically adding of components.I want to add layout into other layout by click on a button ADD. After this I want to remove it by click a button REMOVE.
Specially for stackoverflow I build a small example of what I want to do.
To ADD it's not a problem but remove it's a problem.When I click a "remove" button this remove not what I need (I want remove parent of "remove" button).
After this I want to ask something more important.I will need save all this data to the DB.So I don't know how to get data from each Text Fields and put it into list (or something else) because all this Text Fields have same ID.
So I see two way of solution:
1)Change there ID dynamically
2)Something else))
Thank you very much!!!
This is
sub_fields.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false"
android:id="#+id/detailsLayout"
android:focusableInTouchMode="true">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/txtName"
android:hint="name" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/txtPhone"
android:layout_gravity="center_horizontal"
android:hint="phone" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD"
android:id="#+id/btnAddd"
android:onClick="onClickAddd" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="REMOVE"
android:id="#+id/btnRemove"
android:onClick="onClickAddd" />
</LinearLayout>
</LinearLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false"
android:id="#+id/generalLayout">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="56dp"
android:gravity="center"
android:background="#7d65258a">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="FILL FIELDS"
android:id="#+id/textView" />
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/subLayoutFields">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/txtName"
android:hint="name" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/txtPhone"
android:layout_gravity="center_horizontal"
android:hint="phone" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD"
android:id="#+id/btnAdd"
android:onClick="onClickAdd" />
</LinearLayout>
</LinearLayout>
MainActivity.java
package andrey.adddinamicallycontrolsapp;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onClickAdd(View view) {
LayoutInflater ltInflater = getLayoutInflater();
final LinearLayout subLayoutFields = (LinearLayout) findViewById(R.id.subLayoutFields);
final View view1 = ltInflater.inflate(R.layout.sub_fields, subLayoutFields, true);
Button buttonRemove = (Button)view1.findViewById(R.id.btnRemove);
buttonRemove.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
subLayoutFields.removeView((LinearLayout)(v.getParent().getParent()));
}});
}
Before adding some view by addView(childView) you should save reference to that. In that case removeView(childView) will work. Getting that view by traveling in view tree by getParent() is not a good idea.
By the way, where is your addView call?
maybe try subLayoutFields.removeView(view1) - view1 is final
if you want to somehow differ your widgets (for storing in db, iterating or whatever you want) you may set for them dynamic ids using View.generateViewId() (API17) or using custom method from most voted answer here
Thank you guys!!!
The answer is :
public void onClickAddPanel(View view) {
LayoutInflater ltInflater = getLayoutInflater();
LinearLayout subLayoutFieldsForBtnAdd = (LinearLayout) findViewById(R.id.productDetails);
View view1 = ltInflater.inflate(R.layout.product_details, subLayoutFieldsForBtnAdd, true);
}
public void onClickRemovePanel(View v) {
View v1 = (View) v.getParent();
LinearLayout subLayoutFieldsForBtnRemove = (LinearLayout) findViewById(R.id.productDetails);
subLayoutFieldsForBtnRemove.removeView(v1);
}

Adding a child view to the main view with button inside child view - android

I am trying to add a child view to the main view by pressing a button which is inside the child view but the code doesn't work. Any idea what the problem is. I am beginner in Android
This is the main linear layout with the child linear layout in line with + and - buttons added to it. I would like to add another row under this by pressing + and remove it by pressing -.
Here is the main layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/trainer_main"
android:layout_width="768dp"
android:layout_height="1280dp"
android:background="#121617"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
tools:context=".TrainingProgrammer" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="25dip"
android:textColor="#ffffff"
android:textStyle="bold"
android:text="#string/title_activity_training_programmer" />
<LinearLayout
android:id="#+id/trainer_headline"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/interval_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text="#string/training_programmer_interval" />
<TextView
android:id="#+id/exercise_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dip"
android:paddingRight="95dip"
android:textColor="#ffffff"
android:text="#string/training_programmer_exercise" />
<TextView
android:id="#+id/second_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="25dip"
android:textColor="#ffffff"
android:text="#string/training_programmer_time" />
<TextView
android:id="#+id/weight_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dip"
android:textColor="#ffffff"
android:text="#string/training_programmer_weight" />
</LinearLayout>
and here is the child layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/trainer_progcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/interval_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="14sp"
android:ems="1"
android:text="1" />
<EditText
android:id="#+id/exercise_content"
android:layout_width="127dp"
android:layout_height="wrap_content"
android:textSize="14sp"
android:singleLine="true"
android:textColor="#ffffff"
android:hint="#string/training_programmer_exercise_edit"
android:ems="10" />
<EditText
android:id="#+id/time_content"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:hint="#string/training_programmer_time_edit"
android:inputType="number"
android:maxLength="2"
android:textColor="#ffffff" />
<EditText
android:id="#+id/weight_content"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:hint="#string/training_programmer_weight_edit"
android:inputType="number"
android:maxLength="2"
android:textColor="#ffffff" />
<Button
android:id="#+id/button_minus"
style="?android:attr/buttonStyleSmall"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="20dp"
android:background="#drawable/minus2" />
<Button
android:id="#+id/button_plus"
style="?android:attr/buttonStyleSmall"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="20dp"
android:background="#drawable/plus2" />
and here is the code:
package ca.cruxtec.cruxtectrainer;
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.LinearLayout;
import android.widget.Toast;
public class ProgrammerActivity extends ActionBarActivity {
private Button button_plus_define;
private Button button_minus_define;
static private final int MAX_NO_EXER = 30;
private int i = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_programmer);
final LinearLayout programmer_main_layout = (LinearLayout)findViewById(R.id.trainer_main);
//LinearLayout sequence_layout = (LinearLayout)findViewById(R.id.trainer_progcontent);
// Layout inflation for addition of the child view to the main view
final View adding_child = getLayoutInflater().
inflate(R.layout.sequence, programmer_main_layout, false);
programmer_main_layout.addView(adding_child);
// Plus button
button_plus_define = (Button)findViewById(R.id.button_plus);
button_plus_define.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
i++;
programmer_main_layout.addView(adding_child);
/* Toast.makeText(getApplicationContext(),
"i is :" + i, Toast.LENGTH_LONG).show();*/
}
});
// minus button
button_minus_define = (Button)findViewById(R.id.button_minus);
button_minus_define.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
/*if(i>2) i--;
else {
Toast.makeText(getApplicationContext(),
"i is less than 2:" , Toast.LENGTH_LONG).show();
}
Toast.makeText(getApplicationContext(),
"i is :" +i , Toast.LENGTH_LONG).show();*/
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_programmer, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
the app crashes due to having
programmer_main_layout.addView(adding_child);
inside the + button
and here is the main error:
30799-30799/ca.cruxtec.cruxtectrainer E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Every time you add a view as the child of another view,It should be a newer one.But you are adding the same reference "adding_child" each times.
I hope that you understood why the error "The specified child already has a parent" had occured.

First simple Android app crashed by click on a button

my fist very simple android app is crashed, if I press my button. It's just a test. If the button click is working, I want to put an sql query in myClick function.
package com.example.connectdroid;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
void myClick(){
//here is my problem:
LinearLayout lView = (LinearLayout)findViewById(R.id.mylinear);
TextView selection = (TextView) findViewById(R.id.editText1);
String mytext = "apple";
selection.setText(mytext);
}
}
The 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"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:text="Button"
android:onClick="myClick" />
<LinearLayout
android:id="#+id/mylinear"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button1"
android:layout_marginLeft="17dp"
android:layout_marginTop="57dp" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/mylinear"
android:layout_alignLeft="#+id/button1"
android:layout_marginBottom="22dp"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
</RelativeLayout>
void myClick(){
should be
public void myClick(View view){
Your app is crashing because you declared android:onClick="myClick" but the signature in the java file of myClick is wrong
thats true dear blackbelt,
The method called onClick should be following features
public method and
the method should contain View parameter

i found out that my R.java contains nothing, can someone tell me what to be written in R.java

error is that, R is underlined red! and com.example.guessthenumber.R is also underlined red!
when hovered it says to create a class R in package com.example.guessthenumber.R
xml file:
<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:background="#drawable/Guess"
android:gravity="start"
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=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Done!" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_centerHorizontal="true"
android:text="Enter your guessed number below"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#191970"
android:textStyle="bold" />
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_centerVertical="true"
android:text="0"
android:inputType="number" />
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/editText1"
android:layout_marginTop="29dp"
android:fontFamily="sans-serif-condensed"
android:text="#string/guess"
android:textColor="#000000"
android:textSize="22sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/game" />
</RelativeLayout>
below one is the java file:
package com.example.guessthenumber;
import android.os.Bundle;
import com.example.guessthenumber.R
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView t1 = (TextView) findViewById(R.id.textView1);
final TextView guessText = (TextView) findViewById(R.id.textView2);
final EditText userGuess = (EditText) findViewById(R.id.editText1);
Button b1 = (Button) findViewById(R.id.button1);
final String[] myNames={"Shashank","Sarthak","Gowda","$hankar"};
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String randText="";
// TODO Auto-generated method stub
int rando = (int) (Math.random()* 6);
int userNumber = Integer.parseInt(userGuess.getText().toString());
if(userNumber == rando){
guessText.setText("You got it right!");
}else{
guessText.setText("Guess again! :(");
}
randText=Integer.toString(rando);
t1.setText(randText);
userGuess.setText("0");
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Replace:
import android.R;
by
import com.example.guessthenumber.R
OR
remove import.android.R and use ctrl+shift+O, organise imports if you are using eclipse.
Clean, build your project.
Also, pls read more about R:
Android: What is R? Why is it so Cryptic?
Check your imports - at the top of your activity, below your package name. And make sure it has something like this line:
import your_application_package_name.R;
Agreed with gunar's comment above,
In your xml, the "id" has not been defined properly, for your TextView. Please post the xml code, may be the activity's too.
Correct way for "id" in TextView, xml :
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Check that you are importing the R class from your package and not the Android's one. Check also that your resources do not contains error, otherwise the R class will not be generated.

Source attachment does not contain the source for the file view.class

I am new to android and making a simple application of a temperature converter. i had placed 2 text views and a button on the screen. i just wanted to saw that when i press the button an event comes or not when i run the application no event came after pressing the button so i put it in debug mode then it gives a message
Source Not Found:Source attachment does not contain the source for the file view.class
Here is my code:
Activity_main.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"
android:background="#color/red"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
style="#style/AppTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="136dp"
android:layout_marginLeft="94dp"
android:onClick="onclick"
android:text="#string/Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="34dp"
android:layout_marginTop="38dp"
android:text="#string/Large_Text"
android:textStyle="italic"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="42dp"
android:text="#string/Large_Text1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/textView2"
android:ems="10"
android:inputType="numberSigned" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView1"
android:layout_toRightOf="#+id/button1"
android:ems="10"
android:inputType="numberSigned" />
</RelativeLayout>
Strings.Xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Converter</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="Button">Convert</string>
<string name="Large_Text">Deg C</string>
<string name="Large_Text1">Deg F</string>
<color name="red">#D3D3D3</color>
</resources>
Main_activity.java
package com.example.converter;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I have created a classs1.java
package com.example.converter;
import android.app.Activity;
import android.widget.TextView;
public class Classs1 extends Activity{
public void onclick(){
setContentView(R.layout.activity_main);
TextView t = (TextView)findViewById(R.id.textView1);
t.setText(4);
}
}
U did not call event listener to ur view
public class Classs1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
btn = (Button) findViewById(R.id.ur_button_id);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
setContentView(R.layout.activity_main);
TextView t = (TextView)findViewById(R.id.textView1);
t.setText(4);
}
});
}
Edit:
Error : Source Not Found:Source attachment does not contain the source for the file view.class
Did u add classs1.java in your manifest file Plz check that too.

Categories

Resources