may I know is it possible I use the resource id but I didnt set the Content View in the activity?
for example: abc.xml have one TextView id = "R.id.Text". Can I use the use TextView name = (TextView)findViewById(R.id.Text); method in the activity which was setContentView(R.layout.def);?
P/S: sorry about my bad english, hope you guys understand what I'm talking about.
Thanks
Short answer: no. The code setContentView(R.layout.def); loads only the views from def.xml, not abc.xml. You have to use another method to create the TextView or include it in def.xml.
For another method to use for separate XML files, check out http://developerlife.com/tutorials/?p=303
Related
I am attempting to set a Preference layout in code using setLayoutResource() . But this method requires an int id. I have a preset RelativeLayout that I want to pass as an argument, but I get an error saying Preference cannot be applied to RelativeLayout. I suspect it is because I am not passing an R.layout.id. I need this to work because I will addPreferences dynamically using instances of the same layout with different attribute setting. How can I make this work? Thank you. Sample code below.
rLay=(RelativeLayout)View.inflate(Context,R.layout.account_item, null);
nameView =(TextView) rLay.findViewById(R.id.account_name);
numberView =(TextView) rLay.findViewById(R.id.number_name);
pView = new Preference(Context);
pView.setLayoutResource(rLay); ///ERROR HAPPENS HERE///
If you use same layout with dynamically-changing data,
you can create AwesomePreference extends Preference,
then override onBindView(View) to changing TextView's text or something like that.
This is working like ListView's adapter#getView().
Also don't forget to call Preference#notifyChanged() if you changed bound data.
hope this will help.
I need to exchange data between activity and it's layout xml in Android. But I do not find a way to do this in Android. For example, views and controller in mvc pattern always has a way to exchange data between. So I am wondering is there any way to exchange data between them to should I refresh my mind and realize there is no such way?
use below code to get value from layout in your activity
String value;
EditText editText= (EditText) findViewById(R.id.editText);
value=editText.getText();
code in xml example:
<ImageView
android:id="#+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
in java class (like onCreate()):
ImageView image = (ImageView) findViewById(R.id.image_view);
Then you can do what you want with image
I believe you're unsure exactly what you're asking. If you want to exchange information, such as ID or text entered into a textfield then any good android tutorial should be-able to demonstrate this. Considering your last comment I think you're talking about GET and POST based technologies which can be done usingREST andSOAP, or both if you want.
This questions answer has a good implementation and definition of what both of these webservices are.
P.S. If this is what you're looking for then upvote that answer.
As some additional info, the "view" (XML Layout file) gets set by your activity initially on your activity's onCreate method. Right after you call it's parent method (super.onCreate()).
To maintain scope throughout the activity I tend to declare all the layout widgets that I need to the activity to interact with outside of any methods and within the class.
TextView textWelcomeMessage;
public void MyActivity(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// before calling setContentView() we have the option to change
// window features ex requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_my_activity);
// Now to set the textview
textWelcomeMessage = (TextView) findViewById(R.id.textWelcomeMessage);
// Set some data
textWelcomeMessage.setText("Hello, welcome to my activity");
}
It's not exactly like traditional php style mvc since static typing changes thing up a bit and we have to worry about scope. But the core principles can still apply as far as data abstraction and separation go. Hope this helps =)
Let me explain:
I need to show the name of any building block either it is imagebutton, edittext in my textview field depending upon which of above written will be hover over by the user.
So that my textview could behave like some dynamic display plate.
Any help will highly be appreciated.
mrana..
So something that you can do. Since there it no "setText" for imageviews, you can do something like
String name = "imageview";
imageView.setTag(name);
Then in your onFocusedChangedListener call the following method
void displayInTextView(View selectedView) {
String viewName = (String) selectedView.getTag();
mDisplayText.setText(viewName);
}
Since this is a touch device, "hovering" will not be possible. One solution is to show the name on when long-press. See this solution https://stackoverflow.com/a/4433441/1227692
EDI: Thanks Frank and mrana for pointing out. I agree and take back my comment.
I have just tried to display the value of the variable but is showing some kind of exception
that is FORCE CLOSE.
the code i have tried is
TextView myTextView = (TextView) findViewById(R.id.result9);
myTextView.setText("your score is " +count);
considered count=0 intially.
Can any one suggest me for this problem
thanks in advance
if myTexyView is in dialog you have to do this:
myTextView = (TextView) dialog.findViewById();
anyway do clean project...this happens sometimes:
in eclipse: project->clean->your project
Seems findViewById() returns null. It means that either there's no widget with id result9 in current layout or your forgot to setContentView().
can please check your xml which one you have set, setContainView() it mast have TextView with "result9" this id.
This should be simple, but driving my crazy.
I have the following in my layout, not problems.
<TextView
android:id="#+id/birdinfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#00009c"
android:text="The Robin is a popular bird"
/>
Then I have these arrays which are set up with the list of string resources I have
private Integer[] DetailIds = {
R.string.barnaclegoose,
R.string.barnowl,
R.string.bewicksswan,
R.string.blackbird,
R.string.blackcap_male};
So I simply want to do this!
TextView detail = (TextView)findViewById(R.id.birdinfo);
detail.setText(DetailIds[0]);
setContentView(R.layout.main);
But this causes a force close error.
The string resource looks like this ( without header and footer info of course
<string name="barnaclegoose">What a wonderful goose!</string>
Added to this problem is if I use the resource directly to the resource
detail.setText(R.string.barnaclegoose);
For example, I still get a null exception! I'm sure I've done this before, but maybe I'm missing the obvious???
Any ideas appreciated.
( Eclipse, Android 1.5, Emulator with 1.5 )
I realize this is very old, but it came up in a search... Anyways, you need to call setContentView() before findViewById() etc:
setContentView(R.layout.main);
TextView detail = (TextView)findViewById(R.id.birdinfo);
detail.setText(DetailIds[0]);
your problem is for this line setContentView(R.layout.main);
at first you must define this this line then setText your textView
Thanks for the answer. but if you mean R.string.barnaclegoose for example, this is an integer value for the ID pointing to the string itself in the resource.
Anyway, I finally got it working by just creating the view inline instead of using an resource view.
For example
TextView t= new TextView(ctx);
t.setId(2);
t.setTextColor(Color.BLACK);
t.setText(DetailIds[bird]);
mLinearLayout.addView(t,params);
mLinearLayout.setBackgroundColor(Color.WHITE);
setContentView(mLinearLayout);
And that works perfectly.