Flow Layout in JAVA File - android

This is my Project XML Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/flowLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- enter code here -->
</LinearLayout>
I need to access it in the JAVA file which should contain 3 sentences in 3 text views ...
public class MainActivity extends Activity {
private View layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById();
//enter code here
}
private void findViewById() {
View layout = findViewById(R.id.flowLayout);
//enter code here
}
}

To add a TextView to your layout do this:
LinearLayout layout = (LinearLayout)findViewById(R.id.flowLayout); //depending on which layout is it.
Then, Create a TextView, set it text and add it to the layout:
TextView tvText = new TextView(this);
tvText.setText("your desired sentence");
layout.addView(tvText);
and do this for all 3 of your TextViews, for setting the height and width use LayoutParams (example):
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, 2, 0);
As you can see this is the way you set Mergins as well, in the end set those params to the textView:
tvText.setLayoutParams(layoutParams);

Related

Layout attributes in XML code those are ignored when using addView method

I'm korean, The image under is used korean language. Well.. This fact isn't important. I tried to add the custom view, In Linear layout, And It's be in linear layout successfully. But It's not on center horizontal. I did surely set the gravity of linearlayout center horizontal. But It isn't work. I guess cause of problem is layout params. Custom view class haven't problem, And not appeared any error on debugging. How do I do??
Activity in XML code:
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="NestedScrolling">
<LinearLayout
android:id="#+id/select_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:gravity="center_horizontal"
android:orientation="vertical">
<!--Here is-->
</LinearLayout>
</ScrollView>
AddView method:
ThemeManager mTheme;
Button selectTopBoardButton;
LinearLayout selectBody;
ThemeView c0001;
ThemeView c0002;
ThemeView c0003;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select);
gestureDetectorCompat = new GestureDetectorCompat(this, new MainActivityGoer());
NetworkDetector networkDetector = new NetworkDetector(this);
if (!networkDetector.isConnected()) {
Toast.makeText(SelectActivity.this, "네트워크에 접속할 수 없습니다.", Toast.LENGTH_SHORT).show();
}
selectTopBoardButton = (Button) findViewById(R.id.select_top_board_button);
selectBody = (LinearLayout) findViewById(R.id.select_body);
mTheme = new ThemeManager(this);
c0001 = mTheme.getThemeView("c0001");
c0002 = mTheme.getThemeView("c0002");
c0003 = mTheme.getThemeView("c0003");
selectTopBoardButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String url = getResources().getString(R.string.main_request_btn06_url);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
});
selectBody.addView(c0001);
}
i think your subview layout_width="match_parent" please try this code
c0001.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
selectBody.addView(c0001);
Try to set the gravity for custom views like this
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
c0001.setLayoutParams(params); //your custom ThemeView

setMargrin in Relativelayout Programmatically

I want to add a text view with margins into a Relativelayout by clicking a button, but whenever I add the setMargins to the LayoutParams, it make the TextView disappeared. Here is my code
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="#+id/test11">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/test"
android:onClick="onClick"
android:text="asdf"/>
</RelativeLayout>
Here is my code:
public class tet extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
}
public void onClick(View v){
TextView textView = new TextView(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 30, 30, 30);
textView.setText("asdf");
textView.setLayoutParams(layoutParams);
((RelativeLayout)findViewById(R.id.test11)).addView(textView);
}
}
I'm 99% sure your TextView is behind your Button. There are several problems:
layoutParams.setMargins(30, 30, 30, 30);
This sets the margins in pixels! If you set the margins in a xml Layout they are often set as dp (you can choose the unit there). If you want dp in your code you have to convert px => dp (here you can find an example).
Secondly: You are using an RelativeLayout, hence all Views are arranged relatively in your layout. You didn't provide any infromation to you TextView so it will be placed at the left/top of its parent (so behind your Button).
Try swap your RelativeLayout for a LinearLayout or provide additional arrangement information.
For the future: These kind of problems ("my view is missing") is easily solved by the Hierarchyviewer provided by the Android SDK.

Connecting xml to programmatic layout in Android

I have a custom layout java file that I create and add views to like this:
PredicateLayout layout = new PredicateLayout(this);
for (int i = 0; i < someNumberThatChangesEveryTime; i++)
{
TextView t = new TextView(this);
t.setText("Hello");
t.setBackgroundColor(Color.RED);
t.setSingleLine(true);
layout.addView(t, new PredicateLayout.LayoutParams(2, 0));
}
setContentView(layout);
What I would like to do is define my TextView in an xml, so I can add it like so:
TextView t = (TextView) findViewById(R.id.textview);
And of couse also add other kinds of views. I have no idea how to write this xml file, or how to connect it to this activity. The layout I'm using is this one: Line-breaking widget layout for Android
Find the id of the root layout in xml and add other ui elements programatically to the root layout.
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
andorid:id="#+id/ll
tools:context=".MainActivity" >
<TextView
android:id="#+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Blank text" />
</RelativeLayout>
Your MainActivity
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout ll = (RelativeLayout) findViewById(R.id.ll);
TextView tv= (TextView) findViewById(R.id.tv);
// add other ui elements to the root layout ie Relative layout
}
}

Controls overlap when trying to position programmatically using relative layout (Android)

I am trying to create a fragment. In the fragment I plan to place a text view next to it a Spinner control. I tried using this code. However Spinner always is placed over textview to the left of the screen. Can anyone suggest what could be the problem?
Code:
public class FragmentTest extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
RelativeLayout view = (RelativeLayout)inflater.inflate(R.layout.ll2, null);
RelativeLayout lref = (RelativeLayout)view.findViewById(R.id.ll2ll);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(lref.getLayoutParams());
lp.addRule(RelativeLayout.ALIGN_LEFT);
RelativeLayout rl = new RelativeLayout(getActivity());
TextView tv = new TextView(getActivity());
tv.setId(1);
tv.setLayoutParams(lp);
tv.setText("My ");
rl.addView(tv, lp);
Spinner s = new Spinner(getActivity());
lp.addRule(RelativeLayout.RIGHT_OF, tv.getId());
s.setId(2);
String currencyData[] = {"USD",
"EUR","INR"
};
ArrayAdapter<Object> currencyAdapter; currencyAdapter = new ArrayAdapter<Object>(getActivity(), android.R.layout.simple_spinner_item, currencyData);
currencyAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(currencyAdapter);
rl.addView(s,lp);
return rl;
}
}
XML file is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="fill_parent"
android:layout_marginLeft="10sp"
android:layout_marginRight="10sp"
android:layout_marginTop="10sp"
android:isScrollContainer="true"
android:orientation="horizontal" >
<RelativeLayout
android:id="#+id/ll2ll"
android:layout_width="140dp"
android:layout_height="50dp"
android:textSize="#dimen/font_size" />
</RelativeLayout>
When a view is inflated from a XML you will not get the LayoutParams properly. Try using new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(lref.getLayoutParams());
your problem is this, you can't use the same layout parameters for adding all the layouts and modifying the lp.
because the lp is used after you have initialized all the layouts, that means you are using same parameters for all the layouts spinner and textview both.
try using different layout parameters for all the layouts
RelativeLayout.LayoutParams lp
RelativeLayout.LayoutParams lptextview
RelativeLayout.LayoutParams lpspinner
for all of them
if your problem is solved mark my answser as correct

Android:Drawing Many Text Views

I'm new to android platform , I'd like to settext using textviews , I tried to write set text into two textviews but its just draws one textview why?
I can't draw two textviews
TextView tv1;
TextView tv2;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
layout = new LinearLayout(this);
tv1 = new TextView(this);
tv2 = new TextView(this);
tv1.setText("Hello");
tv2.setText("How are you?");
}
On Android, the user interface normally should be created using XML-files, instead of Java code. You should read up on the tutorials on android.com, especially:
http://developer.android.com/guide/topics/ui/declaring-layout.html
An example:
In your res/layout/main.xml, you define the text TextView's:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="#+id/TextView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="TextView 1"/>
<TextView android:id="#+id/TextView2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="TextView 2"/>
</LinearLayout>
Then if you use setContentView in the activity to display this, the app will show to TextView's:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
}
If you want to programmatically set the text in the Activity, just use findViewById():
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
((TextView)this.findViewById(R.id.TextView1)).setText("Setting text of TextView1");
}
I definitely second TuomasR's suggestion to use XML layouts. However, if you're wanting to add new TextViews dynamically (i.e. you don't know how many you will need until runtime), you need to do a couple of other steps to what you are doing:
First, define your LinearLayout in main.xml (it's just easier that way than LayoutParams, IMO):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_linear_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
/>
Now, you can go to your code, and try the following:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//This inflates your XML file to the view to be displayed.
//Nothing exists on-screen at this point
setContentView(R.layout.main);
//This finds the LinearLayout in main.xml that you gave an ID to
LinearLayout layout = (LinearLayout)findViewById(R.id.my_linear_layout);
TextView t1 = new TextView(this);
TextView t2 = new TextView(this);
t1.setText("Hello.");
t2.setText("How are you?");
//Here, you have to add these TextViews to the LinearLayout
layout.addView(t1);
layout.addView(t2);
//Both TextViews should display at this point
}
Again, if you know ahead of time how many views that you need, USE XML.

Categories

Resources