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);
}
Related
I have a fragment which fetch items from rest api and display the item title in LinearLayout within a scrollview.
Below are my views and codes.
fragment_cards_view.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView"
android:fillViewport="true">
<LinearLayout
android:id="#+id/linear_card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#color/wallet_holo_blue_light"/>
</ScrollView>
list_item.xml
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:layout_marginBottom="10dp">
<TextView
android:id="#+id/list_titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
style="#style/item_header"/>
</RelativeLayout>
</RelativeLayout>
TitleFragment.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(TAG, "In onCreateView");
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_cards_view, container, false);
mLinearLayout = (LinearLayout)v.findViewById(R.id.linear_card_view);
return v;
}
private void createUpcomingView() {
int counter = 0;
for (User user : mUsers) {
LayoutInflater inflater = null;
inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mLinearView = inflater.inflate(R.layout.list_item, null);
mLinearView.setId(user.getId());
TextView titleTextView = (TextView)mLinearView.findViewById(R.id.list_titleTextView);
titleTextView.setText("title " + counter);
mLinearLayout.addView(mLinearView);
counter++;
}
}
The problem I faced is that if the number of items is lesser than the screen height, the display becomes incorrect.
This screenshot has 4 items, but it is only showing the first item and the margins are not aligned correctly.
This screenshot has many items and the margins are aligned correctly.
This screenshot is the first solution:
Remove following layout from list_item.xml then it will work properly.
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:layout_marginBottom="10dp">
</RelativeLayout>
Try writing
inflater.inflate(R.layout.list_item, mLinearLayout, false);
instead of
inflater.inflate(R.layout.list_item, null);
Also, use wrap_content heights in list_item.xml
I have found the solution based on the answers from #clemp6r and #Jain Nidhi.
fragment_cards_view.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView"
android:fillViewport="true">
<LinearLayout
android:id="#+id/linear_card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/wallet_holo_blue_light"/>
</ScrollView>
list_item.xml, add left and right margin, removed nested RelativeLayout, set all layout_height to wrap_content
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<TextView
android:id="#+id/list_titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
style="#style/item_header"/>
</RelativeLayout>
TitleFragment.java, modified the 2nd and 3rd argument of inflate.
private void createUpcomingView(..) {
View mLinearView = inflater.inflate(R.layout.list_item, mLinearLayout, false);
}
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)?
I am trying to create an array in which each item will have an image button and below that image button will be a text view in center . Can anybody tell me how to do this. I tried the below code but not getting it worked
but=new ImageButton(this);
but.setFocusableInTouchMode(true);
but.setId(1);
imbrelp= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
rel.addView(but,imbrelp);
tv= new TextView(this);
tvrelp= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
tvrelp.addRule(RelativeLayout.BELOW, but.getId());
tvrelp.addRule(RelativeLayout.CENTER_HORIZONTAL, but.getId());
rel.addView(tv, tvrelp);
setContentView(rel);
I would avoid trying to create a RelativeLayout in code. It's going to be too messy for what you want to do. Instead, create an xml layout file for your individual item, and then inflate it for each item in your array.
Container layout in res/layout/main.xml:
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
Item layout in res/layout/item.xml:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/image_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_image" />
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/image_button"
android:layout_centerHorizontal="true"
android:text="#string/my_text" />
</RelativeLayout>
Code in your activity:
#Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
ViewGroup container = (ViewGroup) findViewById(R.layout.container);
LayoutInflater inflater = LayoutInflater.from(this);
Item[] items = getMyArrayOfItems();
for (Item i : items) {
View itemView = inflater.inflate(R.layout.item, container, false);
ImageButton button = (ImageButton) itemView.findViewById(R.id.image_button);
TextView textView = (TextView) itemView.findViewById(R.id.text_view);
// TODO set the text and images
container.addView(itemView);
}
}
Anyone know why my button isn't showing up in the dialog window?
Dialog d = new Dialog(AddContact.this);
LayoutInflater li = (LayoutInflater) getSystemService(Service.LAYOUT_INFLATER_SERVICE);
ViewGroup contentView = (ViewGroup) li.inflate(R.layout.dialog,null);
d.setContentView(contentView);
d.setTitle("Please correct these errors:");
TextView error = (TextView) contentView.findViewById(R.id.textView1);
Button closer = (Button) contentView.findViewById(R.id.button1);
closer.setText("Close");
error.setText(errorMessage);
d.show();
This my dialog.xml layout file:
<?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="wrap_content"
android:orientation="vertical" android:padding="5dp">
<TextView android:text="TextView" android:id="#+id/textView1"
android:layout_height="fill_parent" android:layout_width="fill_parent" />
<Button android:text="Button" android:id="#+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:gravity="center" />
</LinearLayout>
What do I need to do so my button shows in the dialog window?
Your dialog is not visible when you use findViewById(), which means that the view with that id is not yet in your view hierachy and can not be found. This results in error beeing null, which will throw a NullPointerException in the following line.
You can solve this by inflating the layout seperately and use findViewById() on the inflated view.
Dialog d = new Dialog(AddContact.this);
LayoutInflater li = (LayoutInflater) getSystemService(Service.LAYOUT_INFLATER_SERVICE);
ViewGroup contentView = (ViewGroup) li.inflate(R.layout.dialog, null);
d.setContentView(contentView);
d.setTitle("Please correct these errors:");
error = (TextView) contentView.findViewById(R.id.textView1);
error.setText(errorMessage);
d.show();
I'm working on a layout where I use a ListView with RelativeLayout line items. The lineitems themselves are not displaying correctly.
The issue is that the txtVideoDuration TextView is drawn at the top of the line item instead of the bottom. Because of this the txtVideoTitle gets a height of 0. As you can see in the XML the txtVideoDuration is supposed to be clamped to the bottom of the ListView.
My goal is to have the layout resemble the tutorial that google gave me. Example: http://android-developers.blogspot.com/2009/02/android-layout-tricks-1.html
I'm using Android 2.0.1. I've even plugged in the example layout with out modification into the ListView and the same behavior happens.
<?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="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/imgVideoThumbnail"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:minHeight="64dip"
android:layout_marginRight="6dip"
android:src="#drawable/icon" />
<TextView
android:id="#+id/txtVideoDuration"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_toRightOf="#+id/imgVideoThumbnail"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:singleLine="true"
android:ellipsize="marquee"
android:text="0:00:00" />
<TextView
android:id="#+id/txtVideoTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/imgVideoThumbnail"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_above="#+id/txtVideoDuration"
android:layout_alignWithParentIfMissing="true"
android:gravity="center_vertical"
android:text="[Title]"
android:textColor="#FFFFFF" />
</RelativeLayout>
This simple case works. This is the root XML layout for the activity. The buggy code at the top is the line items in the list view. It seems that the ListView itself is breaking it.
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/listVideoCatalog"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="#+id/txtName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Hello World" />
</RelativeLayout>
</merge>
This is the getView Code that inflates the line item into the ListView.
static class ViewHolder
{
TextView txtTitle;
ImageView imgThumbnail;
TextView txtDuration;
Uri videoLocation;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if(convertView == null)
{
LayoutInflater li = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.listitem_videolineitem, null);
holder = new ViewHolder();
holder.txtDuration = (TextView) convertView.findViewById(R.id.txtVideoDuration);
holder.txtTitle = (TextView) convertView.findViewById(R.id.txtVideoTitle);
holder.imgThumbnail = (ImageView) convertView.findViewById(R.id.imgVideoThumbnail);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
VideoFile video = this.videos.get(position);
holder.txtDuration.setText(millisToTimestamp(video.videoDuration));
holder.txtTitle.setText(video.videoTitle);
// Some debugger visual code.
holder.txtDuration.setBackgroundColor(Color.GREEN);
holder.txtTitle.setBackgroundColor(Color.CYAN);
return convertView;
}
This was answered in the comment of the accepted answer. Just to make it clear, this is the answer:
Change
li.inflate(R.layout.listitem_videolineitem, null);
to
li.inflate(R.layout.listitem_videolineitem, parent, false);
How are you inflating your row views that are "buggy"? Can you provide a code snippet?
Also, android:orientation="vertical" is not valid for RelativeLayout.