setText to TextView when using the inflater - android

In my android app i have been using the RelativeLayout and inflater. so i need to set the text in textView, which i have used with Inflater, but i cannot set the text by code.
XML file activity_title
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:padding="10dp"
android:layout_height="match_parent"
android:background="#000000" >
<TextView
android:id="#+id/text_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="Text" />
</RelativeLayout>
2 xml file
<RelativeLayout
android:id="#+id/rel_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/linearLayout1"
android:layout_toRightOf="#+id/linearLayout1"
>
</RelativeLayout>
so this is the code, which using the inflater
relativeLayout = (RelativeLayout) findViewById(R.id.rel_container);
((ViewGroup) relativeLayout).removeAllViews();
for (int i = 0; i < titleCalendar.length; i++) {
getApplicationContext();
vi= (LayoutInflater)
getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.activity_title, null);
textViewC = (TextView) v.findViewById(R.id.text_title);
textViewC.setText("sdfsfasfasfasfasf");
textViewC.setTextColor(Color.parseColor("#ffffff"));
final RelativeLayout.LayoutParams params = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, addcnvrtTime);
textViewC.setLayoutParams(params);
((ViewGroup) relativeLayout).addView(v, 0, params);
}
I cannot see the text, which i set on that textview

This is to get Inflater elements:
LayoutInflater inflater = getLayoutInflater();
View customView = inflater.inflate(R.layout.YOUR_LAYOUT_INFLATER, null);
TextView text = (TextView)customView.findViewById(R.id.YOUR_TEXT_VIEW);
text.setText("BlaBlaBlaBlaBla");
Why not put TextView direct inside the Layout and simply do
TextView text = (TextView)findViewById(R.id.YOUR_TEXT_VIEW);
text.setText("BlaBlaBlaBlaBla");
?

try remove params.addRule(RelativeLayout.BELOW, addcnvrtTime)?

Related

text isn't showing after using setText() to TextView() in inflated layout

Yes, I know that similar questions were asked before but I tried many of those. So I have a problem with setting text in TextView in the inflated layout. I tried setContentView() then it works but then activity_main.xml with the menu isn't working. So I tried to inflate layout with TextView that I need. When I try to setText() it just doesn't displaying it. Here is my code:
public void bodAlg() {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = inflater.inflate(R.layout.layout1, null);
EditText editText = (EditText) vi.findViewById(R.id.bod_servers);
String[] serversArray = editText.getText().toString().split(", ");
TextView textView = (TextView) vi.findViewById(R.id.bod_serv_array);
textView.setText(Arrays.toString(serversArray));
}
And here is xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="#+id/rl_01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/ll_01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/bod_servers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cray CS-Storm, Vulcan – Blue Gene/Q, Blue Gene/Q, Stampede – PowerEdge C8220, Piz Daint – Cray XC30"
android:textColor="#color/common_google_signin_btn_text_dark_focused"
android:textSize="13sp"/>
<TextView
android:id="#+id/bod_serv_array"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="15sp"
android:textColor="#color/common_google_signin_btn_text_dark_focused"
android:paddingBottom="5dp"/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
Thank you for answer!
Thanks for comments! Problem is solved!
public void bodAlg() {
LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = inflater.inflate(R.layout.layout1, null);
EditText editText = (EditText) vi.findViewById(R.id.bod_servers);
String[] serversArray = editText.getText().toString().split(", ");
TextView textView = (TextView) vi.findViewById(R.id.bod_serv_array);
textView.setText(Arrays.toString(serversArray));
// solution
CoordinatorLayout mainL = (CoordinatorLayout) findViewById(R.id.main_view);
mainL.removeAllViews(); // remove previous view, add 2nd layout
mainL.addView(vi);
}

Using "toRightOf" programatically when inflating a layout

I have an XML layout looking like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_weight="0.1"
android:text="1"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/child"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<!-- Define these into code
<TextView android:id="#+id/first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="1"/>
<TextView android:id="#+id/second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/first"
android:layout_marginLeft="5dp"
android:textSize="20sp"
android:text="1"/>-->
</RelativeLayout>
<TextView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_weight="0.1"
android:text="1"/>
</LinearLayout>
The TextViews inside LinearLayout are not meant to be changed. RelativeLayout also consists of TextViews which are aligned 5dp away from each other. Unlike the parent layout's ones, they need to be defined inside my Java activity. This is what I have tried:
private View getGeneratedView(String[] texts) {
LayoutInflater inflater =
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View parentLayout = inflater.inflate(R.layout.parent, null);
RelativeLayout childLayout = (RelativeLayout) parentLayout.findViewById(R.id.child);
Integer lastViewId = null;
for (String text: texts) {
TextView displayedText = new TextView(this);
displayedText.setText(text);
displayedText.setTextSize(20);
RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (lastViewId != null) {
params.addRule(RelativeLayout.RIGHT_OF, lastViewId);
params.leftMargin = 5;
}
displayedText.setLayoutParams(params);
lastViewId = displayedText.getId();
childLayout.addView(displayedText);
}
return parentLayout;
}
But what the code above does is putting all of the TextViews in the center, the one on top of the other like so:
(parameter texts was new String[] {"1", "1", "1", "1"})
And this is what I am trying to get:
Can anyone point out what I am doing wrong?
Even though you are generating a new TextView for each string in texts, you do not generate an id for it. As a result, displayedText.getId() will always be -1 (View.NO_ID) and lastViewId will never be other than -1.
To correct the code, you need to generate a new id for each generated TextView. With API 17+, you can use View.generateViewId() to do this.
private View getGeneratedView(String[] texts) {
LayoutInflater inflater =
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View parentLayout = inflater.inflate(R.layout.parent, null);
RelativeLayout childLayout = (RelativeLayout) parentLayout.findViewById(R.id.child);
Integer lastViewId = null;
for (String text : texts) {
TextView displayedText = new TextView(this);
// Need API 17+ for generateViewId
displayedText.setId(View.generateViewId());
displayedText.setText(text);
displayedText.setTextSize(20);
RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (lastViewId != null) {
params.addRule(RelativeLayout.RIGHT_OF, lastViewId);
params.leftMargin = 5;
}
displayedText.setLayoutParams(params);
lastViewId = displayedText.getId();
childLayout.addView(displayedText);
}
return parentLayout;
}

Adding xml layout into my current Activity layout dynamically

i have an ArrayList of usernames for each username i want to add a xml layout (layout_one_user.xml) and put the username String into each TextView (name_user) present in the layout inflated.
my code in the main Activity is this:
ll_counts = (LinearLayout) findViewById(R.id.ll_counts);
lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LayoutInflater inflater_one_user = MainScreen.this.getLayoutInflater();
View layout_one_user = inflater_one_user.inflate(R.layout.layout_one_user, null);
TextView txt_counts = (TextView) layout_one_user.findViewById(R.id.name_user);
ImageView delete_icon = (ImageView) layout_one_user.findViewById(R.id.icon_delete);
ImageView edit_icon = (ImageView) layout_one_user.findViewById(R.id.icon_edit);
for (int i = 0; i < usernames.size(); i++) {
layout_one_user = new View(this);
txt_counts.setText( usernames.get(i) );
ll_counts.addView(layout_one_user);
}
the layout_one_user is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp"
>
<ImageView
android:id="#+id/icon_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:src="#drawable/ic_edit_black_24dp" />
<ImageView
android:id="#+id/icon_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_toLeftOf="#+id/icon_edit"
android:src="#drawable/ic_delete_black_24dp" />
<TextView
android:id="#+id/name_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/icon_delete"
android:layout_centerHorizontal="true"
android:text="text"
/>
and the layout in my main is:
<LinearLayout
android:id="#+id/ll_counts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/action_bar"
android:layout_centerHorizontal="true"
android:orientation="vertical" >
</LinearLayout>
But it doesn't appear anything on the screen.
You inflate your custom layout to layout_one_user:
View layout_one_user = inflater_one_user.inflate(R.layout.layout_one_user, null);
but then, on the very first iteration of your loop, you overwrite this variable with an empty View:
layout_one_user = new View(this);
Thus when you add layout_one_user to your layout, you're adding an "empty view" and you see no result.
It seems to me that you need to inflate a new layout for each user:
for (int i = 0; i < usernames.size(); i++) {
View layout = getLayoutInflater().inflate(R.layout.layout_one_user, ll_counts, false);
TextView textView = (TextView) layout.findViewById(R.id.name_user);
textView.setText(usernames.get(i));
ll_counts.addView(layout);
}

How to inflate new copies of an ImageView object on a layout?

What I am trying to do is create N (in this case 9) copies of the ImageView object R.id.tile, place each of them at different coordinates on the layout, and give each its own unique identifier.
board_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/backgroundcolor"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/topbar"
... >
<ImageButton
android:id="#+id/imagebutton"
... />
</LinearLayout>
<RelativeLayout
android:id="#+id/board"
android:layout_width="match_parent"
android:layout_height="345dp"
android:layout_centerVertical="true"
android:background="#drawable/wwfboard" >
<view class="languid.legend.xsolver.DrawView"
android:id="#+id/myView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ImageView
android:id="#+id/tile"
android:layout_width="21dp"
android:layout_height="21dp"
android:src="#drawable/tile" />
</RelativeLayout>
</RelativeLayout>
BoardLayout.class:
#Override
protected void onCreate(Bundle savedInstance)
{ super.onCreate(savedInstance);
setContentView(R.layout.board_layout);
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View[] tiles = new ImageView[9];
entire_layout = (RelativeLayout)findViewById(R.layout.board_layout);
for(int i = 0; i<tiles.length; i++){
tiles[i] = (ImageView) inflater.inflate(R.layout.board_layout, null);
tiles[i].setId(1000+i);
params = new RelativeLayout.LayoutParams(-1, 345);
params.leftMargin = 32*2*i;
params.topMargin = 34*2*i;
entire_layout.addView(tiles[i]);
}
...
However, I keep getting "Source not found" errors on the last line: layout.addView(tiles[i]);..
Any ideas why?
(Side question: is RelativeLayout better than AbsoluteLayout if you're working with coordinates?)
Edit:
First create a layout with one ImageView with the properties you like
Example:
create file in res/layout/singleimage.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/image2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/ic_launcher"
android:contentDescription="Sample"/>
And then inflate the ImageView to get the copies of it like this
View[] tiles = new ImageView[9];
// get reference to LayoutInflater
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for(int i = 0; i<tiles.length; i++) {
//Creating copy of imageview by inflating it
ImageView image = (ImageView) inflater.inflate(R.layout.singleimage, null);
tiles[i] = image;
tiles[i].setId(i);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(-1, 345);
params.leftMargin = 32*2*3;
params.topMargin = 34*2*3;
layout.addView(tiles[i]);
}
You have set id to i value here
tiles[i].setId(i);
But you are (incorrectly) trying to get it from your other resource (the one you wanted to 'clone'):
layout.addView(tiles[i] = (ImageView) findViewById(R.id.tile)); // INCORRECT
Instead, set the id manually as above, and then:
layout.addView(tiles[i]);
And there is no need to call findViewById()

Adding a ViewGroup to a ViewGroup

I would like to be able to programmatically add a viewgroup to another viewgroup. Both have been defined in xml as follows, along with my onCreate method:
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/firstll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="first text" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="second text" />
secondlayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/secondll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="first text" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="second text" />
onCreate:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Context context = getBaseContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.main, null);
LinearLayout tv = (LinearLayout) inflater.inflate(R.layout.secondlayout, null);
tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
ll.addView(tv);
setContentView(ll);
}
You are trying to find a view before you set the content view when you do findViewById(R.layout.secondlayout). Also, secondlayout isn't the id of a view, it's the name and id of the layout file.
Try doing
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.main, null);
LinearLayout tv = (LinearLayout) inflater.inflate(R.layout.secondlayout, null);
tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
ll.addView(tv);
setContentView(ll);

Categories

Resources