Is there any way to get this thing done. I'm getting input from another activity to create dynamic editText to get input.
public class GetNames extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.names);
LinearLayout ll = findViewById(R.id.lin);
Bundle bundle = getIntent().getExtras();
String entered_no = bundle.getString("no_of_persons");
for (int i = 0; i < (Integer.valueOf(entered_no)); i++) {
EditText editText = new EditText(GetNames.this);
editText.setId(editText.generateViewId());
ll.addView(editText,0);
editText.setHint(i);
}
}
}
Is there any way to resolve this.
Her is my XML file
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:id="#+id/lin"
android:layout_height="wrap_content"
android:orientation="vertical"></LinearLayout>
Here are error messages
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abhishakkrmalviya.perfecthalf/com.abhishakkrmalviya.perfecthalf.GetNames}: android.content.res.Resources$NotFoundException: String resource ID #0x0
Your problem is that you are not setting layout parameters, That have a look at this example.
ConstraintLayout layout = (ConstraintLayout)findViewById(R.id.mainConstraint);
ConstraintSet set = new ConstraintSet();
EditText editText = new EditText(GetNames.this);
editText.setId(View.generateViewId());
editText.setHeight(50);
editText.setWidth(50);
layout.addView(view,0);
set.clone(layout);
set.connect(view.getId(), ConstraintSet.TOP, layout.getId(), ConstraintSet.TOP, 60);
set.applyTo(layout);
You can refer this for more info
Related
I'm trying to create a LinearLayout programmatically but for some reason it is not shown and I have no errors in Logcat or Run terminals.
Here is my Java code:
public class MainActivity extends AppCompatActivity {
String [] arr = {"1","2","3"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout parent = findViewById(R.id.rootLayout);
LinearLayout child;
for(int i = 0; i < arr.length; i++) {
child = new LinearLayout(this);
child.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
child.setOrientation(LinearLayout.VERTICAL);
child.setBackgroundColor(Color.YELLOW);
parent.addView(child);
}
}
}
and my XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
</LinearLayout>
What I'm trying to achieve is to have 3 LinearLayouts created according to the array length as I will pass some TextViews to them later
I tried to follow the answer I found Creating LinearLayout Programmatically/Dynamically with Multiple Views but still cannot see the LinearLayouts created on the simulator.
Here is how it shows:
Simulator Preview
I'm unsure what am I doing wrong?
Thank you for the hints and help.
Used the below and this fixed the issue:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.height = 150;
How can I write a code whereby a user enters the number of buttons needed on a text field, these buttons then displays dynamically based on the number entered on the edit text field. Thanks
Read the count from the edit text and add the buttons like this inside a loop
Button button = new Button(this);
parent.addView(button);
Should be fairly simple to do this. Just add these attributes to your XML first.
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.nero.myapplication.MainActivity">
<EditText
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Number of Buttons"
android:inputType="number"/>
<Button
android:id="#+id/btn_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SUBMIT"/>
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
In your Edittext, make sure you have inputType to number so you can always expect to receive a whole number instead of string or anything else. I've also added a submit button which will listen to your request but you can change that however you like to trigger the function. Also you will need another layout/view where you will be displaying your buttons.
MainActivity
public class MainActivity extends AppCompatActivity {
EditText main;
Button submit;
LinearLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main = (EditText) findViewById(R.id.main);
layout = (LinearLayout) findViewById(R.id.layout);
submit = (Button)findViewById(R.id.btn_submit);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int noOfButtons = Integer.parseInt(main.getText().toString());
for(int i = 0; i < noOfButtons; i++){
Button button = new Button(MainActivity.this);
layout.addView(button);
}
}
});
}
}
Now you basically wire in all of your elements with your main activity class. Please note that I am using the old android studio version and that's why I have to declare the variable type (Button, Edittext, Layout) when I am wiring it in.
So you will simply create a onClickListener for your submit button which will then action the number of buttons the user has requested. It will then take the number in a loop and create a new button as needed.
Let me know if you need further info.
This question already has answers here:
Your content must have a ListView whose id attribute is 'android.R.id.list'
(7 answers)
Closed 8 years ago.
I am new to Android. when I run my project, I got the following error:
Your content must have a ListView whose id attribute is 'android.R.id.list'
it crush on this line:
setContentView(R.layout.activity_main);
my activity_main.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="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="com.soft8.sql_test.MainActivity" >
<ListView
android:id="#+id/TheClient"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
and main class:
public class MainActivity extends ListActivity {
private ArrayList results = new ArrayList();
SQLController dbcon;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView lv1 = (ListView) findViewById(R.id.TheClient);
dbcon = new SQLController(this);
dbcon.open();
dbcon.insertSomeItmes();
results = (ArrayList) dbcon.ReadData();
lv1.setAdapter(new CustomListAdapter(this, results));
dbcon.close();
}
Thank you.
Change this
android:id="#+id/TheClient"
to
android:id="#android:id/list"
There is no need to ListView Cast it because already extends your Activity with ListActivity
and also remove
final ListView lv1 = (ListView) findViewById(R.id.TheClient);
and directly set adapter by using
setListAdapter(new CustomListAdapter(this, results));
When you extend your activity from ListActivity, you don't need to define your list again.
The ListActivity automatically looks in the layout set for it for a ListView with id "list". If it does not find a ListView with id "list" it gives your error.
So change
android:id="#+id/TheClient"
to
android:id="#android:id/list"
and remove definition of your list from activity. (remove findViewById line)
Or just extend normal Activity instead of ListActivity.
One potential error will come from the fact that you use upper case in your id
android:id="#+id/TheClient"
try with lower case
android:id="#+id/the_client"
Rename the id of your ListView
android:id="#+id/TheClient"
to
android:id="#android:id/list"
Since you are using ListActivity your xml file must specify the keyword android while mentioning to a ID
and remove
final ListView lv1 = (ListView) findViewById(R.id.TheClient);
from activity code.
i.e. simply rewrite your code as
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbcon = new SQLController(this);
dbcon.open();
dbcon.insertSomeItmes();
results = (ArrayList) dbcon.ReadData();
setAdapter(new CustomListAdapter(this, results));
dbcon.close();
}
So I have a simple android app where I take text from a text file (that part is working) and I want to set that text to EditText.setText(). The problem I'm having is that I cannot do this in the onCreate() method because the EditText field and the UI hasn't been created yet(from what it seems).
My question is where would be a good place to do this? Is there a function that is called right after the GUI elements are created?
public class MainActivity extends ActionBarActivity
{
private final String FILE_NAME = "awayReply";
private EditText myEditMessage;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null)
{
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
protected void onStart()
{
String message = readMessage();
myEditMessage = (EditText)findViewById(R.id.edit_message);
if(myEditMessage != null)
{
myEditMessage.setText(message, TextView.BufferType.EDITABLE);
}
}
XML Layout:
<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:orientation="horizontal"
android:paddingBottom="16dp"
android:paddingTop="16dp" >
<EditText
android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/lbl_message"
android:inputType="textImeMultiLine" />
<Button
android:id="#+id/btn_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/edit_message"
android:onClick="updateMessage"
android:text="#string/lbl_update" />
<ToggleButton
android:id="#+id/toggle_away"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/lbl_enable"
android:layout_alignBottom="#+id/lbl_enable"
android:layout_toRightOf="#+id/edit_message"
android:textOff="Disabled"
android:textOn="Enabled"
android:onClick="toggleEnabled" />
</RelativeLayout>
You can't set your text in a TextView until you inflate your layout with
setContentView(R.layout.main_activity);
in the Activity's OnCreate
Once that happens you can do:
EditText editText = (EditText)findViewById(R.id.myEditText)
Full Example:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // this needs to happen first
EditText editText = (EditText)findViewById(R.id.myEditText)
editText.setText("my text", TextView.BufferType.EDITABLE); // <-- needs that second parameter
}
Edit: updated for EditText instead of TextView
I think I figured out my problem. I need to do this work in the PlaceholderFragment where onCreateView is defined. When I call findviewbyid here it dosen't return null.
I need to put an ImageView under a TextView that was constructed using java. The textview is displaying information from a previous activity. Here is my code:
package com.example.a_simple_ui;
public class MainActivity2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent recieve = getIntent();
String message = recieve.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
}
So now I need a picture under the textview above and to change the background color. Either by java or XML. Thank you.
You can use any approach from bellow two suggestion:
1) If your layout design is fixed than it is better to use xml based layout (static layout) rather than adding layout run-time.
For that first create xml layout main_Activity.xml like:
main_Activity.xml
<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" >
<TextView
android:id="#+id/tvDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Imageview
android:id="#+id/ivIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher" />
</LinearLayout>
MainActivity2 .java
package com.example.a_simple_ui; public class MainActivity2 extends
Activity {
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(textView); Intent recieve = getIntent();
String message = recieve.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView =(TextView)findViewById(R.id.tvDesc);
textView.setTextSize(40);
textView.setText(message); } }
2) You need to add TextView and Imageview in linearlayout then need to set that linearlayout in setContentView() like:
package com.example.a_simple_ui;
public class MainActivity2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent recieve = getIntent();
String message = recieve.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
ImageView imageView = new ImageView(this);
imageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
imageView.setImageResource(R.drawable.ic_launcher);
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(textview);
layout.addView(imageView);
setContentView(layout);
}
}
Ok, actually I can't understand, why you use TextView class as you content view. In that case, you can, offcourse, make you own textview extending default one and create particular layout with imageview for it, but, the easiest (and more logical) way is to create lauout file for your activity with TextView and ImageView.
For example, it can be something like that:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/myImageView"
android:layout_width="wrap_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/myTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
And your activity onCreate method will be looks something like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(textView);
Intent recieve = getIntent();
String message = recieve.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = (TextView) findViewById(R.id.myTextView);
textView.setTextSize(40);
textView.setText(message);
ImageView imageView = (ImageView) findViewById(R.id.myImageView);
}
You need to call setContentView before finding views by id.
If you have to create texview dynamic, from java (I can't see any reason for it from your code) you can add id property to LinearLayout and than find it by id and add texview to that, and it will be placed after imageview.
p.s. Actually there are a lot of ways to do that, please, define your question more preciselly if my answer doesn't suits you
p.p.s. If you really have to create textView in runtime and you can't use xml layouts, you can create LinearLayout in runtime (LinearLayout layout = new LinearLayout(this) and after that, create textView and ImageView and add that two views to that layout, after that make set this layout as content view for the activity