In my Activity I have to add 10 times the same TextView.
Is it possible to load the definition of textview from layout.xml and repeat it programmatically?
for(int i=0;i<10;i++){
Textview text = new TextView(this);
mainlayout.add(text);
}
public class YourClassName extends Activity
{
#Override
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
// set activity layout
setContentView(R.layout.some_activity_layout);
LinearLayout mainActivityLayout = (LinearLayout)findViewById(R.id.main_layout);
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// then see previous answer
// loop n times {
TextView yourTextView = _li.inflate(R.layout.text_view_layout, null);
mainActivityLayout.addView(yourTextView);
// } end loop
}
}
You may want to read this article on reusing UI components: http://developer.android.com/resources/articles/layout-tricks-reuse.html
Related
So I have a LinearLayout set up in my XML file, and I dynamically add a bunch of CardViews to it through code upon startup of my activity. How can I make it so that when I click on any of the CardViews, I am able to obtain its position?
I tried setting up on an onClickListener for the LinearLayout, as well as an OnClickLIstener for each individual card, but couldn't find a way to obtain the index of the card that was clicked.
I'd really like to know where to put the onClickListener, and how to obtain the position that was clicked so that I can start a new activity based on what was clicked.
Here is my code:
private LinearLayout sLinearLayout;
private CardView[] cardViews;
private Workout[] workouts;
private ViewGroup.LayoutParams params;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.saved_workouts);
Intent intent = getIntent();
Parcelable[] parcelableArrayExtra = intent.getParcelableArrayExtra(MainActivity.EXTRA_WORKOUTS);
workouts = new Workout[parcelableArrayExtra.length];
System.arraycopy(parcelableArrayExtra, 0, workouts, 0, parcelableArrayExtra.length);
sLinearLayout = findViewById(R.id.viewList);
// Set the CardView layoutParams
params = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
populateCardViews();
populateLayout();
}
//TODO: this method will populate the linear layout, filling the list.
private void populateLayout() {
for (CardView cardView : cardViews) {
sLinearLayout.addView(cardView);
}
}
//TODO: this method will fill up the CardViews array with an array of workouts.
private void populateCardViews() {
cardViews = new CardView[workouts.length];
for(int i = 0; i < workouts.length; i++) {
CardView card = new CardView(this);
card.setClickable(true);
card.setLayoutParams(params);
// Set CardView corner radius
card.setRadius(9);
// Set cardView content padding
card.setContentPadding(15, 15, 15, 15);
// Set a background color for CardView
card.setCardBackgroundColor(Color.BLUE);
// Set the CardView maximum elevation
card.setMaxCardElevation(50);
// Set CardView elevation
card.setCardElevation(25);
// Initialize a new TextView to put in CardView
TextView tv = new TextView(this);
tv.setLayoutParams(params);
tv.setText("Workout WorkTime: " + workouts[i].getWorkTime());
tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 30);
tv.setTextColor(Color.WHITE);
card.setTag(workouts[i].getWorkTime());
card.addView(tv);
cardViews[i] = card;
}
}
I think you can setOnClickListener for each individual card. The index of clicked card is i
for (int i = 0; i < workouts.length; i++) {
...
final int finalI = i;
card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("TAG", "You click at card: " + finalI);
}
});
}
I not good to add so much layout programmatically. You can use a recycler view and an adapter to show a list of items. You can read this: https://developer.android.com/guide/topics/ui/layout/recyclerview
By the way, in your case: when you create a Cardview, just add onClickListener to it and the index of your Cardview is the index of it in Cardview list.
CardView cardView = new CardView(this);
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//handle click here
}
});
See if this works.
cardview.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int index = linearLayout.indexOfChild(v);
}
});
How can I find those TextView's another function on same class?
I will use setText(), serBackgroundColor() after create.
This code part is on CreateDesign() and this func calling onCreate():
public class MainActivity extends AppCompatActivity {
private LinearLayout linearLayout;
private TextView textView;
public void CreateDesign(){
linearLayout = (LinearLayout) findById(R.id.linearLayout);
for(int i = 1; i <= 5; i++){
textView = new TextView(this);
textView.setId(i);
textView.setText(i + ". TextView");
linearLayout.addView(textView);
}
}
Well you don't necessarily need to use id here, There are several ways to achieve this:
1.
TextView textView = (TextView) linearLayout.findViewById(i);
i is what you set before from 1 to 5.
2.
TextView textView = (TextView) linearLayout.getChildAt(i);
i here is the number of set item, for instance i=0 is the first textView you added using addView() method.
Either you create a member variable of this TextView which you can then use inside this class or you can use findViewById() on your LinearLayout.
Use the normal findViewById() method. You're giving the TextViews unique IDs from 1 to 5, so you can find those TextViews by supplying 1-5 to findViewById().
However, you probably shouldn't be doing it this way, and you shouldn't have a global textView variable (it'll only hold a reference to the last-created TextView).
Instead, try using an ArrayList and adding all of your TextViews to it. Then you won't need to give them IDs that don't follow the standards.
public class MainActivity extends AppCompatActivity {
private LinearLayout linearLayout;
private ArrayList<TextView> textViews = new ArrayList<>();
public void CreateDesign(){
linearLayout = (LinearLayout) findById(R.id.linearLayout);
for(int i = 1; i <= 5; i++) {
TextView textView = new TextView(this);
textView.setText(i + ". TextView");
linearLayout.addView(textView);
textViews.add(textView); //add it to the ArrayList
}
}
}
This class extends my main Activity.
public class Numbers extends MainActivity{
public ArrayList<ImageView> getNumbers () {
ArrayList<ImageView> numbers = new ArrayList<>();
ImageView one = (ImageView) findViewById(R.id.one);
numbers.add(one);
return numbers;
}
And I've done some digging but can figure out why my variable "one" is coming back null.
My MainActivity has a ContentView set.
This is the content of my onCreate in MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView start = (ImageView) findViewById(R.id.start);
sceneRoot = (ViewGroup) findViewById(R.id.scene_root);
questionView = findViewById(R.id.questionView);
startView = findViewById(R.id.startView);
gameOverView = findViewById(R.id.gameOver);
animSlide = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide);
animSlide.setAnimationListener(this);
animZoom = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.zoom_fade);
animZoom.setAnimationListener(this);
set.addTransition(new Fade())
.addTransition(new Slide(Gravity.RIGHT));
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getQuestion();
TransitionManager.beginDelayedTransition(sceneRoot, set);
startView.setVisibility(View.GONE);
questionView.setVisibility(View.VISIBLE);
}
});
}
public void getQuestion (){
time = (TextView) findViewById(R.id.timeBar);
time.startAnimation(animSlide);
}
I don't call getNumbers() until after start has been clicked and the animation has started.
public void onAnimationStart(Animation animation){
if(animation == animSlide) {
final Questions questions = new Questions();
Numbers n = new Numbers();
for (int i = 0; i < n.getNumbers().size(); i++) {
n.getNumbers().get(i).setVisibility(View.GONE);
n.getNumbersTen().get(i).setVisibility(View.GONE);
}
n.getNumbers().get(0).setVisibility(View.VISIBLE);
EDIT:
If anyone was wondering, I got it to work by extending the class as a Fragment instead of my MainActivity. Then I just used the fragment in my xml.
Because you extended an Activity class doesn't mean setContentView gets called for that class also. It will only do so if properly started and you call super.onCreate(bundle) from your own implementation of onCreate within Numbers
Basically, you should never new any Activity. It has no life-cycle, and therefore no content view, so findViewById just won't work.
Numbers n = new Numbers();
You could not extend anything and have a data-only class around your list of images.
public class Numbers {
private List<ImageView> numbers = new ArrayList<ImageView>();
public Numbers() {}
public void addNumber(ImageView v) { numbers.add(v); }
public List<ImageView> getNumbers() { return numbers; }
}
And from MainActivity you can find and add as you want.
Number n = new Numbers();
n.addNumber((ImageView) findViewById(R.id.one));
However, I don't know if that is useful, really...
Maybe a Fragment would serve a better purpose if you want a "sub-view" of your Activity, but it's hard to tell.
i am newbie in android and i m not getting how to produce this output:
Discipline name:1
{ complete for loop code execute from 0 to 9
}
then it will come outside and will update textview text:ie
Discipline name:2
and will enter inside For Loop
{
complete for loop code execute from 0 to 9
}
here is the java code:
public class SummaryDetailActivity extends Activity
{ TableLayout summaryDetailDisciplineTableLayout;
LayoutInflater mInflater;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.summarydetail_view);
mInflater = getLayoutInflater();
summaryDetailDisciplineTableLayout = (TableLayout)findViewById(R.id.summaryDetail_disciplinetable);
//PLEASE SEE THESE LINE OF CODE:
for(int i=0;i<2;i++)
{
TextView disciplineTextView = (TextView)findViewById(R.id.summaryDetail_disciplinetext);
disciplineTextView.setText("discipline name: "+i);
for(int j=0;j<10;j++)
{TableRow disciplineRow = (TableRow) mInflater.inflate(R.layout.summarydetailrow_view,null);
TextView disciplineDeviceLabel = (TextView) disciplineRow.findViewById(R.id.summaryDetail_info1);
TextView disciplineQuantityLabel = (TextView)disciplineRow.findViewById(R.id.summaryDetail_hypen);
TextView disciplineLocationLabel = (TextView) disciplineRow.findViewById(R.id.summaryDetail_info2);
disciplineDeviceLabel.setText("device no." + j);
disciplineQuantityLabel.setText("quantity No." + j);
disciplineLocationLabel.setText("Location No." + j);
disciplineRow.setId(j);
summaryDetailDisciplineTableLayout.addView(disciplineRow);
}
}
}
}
this code is producing this output:
Why your code is not working the way you want. Is because this
TextView disciplineTextView = (TextView)findViewById(R.id.summaryDetail_disciplinetext);
is initiate as static view that can't be clone in your loop. That's why the value
of the discipline name is 1 not 0. The loop only update the value not clone the view.
You should do this
TextView disciplineTextView = new Textview(this);
disciplineTextView.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
disciplineTextView.setText("discipline name: "+i);
summaryDetailDisciplineTableLayout.addView(disciplineTextView);
for(int j=0;j<10;j++) {
-your code-
}
You should change the xml structure for better UI.
Hope this helps.
Here's how I would've done it.
Inside my XML code, I would create a LinearLayout and within that, a TableLayout.
After fetching the id of my TableLayout, I would dynamically create TableRows and TextViews. You can set their layouts using TableRow.LayoutParams or LayoutParams. You can simply set the text after that and just add them onto the TableRow that you add to TableLayout.
Hope this suggestion helps !
I think that in onCreate() you should use inflater first and then set the view that you have just inflated.
Try this:
View view = getLayoutInflater().inflate(R.layout.summarydetail_view, null);
setContentView(view);
I have ScroolView and it's inside Linear layout. In this linear layout i added programmically around 20 TextView. When i press any of those TextView i change it color to RED (at first it was white). When i press again the same TextView i check his color and if it's is RED i again make it white.
What i want to do:
I press for example 1 TextView and make it RED. Then when i press 2 TextView i make this one RED and i want to make 1 TextView WHITE. This functionality should be at all TextView.
So any idea's how to do this ?
You mean to say at a time you need only one textview to be red. You can do this using 2 variables. One is a boolean colored. This indicates that at least one TextView is colored. Another is a TextView variable. Create a TextView variable lastColoredTextView. Let it be null initially. Then whenever the textview is clicked, assign the lastColoredTextView to the clicked TextView. Then whenever you are clicking, just check if colored then change the color of lastColoredTextView to white.
Change class name and it will work fine.
public class Test_stflowActivity extends Activity {
TextView current_red_txt_box = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = null;
LinearLayout lp = new LinearLayout(getApplicationContext());
lp.setOrientation(LinearLayout.VERTICAL);
View.OnClickListener txt_click = new View.OnClickListener() {
#Override
public void onClick(View v) {
current_red_txt_box.setTextColor(Color.WHITE);
TextView tv = (TextView) v;
tv.setTextColor(Color.RED);
current_red_txt_box = tv;
}
};
for (int i = 0; i < 20; i++) {
tv = new TextView(getApplicationContext());
tv.setId(i);
tv.setTextSize(40);
tv.setText("you text");
tv.setTextColor(Color.WHITE);
tv.setOnClickListener(txt_click);
lp.addView(tv);
current_red_txt_box = tv;
}
setContentView(lp);
}
}