This is a sample try code. I am adding checkbox and textview in the table row by using loop. Everything is working fine except that whenever I am trying to take out(checkBox, Textview) from another method, I am not able to get it. I don't know how to solve this. I tried setId but I couldn't get the Id.
protected void onCreate(Bundle savedInstanceState) {
TableLayout tl = (TableLayout)findViewById(R.id.tableLayout1);
TableRow[] row = new TableRow[5];
TextView tv[] = new TextView[5];
CheckBox[] cb = new CheckBox[5];
Button clickTestRow;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(int j = 0; j < 5; j ++){
row[j] = new TableRow(this);
tl.addView(row[j]);
tv[j] = new TextView(this);
cb[j] = new CheckBox(this);
tv[j].setText("This is text");
row[j].addView(cb[j]);
row[j].addView(tv[j]);
if(j == 0)
cb[j].setChecked(true);
}
clickTestRow = (Button) findViewById(R.id.clickTestRow);
clickTestRow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TableLayout tlid = (TableLayout) findViewById(R.id.tableLayout1);
for(int i = 0; i < 5; i ++){
if(cb[i].isChecked()){
tlid.removeView(row[i]);
}
}
}
});
This is untested, but try this for now.
Key points here are
Fields for your views
Call setContentView before using findViewById
final variables for those that you are trying to use within an anonymous class (the onClickListener)
public class MainActivity extends Activity {
private TableLayout tl;
private Button clickTestRow;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tl = (TableLayout)findViewById(R.id.tableLayout1);
clickTestRow = (Button) findViewById(R.id.clickTestRow);
final TableRow[] row = new TableRow[5];
final TextView tv[] = new TextView[5];
final CheckBox[] cb = new CheckBox[5];
for(int j = 0; j < 5; j ++){
row[j] = new TableRow(this);
tl.addView(row[j]);
tv[j] = new TextView(this);
cb[j] = new CheckBox(this);
tv[j].setText("This is text");
row[j].addView(cb[j]);
row[j].addView(tv[j]);
}
cb[0].setChecked(true);
clickTestRow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for(int i = 0; i < 5; i ++){
if(cb[i].isChecked()){
tl.removeView(row[i]);
}
}
}
});
}
}
Related
I am newbie on Android Studio. I have a question.
I am making a match cards application.
After a failed match cards flipping back after 0,5 sec. But while this time, you can still check other cards. I want to disable that. I'm extends Button to a class. I'm posting both of this scripts. Thanks for your helps.
This is my Card Class.
public class kart extends Button {
boolean acikMi = false;
boolean cevrilebilir = true;
int arkaPlanID;
int onPlanID=0;
Drawable on;
Drawable d;
public kart(Context context, int id) {
super(context);
setId(id);
arkaPlanID = R.drawable.arka1;
if(id%8==1)
onPlanID = R.drawable.c1;
if(id%8==2)
onPlanID = R.drawable.c2;
if(id%8==3)
onPlanID = R.drawable.c3;
if(id%8==4)
onPlanID = R.drawable.c4;
if(id%8==5)
onPlanID = R.drawable.c5;
if(id%8==6)
onPlanID = R.drawable.c6;
if(id%8==7)
onPlanID = R.drawable.c7;
if(id%8==0)
onPlanID = R.drawable.c8;
d = AppCompatDrawableManager.get().getDrawable(context,arkaPlanID);
on = AppCompatDrawableManager.get().getDrawable(context, onPlanID);
setBackground(d);
}
public void cevir(){
if (cevrilebilir){
if(!acikMi){
setBackground(on);
acikMi = true;
}
else{
setBackground(d);
acikMi=false;
}
}
}
And this is my Java code.
public class GameScreen extends AppCompatActivity {
int sonKart = 0;
int skor = 0;
int hamle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_screen);
Intent i = getIntent();
final String s = i.getStringExtra("ad");
TextView tv = (TextView) findViewById(R.id.textView);
tv.setText(s);
GridLayout gr = (GridLayout) findViewById(R.id.grid);
kart kartlar[] = new kart[16];
for (int j = 1; j <= 16; j++) {
kartlar[j - 1] = new kart(this, j);
kartlar[j-1].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
hamle++;
final kart k = (kart)v;
k.cevir();
if (sonKart >0){
final kart k2 = (kart) findViewById(sonKart);
if (k2.onPlanID==k.onPlanID&&k2.getId()!=k.getId()){
k2.cevrilebilir=false;
k.cevrilebilir=false;
sonKart =0;
skor++;
TextView tv = (TextView) findViewById(R.id.textView3);
TextView tv2 = (TextView) findViewById(R.id.textView5);
tv.setText("Score: "+skor);
tv2.setText("Total moves: "+hamle);
if (skor==8){
Intent i = new Intent(GameScreen.this,scoreboard.class);
i.putExtra("ham",hamle);
i.putExtra("isim",s);
startActivity(i);
//oyun bitti
}
}
else{
Handler h = new Handler();
h.postDelayed(new Runnable() {
#Override
public void run() {
k2.cevir();
k.cevir();
}
},500);
sonKart =0;
TextView tv2 = (TextView) findViewById(R.id.textView5);
tv2.setText("Total moves: "+hamle);
}
}
else{
sonKart = k.getId();
}
}
});
}
for (int j=0;j<16;j++){
int rg =(int) (Math.random()*16);
kart k = kartlar[rg];
kartlar[rg] = kartlar [j];
kartlar[j] = k;
}
for (int j = 0; j < 16; j++)
gr.addView(kartlar[j]);
}
}
to disable click on button
button.setEnabled(false);
This is my code for a button. Whenever i click this the app shuts down!I am unable to understand why it's happening?
In my code, I am trying to check if values from dynamically created EditText field has been stored on my variable array values[] and bValues[];
I looked for many answers here on how to retrieve value from dynamic edittext fields. And I came up with this from my understanding.
Can anyone tell me where am I going wrong? And a corrected demo code will be of help!
public class MainActivity extends AppCompatActivity {
private static int a;
private static EditText ainput;
private static Button btn, set;
TextView equal_sign;
private static TableLayout tableLayout;
private static LinearLayout bMatrix;
List<EditText> allEds = new ArrayList<EditText>(a*a);
double[] values = new double[(allEds.size())];
List<EditText> bMatrixValues = new ArrayList<EditText>(a);
double[] bValues = new double[bMatrixValues.size()];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ainput = (EditText) findViewById(R.id.a);
btn = (Button) findViewById(R.id.btn);
set = (Button)findViewById(R.id.set);
tableLayout = (TableLayout) findViewById(R.id.table1);
equal_sign = (TextView) findViewById(R.id.equal_sign);
bMatrix = (LinearLayout) findViewById(R.id.bMatrix);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a = Integer.parseInt(ainput.getText().toString());
tableLayout.removeAllViews();
createMatrix();
}
});
set.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < allEds.size(); i++){
values[i] = Double.parseDouble(allEds.get(i).getText().toString());
}
for (int j = 0 ; j < bMatrixValues.size(); j++){
bValues[j] = Double.parseDouble(bMatrixValues.get(j).getText().toString());
}
if (allEds.size() > 0 && bMatrixValues.size() > 0){
Toast.makeText(MainActivity.this, "Main Matrix Values inserted!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "Insertion failed!", Toast.LENGTH_SHORT).show();
}
}
});
}
private void createMatrix() {
int p = 1;
for (int i = 0; i < a; i++) {
TableRow row = new TableRow(this);
TableRow.LayoutParams lp = new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.CENTER;
row.setLayoutParams(lp);
EditText magicField;
for (int j = 0; j < a; j++) {
magicField = new EditText(MainActivity.this);
allEds.add(magicField);
magicField.setId((p));
magicField.setHint("et-" + (Integer.toString(p)));
magicField.setGravity(Gravity.CENTER);
magicField.setWidth(100);
row.addView(magicField, j);
p++;
}
tableLayout.addView(row, i);
}
bMatrix.removeAllViews();
equal_sign.setText("=");
equal_sign.setTextSize(30);
createBMatrix();
}
private void createBMatrix() {
int id = (a * a) + 1;
for (int s = 0; s < a; s++) {
final EditText b = new EditText(this);
bMatrixValues.add(b);
b.setId(id);
b.setHint("b-" + (Integer.toString(s + 1)));
b.setGravity(Gravity.CENTER);
b.setWidth(100);
bMatrix.addView(b);
id++;
}
}
}
And this is the output screen!
Matrix style edit text field gets created nicely. But when i give values to those fields and click on set value button app shuts down..
I am working on Indoor positioning system with wifi fingerprinting in which I want to store level of specifics BSSID in arrryList I tried this code but it seems not to work
public class calibrate2 extends AppCompatActivity {
WifiManager wifimanager2;
Button cali;
Button reset;
//EditText Tx;
//EditText Ty;
TextView textView;
TextView textView2;
List<ScanResult> list;
Integer j,x,i=0;
ArrayList<String> Ap=new ArrayList<>();;
ArrayList<Integer> si=new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calibrate2);
Ap = getIntent().getStringArrayListExtra("ap");
cali = (Button) findViewById(R.id.button);
reset = (Button) findViewById(R.id.button2);
textView= (TextView)findViewById(R.id.textView);
textView2= (TextView)findViewById(R.id.textView2);
// Tx = (EditText) findViewById(R.id.editText);
// Ty = (EditText) findViewById(R.id.editText2);
wifimanager2 = (WifiManager) getSystemService(Context.WIFI_SERVICE);
cali.setOnClickListener(
new View.OnClickListener() {
public void onClick(View view) {
wifimanager2.startScan();
list = wifimanager2.getScanResults();
for (j = 0; j < Ap.size(); j++) {
for (i = 0; i < list.size(); i++) {
if (list.get(i).BSSID == Ap.get(j)) {
si.add(list.get(i).level);
break;
}
}
for (j = 0; j < Ap.size(); j++) {
textView2.append(si.get(j).toString());
textView2.append(" /n");
}
}
}
}
);
}}
I guess the problem is in the for loop because activity is working perfectly but whenever I click button it goes to parent activity
I got the answer now with this changes it is working
` for (i = 0; i < list.size(); i++) {
if (Ap.get(j).contains(list.get(i).BSSID) {
si.add(list.get(i).level);
break;}}
for (j = 0; j <si.size(); j++) {
//change Ap.size()to si.size()
textView2.append(si.get(j).toString());
textView2.append(" /n");}
I'm in the beginning of my learning how to make apps. I want to make an app which should display randomized inputted tasks to do for the user. Firstly user should choose how many tasks would like to create and then write down tasks in EditText. I am able to create particular amount of EditText but I have no clue how to display all EditText input after pressing button. I have many versions of the code but non of them work. I got stuck and I need advice.
Here is one of my code version for the second activity.
public class TaskActivity extends AppCompatActivity {
LinearLayout containerLayout;
TextView receiverTV;
TextView tv;
TextView displayTaskTv;
EditText et;
Button btn;
Button randomTaskBtn;
int i = 0;
int size;
String inputEditText;
String inputTextView;
String stringsEt[];
String stringsTv [];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_task);
containerLayout = (LinearLayout) findViewById(R.id.linear_layout);
receiverTV = (TextView) findViewById(R.id.receiver_textView);
Intent intent = getIntent();
int number = intent.getIntExtra("Number", defaultValue);
receiverTV.setText("You have chosen to add: " + number + " tasks!");
createEtTvBtn(number);
createBtn();
createTextView();
}
public void createEtTvBtn(int number) {
for (i = 1; i <= number; i++) {
tv = new TextView(this);
tv.setText("Task nr: " + i);
tv.setPadding(16, 16, 16, 16);
tv.setTextColor(Color.parseColor("#008b50"));
tv.setTextSize(20);
tv.setId(i + value);
containerLayout.addView(tv);
et = new EditText(this);
et.setHint("Enter task nr: " + i);
et.setId(i + value);
et.setLines(2);
containerLayout.addView(et);
btn = new Button(this);
btn.setText("Confirm task nr: " + i);
btn.setId(i + value);
containerLayout.addView(btn);
final List<EditText> allEditText = new ArrayList<EditText>();
final List<TextView>allTextView = new ArrayList<TextView>();
final List<Button>allButton = new ArrayList<Button>();
String[] stringsEditText = new String[(allEditText.size())];
String[] stringsTextView = new String[(allTextView.size())];
String[] stringsBtn = new String[(allButton.size())];
for(int i=0; i < allEditText.size(); i++){
stringsEditText[i] = allEditText.get(i).getText().toString();
}
for (int i=0; i < allTextView.size(); i++) {
stringsTextView[i] = allTextView.get(i).getText().toString();
size = allTextView.get(i).getText().toString().length();
}
for(int i=0; i < allButton.size(); i++){
stringsBtn[i] = allButton.get(i).getText().toString();
}
allTextView.add(tv);
allEditText.add(et);
allButton.add(btn);
allButton.get(0).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
inputEditText = allEditText.get(0).getText().toString();
stringsEt = new String[] {allEditText.get(0).getText().toString()};
if (inputEditText.length() > 0) {
allTextView.get(0).setText(inputEditText);
allEditText.add(allEditText.get(0));
allEditText.get(0).setText("");
}
else if (inputEditText.length() ==0){
Toast.makeText(TaskActivity.this, "You need to write down your task", Toast.LENGTH_LONG).show();
}
inputTextView = allTextView.get(0).getText().toString();
stringsTv = new String[] {allTextView.get(0).getText().toString()};
if (inputTextView.length() > 0) {
allTextView.get(0).getText();
allTextView.add(allTextView.get(0));
}
}
});
}
}
private Button createBtn() {
randomTaskBtn = new Button(this);
randomTaskBtn.setText("Task");
containerLayout.addView(randomTaskBtn);
randomTaskBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double luckyTask = Math.random();
luckyTask *=size;
int luckyIndex = (int)luckyTask;
displayTaskTv.setText(stringsTv[luckyIndex]);
}
});
return randomTaskBtn;
}
private TextView createTextView() {
displayTaskTv = new TextView(this);
displayTaskTv.setTextSize(20);
displayTaskTv.setTextColor(Color.parseColor("#dd2626"));
displayTaskTv.setText("");
containerLayout.addView(displayTaskTv);
return displayTaskTv;
}
}
Thank you for any constructive advices.
I am sure my code is big mess. I wanted to created more methods but I didn't succeed.
This is what you should do
Step 1 -
Create multiple EditTexts and store each one of them in an ArrayList say myEditTextList.
Step 2- Take data from all edit texts
String str = ""
for(EditText et: myEditTextList) {
str += et.getText().toString();
}
Step 3- Display data in str wherever you want.
I'm having a tremendous amount of difficulty dealing with static members while trying to make an Android app.
Here's what the app has to do:
There are two tabs, My Classes and Class List, each of which are separate intents. The point of the app is that when you click the "add" button next to a class in the Class List, it should dynamically update the list under My Classes. However, I can't do this because my data structure that holds the classes is dynamic, but the Class List intent is static, so I keep getting the "Cannot reference non-static member from static context" error.
Is there any way to use dynamic intents, and if so, how?
EDIT:
Here's where the intents are declared:
public class TabLayout extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
Intent intent = new Intent(this, MyClasses.class);
tabHost.addTab(tabHost.newTabSpec("My Classes")
.setIndicator("My Classes", res.getDrawable(R.drawable.ic_tab_main))
.setContent(intent));
Intent intent2 = new Intent(this, ClassList.class);
tabHost.addTab(tabHost
.newTabSpec("Class List")
.setIndicator("Class List", res.getDrawable(R.drawable.ic_tab_main))
.setContent(intent2));
Intent intent3 = new Intent(this, SyncList.class);
tabHost.addTab(tabHost
.newTabSpec("Sync List")
.setIndicator("Sync List", res.getDrawable(R.drawable.ic_tab_main))
.setContent(intent3));
tabHost.setCurrentTab(0);
// Set tabs Colors
tabHost.setBackgroundColor(Color.BLACK);
tabHost.getTabWidget().setBackgroundColor(Color.BLACK);
}
}
Here's the MyClassList file:
public class MyClassList extends MyClasses {
int numClasses;
MyClasses myclasses = new MyClasses();
final static TableRow tableRowArray[] = new TableRow[4];
TextView classNameArray[] = new TextView[4];
TextView teacherNameArray[] = new TextView[4];
Button dropButtonArray[] = new Button[4];
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create containers
ScrollView classListScrollView = new ScrollView(this);
TableLayout classListTableLayout = new TableLayout(this);
// Add class entries
for (int i = 0; i < 4; i++)
{
tableRowArray[i] = new TableRow(this);
tableRowArray[i].setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
classNameArray[i] = new TextView(this);
classNameArray[i].setText("");
classNameArray[i].setPadding(10, 20, 10, 20);
classNameArray[i].setTextSize(20);
tableRowArray[i].addView(classNameArray[i]);
teacherNameArray[i] = new TextView(this);
teacherNameArray[i].setText("");
teacherNameArray[i].setPadding(10, 20, 10, 20);
teacherNameArray[i].setTextSize(16);
tableRowArray[i].addView(teacherNameArray[i]);
dropButtonArray[i] = new Button(this);
dropButtonArray[i].setText("Drop");
tableRowArray[i].addView(dropButtonArray[i]);
classListTableLayout.addView(tableRowArray[i], new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tableRowArray[i].setVisibility(View.GONE);
}
classListScrollView.addView(classListTableLayout);
this.setContentView(classListScrollView);
dropButtonArray[0].setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
myclasses.dropClass(0);
}
});
dropButtonArray[1].setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
myclasses.dropClass(1);
}
});
dropButtonArray[2].setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
myclasses.dropClass(2);
}
});
dropButtonArray[3].setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
myclasses.dropClass(3);
}
});
}
public void updateMyClasses(ClassObject myclasses[])
{
for (int i = 0; i < 4; i++)
{
if (myclasses[i].isNull)
{
classNameArray[i].setText(myclasses[i].getClassName());
teacherNameArray[i].setText(myclasses[i].getTeacherName());
tableRowArray[i].setVisibility(View.VISIBLE);
}
else
{
tableRowArray[i].setVisibility(View.GONE);
}
}
}
public MyClasses getMyClasses()
{
return myclasses;
}
}
Here's the MyClasses file:
public class MyClasses extends Activity {
static int numClasses;
static ClassObject MyClassArray[];
public MyClasses()
{
MyClassArray = new ClassObject[4];
for (int i = 0; i < 4; i++)
{
MyClassArray[i] = new ClassObject();
}
numClasses = 0;
}
public int getNumClasses()
{
return numClasses;
}
public void dropClass(int c)
{
for (int i = c; i < 3; i++)
{
MyClassArray[i] = MyClassArray[i+1];
}
//MyClassList.updateMyClasses(MyClassArray);
}
public void addClass(ClassObject c)
{
if (numClasses >= 4)
return;
MyClassArray[numClasses] = c;
//MyClassList.updateMyClasses(MyClassArray);
}
public boolean hasClass(ClassObject c)
{
for (int i = 0; i < 4; i++)
{
if (MyClassArray[i].getClassName() == c.getClassName())
return true;
}
return false;
}
}
And here's the line that gives the error:
addButtonArray[0].setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
MyClasses.addClass(ClassListArray[0]);
}
});
This line is your problem:
MyClasses.addClass(ClassListArray[0]);
You are trying to call the instance method addClass without giving the reference to a MyClasses instance. How you fix this depends on where the code is. Is it embedded in the MyClasses class?