How to set the margin of a textview dynamiclly? - java

I will make a timetable for my organizer app. My problem is:
- I have make a first row and make the second and every other row like this, but dynamically.
- I have a layout.xml for this, but the margin between the Views doesn´t work.
I want make the second row like the blue row/ first row. The yellow row should be dynamically (on runtime).
My sourcecode:
public class StundenplanActivity extends AppCompatActivity {
TableLayout tl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stundenplan);
//Find the TableLayout defined in activity_stundenplan
tl = (TableLayout)findViewById(R.id.myTableLayout);
createRow();
}
public void createRow(){
/* Create a new row to be added. */
LayoutInflater inflater = getLayoutInflater();
TableRow tr = (TableRow) inflater.inflate(R.layout.tablerow_tabelle,null);
String[] day = {"Zeit", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"};
TextView[] spalte = new TextView[8];
for(int i=0; i<=7; i++){
TextView text = (TextView) inflater.inflate(R.layout.text_view_tabelle, null);
text.setText(day[i]);
spalte[i] = text;
}
for(int i=0; i<=7; i++) {
tr.addView(spalte[i]);
}
tl.addView(tr);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_stundenplan, menu);
return true;
}
public boolean onOptionItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.item_einstellungen_stundenplan) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
My layout xml for the dynamically textviews:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:background="#f0d000"
android:padding="#dimen/stundenplanactivity_padding"
android:layout_marginRight="#dimen/stundenplanactivity_marginRight"
android:layout_marginTop="#dimen/stundenplanactivity_marginTop">
</TextView>
Edit: xml for tablerow_tabelle
<?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="match_parent"
android:background="#1cf000">
</TableRow>
And the xml for the first Row (not dynamically):
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myTableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/mainactivity_horizontal_margin"
>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text0"
android:text="#string/Zeit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f0c0"
android:padding="#dimen/stundenplanactivity_padding"
android:layout_marginRight="#dimen/stundenplanactivity_marginRight"
android:layout_marginTop="#dimen/stundenplanactivity_marginTop"
/>
<TextView
android:id="#+id/text1"
android:text="#string/Wochentag0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00f0c0"
android:padding="#dimen/stundenplanactivity_padding"
android:layout_marginRight="#dimen/stundenplanactivity_marginRight"
android:layout_marginTop="#dimen/stundenplanactivity_marginTop"
/>
<TextView
android:id="#+id/text2"
android:text="#string/Wochentag1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f0c0"
android:padding="#dimen/stundenplanactivity_padding"
android:layout_marginRight="#dimen/stundenplanactivity_marginRight"
android:layout_marginTop="#dimen/stundenplanactivity_marginTop"
/>
<TextView
android:id="#+id/text3"
android:text="#string/Wochentag2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f0c0"
android:padding="#dimen/stundenplanactivity_padding"
android:layout_marginRight="#dimen/stundenplanactivity_marginRight"
android:layout_marginTop="#dimen/stundenplanactivity_marginTop"
/>
<TextView
android:id="#+id/text4"
android:text="#string/Wochentag3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f0c0"
android:padding="#dimen/stundenplanactivity_padding"
android:layout_marginRight="#dimen/stundenplanactivity_marginRight"
android:layout_marginTop="#dimen/stundenplanactivity_marginTop"
/>
<TextView
android:id="#+id/text5"
android:text="#string/Wochentag4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f0c0"
android:padding="#dimen/stundenplanactivity_padding"
android:layout_marginRight="#dimen/stundenplanactivity_marginRight"
android:layout_marginTop="#dimen/stundenplanactivity_marginTop"
/>
<TextView
android:id="#+id/text6"
android:text="#string/Wochentag5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f0c0"
android:padding="#dimen/stundenplanactivity_padding"
android:layout_marginRight="#dimen/stundenplanactivity_marginRight"
android:layout_marginTop="#dimen/stundenplanactivity_marginTop"
/>
<TextView
android:id="#+id/text7"
android:text="#string/Wochentag6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f0c0"
android:padding="#dimen/stundenplanactivity_padding"
android:layout_marginRight="#dimen/stundenplanactivity_marginRight"
android:layout_marginTop="#dimen/stundenplanactivity_marginTop"
/>
</TableRow>
</TableLayout>

Here this code works good to make different coloring for different rows. Well, it may be helps you.
In my code, i was used two rows. The first Table layout was Header row and second table layout is value rows. I was set background of header as background image and value layouts having background color.
In XML Layout:
<LinearLayout
android:id="#+id/gridValue1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TableLayout
android:id="#+id/GridFrozenTable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="1dip"
android:layout_marginTop="2dip"
android:stretchColumns="*"
/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
>
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="1dip"
android:layout_marginRight="1dip"
android:layout_marginTop="2dip"
android:layout_toRightOf="#id/GridFrozenTable">
<TableLayout
android:id="#+id/GridContentTable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:stretchColumns="*" />
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
In Java code :
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void PopulateMainTable(ActionCodeReport[] content) {
TableRow HeaderRow = new TableRow(this);
HeaderRow.setPadding(0, 0, 0, 2);
TextView HeaderCell1 = new TextView(this);
HeaderCell1.setText("Action code ");
HeaderCell1.setTextColor(Color.BLACK);
HeaderCell1.setHeight(65);
HeaderCell1.setGravity(Gravity.CENTER_HORIZONTAL);
HeaderCell1.setBackgroundResource(R.drawable.lt); //Here i set background image for header
HeaderRow.addView(HeaderCell1);
TextView HeaderCell2 = new TextView(this);
HeaderCell2.setText("NoOfAccnt");
HeaderCell2.setHeight(65);
HeaderCell2.setBackgroundResource(R.drawable.lt); //Same image
HeaderCell2.setGravity(Gravity.CENTER_HORIZONTAL);
HeaderRow.addView(HeaderCell2);
HeaderCell2.setTextColor(Color.BLACK);
TableRow contentRow = new TableRow(this);
contentRow.setPadding(0, 0, 0, 2);
int orientation = this.getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
TextView Cell1 = new TextView(this);
Cell1.setText(r.getAction_Code());
Cell1.setTextColor(Color.BLACK);
Cell1.setGravity(Gravity.LEFT);
Cell1.setBackgroundColor(Color.LTGRAY); //**Here change your background color as you need**
contentRow.addView(Cell1);
TextView Cell2 = new TextView(this);
Cell2.setText(String.valueOf(r.getNoOfAccount()));
Cell2.setTextColor(Color.BLACK);
Cell2.setGravity(Gravity.CENTER_HORIZONTAL);
contentRow.addView(Cell2);
contentTable.addView(contentRow);
}
Surely, i may helps you!!
Happy Coding!!

This can help
TextView txtvew= (TextView) findViewById(R.id.ForgotPasswordText);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
llp.setMargins(50, 0, 0, 0)
txtvew.setLayoutParams(llp);

Setting the margin using layout XML file.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"/>
For changing some particular side margins use this code.
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp"

Related

Relative Layout adding views programmatically one below other not aligning correctly

I am trying to add Text views programmatically to Relative layout parent one below other. However, the first view is not aligning correctly while the rest of the view got aligned correctly. See attached image.
My code:
private void setupQuestionChoices(int position) {
question.setText(questionList.get(position).question());
choicesTextViewsList.clear();
for (int i = 0; i < questionList.get(position).choices().size(); i++) {
TextView textView = new TextView(getActivity());
textView.setBackgroundResource(R.drawable.qa_button_background);
textView.setMinWidth(300);
textView.setGravity(Gravity.CENTER);
textView.setText(questionList.get(position).choices().get(i));
textView.setId(i);
RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if (i == 0) {
layoutParams.addRule(RelativeLayout.BELOW, question.getId());
} else {
layoutParams.addRule(RelativeLayout.BELOW, choicesTextViewsList.get(i - 1).getId());
}
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
layoutParams.setMargins(20, 20, 20, 20);
relativeLayout.addView(textView, layoutParams);
choicesTextViewsList.add(textView);
choicesTextViewsList.get(i).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
answerSelected = true;
}
});
}
}
Relative layout xml where I add my views to:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:id="#+id/relative_parent" //here is where I add views to
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:textColor="#color/colorPrimary"
android:textSize="30sp"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:layout_marginBottom="30dp"
android:gravity="center">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/button_back"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="30dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="30dp"
android:background="#drawable/rectangle_border"
android:text="Back"
android:textColor="#android:color/darker_gray" />
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/button_next"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="30dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="30dp"
android:background="#drawable/rectangle_border"
android:text="Next"
android:textColor="#android:color/darker_gray" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>
I read some posts related to this issue and followed the guidelines from them but that did not help.
How to lay out Views in RelativeLayout programmatically?
Create a new TextView programmatically then display it below another TextView
I was using the index of the for loop as id for the textViews which I changed to make Android generate Id for it as below and it is working fine now as expected.
I changed the line
textView.setId(i)
to
textView.setId(ViewGroup.generateViewId())
Thanks.

TextViews aren't appearing when created programmatically

Programmatically created TextViews aren't appearing when I run my app. I am trying to download messages and the person that sent them from Firebase and display them in separate TextViews with the sender TextView on top.
Sorry for the lack of information, but I don't know what I've done wrong.
db.collection("messagesIT")
.orderBy("Timesent", Query.Direction.DESCENDING)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot>task) {
if (task.isSuccessful()) {
ArrayList<MessageObj> messages = new ArrayList<>();
for (QueryDocumentSnapshot document : task.getResult())
String message = document.getString("Message");
String sender = document.getString("Sender");
Timestamp timesent = document.getTimestamp("Timesent");
MessageObj mess = new MessageObj(message, sender, timesent);
messages.add(mess);
Log.d(TAG, "m: " + mess.getMessage());
Log.d(TAG, "s: " + mess.getSender());
Log.d(TAG, "t:" + mess.getTimesent());
showToast("b");
}
TableLayout tl = findViewById(R.id.TableLayoutSubjectGroupPage);
//reading and displaying messages
for (int i = 0; i < messages.size(); i++) {
TableRow trMessage = new TableRow(SubjectGroupPage.this);
TableRow trSender = new TableRow(SubjectGroupPage.this);
TextView txtSender = new TextView(SubjectGroupPage.this);
TextView txtMessage = new TextView(SubjectGroupPage.this);
Log.d(TAG, "uv8iygv iyrtgcvfi8ytgvcrcccccccccccccccccccccccccccc");
LinearLayout.LayoutParams Weight = new LinearLayout.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1);
txtSender.setText(messages.get(i).getSender());
txtSender.setLayoutParams(Weight);
txtMessage.setText(messages.get(i).getMessage());
txtMessage.setLayoutParams(Weight);
txtMessage.setTextSize(20);
txtMessage.setTextColor(Color.parseColor("#000000"));
if (messages.get(i).getSender().equals(currentUser.getUid())) {
txtMessage.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END);
txtSender.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END);
} else {
txtMessage.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
txtSender.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
}
//creating message display
trSender.addView(txtSender);
trMessage.addView(txtMessage);
tl.addView(trSender);
tl.addView(trMessage);
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
showToast("There has been an error, please try again later.");
Log.d(TAG, "Error: " + e);
}
});
Layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/txtUsernameAdminStudentPage"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SubjectGroupPage">
<ScrollView
android:id="#+id/scrollviewMessages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#+id/linearLayoutMessageInput"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<TableLayout
android:id="#+id/TableLayoutSubjectGroupPage"
android:layout_width="match_parent"
android:layout_height="wrap_content">
//these were tests for how I want the TextViews
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtExample"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView"
android:textColor="#000000"
android:textSize="20dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView"
android:textAlignment="textEnd" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtExample2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView"
android:textAlignment="textEnd"
android:textColor="#000000"
android:textSize="20dp"
tools:layout_editor_absoluteY="675dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView"
android:textAlignment="textEnd" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView"
android:textAlignment="textEnd"
android:textColor="#000000"
android:textSize="20dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView"
android:textColor="#000000"
android:textSize="20sp" />
</TableRow>
</TableLayout>
</ScrollView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
What the page looks like when run
What it should look like when run
Database
I think you forget to add layout params for table row.
BTW you should use recycler view with two view holders one for sender and 2nd for the receiver.
checkout if you want to improve your code
How to create RecyclerView with multiple view type?
or
https://medium.com/#droidbyme/android-recyclerview-with-multiple-view-type-multiple-view-holder-af798458763b
You explictly set the width of your TextViews to zero, using:
public LinearLayout.LayoutParams (int width, int height, float weight)
...where (see LinearLayout.LayoutParams):
int: the width, either ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT or a fixed size in pixels
MATCH_PARENT has a value of -1 and WRAP_CONTENT a value of -2. So this code sets a fixed width of 0:
LinearLayout.LayoutParams Weight = new LinearLayout.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1)
Instead you should use:
TableLayout.LayoutParams tableParams_forRow = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
and
TableRow.LayoutParams rowParams_forTextView = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
See this answer, for an example

How do I get TableLayout occupy all available space vertically?

I created a board dynamically and put in a LinearLayout ( Vertical ) together with a Chronometer and a Button . I gave the following weights for each of them :
Board : 5
Chronometer : 10
Button : 10
The chronometer button and take their correct places , however the board does not take up all your space on your height . Somebody help me, please?
Java class:
tableLayout = (TableLayout) findViewById(R.id.tabuleiro);
GradientDrawable gd = new GradientDrawable();
gd.setColor(azul);
gd.setStroke(1, preto);
final GradientDrawable gd2 = new GradientDrawable();
gd2.setStroke(1, 0xFF000000);
gd2.setColor(Color.RED);
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
for (int i = 0; i < 10; i++){
tableRow = new TableRow(this);
tableRow.setLayoutParams(rowParams);
for (int j = 0; j < 10; j++){
textView = new TextView(this);
textView.setLayoutParams(rowParams);
textView.setBackgroundDrawable(gd);
textView.setTag(String.valueOf(i + "." + j));
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundDrawable(gd2);
}
});
tableRow.addView(textView, rowParams);
}
tableLayout.addView(tableRow, tableParams);
}
XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lnl"
android:background="#a9c3ff"
android:orientation="vertical">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical|center_horizontal"
android:layout_marginTop="40dp"
android:id="#+id/linearLayout2">
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*"
android:id="#+id/tabuleiro"
android:layout_gravity="center_vertical"
android:layout_weight="5">
</TableLayout>
<Chronometer
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/chrono"
android:layout_gravity="center_horizontal"
android:textSize="40dp"
android:layout_weight="10"
android:gravity="center" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/btn_voltar"
android:id="#+id/btn_voltar"
android:hyphenationFrequency="normal"
android:layout_weight="10" />
</LinearLayout>
</LinearLayout>
IMAGE OF APP
Try the following:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lnl"
android:background="#a9c3ff"
android:orientation="vertical">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical|center_horizontal"
android:layout_marginTop="40dp"
android:id="#+id/linearLayout2">
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:stretchColumns="*"
android:id="#+id/tabuleiro"
android:layout_gravity="center_vertical"
android:layout_weight="5">
</TableLayout>
<Chronometer
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/chrono"
android:layout_gravity="center_horizontal"
android:textSize="40dp"
android:layout_weight="10"
android:gravity="center" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/btn_voltar"
android:id="#+id/btn_voltar"
android:hyphenationFrequency="normal"
android:layout_weight="10" />
</LinearLayout>
</LinearLayout>

android - how to inflate a map fragment in an already inflated layout?

i am creating a Table Layout in my an activity, adding dynamically table rows in a loop.
What i want to achieve, is to add a Google map fragment in each dynamically created Table row.
This is my Table row Layout:
<?xml version="1.0" encoding="utf-8">
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl_rowitem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp">
<RelativeLayout
android:id="#+id/inflated_row_relative"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv_poi_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/orange"
android:textColor="#ffffff"
android:text="Name"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/img_snapshot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tv_poi_name"
android:adjustViewBounds="true"
android:src="#drawable/map_snapshot" />
<TextView
android:id="#+id/tv_poi_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:textColor="#fe8301"
android:layout_below="#+id/img_snapshot"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/distance_layout"
android:layout_below="#+id/tv_poi_address" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/distance_icon" />
<TextView
android:id="#+id/tv_poi_distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/imageView1"
android:text="Distance"
android:paddingLeft="10dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
</RelativeLayout>
</TableRow>
And this is the loop that i am creating the Table rows dynamically:
final TableLayout tblAddLayout = (TableLayout) findViewById(R.id.tl_menu_pois);
TableRow inflateRow = (TableRow) View.inflate(Menu_pois.this, R.layout.menu_pois_rowitem, null);
for (int i=0;i<3;i++){
View inflate = (TableRow) View.inflate(Menu_pois.this, R.layout.menu_pois_rowitem, >tl_menu_pois);
RelativeLayout inflated_row_relative = (RelativeLayout) >inflate.findViewById(R.id.inflated_row_relative);
inflated_row_relative.setTag(i);
TextView poi_name = (TextView) inflate.findViewById(R.id.tv_poi_name);
TextView poi_address = (TextView) inflate.findViewById(R.id.tv_poi_address);
poi_name.setText("Name");
poi_address.setText("Address");
//set tag for each TableRow
inflate.setTag(i);
//add TableRows to TableLayout
tblAddLayout.addView(inflate);
//set click listener for all TableRows
inflated_row_relative.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Object tag = v.getTag();
String string_tag = tag.toString();
}
});
}
I want to display a Google map in each Table row like the image bellow (instead of the sample image, having the map):
http://goo.gl/9erYB7
Is there a way to implement this?
Thank you very much in advance.
I found a solution for adding multiple map fragment on the same View, following this example, in case someone has the same problem:
http://saadroid.blogspot.gr/2013/06/multiple-maps-in-same-view.html

LinearLayout not displaying Items

What i'm trying to do is add create an application that locates some local business. So the activity I'm doing displays the business information. Some business have severals stores located on the city, and some others only have one. So here is my problem: In the View I have Scroll View with all the elements, and a linear layout inside the scrollview. If the business has more then one store, i will add each store information in a new text view and add the text view to the layout (this is done all by code). But at the moment to display the layout, it only displays me one store, instead of showing me 3 or 4. What I'm I doing wrong? Here is the method setupViews(), which is the one in chanrge of the displaying:
private void setupViews() throws SQLException {
ImageView iv = (ImageView) findViewById(R.id.negocio_logo);
try {
iv.setImageBitmap(BitmapFactory.decodeByteArray(toShow.getImgSrc(),
0, toShow.getImgSrc().length));
} catch (NullPointerException npe) {
iv.setBackgroundColor(Color.WHITE);
}
TextView nombreEmpresa = (TextView) findViewById(R.id.nombre_empresa);
nombreEmpresa.setText(toShow.getNombre());
TextView descripcionEmpresa = (TextView) findViewById(R.id.descripcion_empresa);
descripcionEmpresa.setText(toShow.getDescripcion());
TextView direccionEmpresa = (TextView) findViewById(R.id.direccion_empresa);
direccionEmpresa.setText(toShow.getDireccion());
LinearLayout rl = (LinearLayout) findViewById(R.id.linear_layout_si);
TextView suc = (TextView) findViewById(R.id.sucursales_empresa);
sucursalDAO sDAO = new sucursalDAO();
boolean tieneSucursales = sDAO.hasSucursales(toShow.getId());
if (tieneSucursales == false) {
suc.setVisibility(View.GONE);
// sucs.setVisibility(View.GONE);
} else {
suc.setVisibility(View.VISIBLE);
ArrayList<String> sucursales = sDAO.getStringSucursales(toShow
.getId());
ArrayList<TextView> tvs = new ArrayList<TextView>();
for (int i = 0; i < sucursales.size(); i++) {
TextView tv = new TextView(this);
tv.setText(sucursales.get(i));
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tvs.add(tv);
}
for (int i = 0; i < tvs.size(); i++) {
rl.addView(tvs.get(i), i);
}
}
}
And here is the XML of my view:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/White"
android:orientation="vertical" >
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widgetscroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/White"
android:orientation="vertical" >
<ImageView
android:id="#+id/negocio_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:contentDescription="#string/logo_negocio" />
<TextView
android:id="#+id/datos_empresa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/negocio_logo"
android:background="#drawable/barra"
android:text="#string/datos_empresa"
android:textColor="#color/White" />
<TextView
android:id="#+id/nombre_empresa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/datos_empresa"
android:text="#string/testing" />
<TextView
android:id="#+id/descripcion_empresa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/nombre_empresa"
android:text="#string/testing" />
<TextView
android:id="#+id/direccion_empresa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/descripcion_empresa"
android:text="#string/testing" />
<TextView
android:id="#+id/sucursales_empresa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/direccion_empresa"
android:background="#drawable/barra"
android:text="#string/sucursales"
android:visibility="gone" />
<LinearLayout
android:id="#+id/linear_layout_si"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/sucursales_empresa" >
</LinearLayout>
<TextView
android:id="#+id/contacto_empresa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/linear_layout_si"
android:text="#string/testing" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
I think you forgot to set orientation on LinearLayout and it's showing horitzontal
I bet you forgot to set the orientation of the LinearLayout.

Categories

Resources