I would like to clone a View, graphicaly and functionaly (event). For exemple I have a button on my RelativeView, I get it with findViewById(View) and copy the info inside another button, and finaly put it (the clone) on the relativeView. Like that I could have two buttons and when I click on the copy it triggers the same event. I already did clone = findViewById(View) but the app crash. I don't know if it's possible. I would like to know also if it could be possible to make a generic class. Because the view could be a button but also a Text or An Image. I want to know it's possible to avoid to make three differents methods. But if I have to copy info by info, i will need to make three methods.I'm a begginer in Android ;).
Thanks for you answers.
you are smart Sir, unfortunately that is the way to go though, quick tip when you are implementing Button extends TextView so can always call text functions on a Button like View.setText(""), so forget about it being Button or TextView and go for a boolean attack on ImageView or TextView
hope i am smart too
I would recommend to implement custom views which have the desired properties.
Then you can do this:
LinearLayout myLayout = root.findViewByID(R.id.main);
CustomView myView = new CustomView(this);
myLayout.addView(myView);
Related
I have a problem loading previously created layout. I would like to load it and change text on buttons inside, then show it to the user. It will be quiz question and I have to show it many times during one activity. I don't want to create new class for my layout.
What do I have to use? I read something about Inflate class, but I think it is used only to create new classes. I tried setContentView() method, but app stops when method doing this load starts:
LinearLayout layout = (LinearLayout) findViewById(R.id.CapitalQuestionLayout);
setContentView((View) layout);
Can someone give some hints?
Try using the layout field not the id field, when you call from the R class, like so :
LinearLayout layout = (LinearLayout) findViewById(R.layout.CapitalQuestionLayout);
setContentView((View) layout);
For creating a "Quiz" app your basic requirement is:
a layout which has a TextView for Question and 4 Buttons for options.
a set of questions; pretty obvious :-).
You can create a Custom Class - Questions that will hold Text for a question and its associated options (as Strings).
Now, whenever you want to display a new question with different text for buttons just do the following:
If user clicks on right answer then display a Right-Answer-Activity to the user (that has a next-question-Button).
When user clicks on next-question-button you can display Question-Activity and populate the layout-views with a randomly picked question-object's attributes (i.e. Question's text and options' text).
Hope this helps.
This is quite common in Android apps. Do your fields/buttons have an id? For the parent activity, you mostly do
Button someButton = (Button) findViewById(R.id.the_id_to_the_button_you_want_to_change);
This allows you to do things like
someButton.setText("What is the ultimate answer to life, the universe, and everything?");
Look up the Andorid documentation if you want to do other things like set the color. If you have a viewgroup of some sort (RelativeLayout / LinearLayout / etc), you can specify that specific one
Button someButton = (Button)viewGroup.findViewById(R.id.awesomely_named_button_identifier);
The above someOtherButton is used more often with inflated viewGroups
For my project I need a functionality to dynamically add and remove views(textedit or buttons, etc).
I saw this similar functionality in Android "Add Contact" screen, where plus button add new fields and minus button delete the fields.
I found that EditContactActitivity.java is the file behind "Add Contacts".
I tried to find the methods that are called when plus or minus buttons are pressed but unable to find it, seems like "Add Contact" code is spreaded over multiple files. I am having difficulty understanding Android source code because documentation is unavailable.
Any advice?
You can add and remove views by calling .add() or .remove() on the reference to your main layout and passing the view you wish to add or remove;
Here is a simple example of an onCreate method that demonstrates adding and removing a button:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout myMainLayout = (LinearLayout)findViewById(R.id.yourMainLayout);
Button b = new Button(this);
//you can have some b.setXXX calls here to set text, view, click listeners etc...
myMainLayout.add(b);
//to remove
myMainLayout.remove(b);
}
I would consider researching Visibility of views rather than going through all this trouble. For example. I have an app where I have a 'record' entry screen that is relatively simple that appears as a Dialogs content. A few views/viewgroups are currently using visibility of gone, to not appear at all. If the user edits the record to add more detail, I launch an Activity that uses the same xml layout, but instantiates some of the currently 'gone' views and changes their visibility to 'visible'.
It is programmatically easy to toggle a view's visibility so I think it is really the way to go.
The only limitation I'm aware of here, would be the views order or position.
I've been learning and building an Android application from scratch and I've hit a wall.
I read on the android developer page about customizable UI components and figured I could use those to contain messages for a chat page in my application.
I have made the XML files but I have no idea how to call those layout files from my code.
At the moment I read my messages from an array and then go through a loop making a TextView and setting the necessary properties for it through code, then adding it to a RelativeLayout within a scrollView. This all works fine but I want my messages to display more information (such as time and if the recipient has received/read it), this is where the new XML files come in, they have the field and layout already set up, so I figured I could just call those, set some variables and be done with it.
As I've stated before, the problem is that I have no idea how to reference the layout files inside my code. I've already read all the articles on the android developer page concerning building your own UI elements and I've also googled my fair share, but apparently I'm either the only one who has thought of doing it this way, or I can't find the right keywords for it. So now I'm asking you guys. Thanks ahead for any answers you guys can give me.
Edit:
I've been doing some research, and I think what I need to do is write my own adapter to work with my own class of messages and my own XML layout for those messages. However I have no idea where to begin with this, since I can't find any good documentation on how to write your own adapters.
You could do the following, but that will create a new TextView ignoring whatever layout you had:
ScrollView scrollView = (ScrollView)findViewById(R.id.scrollView);
TextView textView = new TextView(this);
textView.setText(message);
scrollView.addView(textView);
textView.setText(timeAndDate);
scrollView.addView(textView);
I've never personally used ScrollView, so I'm not 100% sure on the above functionality, but it matches my experience with LinearLayout and RelativeLayout.
Alternatively, since you have your customr layout already, you would do:
ScrollView scrollView = (ScrollView)findViewById(R.id.scrollView);
TextView textView = (TextView)findViewById(R.id.textView);
textView.setText(messageTimeDate);
scrollView.addView(textView);
That's essentially how you could handle it. I'm not 100% sure if you could add textView more than once in either implementation. If that throws an exception, you could use an array list of text views, such as:
ScrollView scrollView = (ScrollView)findViewById(R.id.scrollView);
ArrayList<TextView> textView = new ArrayList<TextView>();
textView.add((TextView)findViewById(R.id.textView));
textView.get(index).setText(messageTimeDate);
scrollView.addView(textView.get(index).getId());
Wherever I put textView for the id, you could use message or timeDate, and make two of each thing I referenced, then.
For those interested in how I did it (and if you are running in to the same problem yourself):
For every list I wanted to create with my own random set of data I created my own adapter which extends BaseAdapter. By simply overriding the getView method it's possible to inflate and fill the different parts of your listitems.
I used this tutorial to help me with creating my own adapters.
In my android project I've two layouts in that first layout having one button if i click that button I need to display the second layout in the same layout for this I created a view by using LayoutInflater and attached it to "Table Layout" which is present in first layout.
Everything should be fine but the corresponding class file for the second layout is not loading. Without loading I'm not able to call events like click and some other loader events so any one help me how can I load the corresponding class file when i click button in first layout?
It's difficult to understand what you mean by class not loading.
If you want some layout objects to be hidden at a particular time, look into the visibility parameter in xml or setVisibility(true/false) in code.
If you want to display a whole different screen, create a second activity and call it:
Intent i = new Intent(CallingActivity.this, ActivityToStart.class);
startActivity(i);
Your question is difficult to understand, but you appear to be trying to achieve an effect like embedding one activity inside another. You may want to look at the android developer docs about the "fragments" API, which is the currently-recommended way of achieving this kind of effect.
I'm trying to learn how to build apps for Android.
The first simple app, which will become a component of a bigger app I hope to build, is to have a button on the screen where, when tapped, it adds something new to the view.
For instance:
Imagine a layout that only has a button:
[Create!]
When that button is pressed, the view gets a new row added to it:
[Create!]
A Something!
Upon subsequent presses, more rows are added
[Create!]
A Something!
A Something!
And so on.
I've made a LinearLayout and placed the button in it, and have attached a click listener to it. That all works great. What I can't figure out is how to get a handle on the LinearLayout in the onClick function with which I'll addView() the new TextView that says "A Something!"
Am I on the right track? What basic thing am I missing? Thanks!
I think you are approaching this the wrong way. You should look into ListView and SimpleArrayAdapter. This will put the elements into a list format that users will be more familiar with. Google has some good examples that use this (like their Notepad example). Especially if you are new to Android, you should look at their demos to get you through the basics. you can find them here
This is from memory, so it may not be exactly right.
In your layout, you'll want to give the LinearLayout an id.
<
LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/namehere"
... >
Then in your code you'll use findViewById to get a reference to it.
LinearLayout ll = (LinearLayout) findViewById(R.id.namehere);
ll.addView(...);