So, I am trying to get reference to a xml layout from the class of a simple widget I made.
So ,my widget contains an ImageView and two TextViews.I will add the code for this widget, just so no one gets confused.
public class Item extends LinearLayout{
TextView tv1,tv2;
ImageView img;
public Item(Context context,int resid, String t1, String t2) {
super(context);
setOrientation(HORIZONTAL);
setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
img = new ImageView(context);
tv1 = new TextView(context);
tv2 = new TextView(context);
img.setBackgroundResource(resid);
img.setVisibility(View.VISIBLE);
img.setLayoutParams(new ViewGroup.LayoutParams(200, 200));
tv1.setText(t1);
tv1.setTextSize(15);
tv1.setGravity(Gravity.CENTER);
tv1.setLayoutParams(new ViewGroup.LayoutParams(250, 100));
tv2.setText(t2+"lei");
tv2.setTextSize(15);
tv2.setGravity(Gravity.CENTER);
tv2.setLayoutParams(new ViewGroup.LayoutParams(250,100));
tv1.setBackgroundColor(Color.GREEN);
tv2.setBackgroundColor(Color.BLUE);
addView(img);
addView(tv1);
addView(tv2);
}
So, as you can see, there's a clickListener added for each "Item".What I want to do ,is to be able to refer to a xml layout that is a second activity ,so that I can manipulate what is in this layout from within this widget.
The second activity's class:
public class Final extends Activity {
LinearLayout fl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.final_layout);
View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
fl = (LinearLayout) rootView.findViewById(R.id.fl);
}
}
The XML file for this second activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/fl"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
So, once again, to sum up, I want to be able to add stuff to this second activity ,from the class Item ,but I am not able to take reference to the XML layout coresponding to the second activity.
So,practically ,the only way to do this, is to pass variables through the onclick method.
What I am doing right now is this: I get two String variables and one Int.Strings being used for the text wrote in textViews and int for the resource ID for the imageView's backgroundResource, pass them to the second activity and use them there to re-create the Item.
If anyone needs more details on this, leave a comment here and I'll do my best to help.
Related
I know this has been asked before but I cannot make this work so here is what I have so far
class Click extends Activity {
int i=0;
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
final TextView mTextView = (TextView) findViewById(R.id.Counter);
mTextView.setText(""+i);
final Button button = (Button) findViewById(R.id.AddOne);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
TextView tv= (TextView) findViewById(R.id.Counter);
i=i+1;
mTextView.setText(Integer.toString(i));
}
});
}
Every time I run the app in an emulator it crashes
java.lang.IllegalStateException: Could not find a method Click(View) in the activity class com.scouting.corbin.frc_201415_scouting.MainActivity for onClick handler on view class android.widget.Button with id 'AddOne'
I know this is probably something completely stupid but I am new to this and need help thank you in advance.
As per your logcat.
java.lang.IllegalStateException: Could not find a method Click(View)
in the activity class
com.scouting.corbin.frc_201415_scouting.MainActivity for onClick
handler on view class android.widget.Button with id 'AddOne'
I suggest you to add Click(View v) in your MainActivity
public void Click(View v)
{
}
You need to take the root element here. Depending on parent layout include this line in the activity after setContentView().
RelativeLayout layout=(RelativeLayout)findViewById(R.id.yourLayoutId);// If its some other layout change "RelativeLayout" to your opted layout.
and in onClick() method of button, add following.
layout.add(tv);
Yopu want To add one Linearlayout in xml file
and set id for your LinearLayout.
android:id="#+id/linearlayout"
And change your addTextView method to following
public void addTextView(String text){
LinearLayout layout=(LinearLayout)findViewById(R.id.linear);
TextView textView=new TextView(this);
textView.setText(text);
textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
layout.addView(textView);
}
and call this method from your Forloop
Perhaps consider using the android:onClick="example_method" attribute for the button in your xml file. Then create the appropriate method in the class. public void example_method(View v) {} Then place the code you have in your onClick function into the new one. It's easier than using an listener.
Ok so all of you helped I completely got rid of that code which was too comlicated for what I was trying to do. After taking bits of suggestions and some reasearch I came up with this
public void AddOne(View v) {
TextView tv= (TextView) findViewById(R.id.Counter);
i=i+1;
tv.setText(""+i);
}
As you can see much simpler than what I had before and this one works thank you all
Is it possible to add a new EditText automatically in a ListView on a Button click?
If so please let me know.
EditText name = new EditText(youractivity);
convertView.addView(name);
Call notifydatasetchanged() but you have to save the field that you add becouse after scrolling into the list you will have in all list that editText and you have to remove where you don't want to appear.
What I recommand you is to make an editext into your cellView hidden and make it visible when you tap on your button.
You could make a custom adapter for your listview and give it a layout containing the edittext with the visibility set to gone. You can then set it to visible onbuttonclick.
I would like to suggest one code its not about listview but see if it could give you some idea,by adding views dynamically in linear layout which is inside scroll view.
public class MainActivity extends Activity {
ScrollView scrollview;
LinearLayout linearLayout;
LinearLayout.LayoutParams layoutParams;
static int i;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollview = (ScrollView)findViewById(R.id.scrollview);
linearLayout = (LinearLayout)findViewById(R.id.linearlayout);
Button button = (Button)findViewById(R.id.button);
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
TextView view = new TextView(MainActivity.this);
view.setText(++i+" view");
linearLayout.addView(view, layoutParams);
}
});
}}
I am quite new to developing apps. Still I would have thought that this is a basic action, so if there is already a solved thread I would be OK with the link. But since I am searching for over 2 hours for this I am asking anyway:
I want to dynamically add an element to my layout every time the user clicks a button.
By now I have this:
XML (R.layout.game.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/submit_choice"
android:onClick="submitChoice"/>
</LinearLayout>
Java
public void submitChoice(View view)
{
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText("text");
LinearLayout ll = new LinearLayout(this);
ll.addView(View.inflate(ll.getContext(), R.layout.game, null));
ll.addView(textView);
setContentView(ll);
}
Since the XML file does not change, it only works once.
So how can I add a second text when the user clicks the button a second time (without changing the XML file)? Examples are appreciated.
The problem comes from this line, that recreate the whole layout every time:
LinearLayout ll = new LinearLayout(this);
You should define it and setContentView(ll) outside the submitChoice function. Then on click only create and add the textView , then call ll.invalidate(); to see the changes.
Something like:
LinearLayout ll;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
ll = (LinearLayout) findViewById(R.id.ll_game);
}
// More code...
public void submitChoice(View view) {
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText("text");
ll.addView(textView);
ll.invalidate();
}
where ll_game is the id you have to set in xml for your LinearLayout.
I currently have:
final TextView tv = new TextView(this);
final RelativeLayout rL = new RelativeLayout(this);
final EditText editText = (EditText)findViewById(R.id.editText);
final Button b1 = (Button)findViewById(R.id.b1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
rL.addView(tv);
tv.setText(editText.getText());
editText.setText("");
}
});
In my onCreate method but my textView does not show up on the screen when text is inputted and my button is pressed? Is there code to set where it is set up on the screen of my phone?
Here is your problem
final RelativeLayout rL = new RelativeLayout(this);
This RelativeLayout that contains the TextView is not even displayed on the screen, all you are doing is creating a RelativeLayout.
What you should do instead is add a RelativeLayout to your XML layout file (same one containing the EditText and Button and do the following
final RelativeLayout rL = (RelativeLayout)findViewById(R.id.myRelativeLayout);
...
rL.addView(tv);
Now since you are referencing an actual RelativeLayout, your text will be visible.
Hope I made some sort of sense.
Do you have a base layout? You're adding the EditText to the RelativeLayout, but you need to add the RelativeLayout to some already existing layout.
First, inflate some base layout. Then do a findViewById on that layout. Use this to call addView(editText);
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/base_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</RelativeLayout>
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
RelativeLayout rl = (RelativeLayout)findViewById(R.layout.base_layout);
rl.addView(yourTextView);
}
}
How to Create dynamic user interface with events...
Hi Friends i want to create a user interface. in which i will have some buttons and labels
but the number of buttons and lables will be changing time to time depending upon the data retrieved from the server...
Can any one please guide me to do solve this issue... with usefuls links and guidence..
This uses two linear layouts to achieve a grid. The higher level LinearLayout is defined in linearlayout.xml, and holds element views vertically. Within each of these vertical views, a Row (custom LinearLayout) is instanciated, and in doing so, specifies the event handler(s). This LinearLayout holds its elements horizontally.
public class YourActivity extends Activity {
#Override
public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.linearlayout);
ViewGroup main = (ViewGroup) findViewById(R.id.linearlayout);
main.addView(new Row(this));
main.addView(new Row(this));
}
private class Row extends LinearLayout {
public Row(Context context) {
super(context);
TextView text = new TextView(context);
Button button = new Button(context);
text.setText("Text");
button.setText("Button");
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Button Clicked", Toast.LENGTH_SHORT).show();
}
});
this.addView(text);
this.addView(button);
}
}
}
linearlayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearlayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</LinearLayout>
Is this what you were looking for?