A button to create an object on android Studio - android

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, ....

Related

Is there a way to change background color behind a button (button.setBackgroundResource)

I'm a new Android dev student, and I'm trying to create a dynamic layout witch contains a TextView and a button inside the same row.
but I have a little problem.
I set my Button in my Drawables ressources by
button.setBackgroundResource(R.drawable.ic_comment_black_48px);
and now, I cannot change the backgroundcolor behind it.
I have created a newLinearlayout inside my main LinearLayout and have created a new textView and a new Button.
I have put them inside the LinearLayout's child and put it inside the main.
That's work but not the background color behind my button.
is there a way to do that?
my xml layout
<?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"
android:background="#color/grey"
android:id="#+id/historyLayout">
</LinearLayout>
my complete activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
mLinearLayout = findViewById(R.id.historyLayout);
mMoodSaved = new ArrayList(7); // Define the max size of my ArrayList
loadData();
for (int i = 1; i <= 7; i++) {
final TextView textView = new TextView(this);
mLinearLyt = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
mLinearLyt.setOrientation(LinearLayout.HORIZONTAL);
textView.setHeight(300);
textView.setWidth(400);
textView.setBackgroundColor(Color.RED);
textView.setTextColor(Color.BLACK);
textView.setTextSize(12);
textView.setText(String.valueOf(i));
mLinearLyt.setBackgroundColor(Color.YELLOW);
mLinearLyt.addView(textView);
mLinearLyt.setLayoutParams(params);
ImageButton button = new ImageButton(this);
LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
param2.setMargins(20,50,0,0);
param2.height = 100;
param2.width = 100;
button.setLayoutParams(param2);
button.setBackgroundResource(R.drawable.ic_comment_black_48px);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
soundConfirm();
Toast.makeText(getApplicationContext(), textView.getText(), Toast.LENGTH_LONG).show(); //Display Toast Message
}
});
mLinearLyt.addView(button);
mLinearLayout.addView(mLinearLyt);
}
}
Since this an ImageButton, set
button.setImageResource(R.drawable.ic_comment_black_48px)
instead of setBackgroundResource.

How to remove dynamically created views on button click

public void Add_text() {
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setId(i);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView product = new TextView(getActivity());
product.setText(" Product" + 5 + " ");
ll.addView(product);
EditText qty = new EditText(getActivity());
qty.setText(i + "");
qty.setId(i);
qty.setWidth(120);
ll.addView(qty);
Button btn = new Button(getActivity());
ll.addView(btn);
btn.setLayoutParams(params);
btn.setOnClickListener(o);
ly.addView(ll);
i++;
}
I wrote the above code to create the textfields and buttons dynamically; But now I need to remove 2 textfields and a button when the button is clicked. How do I do that?
Try following code.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
LinearLayout linearParent = (LinearLayout) v.getParent().getParent();
LinearLayout linearChild = (LinearLayout) v.getParent();
linearParent.removeView(linearChild);
}
});
Explanation
Here first take "GrandParent" of any view.
Then take its "Parent" view
With reference to "GrandParent" remove that "Parent" view.
this will remove all views which that "Parent" holds. As per your code, your "ll" will be "linearChild" here. And "ly" will be "linearParent" here. So whole "ll" will be removed from "ly" which you have added dynamically.
If you want to permanently remove the views you created.
OnClick(View view){
ly.removeAllViews()
}
If you do not want to permanently remove the views you created.
OnClick(View view){
ly.setVisibility(View.GONE); //This will hide the all views
qty.setVisibility(View.GONE);//This will hide the EditText qty
product .setVisibility(View.GONE);//This will hide the TextView product
}
So use appropriate code line which you want.
EDIT:
Use this code for your situation:
public void Add_text() {
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setId(i);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView product = new TextView(getActivity());
product.setText(" Product" + 5 + " ");
ll.addView(product);
EditText qty = new EditText(getActivity());
qty.setText(i + "");
qty.setId(i);
qty.setWidth(120);
ll.addView(qty);
Button btn = new Button(this);
ll.addView(btn);
btn.setLayoutParams(params);
ly.addView(ll);
i++;
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View button) {
qty.setVisibility(View.GONE);//This will hide the EditText qty
product .setVisibility(View.GONE);//This will hide the TextView product
}
});
}
i think you got to use this method on your LinearLayout :
public void removeView (View view)
first you call :
EditText et = (EditText)linearLayout.findViewById(yourEditText.getId());
then call the remove view method :
linearLayout.removeView (et) ;
and to remove all of the Views that are in the LinearLayout do the following :
public void removeAllViews ()
like the following :
linearLayout.removeAllViews()
and give me some feedback
Hope that Helps .
you can simply use qty.setVisibility(View.GONE) on the onClickListener() of the Button of your choice. like this.
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
qty.setVisibility(View.GONE); //for temporary purpose
//or u can also do this
layout.removeView(qty); //removes permanently
}
});
The benefit of using View.GONE is that you can get the View back if you want but layout.removeView(qty) will remove the view permanently and you have to re add the view again.
[EDIT] 1. changed to View.GONE instead of View.INVISIBLE because of reasons explained here
Hope I answered your question. :)
just use index for which you want to remove your view from linear layout
Linearlayout.removeViewAt();
if you want again that view then you can call addViewAt()in same way.
I hope it will help you.
Add this line
mLayout.removeViewAt(mLayout.getChildCount()-1);

Android -Dynamic TextView over previous TextView

I am making an example of dynamic textview, that are created by pressing a button. the format in which they are created is similar to:
1- Textview (fist created)
2- TextView
3- TextView (last created)
I wonder if we can create them in an ascending order, one above the previous TextView.
something like this:
3- TextView (last created)
2- TextView
1- TextView (first created)
this is my example code:
private OnClickListener onClick() {
return new OnClickListener() {
#Override
public void onClick(View v) {
mLayout.addView(createNewTextView("TextView"));
count = count +1;
}
};
}
private TextView createNewTextView(String text) {
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText(count+ "- " + text);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
return textView;
}}
You can specify the index where TextView has to be added in ViewGroup...
ViewGroup.addView(TextView, 0);
each time you are creating a TextView and adding to mLayout, add with the index 0 which will add on top of previous TextView
mLayout.addView(createNewTextView("TextView"),0);
You could create a LinearLayout and add one below the other, using layout.addView(TextView). I've a psedo code below which explains how it is done.
for(Option option : options){
view = View.inflate(mContext,R.layout.textView,null);
((TextView)view.findViewById(R.id.txt_key)).setText(" SOME TEXT : ");
((TextView)view.findViewById(R.id.txt_value)).setText("SOME VALUE ");
mLlCtr.addView(view);
}

getchild of a view on a click event

Sorry by my english.. this is google traductor
I create (java) for example 10 LLseccion.
Each LLseccionhas multiple objects inside (imageview and 2 textview)
i want read the first textview of a layout clicked
LinearLayout LLseccion = new LinearLayout(getApplicationContext());
LLseccion.setOrientation(LinearLayout.HORIZONTAL);
LLseccion.setId(i);
LLseccion.setClickable(true);
LLseccion.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
TextView tvBIG=new TextView(getApplicationContext());
tvBIG.setText("i want read this");
tvBIG.setTypeface(FontNormal);
tvBIG.setTextSize(bigSize);
tvBIG.setTextColor(getResources().getColor(R.color.GREEN));
tvBIG.setSingleLine(true);
LLseccion.addView(tvBIG);
TextView tvSMALL=new TextView(getApplicationContext());
tvSMALL.setText("other text");
tvSMALL.setTypeface(FontNormal);
tvSMALL.setTextSize(smallSize);
tvSMALL.setTextColor(getResources().getColor(R.color.GREEN));
tvSMALL.setPadding(30, 0, 0, 0);
tvSMALL.setSingleLine(true);
LLseccion.addView(tvSMALL);

Sending data to other activity and display some additional TextViews

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

Categories

Resources