Android XML primitive access - android

I need to edit an xml file, the problem is that I have multiple xmls and in all my classes/activities I can't put the xml as the main View.
So I must acces an xml file without setting it as main view on any activity.Is that even possible?
I mean I can make: View j = (View) findViewById(R.layout.mytest);
But how can I edit a button's text for ex in that mytest.xml file?

although you can inflate any layout using LayoutInflator and make any changes for that instance in it , but you can not look for a persistent change in layout . so first setContentView , then change for that view only .

So you're saying you want to dynamically edit a layout xml file??? Example, you have a layout like...
<LinearLayout>
...
<Button
android:id="#+id/myButton"
...
android:text="Some text" />
</LinearLayout>
...and you want to dynamically edit android:text="Some text" at run time???
If so, that isn't the way to do it. Simply leave the android:text attribute out of the layout xml and just set the text of the button at runtime.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContenView(R.layout.mytest);
Button btn = (Button) findViewById(R.id.myButton);
btn.setText("Whatever");
}

Related

How to add text in dynamically added EditTexts

I want to add text to my dynamically added EditTexts.
As for now its just adding a layout with two EditText in, its getting added to a LinearLayout on button click.
How do I set the text of these Editexts? Because now I can add as many as I like and every EditText has the same id as the have in the layout file.
My mainactivity.java with the inflation upon button clicklooks like this:
final LinearLayout mView = (LinearLayout) getLayoutInflater().inflate(R.layout.more_rest_main, null);
mView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
restLayout.addView(mView);
And here is my layout for more_rest_main.xml
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="#+id/added_rest_from"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:hint="#string/time_from"
android:textSize="15dp"
android:focusable="false"/>
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="#+id/added_rest_to"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:hint="#string/time_to"
android:textSize="15dp"
android:focusable="false" />
The thing to understand here is that you can look for a child view in any ViewGroup (LinearLayout is a ViewGroup).
You're probably familiar with retrieving a View in an Activity onCreate, where after calling setContentView, you have access to any View with findViewById.
So after inflating your ViewGroup, you can use the same approach you would use in an Activity onCreate, but you need to look for your ViewGroup's child views:
final LinearLayout mView = (LinearLayout) getLayoutInflater().inflate(R.layout.more_rest_main, null);
EditText txtAddedRestFrom = (EditText)mView.findViewById(R.id.added_rest_from);
restLayout.addView(mView);
Once you have your reference you can invoke any method on your EditText, such as setText.
You can create an EditText dynamically like following:
EditText editText = new EditText(myContext);
editText.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
//add the same logic as in the XML file
mView.addView(editText);
You can also set an ID dynamically. If you are using API 17 and above, do the following:
editText.setId(View.generateViewId())
Otherwise you can set a positive Integer of your choice as an ID, instead of using the:
View.generateViewId()
With the code above and since these will be added dynamically, you don't need the XML file as the EditTexts will be created dynamically.
If you want to keep the existing XML file, you can just call the
setTag()
to each one of the inflated EditTexts and set a unique value in the setTag method. That way you can distinguish the EditTexts. And since you can do that, you can add whatever text you need to each one of them.
To reply to your comment, on how to set an OnCLickListener:
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// here you can get either the Tag or the ID that
// you've set dynamically. And do whatever you like to do
view.getTag()
view.getId()
}
});

Is both Static and Dynamic Layout Possible?

I have an XML layout having a single TextView
Now I want to add 50 buttons which I want to add dynamically in my java file !.
Is it possible to add attributes to an XML file via java code ??
Or can an activity have 2 layouts at a time ??
for eg,
public class Options extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.options);
Button but=new Button(this);
but.setText("Wassup");
// How do I add this button to the layout ?
}
}
Is it possible to add attributes to an XML file via java code ??
No, but you can add properties to Views and Layouts as you are doing with setText(). resource files themselves cannot be changed after compiled.
Or can an activity have 2 layouts at a time ??
The simple answer is no but you can inflate another layout and add it to the current layout.
Example of what you can do to add a Button
Inflate your root layout and add the Buttons to it with addView(). Something like
Layoutinflater inflater = (LayoutInflater) getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.layout_file);
Button but=new Button(this);
but.setText("Wassup");
// How do I add this button to the layout ?
ll.addView(but);
LayoutInflater
Or if you want to add it to a layout in the current file you can just use findViewById() and use addView() on that to add your Buttons to.
Considering you have an xml layout as below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#id/mainlayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TextView>
</LinearLayout>
In your java code after setContentView(R.layout.options); you can do the following:
LinearLayout linearLayout=(LinearLayout)findViewById(R.id.mainlayout);
Button button=new Button(this);
linearLayout.addView(button);
Now you can add as many buttons you like into the linear layout as seen above.
Yes it is possible. After setContentView(R.layout.options); get your buttons container with findViewById(). You will have a reference to a LinearLayout, RelativeLayout or something else. After that use Layout inflater and programmatically you can add other layouts or components.
Hope it helps!
just use layout.addView() where layout is a ViewGroup that you get by calling findViewById(R.id.layoutId)

Create android buttons programmatically using XML layout as template

I have a LinearLayout that contains a TextView, and always will. There will also always be at least one button located below the TextView, but there might be more than one under certain circumstances.
I can successfully create and add as many buttons as I need programmatically. I can also successfully set whatever appearance related parameters/options that I require for these buttons programmatically.
The problem is that I don't know how to tell a programmatically created button that it should use a XML resource file, which contains the appearance and layout parameters, instead of setting these parameters programmatically.
I've looked at similarly named questions and spent time messing with the API itself, to no avail.
Edit:
Here's an approximation of what I'm trying to do that will hopefully make explanations a bit clearer for me:
private TextView textView;
private SomeObject someObject;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View scrollView = inflater.inflate(R.layout.fragment_play_game, container, false);
textView = (TextView) scrollView.findViewById(R.id.game_data_text);
textView.setText(someObject.getTextForTextView());
LinearLayout layout = (LinearLayout) scrollView.findViewById(R.id.game_data_container);
for (String optionText : someObject.getTextForButtons()) {
layout.addView(createOptionButton(optionText, layout));
}
return scrollView;
}
private View createOptionButton(String optionText, LinearLayout layout) {
Button optionButton = new Button(this.getActivity());
// set button layout/options here, somehow??
optionButton.setText(optionText);
return optionButton;
}
My XML layout file for the fragment looks like this (It's this LinearLayout that I'm trying to add buttons to):
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/game_data_container"
etc... >
<TextView
android:id="#+id/game_data_text"
etc... />
</LinearLayout>
</ScrollView>
Also, if I'm to create an XML layout file for the button (lets call it custom_button.xml) should it look something like this?:
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/play_game_option_button"
etc... />
Update:
Just to expand a bit on what MrFox# is talking about, what I did to get it working was replace this line:
Button optionButton = new Button(this.getActivity());
with this one:
Button optionButton = (Button) inflater.inflate(R.layout.play_game_option_button, layout, false);
...which inflates an xml file containing only a Button layout (the button template). In this case, it returns the root view of that file, which is just the button because there's no parent above the button in the file.
However, if I had have set the last boolean value (attachToParent) to true, it would have returned the root container that the button will be in (which is just the 'layout' variable that was passed into the call).
I can now produce as many buttons as I want using this template.
Have you thought of making a layout that is just the button with the applied XML styles and then inflating it into your linear layout?
something like:
inflater.inflate(R.layout.StyledButton, MyLinearLayout, true);
xml for your button under /res/layout/my_button_layout.xml
<Button xmlns:android="http://schemas.android.com/apk/res/android"
... />
code in your activity
myButton = (Button)inflate.inflate(R.layout.my_button_layout, null);
myView.addView(myButton);

Change visibility button from titlebar programmatically?

I have a problem button visibility. I have 2 button from titlebar.One of them edit, one of them done. First I want to see just edit button and when i clicked edit button, edit button visibility will be false and done button visibility true.
I get their id from xml and when i click one of them i want to change visibility but edit.setVisibility(); it doesnt work.What is wrong?I can see edit button.I want to change buton visibility programmatically.
Can anybody have any idea?
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final boolean customTitle = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
edit=(Button)findViewById(R.id.edit);
done=(Button)findViewById(R.id.done);
edit.setVisibility(View.INVISIBLE);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.main);
if ( customTitle ) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.main);
}
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">
<Button android:id="#+id/edit"
android:layout_width="57px"
android:layout_height="wrap_content"
android:text="edit"/>
<Button android:id="#+id/done"
android:layout_width="57px"
android:layout_height="wrap_content"
android:text="done"/>
</LinearLayout>
First, you're missing the android:orientation parameter in your LinearLayout.
Second, if you want to change between edit and done you can do this:
edit.setVisibility(View.GONE);
done.setVisibiluty(View.VISIBLE);
and the opposite to change to edit button again.. With View.INVISIBLE the button will not show but still use the space where it was.
The problem is that setFeatureInt just sets the resource ID for the title, which will cause a new inflation of the layout resource, which will be placed in a system FrameLayout called id/title_container. This can be inspected using the Hierarchy Viewer in eclipse.
Essentially, you end up with two instances of the main layout. One set as the content view (below the title) and the other set as the title. When you call findViewById, it will only look in the content view for any views matching the ID. This means that the edit and done buttons you retrieve are the ones in the content view.
If you want to access the buttons in the title area, you can use
View v = getWindow().getDecorView();
edit=(Button)v.findViewById(R.id.edit);
done=(Button)v.findViewById(R.id.done);
edit.setVisibility(View.INVISIBLE);
This will search through the whole view structure of the window, not just the content view, thus solving your problem.

How to load linearlayout dynamically after checking a condition in the class file?

Hey guys please help me out I am new to android application development
Here is the scenerio: This is my layout declaring xml file:
<LinearLayout xxx
<Textview aaa>
</TextView>
</LinearLayout>
//The below LinearLayout I need to display when it meets some condition in java class
i.e if(true) then display the following layout else dont. I can check this condition only after user provides some input.
<LinearLayout xxx
<Textview aaa>
To be displayed after the condition is checked
</TextView>
</LinearLayout>
//following layout is displayed with the first one.
<LinearLayout xxx
<Textview aaa>
</TextView>
</LinearLayout>
Any idea how to do it?
Take a few moments to read the android dev guide. It is worth the time: http://developer.android.com/guide/topics/ui/index.html
Basically, you want to use IDs to refer to the xml layout:
android:id="#+id/myxmlid"
and in your java file:
LinearLayout ll = findViewById(R.id.myxmlid);
if (yourCondition)
mainLayout.add(ll);
I'm assuming that you want to add a widgets to the current layout, rather than just change the text of the current TextView.
Also, this assumes that you want to add more than just a new TextView. If you only need that, you don't need to wrap it in a LinearLayout, which is used to add rows or columns of widgets.
You don't replace your entire layout programmatically just to change the text in one TextView. The way this kind of thing is handled in android, is to include a field in your Activity class for your textview, then instantiate it in your onCreate() method with findViewById() after you've called setContentView() to load the layout so that you can access that TextView's fields and methods.
First, you TextView needs an id in the layout xml.
<TextView android:id="#+id/sometext"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Then in your Activity...
TextView mTextView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView = (TextView) findViewById(R.id.sometext);
}
Somewhere else in the program...
public void myMethod(){
mTextView.setText("Text says this now");
}
Hopefully that gets the idea across. Good luck!
Thank Aleadam for suggesting me to read the link. Follwoing was my approach to get the output.
What I did was I assigned my LinearLayout Visibility to "GONE" (android:Visibility="GONE") when declarning the XML, and in the program after the condition is met, changed the visibility to "VISIBLE". (by using layout.setVisibility(View.VISIBLE))

Categories

Resources