Sending data to other activity and display some additional TextViews - android

I have an Android application in which i send data from one activity to another. I can do that part easily but the problem is i want to display some additional TextViews as well. So when i display the sent data using setContentView i can see only the sent data. I cannot see the additional TextViews. I want both of them to be displayed simultaneously.
I searched and tried hard to find a solution without any luck.I am attaching the code for reference.Thanks!
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crop_information);
Intent intent = getIntent();
String message = intent.getStringExtra(CropsListActivity.EXTRA_MESSAGE);
TextView textViewci= new TextView(this);
textViewci.setText(message);
TextView textView1= new TextView(this);
TextView textView2= new TextView(this);
textView1.setText("Major Districts");
textView2.setText("Suitable Soil");
When i use this code,i am unable to see anything and the activity remains blank.Actually i am using a TableLayout here so that i can display data in form of tables

You have to add text view on your layout.
View linearLayout = findViewById(R.id.info);
//LinearLayout layout = (LinearLayout) findViewById(R.id.info);
TextView valueTV = new TextView(this);
valueTV.setText("hallo hallo");
valueTV.setId(5);
valueTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
((LinearLayout) linearLayout).addView(valueTV);

You have to add the text view in the parentview. For example, if you have used the text view in the linear layout then add the text view in the linear layout and then use the addview(linearlayout).
Below is the example
RelativeLayout layout = new RelativeLayout(this);
TextView tv1 = new TextView(this);
tv1.setText("A");
tv1.setId(23);
TextView tv2 = new TextView(this);
tv2.setText("B");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.RIGHT_OF, tv1.getId());
layout.addView(tv1);
layout.addView(tv2, lp);

I have just make a test program for you. first class code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, SecondClass.class);
intent.putExtra("firstValue", "1");
intent.putExtra("secondValue", "2");
startActivity(intent);
}
});
Second class code :
setContentView(R.layout.activity_crop_information);
Intent intent = getIntent();
String message1 = intent.getStringExtra("firstValue");
String message2 = intent.getStringExtra("secondValue");
RelativeLayout layout = new RelativeLayout(this);
TextView textView1= new TextView(this);
TextView textView2= new TextView(this);
TextView textViewci= new TextView(this);
textViewci.setText(message1+""+message2);
Log.e("intent value is", ""+message1+""+message2);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.RIGHT_OF, textView1.getId());
textView1.setText("Major Districts");
textView2.setText("Suitable Soil");
layout.addView(textView1);
layout.addView(textView2);
addContentView(layout, lp);
Try and let me know. YOu have to make related layouts for both classes.
Hope this will help

are you getting any error messages? if yes then please post them here. and the others are right.you need to add yours views to the parent layout.

I have found out the answer by myself.It's fairly easy and there is no need here to use views in the parent layout.I modified the textViews in my xml file and got their values using findViewById.I am attaching the updated code.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crop_information);
Intent intent = getIntent();
String message = intent.getStringExtra(CropsListActivity.EXTRA_MESSAGE);
textView=(TextView)findViewById(R.id.textViewci);
textView1=(TextView)findViewById(R.id.textViewmd);
textView2=(TextView)findViewById(R.id.textViewso);
textView.setText(message);
textView1.setText("Major Districts");
textView2.setText("Suitable Soil");
tf = Typeface.createFromAsset(getAssets(), "fonts/SHRUTIB.TTF");
textView.setTypeface(tf);
}

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

A button to create an object on android Studio

How can we create a button with a command to create an other object on Android ?
i mean, i press the button on the app and it create in the layout, a new object, for exemple a textView.
how could i proceed ?
thanks !
This is a way:
First you defined into your xml file a layout parent for your TextView and get it in your code:
final LinearLayout parentView = (LinearLayout) findViewById(R.id.parent_view_id);
Then you define create your TextView for example:
final TextView textView = new TextView(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.MATCH_PARENT
); // set height and width
textView.setMargins(left, top, right, bottom); // set margin if necessary
textView.setLayoutParams(layoutParams);
and in your listener :
myButton.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View view) {
parentView.addView(textView);
}
});
Hope this helps.
Sorry for my english.
first of all create a click listener on your button
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
}
and then this with add a new textview into your target layout
TextView tv = new TextView(this);
tv.setText("hello word!");
tv.setTextSize(18);
tv.setTextColor(Color.BLACK);
tv.setClickable(true);
tv.setPadding(0, 10, 0, 0);
tv.setGravity(Gravity.CENTER);
mainLayout.addView(tv);
in this ex mainLayout is the view that you want to add new object like textview, ....

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.

Android dynamic layout

I want to dynamically set the layout. I get a question (String type) as shown below :
Enter any two colors -------- and ---------
So using this string I have to create the view. Here the '---------' must be an EditText and the rest of the string can be a TextView. The blanks, i.e, EditText must appear inline as it appears in the question string. I am new to Android any suggestions would be helpful.
use this class for creating your dynamic views.may be it will help you.
public class MainActivity extends Activity {
LinearLayout l1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l1=(LinearLayout)findViewById(R.id.linear_layout);
LinearLayout layout=new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
layout.setOrientation(LinearLayout.HORIZONTAL);
TextView text1=new TextView(this);
text1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
text1.setText("Enter any two colors");
EditText edt1=new EditText(this);
edt1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
edt1.setHint("COLOR1");
TextView text2=new TextView(this);
text2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
text2.setText("and");
EditText edt2=new EditText(this);
edt2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
edt2.setHint("COLOR2");
layout.addView(text1);
layout.addView(edt1);
layout.addView(text2);
layout.addView(edt2);
l1.addView(layout);
}
}

how to get values from edit text without using xml code

I have created edittext and textview without using xml attributes i have defined in java file only but i am unable get values entered in edittext how to get values.i have 2 edittexts if user enters the values i should add the values
public class JavacodeActivity extends Activity {
LinearLayout layout;
TextView view;
EditText edit;
Button btn;
EditText edit1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout=new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
layout.setOrientation(LinearLayout.VERTICAL);
view=new TextView(this);
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
view.setText("enter the value");
view.setGravity(Gravity.CENTER);
view.setTextColor(Color.BLUE);
view.setTextSize(20);
layout.addView(view);
edit1=new EditText(this);
edit1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
edit1.setHint("enter the number");
edit1.setGravity(Gravity.CENTER);
edit1.setTextColor(Color.RED);
edit1.setTextSize(20);
edit1.setInputType(InputType.TYPE_CLASS_NUMBER);
layout.addView(edit1);
btn=new Button(this);
btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
btn.setText("add");
btn.setGravity(Gravity.CENTER_HORIZONTAL);
btn.setTextColor(Color.GREEN);
btn.setTextSize(20);
layout.addView(btn);
view=new TextView(this);
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
view.setText("addition of values");
view.setGravity(Gravity.CENTER);
view.setTextColor(Color.BLUE);
view.setTextSize(20);
layout.addView(view);
I don't know the attribute to retrieve values from edittext if the code is java I am unable to retrieve values. Please help me.
I don't know the attribute to retrieve values from edittext if the code is java I am unable to retrieve values
Read your EditText like any other EditText:
String text = edit.getText().toString();

Categories

Resources