Add TextView dynamically and getTag() - android

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)

Related

Differ between elements when XML reused by including?

I got a LinearLayout where I add the Layout of a seperate XML.
In that seperate XML I added a OnClickListener on that Layout.
Now when the Layout is clicked, a new View is added where the User has to type some information.
When ready the additional View is closed and the data has to be put in the right element.
But it does always put it in the first of them.
How can I tell the Listener in which one it has to put it?
Thanks in advance.
editElement is called if one of those multiple included elements is clicked.
The saveEdit when the finish button of the editor is clicked.
public void editElement(View view) {
FrameLayout base = (FrameLayout) findViewById(R.id.base);
LayoutInflater inflater = (LayoutInflater)this.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
LinearLayout editor = (LinearLayout) inflater.inflate(R.layout.element_editor, null);
editorID = editor.getId();
base.addView(editor);
currEdit = view.getId();
TextView day = (TextView) view.findViewById(R.id.day);
TextView day_editor = (TextView) editor.findViewById(R.id.day_editor);
day_editor.setText(day.getText());
}
public void saveEdit(View view) {
FrameLayout base = (FrameLayout) findViewById(R.id.base);
GridLayout parent = (GridLayout) view.getParent();
LinearLayout element = (LinearLayout) findViewById(currEdit);
TextView dur_act = (TextView) element.findViewById(R.id.duration_activity);
EditText act = (EditText) parent.findViewById(R.id.de_activity);
EditText dur = (EditText) parent.findViewById(R.id.de_duration);
dur_act.setText(dur.getText()+(!dur.getText().equals("")?"":" min ")+act.getText());
TextView comment = (TextView) diaryelement.findViewById(R.id.comment);
EditText comm = (EditText) parent.findViewById(R.id.e_comment);
comment.setText(comm.getText());
base.removeView(findViewById(editorID));
}

android creating text view dynamically

I'm new to android. just playing with the basic things. here's my code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
setContentView(R.layout.activity_display_message);
// Get the message from the intent
Intent intent = getIntent();
String status = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(status);
textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
linearLayout.addView(textView);
}
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff99ccff"
android:orientation="vertical" >
</LinearLayout>
when i run this on my phone it says, "unfortunately, the app has stopped"
Change the order like
setContentView(R.layout.activity_display_message);
linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
You must setContentView(...) first and then initialize your Views.
You can add text view dynamically in other way also :
Create method inside your activity :
private TextView getCustomTextView(Context context, String tvValue, String tvHint) {
TextView textView = new TextView(context);
textView.setText("" + tvValue);
textView.setTextColor(context.getResources().getColor(R.color.black));
textView.setTextSize(20);
// to set font family
Typeface face = Typeface.createFromAsset(getAssets(),
"fonts/epimodem.ttf");
textView.setTypeface(face);
textView.setHint(tvHint+""); // static
textView.setHint(context.getString(R.string.text_hint)); // from string file
textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return textView;
}
Add text view in linearlayout.
linearLayout.addView(getCustomTextView(this, "text value in string", "hint value in string"));
You can more then one text view using same code.

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);
}

How to Place TextView and EditText on same line without using xml?

I used xml and very much familiar with xml only for layouts.
I am trying to code in activity and want to develop view like,
textview editetxt
textview edittext
textview edittext button
LinearLayout lLayout = (LinearLayout)findViewById(R.id.linearLayout);
TextView textViewName = new TextView (MainActivity.this);
textViewName .setText("Name:");
Edittext editTextName= new Edittext (MainActivity.this);
lLayout.addView(textViewName );
lLayout.addView(editTextName);
It will obviously come vertical, but how to place horizontal...
I searched but not got any solution. I tried but, I don't know how to place vertical layout first and inside of it horizontal...
-please help me to sort out this issue if any one knows...
you can create another LinearLayout hLayout inside the lLayout(vertical) and set the orientation of this new layout to be horizontal using
hLayout.setOrientation(LinearLayout.Horizontal);
Add it to llayout
lLayout.addView(hlayout);
Now place both your textview nad edittext inside the hlayout.
package manish.example.testproject;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
int m = 3; // here write number of view want show
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout main = new LinearLayout(this);
main.setOrientation(LinearLayout.VERTICAL);
for (int k = 0; k < m; k++) {
LinearLayout li = new LinearLayout(this);
li.setOrientation(LinearLayout.HORIZONTAL);
TextView t = new TextView(this);
t.setText("Name : ");
EditText e = new EditText(this);
li.addView(t);
li.addView(e);
main.addView(li);
}
setContentView(main);
}
}
I don't understand exactly, but as per my understanding, I am writing my code
LinearLayout lLayout = (LinearLayout)findViewById(R.id.layout1);
LinearLayout Layout2 = new LinearLayout(this);
TextView textViewName = new TextView (MainActivity.this);
textViewName .setText("Name:");
EditText editTextName= new EditText (MainActivity.this);
lLayout.setOrientation(LinearLayout.VERTICAL);
lLayout.addView(Layout2);
Layout2.setOrientation(LinearLayout.HORIZONTAL);
Layout2.addView(textViewName );
Layout2.addView(editTextName);

how to set scroll view in 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.....

Categories

Resources