How to display data in matrix form in java/android - java

I have following data in this format:
(0,0) -10
(1,0) - 20
(1,1) - 30
Medical- (0,0) -Jack
One medical Student
Engineer - (1,0) - Jones
(1,1) - Danny
two Engineer Student
I try this:
for (int j = 0; j < Test.subNameArrayList.size(); j++) {
for (int i = 0; i < 2; i++) {
Subject[j] = new String[1];
Subject[j][i] = Test.subNameArrayList
.get(j);
Name[j] = new String[2];
Name[j][i] = Test.NameArrayList
.get(j);
}
}
how to display in matrix form in java and android. and how to find row size and column size.Thanks in advance.

Xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
</TableLayout>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
</LinearLayout>
Activity:
public class TestActivity extends AppCompatActivity {
TableLayout tableLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
tableLayout = findViewById(R.id.tableLayout1);
int[][] matrix = {
{1,2,3},
{4,5,6},
{7,8,9}
};
fillTable(matrix,tableLayout);
}
private void fillTable(final int[][] matrix, TableLayout table) {
table.removeAllViews();
for (int i = 0; i < matrix.length; i++) {
TableRow row = new TableRow(TestActivity.this);
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
for (int j = 0; j < matrix[i].length; j++) {
TextView textView = new TextView(TestActivity.this);
textView.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL|InputType.TYPE_NUMBER_FLAG_SIGNED);
textView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
textView.setText((matrix[i][j]+""));
textView.setPadding(10,10,10,10);
row.addView(textView);
}
table.addView(row);
}
}
}

Related

Error - java.lang.ClassCastException:com.google.android.material.appbar.AppBarLayout cannot be cast to androidx.appcompat.widget.Toolbar

I run my app and it crashes when I wish to open SheetActivity.java
The error shows the error in my sheet activity but my xml file for that activity does not contain app bar layout . My toolbar.xml contains app bar layout and I have included the toolbar in my activity_sheet.xml.
My SheetActivity.java:-
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import java.util.Calendar;
public class SheetActivity extends AppCompatActivity {
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sheet);
showTable();
setToolbar();
}
private void setToolbar() {
toolbar = findViewById(R.id.toolSheet);
TextView title = toolbar.findViewById(R.id.title_toolbar);
TextView subtitle = toolbar.findViewById(R.id.subtitle_toolbar);
ImageButton back = toolbar.findViewById(R.id.back);
ImageButton save = toolbar.findViewById(R.id.save);
title.setText("Attendance Sheet");
subtitle.setVisibility(View.GONE);
back.setVisibility(View.INVISIBLE);
save.setVisibility(View.INVISIBLE);
}
private void showTable() {
DbHelper dbHelper = new DbHelper(this);
TableLayout tableLayout = findViewById(R.id.tableLayout);
long[] idArray = getIntent().getLongArrayExtra("idArray");
int[] rollArray = getIntent().getIntArrayExtra("rollArray");
String[] nameArray = getIntent().getStringArrayExtra("nameArray");
String month = getIntent().getStringExtra("month");
int DAY_IN_MONTH = getDayInMonth(month);
int rowSize = idArray.length + 1;
TableRow[] rows = new TableRow[rowSize];
TextView[] roll_tvs = new TextView[rowSize];
TextView[] name_tvs = new TextView[rowSize];
TextView[][] status_tvs = new TextView[rowSize][DAY_IN_MONTH + 1];
for (int i = 0; i<rowSize; i++){
roll_tvs[i] = new TextView(this);
name_tvs[i] = new TextView(this);
for (int j = 1; j <= DAY_IN_MONTH; j++){
status_tvs[i][j] = new TextView(this);
}
}
roll_tvs[0].setText("Roll");
roll_tvs[0].setTypeface(roll_tvs[0].getTypeface(), Typeface.BOLD);
name_tvs[0].setText("Name");
name_tvs[0].setTypeface(name_tvs[0].getTypeface(), Typeface.BOLD);
for (int i = 1; i <= DAY_IN_MONTH; i++){
status_tvs[0][i].setText(String.valueOf(i));
status_tvs[0][i].setTypeface(status_tvs[0][i].getTypeface(), Typeface.BOLD);
}
for (int i = 1; i<rowSize; i++){
roll_tvs[i].setText(String.valueOf(rollArray[i-1]));
name_tvs[i].setText(nameArray[i-1]);
for (int j = 1; j <= DAY_IN_MONTH; j++){
String day = String.valueOf(j);
if (day.length()==1) day = "0"+day;
String date = day+"."+month;
String status = dbHelper.getStatus(idArray[i-1], date);
status_tvs[i][j].setText(status);
}
}
for (int i = 0; i<rowSize; i++){
rows[i] = new TableRow(this);
if(i%2==0)
rows[i].setBackgroundColor(Color.parseColor("#EEEEEE"));
else rows[i].setBackgroundColor(Color.parseColor("#E4E4E4"));
roll_tvs[i].setPadding(16,16,16,16);
name_tvs[i].setPadding(16,16,16,16);
rows[i].addView(roll_tvs[i]);
rows[i].addView(name_tvs[i]);
for (int j = 1; j <= DAY_IN_MONTH; j++){
status_tvs[i][j].setPadding(16,16,16,16);
rows[i].addView(status_tvs[i][j]);
}
tableLayout.addView(rows[i]);
}
tableLayout.setShowDividers(TableLayout.SHOW_DIVIDER_MIDDLE);
}
private int getDayInMonth(String month) {
int monthIndex = Integer.parseInt(month.substring(0, 2)) - 1;
int year = Integer.parseInt(month.substring(3));
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, monthIndex);
calendar.set(Calendar.YEAR, year);
return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}
}
My xml file for SheetActivity.java - activity_sheet.xml:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".SheetActivity">
<include
android:id="#+id/toolSheet"
layout="#layout/toolbar"
android:layout_width="427dp"
android:layout_height="wrap_content" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none">
<LinearLayout
android:layout_width="wrap_content"
android:padding="16dp"
android:layout_height="wrap_content">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tableLayout"
android:background="#FFFFFF"
android:divider="#android:color/black"
android:showDividers="beginning|middle|end"
android:stretchColumns="*"/>
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
The error which shows up:-
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.manage.attendx/com.manage.attendx.SheetActivity}: java.lang.ClassCastException: com.google.android.material.appbar.AppBarLayout cannot be cast to androidx.appcompat.widget.Toolbar
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3119)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1839)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6864)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: java.lang.ClassCastException: com.google.android.material.appbar.AppBarLayout cannot be cast to androidx.appcompat.widget.Toolbar
at com.manage.attendx.SheetActivity.setToolbar(SheetActivity.java:28)
at com.manage.attendx.SheetActivity.onCreate(SheetActivity.java:25)
at android.app.Activity.performCreate(Activity.java:7232)
at android.app.Activity.performCreate(Activity.java:7221)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2964)
... 11 more
My xml file for toolbar:-
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.appbar.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="16dp">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/back"
android:background="#android:color/transparent"
android:layout_centerVertical="true"
android:src="#drawable/icon_back"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="36dp"
android:layout_marginLeft="36dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/title_toolbar"
android:gravity="center_horizontal"
android:text="Title"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#FFFFFF"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/subtitle_toolbar"
android:gravity="center_horizontal"
android:text="Subtitle"
android:textSize="12sp"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
</LinearLayout>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/save"
android:background="#android:color/transparent"
android:layout_alignParentRight="true"
android:src="#drawable/icon_save"
android:layout_centerVertical="true"/>
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
Can somebody please help me out with this problem?
Answer - Inorder to resolve the issue , we have to remove the method setToolbar (); from SheetActivity.java .
Otherwise, AppBarLayout cannot be casted to androidx.appcopat.widget.toolbar

row generator with textviews

This is my code for a n app in which i want to add textviews in a row
dynamically on click of a button and not working can you tell me whats wrong with the code,i am just a beginner.
thanking you in anticipation.
public class MainActivity extends AppCompatActivity {
Button save;
TableLayout tableLayout;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button save = (Button) findViewById(R.id.save);
TableLayout tableLayout = (TableLayout) findViewById(R.id.table1);
save.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
TableRow tableRow;
TextView textView;
TableLayout tableLayout = (TableLayout) findViewById(R.id.table1);
for (int i = 0; i < 4; i++) {
tableRow = new TableRow(getApplicationContext());
for (int j = 0; j < 3; j++) {
textView = new TextView(getApplicationContext());
textView.setText("####");
textView.setPadding(20, 20, 20, 20);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}}});
setContentView(tableLayout);
}
}
and my xml layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.kalla.monu.saplist.MainActivity">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="28dp"
android:layout_marginTop="160dp"
android:id="#+id/table1"
android:background="#color/colorPrimaryDark">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"/>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:backgroundTint="#android:color/holo_green_dark" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TableLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
android:id="#+id/save" />
</RelativeLayout>
Try this,
I assume you are trying to add tablerows and textviews both dynamically. So I have removed existing tablerows from layout xml.
TableLayout tableLayout = (TableLayout) findViewById(R.id.table1);
save.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
TableRow tableRow;
TextView textView;
for (int i = 0; i < 4; i++) {
tableRow = new TableRow(this);
for (int j = 0; j < 3; j++) {
textView = new TextView(this);
textView.setText("####");
textView.setPadding(20, 20, 20, 20);
tableRow.addView(textView);
}
table1.addView(tableRow);
}}});
In XML:
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/table1"/>

All buttons of dinamically added matrix of ImageButtons became black

This is how it looked in the beginning. After I created new project and added this code with some modification, the view turn into smth like this(notice that only those buttons in the matrix are black):
Here is what my xml file contains:
<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" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/main_container" >
</LinearLayout>
<RelativeLayout
android:layout_below="#id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:layout_centerHorizontal="true"
android:id="#+id/refresh_button"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/refresh"/>
<TextView
android:layout_centerHorizontal="true"
android:layout_below="#id/refresh_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/minesLeft"
android:text="#string/minesLeft"/>
<TextView
android:layout_centerHorizontal="true"
android:layout_below="#id/refresh_button"
android:layout_toRightOf="#+id/minesLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/minesLeftCounter"/>
</RelativeLayout>
</RelativeLayout>
And this is how I created this:
map = new ArrayList< ArrayList<ImageButton> >();
LinearLayout mainContainer = (LinearLayout) c.findViewById(R.id.main_container);
//Params used for cells
TableRow.LayoutParams params = new TableRow.LayoutParams(
0, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f
);
int pixelsToDp = convertToPixelsDp(-4.5f);
params.setMargins(pixelsToDp, pixelsToDp, pixelsToDp, pixelsToDp);
//Params used for rows
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT
);
rowParams.setMargins(0, 0, 0, 0);
//Params used for table
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT
);
tableParams.setMargins(0, 0, 0, 0);
//Table generation
TableLayout table = new TableLayout(mAppContext);
table.setLayoutParams(tableParams);
table.setShowDividers(TableLayout.SHOW_DIVIDER_NONE);
table.setStretchAllColumns(true);
for(int i = 0; i < 15; i++){
ArrayList<ImageButton> rowOfList = new ArrayList<ImageButton>();
TableRow row = new TableRow(mAppContext);
row.setShowDividers(TableRow.SHOW_DIVIDER_NONE);
row.setLayoutParams(rowParams);
for(int j = 0; j < 10; j++){
ImageButton btn = new ImageButton(mAppContext);
btn.setPadding(0, 0, 0, 0);
btn.setLayoutParams(params);
btn.setScaleType(ImageView.ScaleType.CENTER_CROP);
rowOfList.add(btn);
row.addView(btn);
}
map.add(rowOfList);
table.addView(row);
}
mainContainer.addView(table);
What could cause change the background color of ImageButtons? THank you!

add((RelativeLayout) findViewById) to ArrayList<RelativeLayout>

I have the following code:
public class MainActivity extends Activity implements OnClickListener {
public ArrayList<RelativeLayout> buttons = new ArrayList<RelativeLayout>();
public ArrayList<Integer> drawableId = new ArrayList<Integer>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
linear = (LinearLayout) findViewById(R.id.Rlayout);
scrool = (ScrollView) findViewById(R.id.scrool);
for (int i = 0; i < buttons.size(); i++) {
{
String buttonID = "Llayout" + i;
int resID = getResources().getIdentifier(buttonID, "id",
getPackageName());
//The problem is here
buttons.get(i).add((RelativeLayout) findViewById(resID));
buttons.get(i).setOnClickListener(this);
drawableId.add(getResources().getIdentifier("supa" + i, "drawable", getPackageName()));
}
}
}
#Override
public void onClick(View v) {
for (int i = 0; i < buttons.size(); i++) {
if (buttons.get(i).getId() == v.getId()) {
index = i;
Intent trimite = new Intent(MainActivity.this, RecipeView.class);
Bundle colet = new Bundle();
colet.putString("key", Content.RETETE[i]);
colet.putInt("keyimg", drawableId.get(i));
trimite.putExtras(colet);
startActivity(trimite);
break;
}
}
}
}
I cannot get this to work:
buttons.get(i).add((RelativeLayout) findViewById(resID));
Java error says that: add((RelativeLayout) is undefined for the type RelativeLayout.
The add method works for ArrayList drawableId, one line below but not for buttons. Can someone, please, help me?
Here is my xml layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/scrool"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:scrollbars="vertical"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" >
<LinearLayout
android:id="#+id/Rlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:keepScreenOn="true"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/Llayout0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:onClick="onClick"
android:orientation="vertical" >
<ImageView
android:id="#+id/iv0"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="left"
android:contentDescription="#string/desc"
android:src="#drawable/supa0" />
<TextView
android:id="#+id/tv0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/supa"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/Llayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:onClick="onClick"
android:orientation="vertical" >
<ImageView
android:id="#+id/iv1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="left"
android:contentDescription="#string/desc"
android:src="#drawable/supa0" />
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/supa1"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
I made buttons from RelativeLayouts. Each RelativeLayout button has text and an icon next to the text. All of the RelativeLayouts buttons are in a LinearLayout so that they align under each other.
When the RelativeLayouts buttons and the ArrayList drawableId were simple arrays it worked great but I don't want to modify the array every time I add a new RelativeLayout button.
Any suggestions?
The RelativeLayout class has no add() method.
buttons is an ArrayList of RelativeLayouts. When you call buttons.get(i), you get a single RelativeLayout object. Since RelativeLayout has no add() method, you get this error.
It isn't entirely clear what your intent is with this code, but I am assuming that you are trying to add each of your RelativeLayouts to the buttons list.
If this is correct, you just need to remove the get(i) call, making that line into
buttons.add((RelativeLayout) findViewById(resID));
Try this way,hope this will help you to solve your problem.
public class MainActivity extends Activity implements View.OnClickListener {
private ArrayList<RelativeLayout> buttons = new ArrayList<RelativeLayout>();
private ArrayList<Integer> drawableId = new ArrayList<Integer>();
private LinearLayout linear;
private ScrollView scrool;
private int index;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
linear = (LinearLayout) findViewById(R.id.Rlayout);
scrool = (ScrollView) findViewById(R.id.scrool);
for (int i = 0; i < linear.getChildCount(); i++) {
{
String buttonID = "Llayout" + i;
int resID = getResources().getIdentifier(buttonID, "id",getPackageName());
buttons.add((RelativeLayout)findViewById(resID));
buttons.get(i).setOnClickListener(this);
drawableId.add(getResources().getIdentifier("supa" + i, "drawable", getPackageName()));
}
}
}
#Override
public void onClick(View v) {
for (int i = 0; i < buttons.size(); i++) {
if (buttons.get(i).getId() == v.getId()) {
index = i;
Toast.makeText(MyActivity.this,String.valueOf(index),Toast.LENGTH_SHORT).show();
Intent trimite = new Intent(MainActivity.this, RecipeView.class);
Bundle colet = new Bundle();
colet.putString("key", Content.RETETE[i]);
colet.putInt("keyimg", drawableId.get(i));
trimite.putExtras(colet);
startActivity(trimite);
break;
}
}
}
}

Creating Linear layout with Horizontal scroll view at the bottom of the screen

I am trying to create a 5x5 table with buttons that take 70% of the space Of the screen. Below this i want to put buttons horizontally with a scrollbar (at the bottom of the screen that means).
The java and xml files are shown below.
On the output screen i can only see the 5x5 table created and the bottom space is left blank. I am new to android and layout so please help me with this. Thanks!
xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TableLayout
android:id="#+id/mainPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TableLayout>
<HorizontalScrollView
android:id="#+id/hsvMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/scroll_ll"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:isScrollContainer="true"
android:scrollbars="horizontal"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
Java file:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.match_main_skl);
table = new TableLayout (this);
setControls();
hsv = new HorizontalScrollView(this);
ll = new LinearLayout(this);
mainTable();
}
private void setControls()
{
table = (TableLayout) findViewById(R.id.mainPage);
hsv = (HorizontalScrollView) findViewById(R.id.hsvMain);
ll = (LinearLayout) findViewById(R.id.scroll_ll);
}
private void mainTable()
{
//find screen size of the device (supports all versions)
Point windowSize = MAUIUtil.GetDefaultWindowSize(this);
int cellSize;
if (windowSize.x >= 0.70 * windowSize.y)
cellSize = windowSize.x / numColumns;
else cellSize = (int) ((0.70*windowSize.y)/numColumns);
**//5x5 TABLE**
TableRow.LayoutParams buttonRow = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT, 1.0f);
buttonRow.height = cellSize * numRows;
//buttonRow.height = windowSize.x;
table.setLayoutParams(buttonRow);
table.setStretchAllColumns(true);
for(int rw = 0; rw < numRows; rw++)
{
TableRow row = new TableRow(this);
row.setPadding(0, 0, 0, 0); //setPadding(left,top,right,bottom)
row.setId(ROW_ID_OFFSET + rw);//giving each row an id
TableLayout.LayoutParams tbLp = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,1.0f);
tbLp.height = cellSize;
table.setLayoutParams(tbLp);
for(int col = 0; col < numColumns; col++)
{
int btnID = COLUMN_ID_OFFSET + (numColumns * (rw - 1)) + col;
//Adding buttons
Button btn = new Button(this);
btn.setId(btnID);
btn.setPadding(0, 0, 0, 0);
btn.setTypeface(Typeface.DEFAULT_BOLD);
btn.setBackgroundColor(Color.CYAN);
btn.setText(Integer.toString(btnID));
row.addView(btn);
}
table.addView(row);
}
**//LAST SCROLLABLE HORIZONTAL ROW**
hsv.addView(ll);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
llp.height = windowSize.y - cellSize;
hsv.setLayoutParams(llp);
LinearLayout sll = new LinearLayout(this);
for(int col = 0; col < scrNumColumns; col++)
{
int btnID = SCROLL_COLUMN_ID_OFFSET + col;
//Adding buttons
Button btn = new Button(this);
btn.setId(btnID);
btn.setPadding(0, 0, 0, 0);
btn.setTypeface(Typeface.DEFAULT_BOLD);
btn.setBackgroundColor(Color.RED);
btn.setText(Integer.toString(btnID));
sll.addView(btn);
sll.setGravity(Gravity.BOTTOM);
}
ll.addView(sll);
}
It seems that you created a new LinearLayout and a new HorizontalScrollView after you called findViewById() in the onCreate() method. So you are operating the new created views rather than the views defined in the .xml file.
Try delete these rows:
hsv = new HorizontalScrollView(this);
ll = new LinearLayout(this);
Use below code.I have implemented it as you have described.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TableRow > </TableRow>
<TableRow > </TableRow>
<TableRow > </TableRow>
<TableRow > </TableRow>
<TableRow > </TableRow>
</TableLayout>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button4"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button5"/>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
Hope it will help.You can ask if you have any further queries.
use android:alain_parentBottom="true" in linear layout.and you will achieve what you want.

Categories