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.
Related
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.
I have created an activity with two buttons at the top. One button to show "SMS Logs" and second to show "Call Logs".
On clicking "SMS Logs" button, i am dynamically creating textviews and linear layout to show sms logs.
On Clicking "Call Logs", i am dynamically creating another textviews and linear layout to show call logs.
But the problem is that, once if we click "sms log" button and then we click "call log" button, the previously created linear layouts are not removed and the both(previous layouts and the current layouts) are shown simultaneously.
But i want that the previous layouts should be removed on clicking the second button.
Which function, should i use to remove the previous viewgroups or the layouts. Tell me if you need to read my class file.
Edit:
This is my Activity's code,
public class General extends Activity
{
String phone, message;
TextView Logs;
View layout, callLayout;
TextView data, callData, line, callLine;
Button smsLog, callLog;
LinearLayout ll, callll;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.general_main);
Logs = (TextView)findViewById(R.id.Logs);
layout = findViewById(R.id.layout);
callLayout = findViewById(R.id.layout);
smsLog = (Button)findViewById(R.id.smsLogs);
callLog = (Button)findViewById(R.id.callLogs);
smsLog.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
callLayout.setVisibility(View.GONE);
ll = new LinearLayout(getApplicationContext());
ll.setOrientation(LinearLayout.VERTICAL);
data = new TextView(getApplicationContext());
data.setText("First Line");
data.setTextColor(Color.YELLOW);
line = new TextView(getApplicationContext());
line.setText("Second Line");
((ViewGroup) ll).addView(data);
((ViewGroup) layout).addView(line);
((ViewGroup) layout).addView(ll);
}
});
callLog.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
layout.setVisibility(View.GONE);
callll = new LinearLayout(getApplicationContext());
callll.setOrientation(LinearLayout.VERTICAL);
callData = new TextView(getApplicationContext());
callLine = new TextView(getApplicationContext());
callData.setText("Third Line");
callLine.setText("Fourth Line");
((ViewGroup) callll).addView(callData);
((ViewGroup) callLayout).addView(callLine);
((ViewGroup) callLayout).addView(callll);
}
});
}
}
I have removed the extra code and made it simple to understand.
You can use FrameLayout to solve your problem. But I recommend you to use tabview.Here is the link that demonstrates how to develop tabbed applications.Good Luck
You could implement a TabView.
But having your current setup just change the visibility of one view group to GONE and the other to VISIBLE.
GONE will make the view invisible and it won't take up any space anymore.
EDIT based on the code added to the question
Both your layout and callLayout are using the same XML view. Implement 2 identical views in your xml and keep one visible and one gone. This way when you set layout or callLayout visibility to GONE they are 2 different ones not the same. So your onClick() will have something like this:
for smsLog:
layout.setVisibility(View.GONE);
callLayout.setVisibility(View.VISIBLE);
for callLog:
callLayout.setVisibility(View.GONE);
callLayout.setVisibility(View.VISIBLE);
I just want to dynamicly add buttons to my Layout when i want to.
The buttons should be like this XML Button:
<Button android:text="Text"
android:gravity="bottom"
android:textSize="10dp"
android:textColor="#FFFFFF"
android:layout_width="wrap_content"
android:background="#drawable/attack1"
android:layout_height="wrap_content"
android:id="#+id/workingButton">
</Button>
.
public class GravityIssueActivity extends Activity
{
LinearLayout layout;
Button newButton;
Button buttonByXml;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//the button in the xml file
buttonByXml = (Button)findViewById(R.id.workingButton);
layout = (LinearLayout)findViewById(R.id.layoutToInsert);
//my new programatically "born" button
newButton = new Button(this);
//Setting the properties as i want
newButton.setText("Text");
newButton.setTextSize(10);
newButton.setTextColor(Color.WHITE);
newButton.setBackgroundResource(R.drawable.attack1);
// Gravity = Bottom !!!!!!!!!!
newButton.setGravity(Gravity.BOTTOM);
// getting the XML buttons params just for case...
newButton.setLayoutParams(new LayoutParams(buttonByXml.getLayoutParams()));
//Adding my new Button to the layout
layout.addView(newButton);
}
}
And here is an image of the results:
How is it possible to became different result when I copy all the attributes?
If you want to create dynamic view (like Button,textview etc) then just use this code and run it in your application.
MyActivity.java://your java file
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout1);
Button btn = new Button(this)
btn.setText("My Dynamic Button);
btn.setMinLines(1);
btn.setMaxLines(3);
ll.addView(et);
In XML File:
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="#+id/TextView01"
android:layout_below="#+id/relativeLayout1"
android:orientation="vertical" >
You can absolutely create buttons in code but it's not considered a best-practice unless you have a good reason for dynamically creating the controls. Check out this post Add an array of buttons to a GridView in an Android application.
Try using
Button b = new Button();
This gives you a View instance that can be added to your current parent activity or fragmnet view. For a full reference of possible settings look at http://developer.android.com/reference/android/widget/Button.html
You can use all the set methods provided by parent views in the object hierarchy.
If you need to align text to the bottom of the button, all you need is:
Button button = ...
//apply required paramteres
button.setGravity(Gravity.BOTTOM);
use below code.you also add other parameters
Button submit=new Button(this);
LinearLayout.LayoutParams params= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(25, 0, 25, 0);
submit.setLayoutParams(params);
submit.setText("Attack");
submit.setTextSize(10);
submit.setTextColor(getResources().getColor(R.color.white));
submit.setBackgroundResource(R.drawable.attack);
If I have a page whose layout is designated by XML code, but I want to possibly create a few radio buttons, say, in the middle, but decide that at runtime, how would I do it? I'm new to Android so I'm taking a stab in the dark. Would something like this work?
In the XML, add a LinearLayout to the middle of the page's XML like this:
<LinearLayout android:id="#+id/LinLayBut"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
And then in the java something like this:
public void setupRadioButtons(){
LinearLayout linLay;
linLay = (LinearLayout) findViewById(R.id.LinLayBut);
RadioGroup radGroup = new RadioGroup(this);
RadioButton radBut = new RadioButton(this);
radGroup.addView(radBut, 0);
radGroup.setText("A button");
}
This is not an efficient way to build dynamic UI. You would be better off defining the optional layout in an XML file and then inflate it when you want to use it:
public void setupRadioButtons() {
final LayoutInflater inflater =
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout buttons =
(LinearLayout) inflater.inflate(R.layout.LinLayBut, null);
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main);
mainLayout.addView(buttons);
}
The above code assumes that the radio group and buttons are defined inside the LinearLayout with id LinLayBut and you main layout id is main.
OK, thanks to unluddite, I got it to work. For those tortured souls following the thread, here's the code. The XML doesn't have a layout around it. And if I don't call the method, the radio group takes no vertical space:
<RadioGroup android:id="#+id/radButGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" />
and here's the method:
public void setupRadioButtons(){
RadioGroup radGroup;
radGroup = (RadioGroup) findViewById(R.id.radButGroup);
RadioButton radBut0 = new RadioButton(this);
radGroup.addView(radBut0, 0); //2nd arg must match order of buttons
radBut0.setText("one Button");
RadioButton radBut1 = new RadioButton(this);
radGroup.addView(radBut1, 1);
radBut1.setText("Two Button");
radBut1.setChecked(true); //which button is set
Still new to Android
I want to display some data as an HTML-like ordered list (non-actionable).
Example;
Item One
Item Two
Item Three
I feel I am missing something obvious.
Thanks,
JD
Either use a ListView and do nothing when something is selected, Use an AlertDialog.Builder and call setItems and do nothing in the Select handler, or use a WebView and create the list in html as on ordered list.
There's no default view or widget to do this i guess. You can try with ListView or doing the views programatically (however this is a little dirty)
The xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llMain"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
The Activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Get your main layout from the XML
LinearLayout llMain = (LinearLayout) findViewById(R.id.llMain);
ArrayList<String> alItems = new ArrayList<String>();
//Put some example data
alItems.add("Text1");
alItems.add("Text2");
alItems.add("Text3");
for(int i=0; i<alItems.size(); i++){
//We create a Layout for every item
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
//A TextView to put the order (ie: 1.)
TextView tv1 = new TextView(this);
tv1.setText(i+1 + ". ");
ll.addView(tv1, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0));
//TextView to put the value from the ArrayList
TextView tv2 = new TextView(this);
tv2.setText(alItems.get(i));
ll.addView(tv2, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));
//Add this layout to the main layout of the XML
llMain.addView(ll, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 0));
}
}