Use a single xml layout for multiple actions with different data

I know this is a very simple question, but as a beginner I can't get around. Thus, I want several actions to use the same xml layout (consisting, for example, of 1 image button and several text views with different identifiers). Now for each action, I want them to view the same layout, but redefine the views with data unique to each type of data. What is the best way to do this? In addition, the image button should open different URLs in the video player (YouTube links).

And can someone tell me what is the most practical way to learn Android programming?

UPDATE This is my current code:

public class TemakiActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.contentviewer); } 

}

For example, I have a text image with the identifier "descriptionviewer" and a button with the identifier "video link", now, how do you encode them?

+8
android android-activity layout
source share
2 answers

You can provide the same layout file and set the presentation attributes in the onCreate (..) method for each activity.

If you want to open a different URL for each button of the image, you can set it at runtime as follows

 public void onCreate(Bundle b) { Button button =(Button)findViewById(R.id.button); button.setOnClickListener(new OnClickListener(){ public void onClick(View v) { //different action for each activity } }); } 
+5
source share

Yes, you can! I had several actions to inflate the same layout, but they retain different general settings.

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.same_layout); TextView urlDesc = (TextView)findViewById(R.id.descriptionviewer); urlDesc.setText("url_1"); //now in other activities-- urlDesc.setText("url_2"); ImageButton aButton = (ImageButton)findViewById(R.id.videolink); aButton.setOnClickListener(aButtonListener); } private OnClickListener aButtonListener = new OnClickListener() { public void onClick(View v) { // go open url_1 here. In other activities, open url_x, url_y, url_z finish(); } }; 

The same code simply replaces the text you want to set for the TextView and url to open it in OnClickListener (). Nothing more to change.

+3
source share

All Articles