Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am new to android and I'am writing my first app with androidstudio.

I have two activities: the first has a bunch of buttons, all sharing the same onClick() event, which starts the second activity. The second activity has a single button. Both layouts are designed with the interactive GUI editor.

I want the button of the second activity to appear similar (but not identical) to the one in the first activity, which was touched. After much research, I succeeded in identifying the drawableLeft of the source button, and pass its name to the second activity, which then sets it to the target button. However, I find this method too limiting and complicated, considering that probably I will need to pass other data like color and so on.

This is what I am doing now: buttons in the first activity have their tag set to the name of the resource for drawableLeft. On the onClick() event, the tag is read and sent to the second activity via putExtra(). The second activity receives the name of the resource, obtains a resourceID via getResources().getIdentifier, creates the drawable via getDrawable(), and finally sets it to the destination via setCompoundDrawablesWithIntrinsicsBounds().

Problems are: 1) I have to set the tag property redundantly for every button in activity 1; 2) I also want to copy other properties like color and who knows what else more - should I stuff everything in the tag?

Finally the question: is there a more straightforward way to get GUI attributes and forward them to a second activity?

Thank you, linuxfan

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
202 views
Welcome To Ask or Share your Answers For Others

1 Answer

There are two ways to achieve your goal.Create a seperate xml file for your button.

<Button 
   android:id="@+id/your_btn"
   android:layout_width="50dp"
   android:layout_height="50dp"
   android:padding="10dp"/>

Now what you can do is on your Activity

 public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.yourActivityLayout);
   //this ViewGroup should be inside yourActivityLayout xml file
    ViewGroup group = findViewById(R.id.where_you_want_your_btn);

    getLayoutInflater().inflate(R.layout.your_btn_layout,group,false);

}

or

You can create Fragment or and use it on your activity the process is almost same as above.

 private void addFragment(Fragment fragment){

    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.add(R.id.fragment_container,fragment);
    fragmentTransaction.commit();
}

just create a frament with the layout you want to repeat and call addFragment(new YourFragment()) it from you activity; since you are new get more detail on Click here!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...