I can't do addView() two times.
In fact, I want one TextView(left) and one Button(right) besides.
So how I can position them in LinearLayout?
if (success == 1) {
content = json.getJSONArray(TAG_COMMENTS);
layoutVertical.removeAllViews();
final int N = content.length();
final Button[] myButton = new Button[N];
final LinearLayout[] myLayout = new LinearLayout[N];
final TextView[] myTextView = new TextView[N];
for (int i = 0; i < content.length(); i++) {
JSONObject c = content.getJSONObject(i);
String name = c.getString(TAG_CONTENT);
final LinearLayout layoutHorizontal = new LinearLayout(
getActivity());
layoutHorizontal
.setOrientation(LinearLayout.HORIZONTAL);
layoutHorizontal.setId(i);
layoutHorizontal.setBackground(getResources()
.getDrawable(R.drawable.borderadmincomment));
final TextView rowTextView = (TextView) new TextView(
getActivity());
rowTextView.setText(name);
rowTextView.setId(i);
final Button rowButton = new Button(getActivity());
rowButton.setLayoutParams(new LayoutParams(1,
LayoutParams.MATCH_PARENT));
rowButton.setId(i);
rowButton.setBackgroundResource(R.drawable.croix);
rowButton
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
layoutHorizontal.addView(rowTextView, 0);
layoutHorizontal.addView(rowButton, 1);
rowTextView.setWidth(400);
//rowTextView.setHeight(150);
//rowButton.setHeight(150);
layoutVertical.addView(layoutHorizontal);
myButton[i] = rowButton;
myLayout[i] = layoutHorizontal;
myTextView[i] = rowTextView;
}
}
It's not works.
Thank you.
You forgot to set layout params for Horizontal layout and rowTextView.
For example:
layoutHorizontal.setLayoutParams(new
LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
Create linear layout parameter. Do something like this:-
LinearLayout LL = new LinearLayout(this);
LL.setBackgroundColor(Color.CYAN);
LL.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams LLParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
LL.setWeightSum(2f);
LL.setLayoutParams(LLParams);
Please let me know it this will work.
Make a xml layout file and name it item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical" >
<TextView
android:id="#+id/rowTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_weight="1" />
<Button
android:id="#+id/rowButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Write following code in your Activity
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layoutHorizontal = inflater.inflate(R.layout.item, null);
TextView rowTextView = (TextView) layoutHorizontal.findViewById(R.id.rowTextView);
Button rowButton = (Button) layoutHorizontal.findViewById(R.id.rowButton);
layoutVertical.addView(layoutHorizontal);
Related
I have 3 fragments inside an Activity and in two of the fragments I have a hardcoded TableLayout inside a ConstraintLayout, the headers and rows of the tables are dynamically generated with information from a MySQL server. Given its dynamic nature, the table is able to overflow the page and I want it to be scrollable.
There are already multiple questions on StackOverflow where people have wanted a scrollable table, but in the questions, the tables and their data are hardcoded (don't know if that is relevant or not). I have tried the solutions proposed in those questions but they don't work on my table. The solutions are generally to either wrap the TableLayout in a ScrollView or to use android:isScrollContainer="1" neither of which have worked for me.
TableLayout inside ScrollView. The ScrollView is there for testing, normally it is just the TableLayout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ViewProductLocFragment">
<!-- Rest of the fragment -->
<ScrollView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="16dp"
android:layout_weight="1"
android:scrollbars="none"
app:layout_constraintBottom_toTopOf="#+id/menuBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view">
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
android:stretchColumns="*"
app:layout_constraintBottom_toTopOf="#+id/menuBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view">
</TableLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
Here is the code that fills the table.
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_view_product_main, container, false);
initializeTableLayout(view);
fillTable(((ViewProductActivity)getActivity()).pallets);
return view;
}
private void fillTable(PalletsResult pallets)
{
Integer count = 0;
for(Pallet pallet : pallets.getPallets()) {
String barcode = pallet.getBarcode();
String location = pallet.getCode();
Integer quantity = pallet.getQuantity();
TableRow tableRow = new TableRow(getContext());
if (count % 2 != 0) {
tableRow.setBackgroundColor(getResources().getColor(R.color.lightGrey));
}
tableRow.setId(View.generateViewId());
tableRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
TextView labelBarcode = new TextView(getContext());
labelBarcode.setId(View.generateViewId());
labelBarcode.setGravity(Gravity.CENTER);
labelBarcode.setTextColor(getResources().getColor(R.color.standardTextColor));
labelBarcode.setTextSize(18);
labelBarcode.setText(barcode);
tableRow.addView(labelBarcode);
TextView labelLocation = new TextView(getContext());
labelLocation.setId(View.generateViewId());
labelLocation.setGravity(Gravity.CENTER);
labelLocation.setTextColor(getResources().getColor(R.color.standardTextColor));
labelLocation.setTextSize(18);
labelLocation.setText(location);
tableRow.addView(labelLocation);
TextView labelQuantity = new TextView(getContext());
labelQuantity.setId(View.generateViewId());
labelQuantity.setGravity(Gravity.CENTER);
labelQuantity.setTextColor(getResources().getColor(R.color.standardTextColor));
labelQuantity.setTextSize(18);
labelQuantity.setText(quantity.toString());
tableRow.addView(labelQuantity);
tableLayout.addView(tableRow, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
count++;
}
}
private void initializeTableLayout(View view)
{
tableLayout = view.findViewById(R.id.tableLayout);
TableRow tr_head = new TableRow(getContext());
tr_head.setId(View.generateViewId());
tr_head.setBackgroundColor(getResources().getColor(R.color.lightGrey));
tr_head.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT
));
TextView label_barcode = new TextView(getContext());
label_barcode.setId(View.generateViewId());
label_barcode.setText("BARCODE");
label_barcode.setTextSize(20);
label_barcode.setTextColor(Color.BLACK);
label_barcode.setGravity(Gravity.CENTER);
tr_head.addView(label_barcode);// add the column to the table row here
TextView label_location = new TextView(getContext());
label_location.setId(View.generateViewId());// define id that must be
unique
label_location.setText("LOCATION"); // set the text for the header
label_location.setTextSize(20);
label_location.setTextColor(Color.BLACK); // set the color
label_location.setGravity(Gravity.CENTER);
tr_head.addView(label_location); // add the column to the table row here
TextView label_quantity = new TextView(getContext());
label_quantity.setId(View.generateViewId());// define id that must be
unique
label_quantity.setText("QTY"); // set the text for the header
label_quantity.setTextSize(20);
label_quantity.setTextColor(Color.BLACK); // set the color
label_quantity.setGravity(Gravity.CENTER);
tr_head.addView(label_quantity); // add the column to the table row here
tableLayout.setScrollContainer(true);
tableLayout.addView(tr_head, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
}
This creates a table that runs off the end of the page and that can't be scrolled.
There is a fair bit of other code that I didn't post in regards to the Activity containing the fragments and what not. Let me know if you want any of that.
Thanks for any help.
Update: I implemented a similar table elsewhere in my app and it worked just fine with the code posted in my examples. This means that the problem I am having is realted to the table being in a fragment and not an activity. I will post some of my fragment code later.
I have tried implementing your code over an Activity. I am posting my Activity and XML code for your reference, can you see what you are missing in your complete code so that this can be helpful.
MainActivity.java
public class MainActivity extends Activity {
TableLayout tableLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeTableLayout();
}
#Override
protected void onResume() {
super.onResume();
fillTable();
}
private void fillTable() {
Integer count = 0;
for (int i = 0; i < 100; i++) {
String barcode = "#0000000000";
String location = "Location";
Integer quantity = 1;
TableRow tableRow = new TableRow(MainActivity.this);
if (count % 2 != 0) {
tableRow.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
}
tableRow.setId(View.generateViewId());
tableRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
TextView labelBarcode = new TextView(MainActivity.this);
labelBarcode.setId(View.generateViewId());
labelBarcode.setGravity(Gravity.CENTER);
labelBarcode.setTextColor(getResources().getColor(R.color.colorAccent));
labelBarcode.setTextSize(18);
labelBarcode.setText(barcode);
tableRow.addView(labelBarcode);
TextView labelLocation = new TextView(MainActivity.this);
labelLocation.setId(View.generateViewId());
labelLocation.setGravity(Gravity.CENTER);
labelLocation.setTextColor(getResources().getColor(R.color.colorAccent));
labelLocation.setTextSize(18);
labelLocation.setText(location);
tableRow.addView(labelLocation);
TextView labelQuantity = new TextView(MainActivity.this);
labelQuantity.setId(View.generateViewId());
labelQuantity.setGravity(Gravity.CENTER);
labelQuantity.setTextColor(getResources().getColor(R.color.colorAccent));
labelQuantity.setTextSize(18);
labelQuantity.setText(quantity.toString());
tableRow.addView(labelQuantity);
tableLayout.addView(tableRow, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
count++;
}
}
private void initializeTableLayout() {
tableLayout = this.findViewById(R.id.tableLayout);
TableRow tr_head = new TableRow(MainActivity.this);
tr_head.setId(View.generateViewId());
tr_head.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
tr_head.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
TextView label_barcode = new TextView(MainActivity.this);
label_barcode.setId(View.generateViewId());
label_barcode.setText("BARCODE");
label_barcode.setTextSize(20);
label_barcode.setTextColor(Color.BLACK);
label_barcode.setGravity(Gravity.CENTER);
tr_head.addView(label_barcode);// add the column to the table row here
TextView label_location = new TextView(MainActivity.this);
label_location.setId(View.generateViewId());// define id that must be unique
label_location.setText("LOCATION"); // set the text for the header
label_location.setTextSize(20);
label_location.setTextColor(Color.BLACK); // set the color
label_location.setGravity(Gravity.CENTER);
tr_head.addView(label_location); // add the column to the table row here
TextView label_quantity = new TextView(MainActivity.this);
label_quantity.setId(View.generateViewId());// define id that must be unique
label_quantity.setText("QTY"); // set the text for the header
label_quantity.setTextSize(20);
label_quantity.setTextColor(Color.BLACK); // set the color
label_quantity.setGravity(Gravity.CENTER);
tr_head.addView(label_quantity); // add the column to the table row here
tableLayout.setScrollContainer(true);
tableLayout.addView(tr_head, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="16dp"
android:layout_weight="1"
android:scrollbars="none">
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
android:stretchColumns="*"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"></TableLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
I have manually added 100 dummy rows to test its scroll behaviour and would like to inform you that it is exactly working as it should. There could be a problem in your Activity or Fragment parent code which is preventing the table from scrolling.
You can use TableView library to make your Table scrollable with dynamic data. The link is given below:-
https://github.com/evrencoskun/TableView
I want to display underneath my AutoCompleteTextView what the user entered when they press the button beside it. So basically adding a textview in response to button clicks. I have "hello world" displaying where i want them to be right now, but am stuck on how to do this inside a button listener. Any help is appreciated thank you.
Here is an image example of what I am trying to accomplish exactly.
sample display
my onCreate so far:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = new TextView(this);
textView.setText("hello world");
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_main);
//LayoutParams is a class to specify layout properties when adding a view to a layout
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.addView(textView, layoutParams);
}
}
XML for layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.tester.MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:text="AutoCompleteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/autoCompleteTextView"
android:layout_weight="1" />
<Button
android:text="PRESS ME"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button2"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
Try this
Button button1 = (Button) findViewById(R.id.button2);
AutocompleteTextView autoTv = (autocompleteTextView) findViewById(R.id.autocompleteTextView);
button1.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
String enteredText = autoTv.getText().toString();
if (!enteredText.isEmpty()) {
TextView textView = new TextView(this);
textView.setText(enteredText);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_main);
//LayoutParams is a class to specify layout properties when adding a view to a layout
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.addView(textView, layoutParams);
}
}
});
I am trying to make a to-do list using an EditText and a ListView. How can I change the text font, color and size? I have seen a couple answers using array adapters, but don't know how to apply them to dynamically created ListView items.
Here is what I have so far:
ActivityMain.xml
<RelativeLayout
android:id="#+id/AgendaRL"
android:orientation="vertical"
android:background="#3E2723"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/agenda"
android:layout_width="370sp"
android:layout_height="wrap_content"
android:text="#string/agenda"
android:textSize="40sp"
android:textColor="#b7950b"
android:layout_marginTop="12sp"
android:layout_marginLeft="12sp"
android:layout_marginStart="12sp"
android:layout_marginBottom="0sp" />
<View
android:background="#b7950b"
android:layout_below="#+id/agenda"
android:layout_width="28sp"
android:layout_height="36sp"/>
<EditText
android:id="#+id/aTask"
android:layout_below="#+id/agenda"
android:background="#drawable/ribbon"
android:inputType="text"
android:text="#string/Add_Task"
android:textColor="#3E2723"
android:maxLength="22"
android:maxLines="1"
android:layout_width="330sp"
android:layout_height="36sp"
android:textSize="28sp"
android:layout_marginLeft="28sp"
android:layout_marginStart="28sp"/>
<Button
android:id="#+id/Done"
style="?android:attr/borderlessButtonStyle"
android:layout_marginLeft="250sp"
android:layout_marginStart="250sp"
android:background="#b7950b"
android:text="#string/Done"
android:textColor="#3E2723"
android:textSize="18sp"
android:layout_below="#+id/agenda"
android:layout_width="48sp"
android:layout_height="36sp"
android:onClick="DoneClick"/>
<ListView
android:id="#+id/LVAgenda"
android:layout_below="#+id/aTask"
android:divider="#android:color/transparent"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
MainActivity.Java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView LVAgenda = (ListView) findViewById(R.id.LVAgenda);
arrayListAgenda = new ArrayList<String>();
arrayAdapterAgenda = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayListAgenda);
LVAgenda.setAdapter(arrayAdapterAgenda);
}
public void DoneClick(View v){
EditText aTask = (EditText)findViewById(R.id.aTask);
String agenda = aTask.getText().toString().trim();
if(agenda.isEmpty()){
return;
}
arrayAdapterAgenda.add(agenda);
aTask.setText("Add task");
}
As commonsware said you can use getView() of ArrayAdapter to do this.
I have implemented Facebook friend selector with ListAdapter. I will share the code. May be it helps. Please try.
First make a XML file in layout that defines the layout of each item of your 'to do list'.
In my case it is a facebook profile image and a checked textbox. (There is also a spacer for alignment)
Facebook.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/img"
android:layout_width="50dp"
android:layout_height="50dp"
android:paddingLeft="10dp"/>
<CheckedTextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|right"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:textColor="#android:color/black"
android:textStyle="bold" />
</LinearLayout>
<View
android:id="#+id/spacer"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/white"/>
</LinearLayout>
Now prepare your data array. In my case it is custom class array where each element contains a facebook name, profilepic, and a boolean.
public class Item{
public final String text;
public final Drawable icon;
public boolean isChecked;
public Item(String text, Drawable icon, boolean ischeck) {
this.text = text;
this.icon = icon;
this.isChecked = ischeck;
}
#Override
public String toString() {
return text;
}
}
I call the below code by passing friendsArray from another activity.
final Item[] items = new Item[friendsArray.length];
try {
int a = 1;
for (int i = 0; i < friendsArray.length; i++) {
tsk = new DownloadImageTask();
Bitmap bmp = (Bitmap) tsk.execute(new RaceActivity.FriendInfo[]{friendsArray[i]}).get();
Resources res = getActivity().getResources();
drawable = new BitmapDrawable(res, bmp);
items[i] = new Item(friendsArray[i].name, drawable,false);
}
}
catch(Exception ex)
{
}
Now your data array is prepared. You can pass this to ListAdapter(items in my case).
Its nice to understand the working of a List adapter. I created a scrollable List. What this logic does is it reuses the Views while scrolling.
ListAdapter adapter = new ArrayAdapter<Item>(
getActivity(),
android.R.layout.select_dialog_item,
android.R.id.text1,
items){
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
FaceBookHolder fb = new FaceBookHolder();
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater) getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.facebook,null);
fb.Name = (CheckedTextView) v.findViewById(R.id.name);
fb.img = (ImageView) v.findViewById(R.id.img);
fb.spacer = (View) v.findViewById(R.id.spacer);
fb.Name.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
CheckedTextView cv = (CheckedTextView)v;
if(cv.isChecked())
cv.setChecked(false);
else
cv.setChecked(true);
for(int i =0;i< items.length; i++)
{
if(items[i].text == cv.getText())
{
items[i].isChecked = cv.isChecked();
}
}
}
});
v.setTag(fb);
}
else
fb = (FaceBookHolder) v.getTag();
Item itm = items[position];
fb.Name.setText(itm.text);
fb.img.setImageDrawable(itm.icon);
fb.Name.setChecked(itm.isChecked);
return v;
}
};
you get the view in getView(), so you can modify it however you want.(change color , font etc)
Hope it helps! cheers!
This should be easy, how can I align the star in the middle. I'm adding the top header textviews and stars dynamically in code. I tried setting gravity on the imageview did not work.
UPDATE: I added iv.setScaleType(ImageView.ScaleType.FIT_START) now star aligns to left
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads">
<TableLayout
android:id="#+id/TableAch"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white"
android:layout_alignParentTop="true"
android:shrinkColumns="*"
android:stretchColumns="*"
>
<TableRow
android:id="#+id/RowAchTitle"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center_horizontal"
>
<TextView
android:id="#+id/ViewAchTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Achievements"
android:gravity="center"
android:layout_span="6">
</TextView>
</TableRow>
<TableRow
android:id="#+id/RowAchHeader"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:weightSum="6"
>
</TableRow>
</TableLayout>
</RelativeLayout>
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.acheivements);
tbLayout = (TableLayout) this.findViewById(R.id.TableAch);
rowHeader = (TableRow) this.findViewById(R.id.RowAchHeader);
FlurryAgent.logEvent("Achievements");
TableRow.LayoutParams tableRowParams=
new TableRow.LayoutParams
(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT);
tableRowParams.setMargins(0, 0, 0, 5);
TableRow.LayoutParams params = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT);
params.weight = 1;
params.gravity= Gravity.CENTER;
int fontSize = 12;
TextView tv = new TextView(this);
tv.setLayoutParams(params);
tv.setText(" ");
tv.setTextSize(fontSize);
rowHeader.addView(tv); //This is blank top left corner
for (Level level : Level.values()) { //Then we add all the headers for games
tv = new TextView(this);
tv.setText("EASY");
tv.setTextSize(fontSize);
tv.setLayoutParams(params);
rowHeader.addView(tv);
}
//Next we add the levels for each game, putting a pass image where passed
for (Games game : Games.values()) {
TableRow newRow = new TableRow(this);
newRow.setLayoutParams(tableRowParams);
String[] words = game.name().split("(?=[A-Z])");
String name = "";
for (String word : words) {
if(word.length() < 1) continue;
name += word + "\n";
}
tv = new TextView(this);
tv.setTextSize(fontSize);
tv.setText(name);
tv.setLayoutParams(params);
newRow.addView(tv);
newRow.setWeightSum(6);
for (Level level : Level.values()) {
ImageView iv = new ImageView(this);
iv.setScaleType(ImageView.ScaleType.FIT_START);
iv.setLayoutParams(params);
tv = new TextView(this);
tv.setLayoutParams(params);
tv.setText("EASY");
tv.setTextSize(fontSize);
if( acheivements.getPassed(level, game) )//has achievement
{
iv.setImageResource(R.drawable.star_gold_full_small);
}
else {
iv.setImageResource(R.drawable.star_gold_empty_small);
}
newRow.addView(tv);
}
tbLayout.addView(newRow);
}
setupAds(true);
}
You should do this:
TableRow.LayoutParams params = new TableRow.LayoutParams(0,WRAP_CONTENT);
you should have layout params of tablerow for textviews.
then in this params you should add weight.
params.weight = 1;
params.gravity = GRAVITY.CENTER;
and your table row should have gravity "Center" for its content (not layout gravity).
Try do this:
for (Games game : Games.values()) {
TableRow newRow = new TableRow(this);
newRow.setLayoutParams(tableRowParams);
TableRow.LayoutParams params2 = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT);
params2.weight = .8f;
params2.gravity= Gravity.CENTER;
String[] words = game.name().split("(?=[A-Z])");
String name = "";
for (String word : words) {
if(word.length() < 1) continue;
name += word + "\n";
}
tv = new TextView(this);
tv.setTextSize(fontSize);
tv.setText(name);
tv.setLayoutParams(params2);
newRow.addView(tv);
newRow.setWeightSum(6);
for (Level level : Level.values()) {
ImageView iv = new ImageView(this);
iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
iv.setAdjustViewBounds(true);
iv.setLayoutParams(params);
tv = new TextView(this);
tv.setLayoutParams(params);
tv.setText("EASY");
tv.setTextSize(fontSize);
if( acheivements.getPassed(level, game) )//has achievement
{
iv.setImageResource(R.drawable.star_gold_full_small);
}
else {
iv.setImageResource(R.drawable.star_gold_empty_small);
}
newRow.addView(tv);
}
tbLayout.addView(newRow);
}
In this code i have created new layout params as params2 because the textview was using weight of 1 so that your images were little right sided. i had used .8f weight for the names so now its working ok. so do as per your req.
Hope it Helps!!
Here's a minimal solution based on the above comments:
<ImageView
android:scaleType="fitCenter"
android:layout_gravity="center"
android:layout_weight="1"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#mipmap/cross" />
The first half containes the attributes that seem to be necessary to solve the problem.
I have a little problem updating UI in OnClick Event.
When I do this:
case R.id.radStripMiningCoal :
{
LinearLayout testLayout = (LinearLayout)findViewById(R.id.layouttest);
LinearLayout layout3 = (LinearLayout)findViewById(R.id.layout2);
LinearLayout layout4 = (LinearLayout)findViewById(R.id.layout3);
LinearLayout layout = (LinearLayout)findViewById(R.id.recomended);
LinearLayout layout2 = (LinearLayout)findViewById(R.id.basic);
int origWidth = getWindowManager().getDefaultDisplay().getWidth();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(origWidth,LayoutParams.WRAP_CONTENT);
layout2.setLayoutParams(params);
layout.setLayoutParams(params);
testLayout.setLayoutParams(params);
layout3.setLayoutParams(params);
layout4.setLayoutParams(params);
stripminingcoal.setVisibility(View.VISIBLE);
VSLists.setVisibility(View.GONE);
Next.setVisibility(View.GONE);
AnimationDrawable animation = new AnimationDrawable();
animation.addFrame(getResources().getDrawable(R.drawable.multispd1), 5000);
animation.addFrame(getResources().getDrawable(R.drawable.img1), 5000);
animation.addFrame(getResources().getDrawable(R.drawable.shocktubecluster1), 5000);
animation.setOneShot(false);
ImageView imageAnim = (ImageView) findViewById(R.id.imgBasicIS);
imageAnim.setBackgroundDrawable(animation);
animation.start();
AnimationDrawable animation2 = new AnimationDrawable();
animation2.addFrame(getResources().getDrawable(R.drawable.unidelaysp), 5000);
animation2.addFrame(getResources().getDrawable(R.drawable.trunkline), 5000);
animation2.setOneShot(false);
ImageView imageAnim2 = (ImageView) findViewById(R.id.imgRecomendedIS);
imageAnim2.setBackgroundDrawable(animation2);
animation2.start();
AnimationDrawable animation3 = new AnimationDrawable();
animation3.addFrame(getResources().getDrawable(R.drawable.leadin2), 5000);
animation3.addFrame(getResources().getDrawable(R.drawable.tubestarter),5000);
animation3.setOneShot(false);
ImageView imageAnim3 = (ImageView) findViewById(R.id.imgRecomendedBSS);
imageAnim3.setBackgroundDrawable(animation3);
animation3.start();
txtBasicIS.setText("Shock Tube Multi SPD / Detonating Cord / Cluster");
txtRecomendedIS.setText("Shock Tube Uni-Delay SP / Trunkline");
txtRecomendedBSS.setText("Lead-In Line / Electric Shock Tube Starter");
Everything updates and displays fine.
But s soon as I do the same code in a different on "case" it does not update the images to the new images:
case R.id.radNarrowReefGold:
{
LinearLayout testLayout = (LinearLayout)findViewById(R.id.layouttest);
LinearLayout layout3 = (LinearLayout)findViewById(R.id.layout2);
LinearLayout layout4 = (LinearLayout)findViewById(R.id.layout3);
LinearLayout layout = (LinearLayout)findViewById(R.id.recomended);
LinearLayout layout2 = (LinearLayout)findViewById(R.id.basic);
int origWidth = getWindowManager().getDefaultDisplay().getWidth();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(origWidth,LayoutParams.WRAP_CONTENT);
layout2.setLayoutParams(params);
layout.setLayoutParams(params);
testLayout.setLayoutParams(params);
layout3.setLayoutParams(params);
layout4.setLayoutParams(params);
stripminingcoal.setVisibility(View.VISIBLE);
VSLists.setVisibility(View.GONE);
Next.setVisibility(View.GONE);
AnimationDrawable animation = new AnimationDrawable();
animation.addFrame(getResources().getDrawable(R.drawable.udlp3), 5000);
animation.addFrame(getResources().getDrawable(R.drawable.udlp6), 5000);
animation.addFrame(getResources().getDrawable(R.drawable.trunkline), 5000);
animation.setOneShot(false);
ImageView imageAnim = (ImageView) findViewById(R.id.imgBasicIS);
imageAnim.setBackgroundDrawable(animation);
animation.start();
AnimationDrawable animation2 = new AnimationDrawable();
animation2.addFrame(getResources().getDrawable(R.drawable.udlp3vivid), 5000);
animation2.addFrame(getResources().getDrawable(R.drawable.udlp6vivid), 5000);
animation2.addFrame(getResources().getDrawable(R.drawable.trunkline), 5000);
animation2.setOneShot(false);
ImageView imageAnim2 = (ImageView) findViewById(R.id.imgRecomendedIS);
imageAnim2.setBackgroundDrawable(animation2);
animation2.start();
AnimationDrawable animation3 = new AnimationDrawable();
animation3.addFrame(getResources().getDrawable(R.drawable.leadin2), 5000);
animation3.addFrame(getResources().getDrawable(R.drawable.tubestarter),5000);
animation3.setOneShot(false);
ImageView imageAnim3 = (ImageView) findViewById(R.id.imgRecomendedBSS);
imageAnim3.setBackgroundDrawable(animation3);
animation3.start();
txtBasicIS.setText("Uni-Delay LP (3) / Uni-Delay LP (6) / Trunkline");
txtRecomendedIS.setText("Shock Tube Uni-Delay SP / Trunkline");
txtRecomendedBSS.setText("Lead-In Line / Electric Shock Tube Starter");
}
What am I missing or doing wrong?
The textViews does not change either.
Thanks in advance.
Patrick
It is due to the missing "break;" statement in your switch case structure.
You can test this using the following small snippets i wrote on the fly. If you remove the break; statement, you could not swicth the text to MESSI, it shows only RONALDO. In other words, the logic is broken.
If the "break;" is there, you can switch between MESSI and RONALDO.
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
TextView myText;
private Button b1;
private Button b2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myText = (TextView)this.findViewById(R.id.textView1);
b1 = (Button)this.findViewById(R.id.buttonMessi);
b1.setOnClickListener(this);
b2 = (Button)this.findViewById(R.id.buttonRonaldo);
b2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.buttonMessi:
myText.setText("MESSI");
break;
case R.id.buttonRonaldo:
myText.setText("RONALDO");
break;
}
}
}
activity_main.xml
<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=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="horizontal">
<Button
android:id="#+id/buttonMessi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView1"
android:layout_alignRight="#+id/textView1"
android:layout_marginBottom="40dp"
android:text="Messi" />
<Button
android:id="#+id/buttonRonaldo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView1"
android:layout_alignRight="#+id/textView1"
android:layout_marginBottom="40dp"
android:text="Ronaldo" />
</LinearLayout>
</RelativeLayout>
Try to add img.SetImageResource(0); before the previous line.
It worked kudos to you