How to change the text attribute of widget during run-time, android? - android

Hi I'm trying to create the layout dynamically based on my data model. But I have some problem doing that. The xml file I have is here:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#f9f9ff">
<TextView
android:layout_width="97dp"
android:layout_height="33dp"
android:id="#+id/textView" android:layout_alignParentLeft="true" android:layout_marginLeft="15dp"
android:layout_alignParentTop="true" android:layout_marginTop="19dp"/>
</RelativeLayout>
This is named as dynamic.xml. And I'm trying to inflate this layout at run-time. For that I'm doing:
ViewGroup parent = (ViewGroup) findViewById(R.id.vertical_container);
//getting the TextView and EditText view.
LayoutInflater inflater = this.getLayoutInflater();
View currentView = inflater.inflate(R.layout.dynamic,null);
TextView textView = (TextView) currentView.findViewById(R.id.textView);
//trying to change the attributes. but #fails!
textView.setText("Setting the text from code");
textView.setId(3);
//setting to the view
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.dynamic, null);
parent.addView(view);
but this doesn't seems to be working! The line :
textView.setText("Setting the text from code");
doesn't work. I can see only the empty text in my emulator but not the text Setting the text from code. Not sure where I'm making the mistake. The changes that I made in my code are not seen in my emulator.
I can see only the dynamic xml layout rendered with its attributes specified!
Not sure where I'm making the mistake. Is it possible to achieve what I'm doing here? Thanks in advance.

I think you are setting wrong view for getting result .No need of twice inflation of layout
//getting the TextView and EditText view.
LayoutInflater inflater = this.getLayoutInflater();
View currentView = inflater.inflate(R.layout.dynamic,null);
TextView textView = (TextView) currentView.findViewById(R.id.textView);
//trying to change the attributes. but #fails!
textView.setText("Setting the text from code");
textView.setId(3);
//Now you had customize your Textview .Now just add this customize view to your view
//setting to the view
parent.addView(currentView);

You are currently adding the
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.dynamic, null);
to
parent.addView(view);
But you use textView.setText("Setting the text from code");
from this
View currentView = inflater.inflate(R.layout.dynamic,null);
You are not adding the currentView to the parent
Try this parent.addView(currentView);

Related

Display text programmatically for TextView

I am looking to set a text programmatically, but for some reason, when I am doing so, no text appears on the screen. I have attached my code below.
LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View lay = inflater.inflate(R.layout.fs, null);
lsc = (TextView) lay.findViewById(R.id.fst);
lsc.setText("XYZ performed on this day.");
Any ideas? "fs" is the xml file which has a textView component, whose id is "fst".

Tag design in android using single textview and array of data

I am new to android programming and I want to achieve this in my android app:
My JSON is this:
"Keywords":["medical","science","international"],
and my Android code is this:
String keyStr = "";
for (String str : m.getKeywords())
{
keyStr += str + " ";
}
When I put it into my TextView, it shows the results like:
medical science international
But, I want to give it the effect it is shown in the above picture, which I did in my website. One solution was to use HTML.fromhtml() but it is not working for me, I need any other solution for it.
Assuming that each tag could be remove with the cross on their right, you have to create multiple TextViews and each ImageViews should have onClickListener.
From my point of view, it could be a lot of tags rendering in a small width screen, so I'd use a HorizontalScrollView. So you could create a container:
<HorizontalScrollView
android:id="#+id/horizontal_scrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:id="#+id/tags_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal" />
</HorizontalScrollView>
And regarding the number of tags, do a loop to create dynamically each tag, as follows:
LinearLayout tagsContainer = (LinearLayout) findViewById(R.id.tags_container);
for (String keyword : m.getKeywords()) {
// inflate the child view by using a custom layout
View tagView = LayoutInflater.from(this).inflate(R.layout.tag_layout, null);
TextView tagTextview = (TextView) tagView.findViewById(R.id.tags_textview);
ImageView tagImageview = (ImageView) tagView.findViewById(R.id.tags_imageview);
// set the elements value
tagTextView.setText(keyword);
tagImageView.setOnClickListener(new View.OnClickListener() {
// close the tag
}
// add to the container
tagsContainer.addView(tagView);
}

Android get data from LayoutInflater EditText

In my Activity, dynamically insert a layout that contains an EditText. The layout can have between one and N EditText. How can I get the text of all EditText ?
LinearLayout item = (LinearLayout) findViewById(R.id.rl);
final View child = getLayoutInflater().inflate(R.layout.inflate_ta, null);
item.addView(child);
MaterialEditText ed = (MaterialEditText) child.findViewById(R.id.ed12);
ed.setFloatingLabelText(edNomeCampo.getText().toString());
The best approach would be to go through each view within whatever parent view you're dynamically adding to.
so...
<LinearLayout
android:id="#+id/wrapper"..>
<EditText ... />
<EditText... />
...
...
<!--Nth EditText-->
<EditText... />
</LinearLayout>
Then once you want to get the values in each dynamically added EditText you would iterate through all views within that wrapping LinearLayout
LinearLayout yourLinearLayoutView = (LinearLayout)getView().findViewById(R.id.wrapper);
for(int i=0; i<yourLinearLayoutView.getChildCount(); i++) {
View editText= yourLinearLayoutView.getChildAt(i);
if(editText instanceof EditText){
String str = ((EditText)editText).getText();
//from here you can store str in whatever structure you wish (AWrrayList, etc.)
}
}

text not changing in a TextView while doing Layout Inflater

in my app when the activity get loaded, i am doing the following
setContentView(R.layout.main2);
layoutToAdd = (LinearLayout) findViewById(R.id.linearLayout1);
for(i=0; i<num;i++)
{
LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
View view = inflater.inflate(R.layout.camera, null);
layoutToAdd.addView(view);
}
The value of num differs for each time.
In my LayoutInflater layout i have a text view, edit text and a button.
Now they are shown according to the number of times mentioned in num, now for each time i want the text and button name to be changed. How to set the text for TextView and Button.
Just set them after you inflate the layout
View view = inflater.inflate(R.layout.camera, null);
TextView tv = view.findViewById(R.id.textviewid); //id defined in camera.xml
Button b = view.findViewById(R.id.buttonid); //id defined in camera.xml
tv.setText();
b.setText();
layoutToAdd.addView(view);
What I suggest it,you should create an xml file containing just a LinearLayout.
Then programaticall,you should create TextViews,EditTexts and Buttons and add it to LinearLayout.
You can set different properties of those components like below:
Here is example of setting TextView's properties.For rest of the components,you can set the same.
TextView tv=new TextView(context);
tv.setBackgroundResource(R.drawable.textview_bg);
tv.setPadding(20, 5, 40, 5);
tv.setText("set your text");
tv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setTextColor(Color.RED);
tv.setClickable(true);
tv.setId(id);//where id is an integer which should be unique for each TextView
layout.addView(tv);
Also you need to create and add all these three component inside the for loop,providing unique ids depending on i you use to iterate the loop!And you can have a String array for textviews and buttons to set their names in for loop,which would be easier for you for passing the string you like to set to them in loop.
You must store the reference of the view inflated and store in a List then when you want to modify simply list.get(2).findViewById(R.id.textbox_id).
Hope it help.

How do you declare part of a page programmaticaly

If I have a page whose layout is designated by XML code, but I want to possibly create a few radio buttons, say, in the middle, but decide that at runtime, how would I do it? I'm new to Android so I'm taking a stab in the dark. Would something like this work?
In the XML, add a LinearLayout to the middle of the page's XML like this:
<LinearLayout android:id="#+id/LinLayBut"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
And then in the java something like this:
public void setupRadioButtons(){
LinearLayout linLay;
linLay = (LinearLayout) findViewById(R.id.LinLayBut);
RadioGroup radGroup = new RadioGroup(this);
RadioButton radBut = new RadioButton(this);
radGroup.addView(radBut, 0);
radGroup.setText("A button");
}
This is not an efficient way to build dynamic UI. You would be better off defining the optional layout in an XML file and then inflate it when you want to use it:
public void setupRadioButtons() {
final LayoutInflater inflater =
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout buttons =
(LinearLayout) inflater.inflate(R.layout.LinLayBut, null);
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main);
mainLayout.addView(buttons);
}
The above code assumes that the radio group and buttons are defined inside the LinearLayout with id LinLayBut and you main layout id is main.
OK, thanks to unluddite, I got it to work. For those tortured souls following the thread, here's the code. The XML doesn't have a layout around it. And if I don't call the method, the radio group takes no vertical space:
<RadioGroup android:id="#+id/radButGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" />
and here's the method:
public void setupRadioButtons(){
RadioGroup radGroup;
radGroup = (RadioGroup) findViewById(R.id.radButGroup);
RadioButton radBut0 = new RadioButton(this);
radGroup.addView(radBut0, 0); //2nd arg must match order of buttons
radBut0.setText("one Button");
RadioButton radBut1 = new RadioButton(this);
radGroup.addView(radBut1, 1);
radBut1.setText("Two Button");
radBut1.setChecked(true); //which button is set

Categories

Resources