access to dynamic layout - java

I have created dynamic layouts, but I don't know how to access their information,search on the interent and I got to this code, but it does not enter the first if
Button add, send,delete;
EditText size;
LinearLayout list, list2,list3;
ArrayList<String> text = new ArrayList<>();
Context context;
#SuppressLint("MissingInflatedId")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add = findViewById(R.id.add);
size = findViewById(R.id.size);
send = findViewById(R.id.send);
list =findViewById(R.id.list);
list2 =findViewById(R.id.list2);
list3 =findViewById(R.id.list3);
delete = findViewById(R.id.delete);
context = this;
//Toast.makeText(context,""+et.getText().toString(),Toast.LENGTH_SHORT).show();
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.e("tag","4444444");
for (int i = 0; i<4-list.getChildCount(); i++){
Log.e("tag","555555");
if (list.getChildAt(i) instanceof LinearLayoutCompat) {
Log.d("tag","333333");
LinearLayoutCompat ll = (LinearLayoutCompat) list.getChildAt(i);
for (int j = 0; j < ll.getChildCount() ; j++) {
Log.d("tag","77777");
if(ll.getChildAt(j) instanceof EditText)
{
Log.d("tag","22222222");
EditText et = (EditText) ll.getChildAt(j);
if(et.getId() == R.id.size){
Log.d("tag","1111111");
Toast.makeText(context,""+et.getText().toString(),Toast.LENGTH_SHORT).show();
}
}
}
}
}
}
});
add.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
addView();
}
});
}
what I expected was that in the for it would print the values ​​that I enter in my textView through the toast

Related

How can I show the integer in listview?

I want to show the for loop numbers on the listview, but it doesn't work.
App's Layout
public class MainActivity extends AppCompatActivity {
private ListView marksixlist;
private Button mRandombtn, mCleanbtn;
private TextView mText;
private ArrayList<Integer> marksixnum = new ArrayList<Integer>();
private ArrayAdapter arrayAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
marksixlist = (ListView) findViewById(R.id.listview1);
mRandombtn = (Button) findViewById(R.id.randombtn);
mCleanbtn = (Button) findViewById(R.id.cleanbtn);
mText = (TextView) findViewById(R.id.items);
mRandombtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int j = 1; j <= 6; j++) {
int random = (int) (Math.random()* 49+1);
marksixnum.add(random);
Collections.shuffle(marksixnum);
}
}
});
arrayAdapter = new ArrayAdapter<Integer>(this, R.layout.listitempage, marksixnum);
marksixlist.setAdapter(arrayAdapter);
Log.d("aaa", "The markssix is - " + marksixnum);
}
}
Once you generate the random numbers and shuffle them then at the end of for loop add the arrayAdapter to the listview marksixlist again and it will work as a charm.
I have additionally cleared the ArrayList marksixnum before generating random numbers so that your list doesn't keep on growing.
Its optional, if you need last click elements then delete marksixnum.clear()
mRandombtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do this if you do not want
// numbers from last click
marksixnum.clear();
for (int j = 1; j <= 6; j++) {
int random = (int) (Math.random()* 49+1);
marksixnum.add(random);
Collections.shuffle(marksixnum);
}
// add arrayAdapter to listview
marksixlist.setAdapter(arrayAdapter);
}
});

Select editText view contents randomly from stored array

Basically what I am trying to accomplish is storing editText view Strings in an array and returning the value of a randomly selected array element. The editText views can be dynamically added and removed so I had to take that into account. I've gone over my method and cannot find what I'm getting wrong.
the problem is is getAnswer(). Currently this is crashing the app
Java:
public class MainActivity extends AppCompatActivity {
private LinearLayout mEditTextContainer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mEditTextContainer = (LinearLayout)findViewById(R.id.linearLayoutDecisions);
setContentView(activity_main);
//Button to choose random editText contents
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getAnswer();
}
});
//Button to add editText Field
final Button add_button = (Button) findViewById(R.id.add_button);
add_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Add_Line();
}
});
//Button to remove editText Field
final Button remove_button = (Button) findViewById(R.id.remove_button);
remove_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Remove_Line();
}
});
}
public void Add_Line() {
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayoutDecisions);
// add edittext
EditText et = new EditText(this);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setText(null);
et.setHint("Enter Answer #" + (mEditTextContainer.getChildCount()+1));
et.setGravity(Gravity.CENTER);
mEditTextContainer.addView(et);
}
public void Remove_Line() {
int count = mEditTextContainer.getChildCount();
if(count == 2)
return;
else
mEditTextContainer.removeViewAt(mEditTextContainer.getChildCount()-1);
}
public void getAnswer() {
//get number of possible answers
int count = mEditTextContainer.getChildCount();
//create array to hold answers
String[] options = new String[count--];
//create temporary view to store editText view contents
View tempView;
//Loop to collect and store editText contents
for(int i = 1; i < count-1; i++) {
tempView = mEditTextContainer.getChildAt(i);
if(tempView instanceof EditText){
String tempText = ((EditText) tempView).getText().toString();
if(tempText != null){
options[i] = tempText;
}
else
return;
}
}
int number = (int)(Math.random() * count-1);
String answer = options[number];
TextView answerBox = (TextView)findViewById(R.id.textView7);
answerBox.setText(answer);
}
}
For anyone curious, I found the answer. The variable 'count' should not have been decremented by 1 in my method. Changing this fixed the whole thing.

How to make 100 buttons in android studio without xml file?

I'm searching for a code that it doesn't need a long .xml and I can easily change number of buttons too 200 or everything.
public class buttons extends Activity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buttons);
}
}
in activity file add this:
// create buttons in a loop
for (int i = 0; i < 200; i++) {
Button button = new Button(this);
button.setText("Button " + i);
}
Something like that
public class Buttons extends Activity {
{
Button button;
List<Button> buttonList = new ArrayList<Button>();
LinearLayout.LayoutParams params;
LinearLayout list;
OnClickListener listener;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.long);
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
list = (LinearLayout) findViewById(R.id.list);
listener = new OnClickListener() {
#Override
public void onClick(View v) {
int id = v.getId();
Button b = buttonList.get(id);
///
}
};
for (int i = 0; i < 200; i++)
addButton();
}
public void addButton()
{
button = new CheckBox(this);
button.setLayoutParams(params);
button.setText("TEXT");
button.setId(buttonList.size());
button.setOnClickListener(listener);
buttonList.add(button);
list.addView(button);
}
}

Display ArrayList to TextView

I am trying to do something simple: display my ArrayList to a TextView. I have tried to use methods that would do this without success. Or am I supposed to use a ListView instead of a TextView?
Anyway here is the code. I hope someone can help.
public class MainActivity extends Activity {
Button aButton; // Global Scope
TextView text2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_layout);
aButton = (Button) this.findViewById(R.id.button1);
aButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ArrayList<String> list = new ArrayList<String>();
list.add("Books");
list.add("Newspapers");
list.add("Magazines");
for (int i = 0; i < list.size(); i++) {
//System.out.println(list.get(i));
Log.i("Results", list.get(i));
text2.setText(text2.getText());
}
}
});
}
You are not changing text of TexView:
text2.setText(text2.getText());
You rather thought about:
text2.setText(list.get(i));
eventually:
text2.setText((text2.getText() != null ? text2.getText() : "") + list.get(i));
try this :
aButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ArrayList<String> list = new ArrayList<String>();
list.add("Books");
list.add("Newspapers");
list.add("Magazines");
String listString = "";
for (String s : list) {
listString += s + " ";
}
text2.setText(listString);
}
});
Your prolem is here
text2.setText(text2.getText())
The working code will be:
public class MainActivity extends Activity {
Button aButton; // Global Scope
TextView text2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_layout);
aButton = (Button) this.findViewById(R.id.button1);
aButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ArrayList<String> list = new ArrayList<String>();
list.add("Books");
list.add("Newspapers");
list.add("Magazines");
for (int i = 0; i < list.size(); i++) {
//System.out.println(list.get(i));
Log.i("Results", list.get(i));
text2.setText(list.get(i));
}
}
});
}

Iterating multiple textviews with onClickListeners

I am setting up a textView Keyboard for a vocabulary game. I am looking to clean up my code and was wondering if I could iterate all 26 letters. As of now I declare them all
individually as shown in the code.
I would like to use a for loop and have experimented with no luck.
private void setupKeyBoard() {
A = (TextView) getView().findViewById(R.id.A);
A.setTextSize(20 * getResources().getDisplayMetrics().density);
A.setText("A");
A.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
checkGuess('A');
A.setClickable(false);
A.setAlpha((float) .1);
}
});
B = (TextView) getView().findViewById(R.id.B);
B.setTextSize(20 * getResources().getDisplayMetrics().density);
B.setText("B");
B.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
checkGuess('B');
B.setClickable(false);
B.setAlpha((float) .1);
}
});
C = (TextView) getView().findViewById(R.id.C);
C.setTextSize(20 * getResources().getDisplayMetrics().density);
C.setText("C");
C.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
checkGuess('C');
C.setClickable(false);
C.setAlpha((float) .1);
}
});
} //stop at Z
I would recommend this method :
for(char letter = 'A'; letter <= 'Z';letter ++){
int letterID = getResources().getIdentifier(""+letter, "id", getPackageName());
TextView txtview = (TextView) getView().findViewById(letterID);
txtview .setTextSize(20 * getResources().getDisplayMetrics().density);
txtview .setText(letter);
txtview .setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
checkGuess(letter);
txtview.setClickable(false);
txtview.setAlpha((float) .1);
}
});
}
A better to make the textviews is create your own textviews in a loop and store it in a hashmap with a single id so you can get each one with out a problem. But if you want to get all one maybe you can do this from a parent you can do this:
String[] lettersMap = {"A", "B", "C",....,"Z"};
HashMap<String, TextView> map = new HashMap<String, TextView>();
// ll is your linear layout or your relative (i.e. your parent of all the textviews)
int childcount = ll.getChildCount();
for (int i=0, letMap = 0; i < childcount; i++){
View v = ll.getChildAt(i);
if (v instanceof TextView) {
TextView tv = (TextView) v;
tv.setTextSize(20 * getResources().getDisplayMetrics().density);
tv.setText(lettersMap[letMap]);
tv.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
checkGuess(lettersMap[letMap]);
tv.setClickable(false);
tv.setAlpha((float) .1);
}
});
map.put(lettersMap[letMpa], tv);
}
}

Categories

Resources