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>
Related
Hello i am doing a little web scraping project to learn in android studio.
What my app does Right now is that it ask you to fill a field with a query then when you push a button it takes your query and it changes to a new view and searches in that page all possible instances of it and changes to a new view and shows you the information, right until this point i got it, now the problem is to show the results and how to do it because with just 1 result it shows the info with 0 problems but i want to show more than 1 results. so it shows
This image but one bellow the other until there are no more results
Here is the layout code that i want to put in a for loop
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imgViewEbay"
android:scaleType="fitCenter"
android:layout_width="match_parent"
android:layout_height="200dp"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textStyle="bold"
android:layout_weight="3"
android:text="Nombre"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_weight="1"
android:id="#+id/txtTitleEbay"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textStyle="bold"
android:layout_weight="3"
android:text="Precio"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_weight="1"
android:textColor="#color/design_default_color_on_primary"
android:id="#+id/txtPrecioEbay"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textStyle="bold"
android:layout_weight="3"
android:text="Localizacion"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_weight="1"
android:textColor="#color/design_default_color_on_primary"
android:id="#+id/txtlocaEbay"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textStyle="bold"
android:layout_weight="3"
android:text="Tipos de pago"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_weight="1"
android:textColor="#color/design_default_color_on_primary"
android:id="#+id/txtpagoebay"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textStyle="bold"
android:layout_weight="3"
android:text="Condition"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_weight="1"
android:id="#+id/txtDescripcionebay"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textStyle="bold"
android:layout_weight="3"
android:text="Tiempo"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_weight="1"
android:textColor="#color/design_default_color_on_primary"
android:id="#+id/txttiempoEbay"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/txtRatingebay"
android:gravity="center"
android:textStyle="bold"
android:layout_weight="3"
android:text="4.5"
android:textSize="28sp"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RatingBar
style="#style/Widget.AppCompat.RatingBar.Indicator"
android:id="#+id/rating_barebay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btnbackebay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Volver" />
<Button
android:id="#+id/btnLinkebay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Link" />
</LinearLayout>
</LinearLayout>
</ScrollView>
And here is my attempt to do it :
```
Intent intent = getIntent();
String busqueda = intent.getStringExtra("busqueda");
final TextView txtitle = findViewById(R.id.txtTitleEbay);
final TextView txtprice = findViewById(R.id.txtPrecioEbay);
final ImageView img = findViewById(R.id.imgViewEbay);
final TextView txtlocation = findViewById(R.id.txtlocaEbay);
final TextView txtPayments = findViewById(R.id.txtpagoebay);
final RatingBar ratingBar = findViewById(R.id.rating_barebay);
final TextView txtRating = findViewById(R.id.txtRatingebay);
final TextView txtCondtion = findViewById(R.id.txtDescripcionebay);
final TextView txttime = findViewById(R.id.txttiempoEbay);
try {
for (int i = 0; i < buscadorebay(busqueda).length; i++) {
String url= buscadorebay(busqueda)[i];
new Thread(new Runnable()
{
final EbayProduct product = new EbayProduct();
#Override
public void run()
{
try
{
Document doc = Jsoup.connect(url)
.timeout(6000).get();
Elements image = doc.select("#icImg");
Elements title = doc.select("#vi-lkhdr-itmTitl");
Elements price = doc.select("#vi-mskumap-none #prcIsum");
Elements location = doc.select("#SRPSection [class='ux-textspans ux-textspans--SECONDARY']");
Elements payments = doc.select("#SRPSection [role='img']");
ArrayList<String> paymentMethods = new ArrayList<>();
for (Element payment : payments) {
paymentMethods.add(payment.attr("aria-label"));
}
Elements ratingEle = doc.select("#LeftSummaryPanel [itemprop='ratingValue']");
String ele = ratingEle.attr("content");
Elements condition1 = doc.select("#viTabs_0_is .ux-layout-section__row .ux-labels-values__values .ux-labels-values__values-content .ux-textspans");
Elements condtion2 = doc.select("#vi-desc-maincntr");
Elements Time = doc.select("#vi-cdown_timeLeft");
product.setTitle(title.text());
product.setImagUrl(image.attr("src"));
product.setPrice(price.text());
product.setLocation(location.text().replace("See detailsfor shipping Located in:", "").replace("See details- for more information about returns Learn moreLearn more about eBay global shipping program", ""));
product.setPayment(paymentMethods.toString());
product.setRating(Float.parseFloat(ratingEle.text()));
product.setCondition(condition1.text().replace(" Read moreabout the condition", "").replaceAll("[()]", "").replaceAll("[.]", ""));
product.setTime(Time.text());
}catch (Exception ex){}
runOnUiThread(new Runnable()
{
#Override
public void run()
{
txtitle.setText(product.getTitle());
Picasso.get()
.load(product.getImagUrl())
.into(img);
txtprice.setText(product.getPrice());
txtlocation.setText(product.getLocation());
txtPayments.setText(product.getPayment());
txtCondtion.setText(product.getCondition());
ratingBar.setRating(product.getRating());
txtRating.setText(product.getRating()+"");
txttime.setText(product.getTime());
}
});
}
}).start();
}} catch (IOException e) {
e.printStackTrace();
}
As you Can see i try to enter the view in a loop but i guess it is wrong since without the for loop and with just the normal url it goes without a problem. How then to make it so that it takes the info and then puts the views one below the other?
If you guys could help i would be thankful.
The main problem here is that the parent layout is nested scroll view so the height must be wrap content
so the view is like first image
what I want is to make the "don't have an account? register" TextView
to be at the bottom of the screen so I calculate the height of my
phone then the height of layout to subtract to subtract them and
assign the result to the top margin of textview
and here's my code
ViewTreeObserver observer = binding.parentConstraint.getViewTreeObserver();
if (observer.isAlive()) {
observer.addOnGlobalLayoutListener(() -> {
DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
float parentViewHeightInDP = (int) (binding.parentConstraint.getHeight() / displayMetrics.density);
if (getContext() != null) {
float heightInDP = displayMetrics.heightPixels / displayMetrics.density;
float distanceBetweenLayoutBottomAndParenBottom = heightInDP - parentViewHeightInDP;
if (distanceBetweenLayoutBottomAndParenBottom > 32) {
if (topMargin == 0) {
topMargin = 1;
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) binding.loginTextDontHaveAccount.getLayoutParams();
layoutParams.topMargin = Math.round(distanceBetweenLayoutBottomAndParenBottom);
Handler handler = new Handler();
handler.postDelayed(() -> {
binding.loginTextDontHaveAccount.requestLayout(); },
50);
}
}
but that the result I got
so my real question here is why there still some space here, I mean
the TextView should be exactly at the bottom
What I would suggest you to make NestedScrollView as a child instead of parent.
Make RelativeLayout as your parent and use layout_alignParentBottom to set TextView at bottom. Something like this.
<?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="match_parent">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:colorBackground"
android:clickable="true"
android:fillViewport="true"
android:focusable="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="650dp"
android:background="#mipmap/ic_launcher" />
<ImageView
android:layout_width="match_parent"
android:layout_height="650dp"
android:layout_below="#+id/img1"
android:background="#color/colorPrimary" />
</LinearLayout>
</RelativeLayout>
</androidx.core.widget.NestedScrollView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
Try this:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<AutoCompleteTextView
android:id="#+id/emailtext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:hint="Email"
android:inputType="textEmailAddress"
android:textColorHint="#80000000" />
<EditText
android:id="#+id/passwordtext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:hint="Password"
android:inputType="textPassword"
android:textColorHint="#80000000" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<Button
android:id="#+id/enter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Enter"
android:textAllCaps="false"
app:layout_constraintBottom_toTopOf="#id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="This is my app"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/enter" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
You can use constraint layout. Pay attention to layout_constraintBottom_toTopOf button and layout_constraintTop_toBottomOf textview.
This is the code I have:
package mika.actual;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class accordion extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.accordion);
final View panels[] = {
findViewById(R.id.panelProfile),
findViewById(R.id.panelSettings),
findViewById(R.id.panelPrivacy)
};
final Button buttons[] = {
(Button) findViewById(R.id.btnProfile),
(Button) findViewById(R.id.btnSettings),
(Button) findViewById(R.id.btnPrivacy)
};
for (int i = 0; i < panels.length; i++){
panels[i].setVisibility(View.GONE);
}
for (int i = 0; i < buttons.length; i++){
final int x = i;
buttons[i].setBackgroundColor(getResources().getColor(R.color.button_not_pressed));
buttons[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for (int y = 0; y < panels.length; y++){
panels[y].setVisibility(View.GONE);
}
panels[x].setVisibility(View.VISIBLE);
for (int y = 0; y < buttons.length; y++){
buttons[y].setBackgroundColor(getResources().getColor(R.color.button_not_pressed));
}
buttons[x].setBackgroundColor(getResources().getColor(R.color.button_pressed));
}
});
}
}
}
and XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFFFF"
android:orientation="vertical">
<Button
android:id="#+id/btnProfile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Profile"
android:textColor="#FFFFFFFF" />
<LinearLayout
android:id="#+id/panelProfile"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFFFF">
<LinearLayout
android:id="#+id/panelProfile1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF">
<TextView
android:id="#+id/strName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
<EditText
android:id="#+id/txtName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/panelProfile2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF">
<TextView
android:id="#+id/strSurname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Surname" />
<EditText
android:id="#+id/txtSurname"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
<Button
android:id="#+id/btnSettings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Settings"
android:textColor="#FFFFFFFF" />
<LinearLayout
android:id="#+id/panelSettings"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/panelSettings1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF">
<TextView
android:id="#+id/strMail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="e-mail" />
<EditText
android:id="#+id/txtMail"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/panelSettings2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF">
<TextView
android:id="#+id/strPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone" />
<EditText
android:id="#+id/txtPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
<Button
android:id="#+id/btnPrivacy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Privacy"
android:textColor="#FFFFFFFF" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/panelPrivacy"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">
<CheckBox
android:id="#+id/checkFacebook"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Facebook"
android:textColor="#ff355689">
</CheckBox>
<CheckBox
android:id="#+id/checkLinkedIn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LinkedIn"
android:textColor="#ff355689">
</CheckBox>
<CheckBox
android:id="#+id/checkTwitter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Twitter"
android:textColor="#ff355689">
</CheckBox>
</LinearLayout>
</ScrollView>
</LinearLayout>
I want to create the two arrays buttons[] and panels[] dynamically, taking all the child views inside the LinarLayout (with the id root_layout). Any suggestions appreciated. Thanks in advance :)
Maybe iterate over the childrens of your Viewgroup and adding them in a Arraylist?:
LinearLayout rootView = (LinearLayout) findViewById(R.id.mylinearlayoutid);
ArrayList<Button> buttons = new ArrayList<>(rootView.getChildCount());
for(int i = 0; i < rootView.getChildCount(); i++){
buttons.add ((Button) rootView.getChildAt(i));
}
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"
I've set my buttons to be arranged from top to bottom in order of Facebook, Instagram, Twitter and Reddit but it's not doing that, it's only showing Facebook and Reddit and I don't know why.
I've got my buttons to automatically resize on Activity creation with this code:
public void getScreenRes() {
DisplayMetrics display = this.getResources().getDisplayMetrics();
int screenwidth = display.widthPixels;
int screenheight = display.heightPixels;
double buttonheight = screenwidth / 2.66666667;
int buttonheightint= (int) Math.round(buttonheight);
ImageButton fbLogin = (ImageButton) findViewById(R.id.facebookLogin);
ViewGroup.LayoutParams fb = fbLogin.getLayoutParams();
fb.width = screenwidth;
fb.height = buttonheightint;
fbLogin.setLayoutParams(fb);
ImageButton instaLogin = (ImageButton) findViewById(R.id.instagramLogin);
ViewGroup.LayoutParams insta = instaLogin.getLayoutParams();
insta.width = screenwidth;
insta.height = buttonheightint;
instaLogin.setLayoutParams(insta);
ImageButton twitLogin = (ImageButton) findViewById(R.id.twitterLogin);
ViewGroup.LayoutParams twit = instaLogin.getLayoutParams();
twit.width = screenwidth;
twit.height = buttonheightint;
twitLogin.setLayoutParams(twit);
ImageButton redditLogin = (ImageButton) findViewById(R.id.redditLogin);
ViewGroup.LayoutParams reddit = instaLogin.getLayoutParams();
reddit.width = screenwidth;
reddit.height = buttonheightint;
redditLogin.setLayoutParams(reddit);
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="16dp"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.eren.valour.FirstTimeLogin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/getStarted"
android:id="#+id/getStartedText"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:background="#drawable/facebook"
android:id="#+id/facebookLogin"
android:onClick="facebookLogin"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/instagram"
android:layout_below="#+id/facebookLogin"
android:id="#+id/instagramLogin"
android:onClick="instagramLogin"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/twitter"
android:layout_below="#+id/instagramLogin"
android:id="#+id/twitterLogin"
android:onClick="twitterLogin"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/reddit"
android:layout_below="#+id/twitterLogin"
android:id="#+id/redditLogin"
android:onClick="redditLogin"
/>
What it looks like now:
You need to get the corresponding Layoutparams.
ViewGroup.LayoutParams reddit = instaLogin.getLayoutParams();
should be
ViewGroup.LayoutParams reddit = redditLogin.getLayoutParams();
the same with the Twitter Login:
ViewGroup.LayoutParams twit = twitLogin.getLayoutParams();
What youre doing is taking the instagram layoutparams and applying it to 3 buttons so they all get placed on top of another, so only the last shows. Probably just a copy+past error on your side, but that should fix it.
If that does not fix it, the problem should be in the layout file. Try using a LinearLayout or setting the correct attributes for your RelativeLayout.
Try to change your Layout from RelativLayout to LinearLayout. That should do it.
You can put it into a LinearLayout with a weightsum Like so. Without the java method.
<LinearLayout 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" android:orientation="vertical"
android:paddingTop="16dp" android:weightSum="5"
tools:context="com.eren.valour.FirstTimeLogin">
<TextView
android:id="#+id/getStartedText" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" android:layout_weight="1" android:padding="50dp" // you can tweak this figure here
android:text="getStarted\nhere" android:textAppearance="?android:attr/textAppearanceLarge"/>
<ImageButton
android:id="#+id/facebookLogin" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_weight="1"
android:background="#drawable/fb" android:onClick="facebookLogin"/>
<ImageButton
android:id="#+id/instagramLogin" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_weight="1"
android:background="#drawable/insta" android:onClick="instagramLogin"/>
<ImageButton
android:id="#+id/twitterLogin" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_weight="1"
android:background="#drawable/twit" android:onClick="twitterLogin"/>
<ImageButton
android:id="#+id/redditLogin" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_weight="1"
android:background="#drawable/reddit" android:onClick="redditLogin"/>
</LinearLayout>
Update your XMl..
Instead of RelativeLayout ,I Think you should you should use LinearLayout and Weight propert like this ...
<LineaLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="16dp"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.eren.valour.FirstTimeLogin"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/getStarted"
android:id="#+id/getStartedText"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_weight="1"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="100dp"
android:background="#drawable/facebook"
android:id="#+id/facebookLogin"
android:onClick="facebookLogin"
android:layout_weight="1"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/instagram"
android:layout_below="#+id/facebookLogin"
android:id="#+id/instagramLogin"
android:onClick="instagramLogin"
android:layout_weight="1"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/twitter"
android:layout_below="#+id/instagramLogin"
android:id="#+id/twitterLogin"
android:onClick="twitterLogin"
android:layout_weight="1"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/reddit"
android:layout_below="#+id/twitterLogin"
android:id="#+id/redditLogin"
android:onClick="redditLogin"
android:layout_weight="1"
/>
Also you don't need this method getScreenRes() anymore.