This is my code to add a new linear layout:
public void onClick_addContact(View v)
{
LinearLayout layout = (LinearLayout) findViewById(R.id.layoutLinear);
layout.addView(linearlayout(_intMyLineCount));
_intMyLineCount++;
}
private EditText editText(int _intID) {
EditText editText = new EditText(this);
editText.setId(_intID);
editText.setHint("My lines");
editText.setWidth(180);
editTextList.add(editText);
return editText;
}
private TextView textView(int _intID)
{
TextView txtviewAll=new TextView(this);
txtviewAll.setId(_intID);
txtviewAll.setText("My lines:");
textviewList.add(txtviewAll);
return txtviewAll;
}
private RadioButton button(int _intID)
{
RadioButton btn = new RadioButton(this);
btn.setId(_intID);
btn.setOnClickListener(newContact);
return btn;
}
OnClickListener newContact = new OnClickListener() {
//onClick view
public void onClick(View v) {
LinearLayout layout = (LinearLayout) findViewById(R.id.layoutLinear);
layout.addView(linearlayout(_intMyLineCount));
_intMyLineCount++;
}
};
private LinearLayout linearlayout(int _intID)
{
LinearLayout LLMain=new LinearLayout(this);
LLMain.setId(_intID);
LLMain.addView(textView(_intID));
LLMain.addView(editText(_intID));
LLMain.addView(button(_intID));
LLMain.setOrientation(LinearLayout.HORIZONTAL);
linearlayoutList.add(LLMain);
return LLMain;
}
}
As of now, if any radio button is clicked then a new linear layout gets added. How do I change this, if any radio button is clicked, then the corresponding linear layout gets removed?
Use the setVisibility method. If you know the ID of the layout, you can do do a findViewById and then use myLayout.setVisibility(View.GONE).
LinearLayout layout = (LinearLayout) findViewById(R.id.layoutLinear);
layout.removeView(findViewById(removeId));
Should be helpful..
You set a tag at the new LinearLayout for exmeple "the layout corresponding to the first radio button" and you can retrieve this layout for delete it with findViewByTag.
Related
I want to add spinner and edittext in my android activity on button click which looks like same as originally added in xml file. The spinner and edittext fields are adding properly upon button click on there right place.
Controls added code in MainActivity.java
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
productsIds++;
LinearLayout lv = findViewById(R.id.lnear_layout);
lv.addView(createLinearLayout());
}
public Spinner createProductSpinner() {
final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(Math.round(getResources().getDimension(R.dimen.product_width)), Math.round(getResources().getDimension(R.dimen.control_height)));
final Spinner spinner = new Spinner(MainActivity.this);
spinner.setLayoutParams(lparams);
spinner.setAdapter(productAdapter);
return spinner;
}
public EditText createQuantityEditText() {
final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(Math.round(getResources().getDimension(R.dimen.quantity_width)), Math.round(getResources().getDimension(R.dimen.control_height)));
final EditText editText = new EditText(MainActivity.this);
editText.setLayoutParams(lparams);
editText.setMovementMethod(new ScrollingMovementMethod());
editText.setHint("Quantity");
return editText;
}
public Spinner createUnitSpinner() {
final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(Math.round(getResources().getDimension(R.dimen.unit_width)), Math.round(getResources().getDimension(R.dimen.control_height)));
final Spinner spinner = new Spinner(MainActivity.this);
spinner.setLayoutParams(lparams);
spinner.setAdapter(productAdapter);
return spinner;
}
public LinearLayout createLinearLayout() {
final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
final LinearLayout subLayout = new LinearLayout(MainActivity.this);
subLayout.setOrientation(LinearLayout.HORIZONTAL);
subLayout.setLayoutParams(lparams);
subLayout.addView(createProductSpinner());
subLayout.addView(createQuantityEditText());
subLayout.addView(createUnitSpinner());
return subLayout;
}
});
I have about 50 different textviews in a linear layout in a scrollview
its like this
1.This is the first textview
2.This is the second textview
3.This is the third textview
4.This is the fourth textview
5.This is the fifth textview
6.This is the sixth textview
7.This is the seventh textview
8.This is the eighth textview
9.This is the ninth textview
10.This is the tenth textview
I have an edittext and button for search ,what i want is when i enter a number say 6 in the edittext and click the button then it should show or go to 6.This is the sixth textview,also i dont want other textviews to disappear,i just want the camera to move to textview number 6.
Please Help!!
public static void scrollToView(final ScrollView scrollView, final View view) {
view.requestFocus();
final Rect scrollBounds = new Rect();
scrollView.getHitRect(scrollBounds);
if (!view.getLocalVisibleRect(scrollBounds)) {
new Handler().post(new Runnable() {
#Override
public void run() {
scrollView.smoothScrollTo(0, view.getBottom());
}
});
}
}
Button clickButton = (Button) findViewById(R.id.yout_button_id);
clickButton.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
String n = ((EditText) findViewById(R.id.your_edittext_id)).getText();
int tn = Integer.parseInt(n); //TODO: check the view exists
TextView tv = (TextView)((LinearLayout)findViewById(R.id.your_linearlayout_id)).getChildAt(tn);
scrollToView((ScrollView)findViewById(R.id.yout_scrollview_id, tv);
}
});
When a user enters a word, it creates Buttons - one Button per letter of the word:
Illustration:
If the user enters "so" it creates 2 Buttons - 's', 'o'
If the user enters "make" it creates 4 Buttons - 'm', 'a', 'k', 'e'
I was having a hard time deciding how I should design this. Ultimately I decided to do the following: Each word is added to a vertical LinearLayout. And for each word, each letter is added to a horizontal LinearLayout. So it's a LinearLayout within a LinearLayout approach.
Here's the code I created which works:
//creates words dynamically
public void makeNewWord(LinearLayout ll, View v, EditText e){
//the horizontal linear layout
LinearLayout linearLayout2 = new LinearLayout(v.getContext());
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
//the parameters for the horizontal linear layout
LinearLayout.LayoutParams rlp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
//e is the user input
int size = e.getText().toString().length();
for (int i=0; i<size; i++){
final Button dynamicButtons = new Button(v.getContext());
dynamicButtons.setLayoutParams(rlp);
//add the buttons to the horizontal linear layout
linearLayout2.addView(dynamicButtons, rlp);
}
// ll is the vertical linear layout which I created in xml
// so for each entered word, I am adding horizontal linear layouts to my vertical layout
ll.addView(linearLayout2, 0);
}
But now I realized it's probably more efficient using a ListView, especially since I want to make the list of words to be expandable and collapsible. But Is it possible to create the above illustration using a ListView? How would I go about doing so?
I tried creating an ArrayAdapter as follows: ArrayAdapter<LinearLayout> adapter = new ArrayAdapter<LinearLayout>(this, R.id.listview). So basically it would be a ListView of horizontal LinearLayouts. Or should I make an ArrayAdapter of Buttons instead? What is the correct approach?
I can give some idea you can transform this idea in code
1. onTextChanged() method try to get length of text.
2. If you able to get text length then by subString() method get last entered text
3. Then recreate new button instance
You can use a TableLayout for this.
test.xml
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TableLayout
android:id="#+id/table_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TableLayout>
</ScrollView>
Activity Code
private TableLayout tableLayout;
private HashMap<String, TableRow> tableRows;
#Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.test);
super.onCreate(savedInstanceState);
tableLayout = (TableLayout) findViewById(R.id.table_layout);
tableRows = new HashMap<String, TableRow>();
}
public void addWord(String word) {
if (!tableRows.containsKey(word)) {
TableRow tableRow = new TableRow(this);
for (int i = 0; i < word.length(); i++) {
String letter = String.valueOf(word.charAt(i));
Button btnLetter = new Button(this);
btnLetter.setText(letter);
tableRow.addView(btnLetter);
}
tableRows.put(word, tableRow);
tableLayout.addView(tableRow);
}
}
public void removeWord(String word) {
TableRow tableRow = tableRows.remove(word);
if (tableRow != null) {
tableLayout.removeView(tableRow);
}
}
public void showWord(String word) {
TableRow tableRow = tableRows.get(word);
if (tableRow != null) {
tableRow.setVisibility(View.VISIBLE);
}
}
public void hideWord(String word) {
TableRow tableRow = tableRows.get(word);
if (tableRow != null) {
tableRow.setVisibility(View.GONE);
}
}
Assuming you want a specific button setup, you can inflate an xml button layout dynamically. See here for details.
I would just use a horizontal list view per word.
I you want to be fancy you create a custom layout manager and the new RecyclerView.
Each character of your word would be then a item in your list view. The layout then could be simply a button.
class ListAdapter extends BaseAdapter {
private final Context fContext;
private String mWord;
public ListAdapter(Context context) {
fContext = context;
}
public void updateWord(String word) {
mWord = word;
notifyDataSetChanged();
}
#Override
public int getCount() {
return mWord == null ? 0 : mWord.length();
}
#Override
public String getItem(int position) {
return String.valueOf(mWord.charAt(position));
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Button button;
if (convertView == null) {
button = new Button(parent.getContext());
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
} else {
button = (Button) convertView;
}
button.setText(getItem(position));
return button;
}
}
On every text change you can then just update the list.
adapter.updateWord();
Be aware the code is just out of my head and i haven't tested this, but should be enough to give you and idea.
I have a custom view that holds, among other views, a RadioButton.
The SingleRadioItem:
public class SingleRadioItem extends LinearLayout {
private TextView mTextKey;
private RadioButton mRadioButton;
private ImageView mImageSeparator;
public SingleRadioItem(Context context, AttributeSet attrs) {
super(context, attrs);
View view = LayoutInflater.from(context).inflate(R.layout.rtl_single_radio_item, this, true);
mTextKey = (TextView)view.findViewById(R.id.single_radio_item_text_key);
mRadioButton = (RadioButton)view.findViewById(R.id.single_radio_item_button);
mImageSeparator = (ImageView)view.findViewById(R.id.single_image_separator);
}
public void setKey(String key) {
mTextKey.setText(key);
}
public boolean getSelectedState() {
return mRadioButton.isSelected();
}
public void setSelectedState(boolean selected) {
mRadioButton.setSelected(selected);
}
}
I want to create instances of this view, add them to a RadioGroup and add the RadioGroup to a LinearLayout.
When I do so, it allows me to set all the radio buttons as selected, which means, the RadioGroup isn't functioning well (probably because how I do it..)
RadioGroup radioGroup = new RadioGroup(this);
radioGroup.setOrientation(RadioGroup.VERTICAL);
SingleRadioItem radio1 = new SingleRadioItem(this, null);
SingleRadioItem radio2 = new SingleRadioItem(this, null);
radioGroup.addView(radio1);
radioGroup.addView(radio2);
updateDetailsView.addView(radioGroup);
Obviously, when I add RadioButton radio1 = new RadioButton(this); the RadioGroup works well.
Is it even possible to add a view that holds a radio button to a radiogroup and I'm just missing something or not possible at all?
Thanks!
SOLUTION:
To extend #cosmincalistru Answer and help others:
for each SingleRadioItem I added to the LinearLayout I attached a listener like this:
radio1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (lastRadioChecked != null) {
lastRadioChecked.setCheckedState(false);
}
lastRadioChecked = (SingleRadioItem)v;
lastRadioChecked.setCheckedState(true);
}
});
You also need to set the RadioButton View inside the SingleRadioItem XML to clickable:false.
The RadioButton has to be directly subordinated to the RadioGroup, otherwise your buttons will be considered as from different groups.
The best idea is to use listeners on each RadioButton in your case.
EDIT:
Whenever i want to make a group of RadioButtons as part from a group but can't use the RadioGroup i do something like this :
RadioButton r1,r2,....;
// Instantiate all your buttons;
...
// Set listener on each
for(each RadioButton) {
rx.setOnCheckedChangeListener(OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
//set all buttons to false;
for(each RadioButton) {
rx.setChecked(false);
}
//set new selected button to true;
buttonView.setChecked(true);
}
}
});
}
When you add a view to RadioGroup, only if the view is a instanceof RadioButton only then will the group work correctly. In your case you are adding a LinearLayout. So SingleRadioItem should extend RadioButton.
I am adding programatically and dynamically some elements (buttons and text views) with android. I also need to set the setOnClickListener event for each of these buttons and from that event execute some action on the click of button:
do
{
EditText txt1 = new EditText(this);
EditText txt2 = new EditText(this);
Button showtxt = new Button(this);
linearLayout.addView(showtxt );
linearLayout.addView(txt1);
linearLayout.addView(txt2);
showtxt.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String aaa= txt1 .getText().toString();//HOW TO ACCESS txt1 and txt2 from here
String bbb= txt2 .getText().toString();
}
}
}
while(somecondition)
I am almost new to android. How can I access to txt1 and txt2 in the click callback function?
You need to make the define the variables where they will have class wide scope:
public class Example extends Activity {
EditText txt1;
EditText txt2;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
txt1 = new EditText(this);
txt2 = new EditText(this);
...
Now your onClick function will be able to see txt1 and txt2.
Alternatively
Since you appear to be creating a lot of txt1 and txt2 in one LinearLayout, you can pass your Button a reference to its EditTexts:
do {
...
// EditText[] array = { txt1, txt2 };
// is the short version of
EditText[] array = new EditText[2];
array[0] = txt1;
array[1] = txt2;
showtxt.setTag(array);
showtxt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText[] array = (EditText[]) v.getTag();
String aaa = array[0].getText().toString();
String bbb = array[1].getText().toString();
Log.v("Example", aaa + " " + bbb);
}
});
} while(some condition)
This may not be not ideal, however without any further context I cannot guess your ultimate goal. Hope that helps!
Last Suggestion
If we call the Button and two EditTexts a row, you could store each row in a ViewGroup or View of its own. Say you wanted to have background colors for each row:
View row = new View(this); // or this could be another LinearLayout
row.setBackgroundColor(0x0000ff);
// Create and add the Button and EditTexts to row, as in row.addView(showtxt), etc
...
linearLayout.addView(row);
showtxt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
View row = v.getParent()
String aaa = ((EditText) row.getChildAt(1)).getText().toString();
String bbb = ((EditText) row.getChildAt(2)).getText().toString();
Log.v("Example", aaa + " " + bbb);
}
});