I understand how to display some text with the user input, but what I'm confused about is how to add a completely new block of text under the one I have right now (I want to put a new block of text because I want the size of the text to be different). Right now, I display "Welcome [user input]!"
In my activity file I have:
public class DisplayMessageActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = " Welcome " + intent.getStringExtra(MainActivity.EXTRA_MESSAGE) + "!";
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(25);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
And in my fragment.xml file, I have:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.myfirstapp.DisplayMessageActivity$PlaceholderFragment"
android:gravity="center_horizontal"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:text="TEST"
android:layout_height="wrap_content" />
</LinearLayout>
With some help from the feedback I got from other users, I added android:text="TEST" too the second "TextView". And I also added the android:orientation="vertical" to LinearLayout. But the "TEST" still doesn't show up when I run the app! Thanks in advance.
You are not associating your layout to your Activity. Instead, you are adding a TextView on runtime, that is created on runtime. That's why only one TextView appears. You have to use setContentView(R.layout.mylayout); instead of setContentView(textView);, so your Activity fetches your layout.
Edit: In your layout, add ids to your TextViews, so you can access them from your Activity code, like this:
<TextView
android:id="#+id/myFirstTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TEST1" />
<TextView
android:id="#+id/mySecondTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TEST2" />
Then on your Activity, inside onCreate():
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
// Get the message from the intent
Intent intent = getIntent();
String message = " Welcome " + intent.getStringExtra(MainActivity.EXTRA_MESSAGE) + "!";
TextView textView1 = (TextView) findViewById(R.id.myFirstTextView);
TextView textView2 = (TextView) findViewById(R.id.mySecondTextView);
textView1.setTextSize(25);
textView1.setText(message);
}
Assuming fragment.xml is in the res/layout folder
setContentView(R.layout.fragment);
instead of
setContentView(textView);
Related
I am trying to add an EditText within a TextView. My idea is to build a two activities, wherein in the first activity, fill in the blanks type of question will be entered in EditText and will be sent to second activity on button click and in second activity I want the text entered in the first activity to be displayed along with the EditText in the place of blank where it needs to answered.
My whole idea was to enter fill in the blank question with underscore at the place to be answered in the first activity and to display the question in the second activity by replacing the underscore with EditText.
first activity class code:
public class FirstActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
}
public void onButtonClick(View view)
{
if (view.getId() == R.id.button)
{
EditText editText = (EditText) findViewById(R.id.editText);
String text = editText.getText().toString();
Intent intent = new Intent(this, Main2Activity.class);
intent.putExtra("mytext", text);
startActivity(intent);
}
}
}
First activity layout code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".FirstActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_marginTop="48dp"
android:id="#+id/editText"
android:hint="Enter here"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:id="#+id/button"
android:onClick="onButtonClick" />
</RelativeLayout>
Second Activity Class:
public class SecondActivity extends AppCompatActivity {
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView textview1 = (TextView) findViewById(R.id.textView);
int positionX = textview1.getLeft();
int positionY = textview1.getRight();
EditText ed = new EditText(this);
ed.setEnabled(false);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) ed.getLayoutParams();
params.leftMargin = positionX;
params.topMargin = positionY;
params.width = 30;
ed.setLayoutParams(params);
textview1.setText(getIntent().getStringExtra("mytext"));
}
}
Second Activity Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".SecondActivity">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="33dp"
android:layout_marginTop="47dp"
android:id="#+id/textView" />
</RelativeLayout>
You could split your text with your underscore characters, then, programmatically add TextViews and EditText.
Take a look at this.
Developing an android app here I am dynamically generating EditText and button like Call and Remove from row.xml file. adding these control to LinearLayout I implemented as I want but my problem is that I want to save the EditText values into xml file. so that next time I open this app the particular values gets populated from xml file.
Adding name and Mobile number to TextBox and click on "Add" button these values are dynamically added to LinearLayout. when I click on Add this values are stored into xml file and whenever next time I launch the app stored xml values are populated into LinearLayout.
this is my code
public class MainActivity : Activity
{
int count = 1;
EditText textIn, txtPhoneNo;
Button buttonAdd;
LinearLayout container;
EditText textOut;
System.Collections.ArrayList arrList = new System.Collections.ArrayList();
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
textIn = (EditText)FindViewById(Resource.Id.textin);
txtPhoneNo = (EditText)FindViewById(Resource.Id.txtPhoneNo);
buttonAdd = (Button)FindViewById(Resource.Id.add);
container = (LinearLayout)FindViewById(Resource.Id.container);
}
private void buttonAdd_Click(object sender, EventArgs e)
{
LayoutInflater layoutInflater = Application.Context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
View addView = layoutInflater.Inflate(Resource.Layout.row, null);
textOut = (EditText)addView.FindViewById(Resource.Id.textout);
arrList.Add(txtPhoneNo.Text);
if (textIn.Text != "" && txtPhoneNo.Text != "")
{
textOut.SetText(textIn.Text + " : " + txtPhoneNo.Text, TextView.BufferType.Normal);
container.AddView(addView);
Button btnCall = (Button)addView.FindViewById(Resource.Id.btnCall);
btnCall.Click += BtnCall_Click;
Button buttonRemove = (Button)addView.FindViewById(Resource.Id.remove);
buttonRemove.Click += ButtonRemove_Click;
}
else
{
Toast.MakeText(this, "Field can not be blank.", ToastLength.Short).Show();
}
}
private void BtnCall_Click(object sender, EventArgs e)
{
var callDialog = new AlertDialog.Builder(this);
string strNo = After(textOut.Text,":");
callDialog.SetMessage("Call " + strNo + "?");
callDialog.SetNeutralButton("Call", delegate
{
var callIntent = new Intent(Intent.ActionCall);
callIntent.SetData(Android.Net.Uri.Parse("tel:" + strNo));
StartActivity(callIntent);
});
callDialog.SetNegativeButton("Cancel", delegate { });
// Show the alert dialog to the user and wait for response.
callDialog.Show();
}
}
}
Main.axml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="#+id/textin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:hint="name" />
<EditText
android:id="#+id/txtPhoneNo"
android:layout_width="345.0dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:hint="Phone No." />
<Button
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Add" />
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</LinearLayout>
row.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Remove"/>
<Button
android:id="#+id/btnCall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/remove"
android:text="Call"/>
<EditText
android:id="#+id/textout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/remove"/>
</RelativeLayout>
No you can't
you cannot store controls dynamically into your xml layout.
Either you add it manually into xml or you can create separate xml for control and then include it xml into your parent xml
<include
android:layout="id of your layout"
//height ... width
/>
In both ways, you have to create xml.
We are creating xml and inflate it in our java class. what you want to do is totally reverse of that.
Update
use shared preference
SharedPreference shared= context.getSharedPreferences("your preference name",
Context.MODE_PRIVATE);
String value = shared.getString("UserPhone","no data");
//set this value to your control
I think you need to refer SharedPreferences in android. This will serve your purpose.
I created a message activity and it works but my problem is the xml layout that I created is not showing, only the message or vice versa (the layout is showing without the message).How do I solve this?
here is the code of the class:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra (MESSAGE_KEY);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText (message);
setContentView(textView);
setContentView(R.layout.activity_fire_alert);
}
and here is the xml file that i want to show
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.user.tictactoeniandy.FireAlertActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="View Sender Location"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="91dp"
android:id="#+id/loc_button1"/>
</RelativeLayout>
You can only have 1 content view. The second one you set overwrites the other. The correct way to do this is to have a TextView inside your layout, and to set the text on that view to the message
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation:"vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.user.tictactoeniandy.FireAlertActivity">
<TextView
android:id="#+id/loc_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="View Sender Location"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="View Sender Location"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="91dp"
android:id="#+id/loc_button1"/>
</LinearLayout>
and in your activity code,
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fire_alert);
Intent intent = getIntent();
String message = intent.getStringExtra (MESSAGE_KEY);
TextView textView = (TextView)findviewbyid(R.id.loc_textview);
textView.setTextSize(40);
textView.setText (message);
}
you can have only one set content view. try to update your layout and code with mine and check it will show both.
The setContentView() method, as the name suggests, sets the content of your activity. To solve this, place your TextView directly on your layout file, for example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
...>
<Button
android:layout_width="wrap_content"
android:layout_height="
...
.../>
<TextView
android:id="#+id/text_view_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:layout_centerInParent="true"/>
</RelativeLayout>
On your Java code, make a TextView using the id you just created on your layout, then set the message text on it.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fire_alert);
TextView textView = (TextView) findViewById(R.text_view_message);
Intent intent = getIntent();
String message = intent.getStringExtra (MESSAGE_KEY);
textView.setText (message);
}
I have a custom android dialog which has several textViews. each textview is showing a different text. The intention is when user clicks on a textview, the dialog should be closed and the textColor of that textview should be returned to the parent.
This is my dialog layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff000000">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="White"
android:id="#+id/textView_white"
android:layout_gravity="center_horizontal"
android:textSize="30dp"
android:textIsSelectable="true"
android:clickable="true"
android:gravity="center"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textColor="#fffffbfd" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Black"
android:id="#+id/textView_black"
android:layout_gravity="center_horizontal"
android:textSize="30dp"
android:textIsSelectable="true"
android:clickable="true"
android:gravity="center"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textColor="#ffffffff"
android:password="false"
android:background="#ff000000" />
</LinearLayout>
I am launching the dialog this way from the parent activity:
final Context context = MyWidgetConfigureActivity.this;
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.color_chooser);
dialog.setTitle("Choose Text Color");
dialog.show();
I want to add same onClickListener to every textView inside the dialog. How to acheive this? There are 20+ textViews in the dialog, I dont want to add onClickListener to each textViews manually? is there any better way to do the same?
I have two ways, first I would set a id for each TextView, then use the method dialog.findViewById(id) to get each TextView, and set a onClickListener, but I think this way is a pain in the neck. So the second way is: firstly give the dialog layout root view a id, like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/dialog_root"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff000000">
then the java code is:
LinearLayout rootLayout = (LinearLayout) dialog.findViewById(R.id.dialog_root);
for (int i = 0; i < rootLayout.getChildCount(); i++) {
TextView tv = (TextView) rootLayout.getChildAt(i);
tv.setOnClickListener(this);
}
...
the onClick method is something like this.
#Override
public void onClick(View v) {
int color = ((TextView) v).getCurrentTextColor();
// TODO ...
}
OnClickListener click = new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(this,"You can use", Toast.LENGTH_LONG).show();
}
};
TextView t1 = (TextView)findViewById(id);
TextView t2 = (TextView)findViewById(id);
TextView t3 = (TextView)findViewById(id);
t1.setOnClickListener(click);
t2.setOnClickListener(click);
t3.setOnClickListener(click);
I'd suggest to change your approach. If you have 20+ TextViews which probably look the same, why not use ListView? Take a look here.
Normally null pointer exceptions seem to be view related - where the wrong layout is targeted.
This is different I reckon. I have four textviews in a layout and one returns a null, the rest work fine. Here is the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello" />
<TextView
android:id="#+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="stringello2" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ip"
android.id="#+id/iptest"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hostname"
android:id="#+id/hostname"
/>
</LinearLayout>
And here is the test code:
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
protected TextView text;
protected TextView ip;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView) findViewById(R.id.text);
text.setText("goodbye");
text = (TextView) findViewById(R.id.hostname);
text.setText("hostname flibble");
// text = (TextView) findViewById(R.id.text2);
text = (TextView) findViewById(R.id.iptest);
text.setText("ip flibble");
}
}
If I switch the comment to the other textview, it works fine. If I target iptest it returns null and raises an exception.
Any ideas why? All four appear in gen and they all reappear if I delete gen and recompile.
in your TextView Tag
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ip"
android.id="#+id/iptest"
/>
you had taken a .(dot) instead of :(colon)
android.id="#+id/iptest"
shoulb de like this
android:id="#+id/iptest"
Moreover please clean your project regularly.
your R class is not holding iptest reference . android.id="#+id/iptest" is wrong . It should be android:id