I am new to Android. I tried to add TableRows to a TableLayout but it doesn't work. I have no idea why it doesn't show. I have tried to look from the internet, but seems like my problem isn't the same. I have no exceptions or erros in the logcat. I tried to debug and found out that the views are added but for some reason they don't show. Does anyone have an idea about this? Thanks in advance.
My code:
private void addInfosToTable(){ //this is called in onCreate() function
//these are just dummy infos i want to test the how the table works
Cell cell = new Cell("1234",5000,3000);
cell.setStatus(Cell.Status.COMPLETE);
Cell cell1 = new Cell("1234",5000,3000);
cell1.setStatus(Cell.Status.FAILED);
addCellRowToTable(cell);
addCellRowToTable(cell1);
}
private void addCellRowToTable(Cell cell){
//init cell info
String cellID = cell.getCellID();
double targetSpeed = cell.targetSpeed();
double targetPoint = cell.targetPoint();
Cell.Status status = cell.getStatus();
//create tableRow
TableRow tableRow = new TableRow(this);
tableRow.addView(getTextView(cellID,true));
tableRow.addView(getTextView("n.a"+"/ "+"n.a",false));
tableRow.addView(getTextView(targetSpeed+"/ "+targetPoint,false));
tableRow.addView(getStatusView(status));
tableRow.setPadding(0,R.dimen.table_marginVertical,R.dimen.table_marginVertical,0);
tableRow.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.MATCH_PARENT));
//add tableRow to tableLayout
this.cellTable.addView(tableRow);
}
private TextView getTextView(String text, boolean isFirst){
TextView textView = new TextView(this);
textView.setTextSize(R.dimen.table_textSize);
textView.setText(text);
textView.setTextColor(ContextCompat.getColor(this,R.color.black));
if (isFirst){
textView.setPadding(R.dimen.table_marginVertical,0,0,0);
}
return textView;
}
private ImageView getStatusView(Cell.Status status){
ImageView imageView = new ImageView(this);
switch (status){
case COMPLETE:
imageView.setImageResource(R.drawable.success);
return imageView;
default:
imageView.setImageResource(R.drawable.failed);
return imageView;
}
}
My table has 4 columns, and it looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="superapp.networkapp.MainActivity"
android:focusableInTouchMode="true">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.google.android.gms.maps.MapFragment"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="false"
android:layout_alignParentEnd="false">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/warning_box"
android:padding="10dp"
android:layout_margin="3dp"
android:weightSum="1">
<TextView
android:layout_width="276dp"
android:layout_height="wrap_content"
android:text="#string/warning_text"
android:id="#+id/main_boxText"
android:layout_margin="3dp"
android:textColor="#color/black"
android:textStyle="bold"
android:layout_weight="0.49"
android:textSize="12sp"
android:textIsSelectable="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/main_boxIcon"
android:src="#drawable/warning_icon"
android:layout_weight="0.47" />
</LinearLayout>
<TableLayout
android:stretchColumns = "*"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/cellListTable">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/blue"
android:paddingTop="#dimen/table_marginVertical"
android:paddingBottom="#dimen/table_marginVertical"
android:id="#+id/cellTableHeading">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/cells"
android:id="#+id/cell"
android:layout_column="0"
android:textColor="#color/white"
android:textColorHighlight="#color/blue"
android:textSize="#dimen/table_textSize"
android:textIsSelectable="true"
android:paddingLeft="#dimen/table_marginVertical" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/table_current_info"
android:id="#+id/currentInfo"
android:layout_column="1"
android:textColor="#color/white"
android:textColorHighlight="#color/blue"
android:textSize="#dimen/table_textSize"
android:textIsSelectable="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/table_target_info"
android:id="#+id/targetInfo"
android:layout_column="2"
android:textColor="#color/white"
android:textColorHighlight="#color/blue"
android:textSize="#dimen/table_textSize"
android:textIsSelectable="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/status"
android:id="#+id/status"
android:layout_column="3"
android:textColor="#color/white"
android:textColorHighlight="#color/blue"
android:textSize="#dimen/table_textSize"
android:textIsSelectable="true"
android:textAlignment="textEnd"
android:paddingRight="#dimen/table_marginVertical" />
</TableRow>
</TableLayout>
</LinearLayout>
I have no idea why it doesn't show. I have tried to look from the internet, but seems like my problem isn't the same. I have no exceptions or erros in the logcat. I tried to debug and found out that the views are added but for some reason they don't show. Does anyone have an idea about this? Thanks in advance.
UPDATE
I edited the layout height of the default view to all wrap_content. I also added this line of code: before adding the tableRow to the tableLayout. But it still does not work.
tableRow.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.MATCH_PARENT));
you have to define a layout for each table row and bind the textViews and ImageView to the corresponding elements in the layout.
Related
I have a ListView with a TextView (which has an integer as string) and a Seekbar.
I need to extract the integer value and set it to the Seekbar.
Unfortunately, when I do this, I get a NullPointer exception because the content that populates the ListView item is not recognized by the onCreateView of the PlaceholderFragment.
String progressValue = percentileTextView.getText().toString();
bar.setProgress(Integer.parseInt(progressValue));
I have an activity layout for this activity, then a fragment layout which contains the listview and then I have a content.xml where the actual TextView is present. My question is how to reference this TextView? Am I doing this right, i.e. Activity layout contains a ConstraintLayout with Toolbars etc, then the Fragment Layout contains the ListView and I am using a separate content.xml and applying that to the ListView via a SimpleAdapter.
newAdapter = new SimpleAdapter(getContext(), newList,
R.layout.content, new String[] {
"name",
"percent"
},
new int[] {
R.id.name1,
R.id.percentile1
});
I hope you understand my problem.
When I do the following, it works (i.e. Android Studio understand that is the TextView) - but throws a null pointer exception upon build.
TextView percentileTextView = (TextView) rootView.findViewById(R.id.percentile1);
UPDATE 1 for pskink:
Newlist contents:
[{TA=Item1, IA=46}, {TA=Item2, IA=98}, {TA=Item3, IA=70}, {TA=Item4, IA=54}, {TA=Item5, IA=99}, {TA=Item6, IA=98}]
Row item layout (content.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="wrap_content"
android:orientation="vertical"
android:padding="#dimen/activity_horizontal_margin">
<TextView
android:id="#+id/name1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:textStyle="bold"
android:textColor="#color/colorAccent" />
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:padding="5dp">
<TextView
android:id="#+id/greater_than"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="#string/greater_than" />
<TextView
android:id="#+id/percentile1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:layout_toRightOf="#id/greater_than"
android:layout_toEndOf="#id/greater_than"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:layout_toRightOf="#id/percentile1"
android:layout_toEndOf="#id/percentile1"
android:text="#string/percentile_text"/>
</RelativeLayout>
<SeekBar
android:id="#+id/seekBar2"
android:paddingTop="12dip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end|bottom"
android:paddingTop="12dip"
android:orientation="horizontal">
<ImageView
android:contentDescription="info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/someimg" />
</LinearLayout>
</LinearLayout>
A page on my app has a table layout page, and one row of it seems to not be working. My goal is to evenly split the page between the left cell and right cell. The left cell contains a string, the right cell an image and string. I have tried to do so using weighSum. The left cell got weight of 4/8, and right cell was put into a linear layout, within which the image got weight of 1/8 and the string got weight 3/8. However, in the layout the left cell is taking up the great majority of the row, about 75% and I'm not sure why.
My rows below this one attempt almost the same thing, but don't have the same problem.
My xml(cut for relevancy):
<TableRow
android:layout_width="match_parent">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Class"
android:id="#+id/classTextView"
android:layout_marginTop="14dp"
android:layout_weight="1"
android:gravity="center"/>
<LinearLayout>
<View
android:layout_width="4dp"
android:gravity="left"
android:layout_height="match_parent"
android:background="#663300"/>
<ImageView
tools:ignore="ContentDescription"
android:id="#+id/classicon"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/barbarianicon50"
android:layout_gravity="end"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Class Value"
android:id="#+id/classValueView"
android:layout_marginTop="14dp"
android:layout_weight="3"
android:gravity="left"/>
</LinearLayout>
</TableRow>
<View
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="#663300"/>
<TableRow android:layout_width="match_parent">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Strength"
android:id="#+id/strengthTextView"
android:layout_marginTop="20dp"
android:layout_weight="4"
android:gravity="center" />
<LinearLayout>
<View
android:layout_width="4dp"
android:gravity="left"
android:layout_weight="0"
android:layout_height="match_parent"
android:background="#663300"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Strength Value"
android:id="#+id/strengthValView"
android:layout_marginTop="20dp"
android:layout_weight="4"
android:gravity="center" />
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Intelligence"
android:id="#+id/intelligenceTextView"
android:layout_marginTop="20dp"
android:layout_weight="4"
android:gravity="center" />
<LinearLayout>
<View
android:layout_width="4dp"
android:gravity="left"
android:layout_weight="0"
android:layout_height="match_parent"
android:background="#663300"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Intelligence Value"
android:id="#+id/intelligenceValView"
android:layout_marginTop="20dp"
android:layout_weight="4"
android:gravity="center" />
</LinearLayout>
</TableRow>
The first table row is the problematic one - it should be evenly split between the left and right cells, but is mostly towards the right.
The second table row is an example of it working correctly - this row has 2 pairs of cells, but each of the cells, and the pairs of cells are correctly splitting the width between themselves evenly.
I can't figure out why the first doesn't work. I appreciate any help!
I have finally succeeded in fixing the layout!
The method used was to <include /> another layout as a row element inside this one. All these row elements had the same data, so I then programmatically filled the row elements with their necessary data whenever the view changed. You could also do this as a ListView, with each row in the ListView being another pair of elements, but I'm not sure how well that works for the 4 element wide rows.
The XML of each row:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/statNameString"
android:id="#+id/statName"
android:layout_marginTop="30dp"
android:gravity="center" />
<View
android:layout_width="4dp"
android:gravity="left"
android:layout_height="match_parent"
android:background="#663300"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/statValString"
android:id="#+id/statVal"
android:layout_marginTop="30dp"
android:gravity="center" />
</LinearLayout>
The top row(which represented the Class of the Character in my app) needed to include an ImageView, so it had a slightly changed layout. The ImageView was placed after the bar View, and before the last TextView.
The XML including each row into the final layout, for the Land layout:
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
layout="#layout/horizontal_class_row_layout"
android:id="#+id/strValues"
android:layout_column="0" />
<include
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
layout="#layout/horizontal_class_row_layout"
android:id="#+id/intValues"
android:layout_column="0" />
</TableRow>
If you only wanted 1 pair of elements in each row, you can simply remove one of the include tags. As mentioned before, you could also use the ListView to do these, and fill the data, and I will probably modify it to do that eventually.
Finally, you programmatically fill the elements with their data in the Java, so they don't just say "Class" and "Class Value" or whatever default data:
private void fillViewValues() {
//Gather views
LinearLayout llClass = (LinearLayout)findViewById(R.id.classValues);
LinearLayout llStr = (LinearLayout)findViewById(R.id.strValues);
LinearLayout llDex = (LinearLayout)findViewById(R.id.dexValues);
LinearLayout llCon = (LinearLayout)findViewById(R.id.conValues);
LinearLayout llInt = (LinearLayout)findViewById(R.id.intValues);
LinearLayout llWis = (LinearLayout)findViewById(R.id.wisValues);
LinearLayout llCha = (LinearLayout)findViewById(R.id.chaValues);
//Get the layout's locations
TextView classNameView = (TextView) llClass.findViewById(R.id.statName);
TextView classValView = (TextView) llClass.findViewById(R.id.statVal);
ImageView classImage = (ImageView) llClass.findViewById(R.id.classicon);
TextView strNameView = (TextView) llStr.findViewById(R.id.statName);
TextView strValView = (TextView) llStr.findViewById(R.id.statVal);
TextView dexNameView = (TextView) llDex.findViewById(R.id.statName);
TextView dexValView = (TextView) llDex.findViewById(R.id.statVal);
TextView conNameView = (TextView) llCon.findViewById(R.id.statName);
TextView conValView = (TextView) llCon.findViewById(R.id.statVal);
TextView intNameView = (TextView) llInt.findViewById(R.id.statName);
TextView intValView = (TextView) llInt.findViewById(R.id.statVal);
TextView wisNameView = (TextView) llWis.findViewById(R.id.statName);
TextView wisValView = (TextView) llWis.findViewById(R.id.statVal);
TextView chaNameView = (TextView) llCha.findViewById(R.id.statName);
TextView chaValView = (TextView) llCha.findViewById(R.id.statVal);
//Fill those values
//Names of sections
classNameView.setText(getResources().getString(R.string.classString));
strNameView.setText(getResources().getString(R.string.strString));
dexNameView.setText(getResources().getString(R.string.dexString));
conNameView.setText(getResources().getString(R.string.conString));
intNameView.setText(getResources().getString(R.string.intString));
wisNameView.setText(getResources().getString(R.string.wisString));
chaNameView.setText(getResources().getString(R.string.chaString));
//Values
classValView.setText(classString);
setImage(classImage, classString);
strValView.setText(String.format("%d", finalStats[0]));
dexValView.setText(String.format("%d", finalStats[1]));
conValView.setText(String.format("%d", finalStats[2]));
intValView.setText(String.format("%d", finalStats[3]));
wisValView.setText(String.format("%d", finalStats[4]));
chaValView.setText(String.format("%d", finalStats[5]));
}
This follows the advice at at this StackOverflow post.
One thing I would like to note, is that when you change the layout, you need to reset the content view to your current layout. In my case, the function looked like:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
setContentView(R.layout.activity_see_character);
fillViewValues();
}else if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
setContentView(R.layout.activity_see_character);
fillViewValues();
}
}
where fillViewValues() is the previous function. I believe that this sets the layout to either the portrait or landscape version of the layout, which can then have its elements accessed. If you don't do this when you try to access the elements it can't find them and gives a NullPointerException.
I am making one chat application in this application I have to display all users names who are connected to login user,so I am showing this data in dialog,actually problem is different text views with different styles in relative layout which wiil be arranged automatically side by side,if layout width occupied by 3 text views remaining two text views should be in third line like that.please help me
ex:
car bus bicycle
train ship bike
aeroplane
Try this:
<LinearLayout
android:id="#+id/categoriesContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:background="#color/view_background"
android:orientation="vertical" >
<TextView
android:id="#+id/tagsTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="4dp"
android:text="#string/title"
android:textColor="#color/view_title"
android:textSize="18sp" />
<LinearLayout
android:id="#+id/tagsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="8dp"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:gravity="left"
android:orientation="vertical" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="horizontal"
android:padding="2dp" >
<Button
android:id="#+id/categoryIndications"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:background="#drawable/xml_category_count_bubble"
android:enabled="false"
android:minWidth="20dp"
android:padding="4dip"
android:textColor="#FFFFFF"
android:textSize="14sp"
android:textStyle="bold" />
<Button
android:id="#+id/categoryName"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:background="#drawable/xml_category_name_bubble"
android:enabled="false"
android:minWidth="40dp"
android:padding="4dip"
android:textColor="#color/view_text"
android:textSize="14sp" />
And this:
private void setTags(){
LinearLayout tagsContainer;
// Categories
tagsContainer = (LinearLayout) rootView.findViewById(R.id.tagsContainer);
// Container of the tags
tagsContainer.removeAllViews();
tagsContainer.refreshDrawableState();
// Indicate category layout
LinearLayout indicateLayout = (LinearLayout)
LayoutInflater.from(getActivity().getApplicationContext()).
inflate(R.layout.message_indicate_category, null);
if(YOUR_TEXTVIEW_STRINGS_ARRAY.size()>0){
Button textButton;
LinearLayout tag;
for(int i = 0; i<YOUR_TEXTVIEW_STRINGS_ARRAY.size();i++){
// View of the tag
tag = (LinearLayout)
LayoutInflater.from(getActivity().getApplicationContext()).
inflate(R.layout.customview_tag, null);
textButton= (Button)tag.findViewById(R.id.categoryName);
textButton.setText(YOUR_TEXTVIEW_STRINGS_ARRAY..get(i));
textButton = (Button)tag.findViewById(R.id.categoryIndications);
textButton.setText(YOUR_TEXTVIEW_STRINGS_ARRAY.get(i));
tagsContainer.addView(tag);
}
}
else{
// View of the tag
indicateLayout = (LinearLayout)
LayoutInflater.from(getActivity().getApplicationContext()).
inflate(R.layout.message_indicate_category, null);
tagsContainer.addView(indicateLayout);
}
}
I'm programatically adding views into my layout inside a for loop.
The xml file of the layout I'm adding is like this one:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout_consulta_item_list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView_consulta_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/LinearLayout_dados_consulta"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/LinearLayout_dados_consulta"
android:layout_centerHorizontal="true"
android:layout_centerInParent="false"
android:adjustViewBounds="true"
android:maxWidth="70dp"
android:padding="10dp"
android:scaleType="centerInside"
android:src="#drawable/estrela_off" />
<LinearLayout
android:id="#+id/LinearLayout_dados_consulta"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_toLeftOf="#+id/imageView_consulta_action"
android:background="#660000"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="#string/origem"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#ffaaaa"
android:textSize="10sp" />
<TextView
android:id="#+id/textView_origem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="YYY"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#eeeeee" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:text="#string/data_ida"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#ffaaaa"
android:textSize="10sp" />
<TextView
android:id="#+id/textView_data_ida"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2014-05-05"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#eeeeee"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
In my main_activity, I call the constructor:
for (int i=0;i<resultList.length-1;i++){
RelativeLayout viewToLoad = (RelativeLayout)LayoutInflater.from(this).inflate(R.layout.consulta_item, null);
ll.addView(viewToLoad);
ImageView ImgConsulta_action = (ImageView)findViewById(R.id.imageView_consulta_action);
LinearLayout dados_consulta = (LinearLayout)findViewById(R.id.LinearLayout_dados_consulta);
dados_consulta.setOnLongClickListener(...);
ImgConsulta_action.setOnLongClickListener(...);
Well, I keep setting SetText for all the views in the layout and so on.
But, in order to make the setOnClickListener to work for each view, I must set a id to the views inside the for loop:
dados_consulta.setId(4000+i);
ImgConsulta_action.setId(5000+i); //(*lastline)
The weird thing is happening is that:
If I comment this last line, the layout is inserted correctly with the imageview showing a star as it is suposed to do and the listener at "dados_consulta" works fine.
But if I remove the comment at this last line, the star dissapear from the screen.
Can anybody help me to understand what is going on?
I had an layout params referenced to the ID of the layout elements at the original view.
Since I have to change the ID dynamically while I'm adding this layout several times into my activity, I also need to reset the layoutparams to the new Ids.
So, I added this lines:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)ImgConsulta_action.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_TOP, dados_consulta.getId());
params.addRule(RelativeLayout.ALIGN_BOTTOM, dados_consulta.getId());
ImgConsulta_action.setLayoutParams(params);
RelativeLayout.LayoutParams paramsDadosConsulta = (RelativeLayout.LayoutParams)dados_consulta.getLayoutParams();
paramsDadosConsulta.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
paramsDadosConsulta.addRule(RelativeLayout.LEFT_OF, ImgConsulta_action.getId());
dados_consulta.setLayoutParams(paramsDadosConsulta);
Now it's working perfectly.
Notice that the problem is not about the layout disappearing it was about a layout get in front of the other one. (making this other one invisible)
I have a layout where I put i rows dynamically in a tablelayout that is inside a scrollview. Everything is working smoothly except that I don't can get the checkbox text to wordwrap (See xml below). The text is also set at runtime. I have searched the internet and has made a lot of trying to set different properties with no luck so far.
Is this task possible to achieve?
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dip">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="#+id/bes_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<EditText
android:id="#+id/bes_kommentar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/bes_checkbox" />
</RelativeLayout>
</TableRow>
CheckBox extends TextView, so I assume you can wrap its text like they solve the issue in this post :
Android TextView Text not getting wrapped
If you cannot get it to work like that, you can maybe also try, using match_parent in the checkbox.
<CheckBox
android:id="#+id/bes_checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
hope it helps.
use table row inside the Linear layout instead of table layout..
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dip" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/bes_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="#+id/bes_kommentar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/bes_checkbox" />
</RelativeLayout>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dip" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/bes_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="#+id/bes_kommentar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/bes_checkbox" />
</RelativeLayout>
</TableRow>
</LinearLayout>
I have made dynamic layouts with checkboxes in the past and used this approach:
// Create new view to add layout to
sv = new ScrollView(this);
// Create new layout to add check boxes to
ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
// Add layout to view
sv.addView(ll);
// Create 6 check boxes
int itemSize = 6;
for(int j=0; j<itemSize; j++){
CheckBox cb = new CheckBox(this);
cb.setText("number " + j);
ll.addView(cb);
}
// Finalize view
this.setContentView(sv);
Is this what you are looking for?