Declare EditText as TextView in Android - android

i have a xml layout main.xml like this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="text"
android:textColor="#000"
android:textSize="16dp"
android:id="#+id/edtxt"
android:gravity="center|top|left"/>
</RelativeLayout>
and i declare this in my activity like this
public class MyActivity extends Activity {
private TextView txt = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt= (TextView)findViewById(R.id.edtxt);
}
}
but i have no problem in my activity. why?

see this link http://developer.android.com/reference/android/widget/EditText.html
and look here
you will find your answer

That would be because EditText is a subclass of TextView. You're casting an EditText to a TextView, which works because an EditText IS a TextView.
Check out the EditText's docs: http://developer.android.com/reference/android/widget/EditText.html
And here's a little something so that you can learn about inheritance, which is what is happening here: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

Answer is simple, it is so because EditText extends TextView.

Related

TextView settext no effect in Activity

Here's the problem with setting text in TextView in Activity. I guess the problem was that I used the same id for each fragment XMLin another activity. I used the same XML code for relative layout, but all worked well in fragments. The problem with TextView is in another activity. Please have a look at my code :
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.map_main);
TextView townNameTxt = (TextView)findViewById(R.id.town_name_txt);
TextView tradeTxt = (TextView)findViewById(R.id.trade_txt);
String townName1 = "London";
String trade1 = "buy sum";
townNameTxt.setText(townName1);
tradeTxt.setText(trade1);
}
and xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/action_bar_title_map"
android:layout_width="match_parent"
android:layout_height="#dimen/actionbar_height"
android:background="#null"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/town_name_txt"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:ellipsize="end"
android:textSize="#dimen/actionbar_title2"
android:paddingLeft="#dimen/side_padding"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/trade_txt"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:ellipsize="end"
android:textSize="#dimen/actionbar_title2"
android:paddingLeft="#dimen/side_padding"/>
</RelativeLayout>
<org.osmdroid.bonuspack.mapsforge.GenericMapView
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/action_bar_title_map"/>
</RelativeLayout>
first what I do to fix this: I have change id of TextView. But it hasn't effect.
I tried to delete relative layout in XML, and it deletes from screen. No contact to XML file from Java file.
Sorry for bothering and thanks to all answers. The problem was a dupplicate setcontentView() method:
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.map_main);
TextView townNameTxt = (TextView)findViewById(R.id.town_name_txt);
TextView tradeTxt = (TextView)findViewById(R.id.trade_txt);
String townName1 = "London";
String trade1 = "buy sum";
townNameTxt.setText(townName1);
tradeTxt.setText(trade1);
setContentView(R.layout.map_main);
}
p.s espacially Thanks to Sabya
It is weird that you don't get an exception in this code.
Because there is no method settext(). There is method called setText().
Change the method name from settext() to setText()..
Case Sensitive problem with method settext() to setText().
Use your onCreate method from this one:
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.map_main);
TextView townNameTxt = (TextView)findViewById(R.id.town_name_txt);
TextView tradeTxt = (TextView)findViewById(R.id.trade_txt);
String townName1 = "London";
String trade1 = "buy sum";
townNameTxt.setText(townName1);
tradeTxt.setText(trade1);
}
Clean and Build project and run it again.
Hope, it will help you
If you are running it on Android Lollipop version, Uninstall and re-install the application and check.

I am getting nullpointer exception on Textview's settext(charSeq) method?

I have given my code below.I am just want to create a custom dialog in android.whenever i click on button it will show a custom dialog box.i have created a xml "alert.xml".after click on button i will show the content from string.xml
public class TriangleActivity extends Activity implements View.OnClickListener{
/** Called when the activity is first created. */
private Button bt;
private Dialog dialog;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt=(Button) findViewById(R.id.button1);
bt.setOnClickListener(this);
//Context mContext = getApplicationContext();
dialog = new Dialog(TriangleActivity.this);
dialog.setContentView(R.layout.alert);
dialog.setTitle("This is my custom dialog box");
TextView t=(TextView) findViewById(R.id.tv1);
t.setText(getString(R.string.h));
}
public void onClick(View v)
{
dialog.show();
}
}
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"
>
<Button
android:id="#+id/button1"
android:layout_width="60dp"
android:layout_height="60dp"
/>
</LinearLayout>
alert.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbarAlwaysDrawVerticalTrack="true" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv1" >
</TextView>
</LinearLayout>
</ScrollView>
string.xml
<?xml version="1.0" encoding="utf-8"?>I am getting
<resources>
<string name="hello">Hello World, TriangleActivity!</string>
<string name="app_name">Triangle</string>
<string name="h">The Royal Society of Chemistry’s interactive periodic table which </string>
</resources>
use this to find the textview:
TextView t=(TextView)dialog.findViewById(R.id.tv1);
TextView t=(TextView)dialog.findViewById(R.id.tv1);
This is ur solution dude
Modify your textView initialization.. because its unable to find the dialog view..so do this--
TextView t=(TextView)dialog.findViewById(R.id.tv1);
Hope this will help you..
if t.setText(getString(R.string.h)); is throwing exception NullPointerException, it means, either t or getString(R.string.h) is null.
t might be null, if main.xml does not have a TextView with id, #+id/tv1.
TextView text = (TextView)findviewByid(R.id.text1);

Communication between custom SurfaceView and default android's views

My problem is communication between custom GameView (extends SurfaceView) and TextView: I want to set TextView's text from inside of the GameView.
In main activity i'm using this layout file, it should explain structure of my app:
<?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"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00ff00"
>
<TextView
android:id="#+id/scoreTV"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="score: 0"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="back"
android:layout_alignParentRight="true" />
</RelativeLayout>
<org.gk.grApp.GameView
android:id="#+id/gameplayScreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
I can't change TextView's text in my GameView object, because it's impossible to touch UI thread from another.
Handler doesn't work too, because i can't give a handler's reference to GameView's constructor, that is performed after loading this xml file (a read about default constructor for xml files eg here How can I use GLSurfaceView in a LinearLayout together with other Views, such as TextView or Button?).
Do you have any idea what I should do now? Maybe my deduction is wrong, so please, tell me about this.
EDIT: I changed my xml file, instead of GameView I have now:
<LinearLayout
android:id="#+id/gameplayScreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</LinearLayout>
Also I added an argument (third) into constructor's signature:
public GameView(Context context, AttributeSet as, Handler h) { ... }
and changed my onCreate in GameplayActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gameplay);
LinearLayout ll = (LinearLayout)findViewById(R.id.gameplayScreen);
GV = new GameView(this, null, scoreHandler);
ll.addView(GV);
}
It works, now I can set TextView's text but after clicking on the back button another exception is thrown:
"Performing pause of activity that is not resumed: {org.gk.grApp/org.gk.grApp.MainMenuActivity}". I just started searching information about this.
First make a reference to the TextView on the activity level:
TextView txv;
In onCreate assign this reference:
txv = (TextView) findViewById(R.id.MyTextView);
Then make a method in your Activity after onCreate like this:
public void setTextView(final String txt){
MyActivity.this.runOnUiThread(new Runnable() {
public void run() {
txv.setText(txt);
}
});
}
Then you make a call from your custom view:
((MyActivity) getContext()).setTextView(str);

Dynamic Strings and Xml

I'm new to Android (and surely I'm not an expert Java developer), so I'm sorry for the dumb question
I would like to "capture" the id of a textView and to set its text value dynamically,
here is how I'm trying to do this:
public class AndStringTestActivity extends Activity {
/** Called when the activity is first created. */
String abc = "abcabc";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = (TextView)findViewById(R.id.test);
tv.setText(abc);
setContentView(R.layout.main);
}
}
and here is the Xml from which i'm reading the id of the textview i would like to write inside
// 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"
>
<TextView
android:id="#+id/test"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
the error i receive is the usual Application stopped unexpectedly
Thanks.
Call setContentView before trying to find the TextView. The TextView does not exist before you have called setContentView (which will inflate your XML layout)
Call setContentView(R.layout.main) before you call findViewById().

Drawing into scrollView

my problem I know is simple, but for the life of me I cant get an answer...
I have a LinearLayout that got a scrollview inside it... in the scroll view (in code) it should call a custom ScrollView to show on the application... it only shows black, nothing in it... here is my code
first the xml... simple LL with a textview, scrollview and button
<?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_height="wrap_content"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:text="test field" android:layout_alignParentTop="true"/>
<ScrollView android:layout_width="600dp"
android:layout_height="300dp"
android:id="#+id/v1"
android:fillViewport="true"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/gfx"
android:text="Gfx"
></Button>
</LinearLayout>
Now the onCreate of that activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
cw = getWindowManager().getDefaultDisplay().getWidth();
ch = getWindowManager().getDefaultDisplay().getHeight();
v = Draw2d findViewById(R.id.v1);
context = this;
setContentView(R.layout.gfx);
mDbHelper = new DbAdapter(this);
mDbHelper.open();
}
Draw2d is the class of the custom scrollView declared like so (it does have onDraw and everything. If I set the contentview to the Draw2d object directly it'll show no problem, but I need it withen a LL)
public static class Draw2d extends ScrollView{
public Draw2d(Context context) {
super(context);
}
Please any help is appreciated
Maybe because of you have nothing in the ScrollView? Try putting something in it. Check this link, you see there is another linear layout in the scrollview with "30px" layout height making the scrollview appear. If the layout_height was wrap_content, you would not see the scrollview if there is enough place on the screen for it.

Categories

Resources