Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
208 views
in Technique[技术] by (71.8m points)

Android: how to recreate same button (View) in another activity

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

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

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...