I have a MainActivity initially empty. When I click on the "plus" button, you open a DialogFragment where there are two buttons("cancel" and "continue") and a EditText. In EditText you can write the name of the project (project 1 in this case) and by clicking "continue" you can create at run time the "Project 1", a "play" button and a TextView "hello" placed as shown in the image below.
I would like the TextView "hello" was positioned next to the "play" button on the same line.
Here is my code:
Method of the button "continue":
#Override
public void onDialogPositiveClick(DialogFragment dialog) {
EditText editText = (EditText) dialog.getDialog().findViewById(R.id.project_name);
String projectName = editText.getText().toString();
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp1.gravity = Gravity.END;
Button projectButton = new Button(this);
projectButton.setText(projectName);
projectButton.setLayoutParams(lp1);
projectButton.setOnClickListener(projectListener);
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp2.gravity = Gravity.START;
lp2.setMarginStart(10);
Button playButton = new Button(this);
playButton.setBackgroundResource(R.drawable.play_button_green);
playButton.setLayoutParams(lp2);
TextView buttonView = new TextView(this);
buttonView.setText("Hello");
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.button_row);
linearLayout.addView(projectButton);
linearLayout.addView(playButton);
linearLayout.addView(buttonView);
}
MainActivity XML layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/button_row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" >
</LinearLayout>
</RelativeLayout>
The whole layout must be created dynamically and then using java code not XML.
Anyone have any ideas?
Put Button and TextView inside LinearLayout with orientation="horizontal"
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.addView(playButton);
ll.addView(buttonView);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.button_row);
linearLayout.addView(projectButton);
linearLayout.addView(ll);
by using horizontal android:orientation
<?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="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="textView"
android:layout_weight="1"/>
</LinearLayout>
Rather than using Button and Text View you can use a single button also.
Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );
Button playButton = new Button(this);
playButton.setText("Hello");
playButton.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null );
playButton.setLayoutParams(lp2);
now you will get a button with text and icon on its left.
Related
In My Activity i have a Edit Text and Button When the user enter the button " Add " the checklist want to create Automatically with the name of the user entered in the edit text
how to create a checkbox in java program based on the user input
when i click the Add Button the list was not updated ( the userinput was not converted into checkbox and it doesnot display the name in bottom )
Here is My XML Code
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Name to Add in the list"
android:id="#+id/ed_name"
android:layout_marginTop="15dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:id="#+id/btn_add"
android:layout_below="#+id/ed_name"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
/>
</RelativeLayout>
here is My java code
public class MainActivity extends AppCompatActivity {
EditText uinput;
Button btnadd;
CheckBox cbname;
ScrollView sv;
RelativeLayout ll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sv = new ScrollView(this);
ll = new RelativeLayout(this);
// ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
uinput = findViewById(R.id.ed_name);
btnadd = findViewById(R.id.btn_add);
btnadd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String Name = uinput.getText().toString();
CheckBox cb = new CheckBox(getApplicationContext());
cb.setText(Name);
ll.addView(cb);
}
});
}
I suggest you to add everything that is static in the activity_main.xml layout file as :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity2">
<EditText
android:id="#+id/ed_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:hint="Enter name to add in the list"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Add"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/ed_name" />
<ScrollView
android:id="#+id/scrollview"
android:layout_width="409dp"
android:layout_height="612dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btn_add">
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Now, Checkbox are the one which we will generate programmatically and add them to the LinearLayout which is the only child of ScrollView(it supports single child only). A sample code for MainActivity doing that is here :
public class MainActivity extends AppCompatActivity {
// declare the required views variable
private EditText uInput;
private LinearLayout linearLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
// initializes required view variable
uInput = findViewById(R.id.ed_name);
linearLayout = findViewById(R.id.linear_layout);
Button btnAdd = findViewById(R.id.btn_add);
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name = uInput.getText().toString();
if (!name.isEmpty()) {
CheckBox checkBox = new CheckBox(MainActivity.this);
checkBox.setText(name);
linearLayout.addView(checkBox);
} else
Toast.makeText(MainActivity.this, "The name cannot be empty!", Toast.LENGTH_LONG).show();
}
});
}
}
It also checks whether or not the text in EditText is empty and if it is generates a suitable Toast message to not permit the user create a empty Checkbox. Hope, this helps!
You didn't add the ScrollView to your layout, So the R.layout.activity_main know nothing about your added views.
So, inflate your root ViewGroup of your layout, add the ScrollView to it and set constraints/attributes according to the type of your root ViewGroup.
Add an id to the root layout
<?xml version="1.0" encoding="utf-8"?>
<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"
android:id="#+id/root"
tools:context=".MainActivity">
Replace the ScrollView inner RelativeLayout with a LinearLayout and add the ScrollView with an attribute param RelativeLayout.BELOW to be at the bottom of the btn_add
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
// ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
EditText uinput = findViewById(R.id.ed_name);
Button btnadd = findViewById(R.id.btn_add);
RelativeLayout rootLayout = findViewById(R.id.root);
// Set constraints/attributes to the sv
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, btnadd.getId());
rootLayout.addView(sv, params);
btnadd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String Name = uinput.getText().toString();
CheckBox cb = new CheckBox(getApplicationContext());
cb.setText(Name);
ll.addView(cb);
}
});
This code will add a checkbox in your view
since you don't have any provided code, ill give you an idea to come up on your own strategies or style
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
Button b = new Button(this);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
CheckBox cb = new CheckBox(getApplicationContext());
cb.setText(myTextBoxText.getText());
ll.addView(cb);
}
});
I am trying to set the text of a Check Box from an Edit Text but it's not working.
I've created an Edit Text with a Button that when clicked creates a Check Box and gets the text for the Check Box from the Edit Text. The app crahes when I click on the Button.
XML
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<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/rootContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="#+id/new_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="New Task"
android:inputType="textCapSentences" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Create" />
</LinearLayout>
</ScrollView>
JAVA
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LinearLayout linearLayout = findViewById(R.id.rootContainer);
EditText newTask = findViewById(R.id.text);
CheckBox checkBox = new CheckBox(MainActivity.this);
String task = newTask.getText().toString();
newTask.setText(task);
checkBox.setText(task);
// Add Checkbox to LinearLayout
if (linearLayout != null) {
linearLayout.addView(checkBox);
}
}
});
}
id of EditText in your layout is android:id="#+id/new_task"
whereas in Activity you using
EditText newTask = findViewById(R.id.text);
So you should use same id in Activity as in xml
Remove below line from onClick event
LinearLayout linearLayout = findViewById(R.id.rootContainer);
and add that line in onCreate() method.
Because linearLayout is your root container.
you don't need to find every time.
And same for the button.
Also there is issue related id.
You are finding text id but you mentation new_task in xml.
So below line should be EditText newTask = findViewById(R.id.new_task);
I want to display underneath my AutoCompleteTextView what the user entered when they press the button beside it. So basically adding a textview in response to button clicks. I have "hello world" displaying where i want them to be right now, but am stuck on how to do this inside a button listener. Any help is appreciated thank you.
Here is an image example of what I am trying to accomplish exactly.
sample display
my onCreate so far:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = new TextView(this);
textView.setText("hello world");
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_main);
//LayoutParams is a class to specify layout properties when adding a view to a layout
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.addView(textView, layoutParams);
}
}
XML for layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:orientation="vertical"
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.tester.MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:text="AutoCompleteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/autoCompleteTextView"
android:layout_weight="1" />
<Button
android:text="PRESS ME"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button2"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
Try this
Button button1 = (Button) findViewById(R.id.button2);
AutocompleteTextView autoTv = (autocompleteTextView) findViewById(R.id.autocompleteTextView);
button1.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
String enteredText = autoTv.getText().toString();
if (!enteredText.isEmpty()) {
TextView textView = new TextView(this);
textView.setText(enteredText);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_main);
//LayoutParams is a class to specify layout properties when adding a view to a layout
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.addView(textView, layoutParams);
}
}
});
I've made the next layout xml code which I call it layout_one -
<?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" >
<LinearLayout
android:id="#+id/layout_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:layout_marginTop="50dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="Testing 1"
android:textSize="20sp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/info_layout"
android:visibility="gone">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
As you can see there are two layout in it - one with textview and button and one with textview.
The layout with the only textview - is gone, And i want by a click on the button, from the visible layout, it will be shown.
Now at the activity i wrote the next code -
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
LinearLayout newView0 = (LinearLayout)View.inflate(this, R.layout.layout_one, null);
LinearLayout newView1 = (LinearLayout)View.inflate(this, R.layout.layout_one, null);
LinearLayout newView2 = (LinearLayout)View.inflate(this, R.layout.layout_one, null);
LinearLayout newView3 = (LinearLayout)View.inflate(this, R.layout.layout_one, null);
ll.addView(newView0);
ll.addView(newView1);
ll.addView(newView2);
ll.addView(newView3);
setContentView(sv);
newView1.findViewById(R.id.layout_one).setBackgroundColor(0xff0000ff);
TextView tv3 = (TextView) newView3.findViewById(R.id.textView1);
tv3.setText("Suprise Suprise");
infoLay = (View) findViewById(R.id.info_layout);
ll.addView(newView3);
}
Now there is something I don't understad - How could I set an on click listener to the button that will know which layout to show?
Tanks for any kind of help
Actually, there are three linear layouts, which seems redundant.
You can set the onclick listener by checking if textview of each layout is empty or not.
First off, have a look at this http://developer.android.com/training/improving-layouts/reusing-layouts.html. Use that <include> tag instead of dynamically creating all these layouts in your Activity class. Define your ScrollView, and all your LinearLayouts in your test_layout.xml file.
Then, in your Activity, all you have to do is get a reference to those views that's you've defined using findViewById.
Once you've got that working we can address your question as follows:
setContentView(R.layout.test_layout);
LinearLayout layout1 = (LinearLayout)findViewById(R.id.layout1);
LinearLayout layout2 = (LinearLayout)findViewById(R.id.layout2);
LinearLayout layout3 = (LinearLayout)findViewById(R.id.layout3);
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View view) {
((View)view.getParent().getParent()).findViewById(R.id.textView2).setVisibility(View.VISIBLE);
}
});
layout1.findViewById(R.id.button1).setOnClickListener(listener);
layout2.findViewById(R.id.button1).setOnClickListener(listener);
layout3.findViewById(R.id.button1).setOnClickListener(listener);
I'd also add that it seems like you're trying to create a list here, in which case you should use a ListView instead of many LinearLayouts.
Good day.
I have three layouts: first is the root, second and third lie in first. I try add TextView object in third layout and objects had been added in third layout (I saw it in debage mode) but this objects didn't showed on screen.
May be someone know where is the problem?
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/addJokeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
/>
<EditText
android:id="#+id/newJokeEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
protected void initLayout() {
setContentView(R.layout.advanced);
LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(
R.layout.advanced, null);
m_vwJokeEditText = (EditText) findViewById(R.id.newJokeEditText);
m_vwJokeButton = (Button) findViewById(R.id.addJokeButton);
m_vwJokeLayout = (LinearLayout) linearLayout.getChildAt(1);
}
protected void addJoke(Joke joke) {
m_arrJokeList.add(joke);
LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
TextView textView = new TextView(this);
setColor(textView);
textView.setLayoutParams(lparams);
textView.setText(joke.getJoke());
m_vwJokeLayout.addView(textView);
}
Refer this sample example
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
public class HelloAndroid extends Activity {
TextView textview;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout ll= (LinearLayout) findViewById(R.id.LinearLayout01);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LinearLayout childLayout= new LinearLayout(this);
childLayout.setOrientation(LinearLayout.VERTICAL);
TextView text = new TextView(this);
text.setText("High");
childLayout.addView(text);
ll.addView(childLayout, lp);
}
}`
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/textview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/hello"/>
</LinearLayout>
or make use of following link
Creating Linear Layout with TextViews using a for loop
Try to access LayoutParams like this
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
and also you are declaring linearLayout as local variable on fallowing line use it as class variable. so that it is accessible out side method.
LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(
R.layout.advanced, null);