1

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.

h3wro
  • 62
  • 1
  • 13

2 Answers2

1

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.

Samuel Agbede
  • 380
  • 1
  • 4
  • 14
  • Unfortunatelly, there will be much more layouts and I'm looking for something which can help me identifying source of click, but thanks for your answer! :) – h3wro Dec 03 '16 at 22:18
  • @h3wro, Alright I think I get you. You could still pass the id of the original view that would be clicked. IDs are stored as integers. All you need to do the the receiving end could be to use a switch statement with the id gotten. – Samuel Agbede Dec 03 '16 at 22:21
  • Maybe **contains()** will be a good choice to check if given string contains id of source. – h3wro Dec 03 '16 at 22:27
  • @h3wro did you check my comment on your question? – Hedegare Dec 03 '16 at 22:34
  • If you would be checking for many layouts, I'd suggest you use switch so you don't have to do if (something.contains("something")){ } many times. If say you have two strings say "sam" and "sammy". Both strings contain "sam". Switch might be neater. All the best! :) – Samuel Agbede Dec 03 '16 at 22:35
  • I think @Jackowski 's link answers your question perfectly. – Samuel Agbede Dec 03 '16 at 22:37
  • Thank you @SamuelDamilolaAgbede! Yes @Jackowski, I'm trying to implement it. – h3wro Dec 03 '16 at 22:43
  • You're welcome. Thank you for asking the question. I ended up adding to my knowledge too :). Thanks @Jackowski for sharing the link. I just knew about today. – Samuel Agbede Dec 03 '16 at 22:50
0

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;
    }
}
Community
  • 1
  • 1
h3wro
  • 62
  • 1
  • 13