can LinearLayout put two items on the same line? - android

I have the code as below, and I need 2 things:
I wish to put tv[i] on the same line as txt[i]
and I want keyboard to appear after touching tv[i].
I'm a complete beginner to Android. Thank you.
public void click2(View view) {
Button button3 = findViewById(R.id.button2);
button3.setText("hello");
// Button[] buttons = new Button[10](this);
/*
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_main);
Button txt1 = new Button(this);
// linearLayout.setBackgroundColor(Color.TRANSPARENT);
linearLayout.addView(txt1);
*/
Integer.toBinaryString(12);
// Put buttons into an array
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_main);
// GridLayout gridLayout=(GridLayout) findViewById(R.id.activity_main);
// Put buttons into an array
// Button[] txt = {new Button(this), new Button(this)};
Button[] txt = new Button[8];
TextView[] tv = new TextView[8];
// loop over all values of i between 0 to the end of the button array
for (int i = 0; i < txt.length; i = i + 1) {
// Access array elements by index so txt1 is txt[1], etc.
txt[i]=new Button(this);
txt[i].setText(Integer.toBinaryString(i));
linearLayout.addView(txt[i]);
tv[i]=new TextView(this);
linearLayout.addView(tv[i]);
}
};
MAYBE I'm putting your line
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
at a wrong place.
KOD MainActivity.java
public void click2(View view) {
Button button3 = findViewById(R.id.button2);
button3.setText("hello");
// Put buttons into an array
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_main);
// GridLayout gridLayout=(GridLayout) findViewById(R.id.activity_main);
// Put buttons into an array
// Button[] txt = {new Button(this), new Button(this)};
Button[] txt = new Button[8];
TextView[] tv = new TextView[8];
// loop over all values of i between 0 to the end of the button array
for (int i = 0; i < txt.length; i = i + 1) {
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
// Access array elements by index so txt1 is txt[1], etc.
txt[i]=new Button(this);
txt[i].setText(Integer.toBinaryString(i));
linearLayout.addView(txt[i]);
tv[i]=new TextView(this);
linearLayout.addView(tv[i]);
}
};

Call
linearLayout.setOrientation(LinearLayout.VERTICAL);
to make 2 items appear side by side.
For showing keyboard, take a look here:
How do you close/hide the Android soft keyboard programmatically?

Looks like you are referencing your xml file here:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_main);
You should go to your xml file, add a id (android:id="#+id/some_id") tag to your <LinearLayout> and use this id in your LinearLayout linearLayout =(LinearLayout) findViewById(R.id.HERE)
Other option is to go to your xml file and add sandroid:orientation="horizontal" like this:
<LinearLayout
android:id="#+id/some_id"
android:orientation="vertical"
...>

Related

Is there a way to change background color behind a button (button.setBackgroundResource)

I'm a new Android dev student, and I'm trying to create a dynamic layout witch contains a TextView and a button inside the same row.
but I have a little problem.
I set my Button in my Drawables ressources by
button.setBackgroundResource(R.drawable.ic_comment_black_48px);
and now, I cannot change the backgroundcolor behind it.
I have created a newLinearlayout inside my main LinearLayout and have created a new textView and a new Button.
I have put them inside the LinearLayout's child and put it inside the main.
That's work but not the background color behind my button.
is there a way to do that?
my xml layout
<?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="match_parent"
android:orientation="vertical"
android:background="#color/grey"
android:id="#+id/historyLayout">
</LinearLayout>
my complete activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
mLinearLayout = findViewById(R.id.historyLayout);
mMoodSaved = new ArrayList(7); // Define the max size of my ArrayList
loadData();
for (int i = 1; i <= 7; i++) {
final TextView textView = new TextView(this);
mLinearLyt = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
mLinearLyt.setOrientation(LinearLayout.HORIZONTAL);
textView.setHeight(300);
textView.setWidth(400);
textView.setBackgroundColor(Color.RED);
textView.setTextColor(Color.BLACK);
textView.setTextSize(12);
textView.setText(String.valueOf(i));
mLinearLyt.setBackgroundColor(Color.YELLOW);
mLinearLyt.addView(textView);
mLinearLyt.setLayoutParams(params);
ImageButton button = new ImageButton(this);
LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
param2.setMargins(20,50,0,0);
param2.height = 100;
param2.width = 100;
button.setLayoutParams(param2);
button.setBackgroundResource(R.drawable.ic_comment_black_48px);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
soundConfirm();
Toast.makeText(getApplicationContext(), textView.getText(), Toast.LENGTH_LONG).show(); //Display Toast Message
}
});
mLinearLyt.addView(button);
mLinearLayout.addView(mLinearLyt);
}
}
Since this an ImageButton, set
button.setImageResource(R.drawable.ic_comment_black_48px)
instead of setBackgroundResource.

Find child of a LinearLayout inside a LinearLayout

I have a LinearLayout ("ll") that is already created in xml and the app dynamically creates another LinearLayout inside of it and creates an EditText and a Button inside of that view. The button makes the whole LinearLayout destroy itself along with the EditText and Button inside it (the whole system is a player name entering activity). Anyway, I am trying to find a way to get the text from all of the EditTexts. I have tried using a for loop on "ll" and using ll.getChildAt() but I can't seem to use .getChildAt() on whatever ll.getChildAt() generates because getChildAt() generates a "View" not a "LinearLayout." I basically just need a way to search two children in, rather than just one. Also, if there is just a better way I should be doing this, let me know. I'm open to suggestions.
Here's my code if it will help:
NewGameCreate.java
public class NewGameCreate extends Activity {
int numOfPlayers = 0;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_game_create);
}
public void newPlayer(View view) {
numOfPlayers++;
final LinearLayout ll = findViewById(R.id.playerView);
final LinearLayout llNew = new LinearLayout(getApplicationContext());
llNew.setOrientation(LinearLayout.HORIZONTAL);
llNew.setId(numOfPlayers);
ll.addView(llNew);
EditText newName = new EditText(this);
newName.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
newName.setHint("Enter Player Name");
newName.setId(numOfPlayers);
newName.setWidth(0);
llNew.addView(newName);
final Button delete = new Button(this);
delete.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0));
delete.setText("Delete");
delete.setId(numOfPlayers);
delete.setWidth(0);
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int id = delete.getId();
ll.removeViewInLayout(findViewById(id));
Drawable back = ll.getBackground();
ll.setBackgroundColor(00000000);
ll.setBackground(back);
ll.invalidate();
}
});
llNew.addView(delete);
}
public void startGame(View view){
LinearLayout ll = findViewById(R.id.playerView);
List text = new ArrayList();
for(int loop = 0; loop < ll.getChildCount(); loop++) {
//this is the code in question and where I want to get the text from
//all my EditTexts
LinearLayout inner = ll.getChildAt(loop);
}
}
}
I think I found the answer to it. You need to change a little bit of code in the startGame() method I m providing the code for startGame below.
public void startGame(View view) {
LinearLayout ll = findViewById(R.id.playerView);
List text = new ArrayList();
for (int loop = 0; loop < ll.getChildCount(); loop++) {
//this is the code in question and where I want to get the text from
//all my EditTexts
LinearLayout inner = (LinearLayout) ll.getChildAt(loop);
for (int j = 0; j < inner.getChildCount(); j++) {
if (inner.getChildAt(j) instanceof EditText) {
EditText textET = (EditText) inner.getChildAt(j);
Log.d("TAG",textET.getText().toString());
}
}
}
}
In the above code you were able to get the first child only but as you have added a linearLayout with orientation Horizontal in a parent LinearLayout with orientation Vertical, you have written code for the child of parent layout i.e playerView. I have modified the code to get the elements of the child Linear layout and Log prints all the text from the EditText.
Hope that helps!!

Dynamic buttons in table layout adding Horizontally

I'm trying to add buttons in Table layout.
Initially I will have a single row with a single button.
When the user clicks that button, It should create 2 dynamic buttons in the next row 'Horizontally'.
But with my code, I'm getting vertically dynamic buttons Instead of horizontally.
while(id <rowcount+2)
{
tr = new TableRow(MainActivity.this);
table.addView(tr);
Button btn1 = new Button(MainActivity.this);
btn1.setText("");
btn1.setOnClickListener(this);
btn1.setId(id);
tr.addView(btn1);
id++;
}
you are creating new object of TableRow each time, TableRow represents elements in Horizontal and TableLayout represent data in Vertical that's why you getting Buttons in Vertical manner
tr = new TableRow(MainActivity.this);
while(id <rowcount+2)
{
Button btn1 = new Button(MainActivity.this);
btn1.setText("");
btn1.setOnClickListener(this);
btn1.setId(id);
tr.addView(btn1);
id++;
}
table.addView(tr);
you are creating a new row each time for the loop
try this.
tr = new TableRow(MainActivity.this);
while(id <rowcount+2) {
Button btn1 = new Button(MainActivity.this);
btn1.setText("");
btn1.setOnClickListener(this);
btn1.setId(id);
tr.addView(btn1);
id++;
}
table.addView(tr);

Android layout for Loop Text

I would like to be able to for loop in my layout and add text to the textviews dynamically, this does not error out but I get the lost row in the display for example
Tw04 One4
I would like to be able to display
One1 Two1
One2 Two2
etc...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rel_layout);
for (int i = 0; i < 5; i++) {
TextView woTxt = (TextView) findViewById(R.id.ticker_price);
woTxt.setText("One"+i);
TextView cusTxt = (TextView) findViewById(R.id.ticker_symbol);
cusTxt.setText("Two"+i);
}
}
You may add TextViews programmatically to your layout as below :
TextView [] txt1 =new TextView[5];
for(int i=0;i<5;i++)
{
txt1[i]=new TextView(YourActivity.this);
txt1[i].setText("One"+i);
txt1[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
linear.addView(txt1[i]);
}
where linear is a LinearLayout from your layout.
You have only two text views, where in fact you need 10 on your example, one text view for each item you want to display. I suggest that you make a ListView instead, where each line of the list will be a couple of text views.

Android Dynamic Button - Same arithmatic operator displayed again and again and also not formatted - Its worked

I am new to android. I want to display the 5 numbers in the Button. And also needs to display the arithmetic buttons also like below
14 12 13 12 11 ( These number to be displayed in the button )
+ - * / ( These arithmetic button also displayed in button)
I want to display like above dynamically. And i am using this code, but it displaying like below.( not displaying +, - *, / )
/ 14
/ 12
/ 13
/ 12
/ 11
I want to display like mentioned initially and also i want listen the button clicked
Its displaying Normal buttons, how i can display as like Graphic buttons. ( so that it will give more attraction)
Please find the code below.
setContentView(R.layout.dynamically_create_view_element);
final LinearLayout lm = (LinearLayout) findViewById(R.id.linearMain);
// create the layout params that will be used to define how your
// button will be displayed
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for(int j=0;j<5;j++)
{
// Create LinearLayout
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
// Create TextView
final Button btn1 = new Button(this);
// Give button an ID
btn1.setId(1);
btn1.setText("+");
btn1.setId(2);
btn1.setText("-");
btn1.setId(3);
btn1.setText("*");
btn1.setId(4);
btn1.setText("/");
// Create Button
final Button btn = new Button(this);
// Give button an ID
btn.setId(j+1);
int random1 = (int )(Math.random() * 20 + 1);
btn.setText(""+numbers[j]+"");
// set the layoutParams on the button
btn.setLayoutParams(params);
final int index = j;
// Set click listener for button
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.i("TAG", "index :" + index);
Toast.makeText(getApplicationContext(),
"Clicked Button Index :" + index,
Toast.LENGTH_LONG).show();
}
});
//Create four
//Add button to LinearLayout
ll.addView(btn1);
ll.addView(btn);
//Add button to LinearLayout defined in XML
lm.addView(ll);
}
Ok there are multiple issues with your code:
First, you're getting the below result
/ 14
/ 12
/ 13
/ 12
/ 11
because inside your for-loop you're setting the text of btn1 to / after setting the text of the Button to + then to - and then to *.
Also because you're creating a LinearLayout for each Button you're getting the numbers below each other.
I've taken the liberty to re-write the code a bit into something a bit more generic:
public class MainActivity extends Activity {
private LinearLayout lm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lm = (LinearLayout) findViewById(R.id.lm);
// Create a LinearLayout for each row inside the outer LinearLayout.
LinearLayout firstRow = new LinearLayout(this);
firstRow.setOrientation(LinearLayout.HORIZONTAL);
// Create a String array of items you want to add to the inner LinearLayout.
String[] itemsToAddToFirstRow = { "14", "12", "13", "12", "11" };
createButtonsDynamically(firstRow, itemsToAddToFirstRow);
String[] itemsToAddToSecondRow = { "+", "-", "*", "/" };
LinearLayout secondRow = new LinearLayout(this);
secondRow.setOrientation(LinearLayout.HORIZONTAL);
createButtonsDynamically(secondRow, itemsToAddToSecondRow);
}
private void createButtonsDynamically(LinearLayout layoutToAddButtonsTo, String[] itemsToAdd) {
for (int i = 0; i < itemsToAdd.length; i++) {
final Button buttonToAdd = new Button(this);
buttonToAdd.setText(itemsToAdd[i]);
buttonToAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "I was clicked: " + buttonToAdd.getText(), Toast.LENGTH_SHORT).show();
}
});
layoutToAddButtonsTo.addView(buttonToAdd);
}
// In the end add all buttons inside the inner LinearLayout to the outer LinearLayout.
lm.addView(layoutToAddButtonsTo);
}
}
Add your own code to add the ids to the Buttons.
The end-result will look like this:
As far as your #2. goes:
You could a background image to your Buttons by calling setBackground(Drawable drawable) for the buttons or you completely re-style the standard Button.
Have a go at this guide from the official guidelines.
Hope this helps :-)

Categories

Resources