how to set scroll view in android - android

RelativeLayout blur = (RelativeLayout) findViewById(R.id.relative5);
TextView name_tv = new TextView (this);
name_tv.setText("\n\n First Name");
blur.addView(name_tv);
Typeface font=Typeface.SERIF;
name_tv.setTypeface(font);
final EditText nameedit = new EditText(this);
nameedit.setHint("First name");
blur.addView(nameedit);
Typeface font33=Typeface.SERIF;
nameedit.setTypeface(font33);
nameedit.setMaxLines(1);
nameedit.setImeOptions(EditorInfo.IME_ACTION_NEXT);
nameedit.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
Similarly i add few text view and Edit text dynamically in this class.The problem is when i add scrollview in xml it show me error.....

Related

TextView auto create new line when end of the line and edittex not align with textview

I have 4 TextView: " what is your name?", "in some circumstances" ,you might in some circumstances,you might", "________" and " to help user understand why your app needs a permission.For example " and a Edittext.
Now I want create a layout and when I user layout add view in my class ( not file.xml ) it can show same :
I used custom layout same here : Custom RowLayout and add view in code :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main22);
int permissionCheck =
rowLayout = (RowLayout) findViewById(R.id.row);
TextView tv1 = new TextView(this);
tv1.setText("what is your name?");
rowLayout.addView(tv1);
EditText ed1 = new EditText(this);
ed1.setWidth(50);
rowLayout.addView(ed1);
TextView tv2 = new TextView(this);
tv2.setText(" in some circumstances ,you might in some circumstances,you might");
rowLayout.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setText("_____");
rowLayout.addView(tv3);
TextView tv4 = new TextView(this);
tv4.setText("to help user understand why your app needs a permission.For example");
rowLayout.addView(tv4);
<convert.htv.ex.convertdata.RowLayout
android:id="#+id/row"
android:layout_width="match_parent"
android:layout_height="wrap_content"></convert.htv.ex.convertdata.RowLayout>
and result
You can see two problems in here:
1. Edittext not in line with text view.
2. when string text length it create a new line. I want a part off textview will show in old line and when end of the line it will show the rest of textview in newline ( sample picture 1).
How I can do it? please help me.
1)Answer for first problem,
add this line of code
tv1.setGravity(Gravity.CENTER)
2) Change this
ed1.setWidth(50);
to
ed1.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);

How to edit view created programmatically

So in my application i have a linear layout, to which i'm adding programmatically some CardViews (android L cardview) like this :
//This is my LinearLayout
LinearLayout myLayout = (LinearLayout) findViewById(R.id.accounts_layout);
//Here i create my CardView from a prepared xml layout and inflate it to the LinearLayout
View card = View.inflate(getApplicationContext(), R.layout.account_card, myLayout);
//Now i change the 'text' value of the Card's text views
TextView cardTitle = (TextView) card.findViewById(R.id.text_card_title);
cardTitle.setText("Title1");
TextView cardDecription = (TextView) card.findViewById(R.id.text_card_description);
cardDecription.setText("Description1");
//...
//Now i do the same thing for another card
View card2 = View.inflate(getApplicationContext(), R.layout.account_card, myLayout);
TextView cardTitle2 = (TextView) card2.findViewById(R.id.text_card_title);
cardTitle2.setText("Title2");
TextView cardDecription2 = (TextView) card2.findViewById(R.id.text_card_description);
cardDecription2.setText("Description2");
//...
The two cards are displayed properly, but what happens is than the first card displayed has "Title2" and "Description2" written in the textViews, while the second card has the default values defined in the xml.
It seems to me that by calling card.findViewById() or card2.findViewById() i get always the TextView of the first card.
So my question is : how do i manage to differentiate the cards i create programmatically and then correclty access the view within them?
Try this way,hope this will help you to solve your problem.
LinearLayout myLayout = (LinearLayout) findViewById(R.id.accounts_layout);
for (int i=1;i<=2;i++){
View card = View.inflate(getApplicationContext(), R.layout.account_card, null);
TextView cardTitle = (TextView) card.findViewById(R.id.text_card_title);
cardTitle.setText("Title"+i);
TextView cardDecription = (TextView) card.findViewById(R.id.text_card_description);
cardDecription.setText("Description"+i);
card.setTag(i);
card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = (Integer) v.getTag();
Toast.makeText(context,pos,Toast.LENGTH_SHORT).show();
}
});
myLayout.addView(card);
}

Add new EditText component to an Activity at run time

How can i add a new EditText component to an Activity layout? I don't want to add the component from the GUI editor statically, but add it to the activity at run time, for example when the user clicks a button?
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ur_layout);
EditText editTextView = new EditText(this);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 1);
editTextView.setLayoutParams(params);
linearLayout.addView(editTextView);
Have the EditText in the layout XML for your activity with android:visibility="gone".
When you want to show the EditText do:
EditText ed = (EditText) findViewById(R.id.your_edit_text);
ed.setVisibility(View.VISIBLE);

Add TextView dynamically and getTag()

I have one class calls "SplitText" and extends AsyncTask.
There i added a dynamic textview:
TextView masterTV = new TextView(mActivity);
tv.setTag("masterTV");
...
masterTV.setText(Activity_Start.chapterContentList.get(0));
LinearLayout sv = (LinearLayout) mActivity.findViewById(R.id.linearlayout);
sv.removeAllViews();
sv.addView(masterTV);
This works fine.
Now i want to get the text from this TextView from an other class calls Activity_Start which extends Activity..
on there onOptionsItemSelected() i have:
case R.id.menu_previous:
LinearLayout ll = (LinearLayout) findViewById(R.id.linearlayout);
TextView tv = (TextView) ll.findViewWithTag("masterTV");
Log.e("previs", tv.getText().toString());
return true;
But this returns always a NullPointerException..
What i do wrong?
UPDATE:
I have found an other way to get my TextView:
LinearLayout ll = (LinearLayout) findViewById(R.id.linearlayout);
TextView tv = (TextView) ll.getChildAt(0);
This isn't the exactly way i want but i runs and do what i want.
(Provided your Textview is child no 0)

how set value to a dynamically created edittext

in my app i create a set of editText dynamically. how can we set values to a dynamically created editText. i create the editText and add this editText to list
the code for creating the editText is given below
LinearLayout layout = new LinearLayout(context);
layout.setId(expRow2);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setGravity(Gravity.CENTER);
for(int i=1;i<=4;i++)
{
EditText editText = new EditText(context);
editText.setHint(exp_EditTextName[i]);
editText.setId(expRow+i);
editText.setEms(10);
editText.setLayoutParams(margins);
layout.addView(editText);
expEditTextList.add(editText);
}
and i need to setValues to editText on other part of my code
final LinearLayout exp = new LinearLayout(context);
exp.setOrientation(LinearLayout.VERTICAL);
exp.setBackgroundColor(Color.GRAY);
exp.setDescendantFocusability(LinearLayout.FOCUS_AFTER_DESCENDANTS);
Button expAdd = new Button(context);
expAdd.setId(123);
expAdd.setBackgroundResource(R.drawable.small_add);
expAdd.setLayoutParams(marginLeftElement);
exp.addView(expAdd);
exp.addView(layout);
EditText edittext = (EditText)expEditTextList.get(0);
edittext.setText("hai");
but i cant set values to the editText.
try like this..
final EditText text1 = (EditText) findViewById(R.id.text1);
final EditText text2 = (EditText) findViewById(R.id.text2);
final EditText text3 = (EditText) findViewById(R.id.text3);
text2.addTextChangedListener(new TextWatcher() {
void afterTextChanged(Editable s) {
text3.setText(text1.getText().toString() + text2.getText().toString());
};
});
for more information try the link
set text on dynamically created edittext in android?

Categories

Resources