Reputation: 62
how can I get readable id of specyfic layout?
Fragment of my activity_miejsce.xml file:
<LinearLayout
android:id="@+id/elementPlace"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/layout_main_menu_selector"
android:clickable="true"
android:onClick="Place">
...
</LinearLayout>
Activity for this layout:
public class PlacesActivity extends AppCompatActivity {
LinearLayout element;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_places);
element = (LinearLayout) findViewById(R.id.elementPlace);
}
public void Place(android.view.View v){
Intent place = new Intent(this, Miejsce.class);
place.putExtra("source", element.toString());
startActivity(place);
}}
Miejsce.java (destination of our intent):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_miejsce);
Intent src = getIntent();
String source = src.getStringExtra("source");
TextView msg = (TextView) findViewById(R.id.napis);
msg.setText(getIntent().getStringExtra("source"));
Toast.makeText(getApplicationContext(), "Source: " + source, Toast.LENGTH_SHORT).show();
}
Result (from Toast): Source: android.widget.LinearLayout{14e02738 V.E...C. ...P....0,165-1080,375#7f0c007e app:id/elementPlace}
I want to use id of specyfic layout, because Miejsce.java will generate a layout which will depend on source. For example: There are 3 layouts (hotel, petrol station, restaurant). User had clicked one of them (@+id/restaurant) and intent was sent with id of source (trigger). Java code generated specyfic layout with restaurant.
Upvotes: 1
Views: 569
Reputation: 62
Thanks for the link @Jackowski. PlacesActivity.java after changes:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_places);
}
public void Place(android.view.View v){
switch(v.getId()){
case R.id.elementPlace:
Intent place = new Intent(this, Place.class);
place.putExtra("source", "elementPlace");
startActivity(place);
break;
}
}
Upvotes: 0
Reputation: 380
I would say since you know the id name already, You could do something like String id_name = "R.id.id_name" and pass that through your intent.
Upvotes: 1