I am dynamically adding a TextView which does not appear, what seems to be the problem?
Is there a difference between following two lines:
receiptLayout.addView(order_1_confirm);
((LinearLayout) receiptLayout).addView(order_1_confirm);
Well, neither one of them does not work!
My activity_receipt.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/receiptMainLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/image_view_rsc_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/rsc_logo"
android:src="#drawable/rsc" />
and my ReceiptActivity.java:
public class ReceiptActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receipt);
LinearLayout receiptLayout = (LinearLayout)findViewById(R.id.receiptMainLinearLayout);
TextView order_1_confirm = new TextView(this);
order_1_confirm.setText("hello");
//receiptLayout.addView(order_1_confirm);
((LinearLayout) receiptLayout).addView(order_1_confirm);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Just give android:orientation="vertical" in your LinearLayout.
And in
LinearLayout receiptLayout = (`LinearLayout`)findViewById(R.id.receiptMainLinearLayout);
you are already casting view to LinearLayout so no need to cast it again
Set the size and color of the TextView. Perhaps it seems invisible because it is the same color as the background.
you should always add orientation to the LinearLayout, adding android:orientation="vertical" would works fine.
Related
I've created a custom ActionBar with a simple TextView, the text is supposed to be aligned to the center. Then I added a MenuItem with onCreateOptionsMenu method. The result was that the each of the components were at each side of the ActionBar, the menu-item was placed at the left corner and the text-view was placed at the right corner. How can I make the text-view remain centered despite adding menu-items to the action-bar?
Here is my custom action_bar_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/app_name"
android:textColor="#ffffff"
android:id="#+id/action_bar_text"
android:textSize="18sp"/>
</LinearLayout>
Here is my menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.rashish.myapplication.MainActivity">
<item
android:id="#+id/back_action"
android:orderInCategory="100"
android:title="#string/back"
app:showAsAction="always"/>
</menu>
Here is my onCreateOptionsMenu method:
public boolean onCreateOptionsMenu(Menu menu) {
this.mMenu = menu;
getMenuInflater().inflate(R.menu.menu_main, mMenu);
MenuItem item = menu.findItem(R.id.back_action);
item.setVisible(true);
return true;
}
and this is how I use my custom action bar in onCreate method
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.action_bar_layout);
//MORE CODE
I'm current having a problem designing an activity which can switch from a 'grid' to 'list' view. I'm using a GridView and to switch to the 'list', I set the maximum number of columns to 1. Now my problem is that when I switch to 'list' view, I want the image on the left along with some text on the right and not just an image in the center. All the data is taken from a custom class which is populated manually. The following images will demonstrate.
What I want:
What I have:
My Code (XML and Java) is as follows:
single_item.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" >
<ImageView
android:contentDescription="#string/content"
android:id="#+id/imageView1"
android:layout_width="128dp"
android:layout_height="128dp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.gridview.MainActivity"
tools:ignore="MergeRootFrame" >
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:columnWidth="128dp"
android:verticalSpacing="10dp"
>
</GridView>
</FrameLayout>
MainActivity.java
private GridView myGrid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myGrid = (GridView) findViewById(R.id.gridView1);
myGrid.setAdapter(new myGridAdapter(this));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
GridView gv = (GridView) findViewById(R.id.gridView1);
//ImageView iv = (ImageView) findViewById(R.id.imageView1);
switch (item.getItemId()) {
case R.id.action_grid:
Toast.makeText(this, "Grid Button", Toast.LENGTH_SHORT).show();
gv.setNumColumns(GridView.AUTO_FIT);
return true;
case R.id.action_list:
Toast.makeText(this, "List Button", Toast.LENGTH_SHORT).show();
gv.setNumColumns(1);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Does anyone have any idea how I can accomplish this? I've been reading for quite some time now and have also thought of viewSwitcher and viewFlipper but don't think that they are exactly what I'm looking for. I've also juggled with the idea of creating separate activities for each layout but then also read about the drawbacks.
Thanks for your time.
The simplest option would probably be:
Add a TextView in single_item.xml, and position it to the right of the image.
Pass the current state of the GridView (i.e. the mode it's in) to your custom adapter.
In the Adapter's getView(), depending on this state:
Call setVisibility() on the TextView, with either VISIBLE or GONE.
Update the ImageView's layoutParams, to align it to the center or right of its parent.
This does not require any duplication.
Example code:
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
convertView = LayoutInflater.from(mContext)inflate(...);
}
TextView tv = convertView.findViewById(R.id.textView1);
ImageView imageView = convertView.findViewById(R.id.imageView1);
// Show/hide text according to ListView mode.
tv.setVisibility(mMode == SHOW_TEXT ? View.VISIBLE : View.GONE);
// also update imageView.getLayoutParams() according to mMode.
I am trying to programatically add a whole bunch of textViews of a certain width and at a certain location onto a tab. Now their setX() might be placed beyond the resolution of the screen. For instance, my tab is 1240 pixels in width, and I want to place a TextView at 2000 pixels and of course have a horizontal scroll feature available. I'm essentially creating a timeline on the fly depending on the data pulled.
I'm just trying to (at the moment) get multiple TextViews thrown on to the screen, and to have the horizontal scroll view for them. I am not sure if even doing a setX(2000); will populate a TextView beyond the screen. How can I get the HorizontalScrollView to work so that I may slide my main layout to the right to see the remaining two TextViews that were created?
Some basic code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relative_layout"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="horizontal">
<HorizontalScrollView
android:id="#+id/horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</HorizontalScrollView>
</RelativeLayout>
The MainActivity:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.relative_layout);
for(int i = 50; i < 550; i+=100){
TextView myText = new TextView(this);
myText.setX(i * 3);
myText.setText("HELLLLLOOOO");
layout.addView(myText);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
MainActivity
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout sv = (LinearLayout)findViewById(R.id.ll);
for(int i = 50; i < 550; i+=50){
TextView myText = new TextView(this);
myText.setX(i * 3);
myText.setText("HELLLLLOOOO");
sv.addView(myText);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
xml
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
Create one custom view for example
public class Example extends HorizontalScrollView {
}
Execute all unimplemented methods, then write this:
LinearLayout l = new LinearLayout(context);
TextView tv = new TextView(context);
tv.setText("Example");
tv.setTextColor(Color.RED);
l.addView(tv);
addView(l);
Now in your xml, put the packagename and class name like this:
<com.argha.Example android:height and all />
Done, now you have HorizontalScroll view with a TextView... more text you want just do same as above.
Sorry if you found any code error, because I typed with my phone.
"FC Source not found on launch 1" asks my question, but the "answer" given is almost perfectly useless.
I have a program that contains two ImageViews. When I fire it up, I see both my images on my tablet. So far, so good.
Adding one line of code, defining 'coil_image,' means it will no longer load. There is no error message, it just apologizes for having stopped. If I fire it up by hitting F11, I get the "Source not found" error.
Does anyone have a useful answer telling me what I need to do to get past this point?
public class MainActivity extends Activity {
ImageView coil_image = (ImageView) findViewById(R.id.imageInductionCoil);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#android:color/black"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/imageInductionCoil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="300dp"
android:minHeight="48dp"
android:minWidth="48dp"
android:src="#drawable/coil_green" />
<ImageView
android:id="#+id/imageFuelGauge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:minHeight="125dp"
android:minWidth="125dp"
android:src="#drawable/fuel_gauge" />
</RelativeLayout>
You first need to set your layout for your activity to be able to search for your elements on that layout.
Simple try this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView coil_image = (ImageView) findViewById(R.id.imageInductionCoil);
}
I've been trying to use the setActionView from the ActionBar in ICS
Seems like it should be straight forward but somehow I'm not getting the layout alignment that I would hope for. As you can see in the image below the 'target' icon is centered correctly within it's layout. But when I setActionBar(progress) the progress view is always aligned to the right whatever I try.
Here are the 2 states, before and after clicking the menu item. As you can see the progress view is always aligned to the right. I've tried changing the gravity options in the my progress layout xml from left to right to center and whatever I do change it doesn't seem to change anything.
I haven't found any info regarding this problem so I'm thinking I must be doing something wrong.
Do anyone have a clue?
Thanks for the help!
Here's my action bar menu layout 'action_bar_menu.xml'
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/locate"
android:title="locate"
android:icon="#drawable/locate"
android:showAsAction="ifRoom|withText" />
</menu>
Here's my progressbar layout 'inderterminate_progress.xml'
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ProgressBar android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="center"
android:indeterminate="true"
style="?android:attr/progressBarStyleInverse"/>
</FrameLayout>
And finally here's my testx Activity
public class HelloAndroidActivity extends Activity {
/**
* Called when the activity is first created.
* #param savedInstanceState If the activity is being re-initialized after
* previously being shut down then this Bundle contains the data it most
* recently supplied in onSaveInstanceState(Bundle). <b>Note: Otherwise it is null.</b>
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getActionBar().setTitle("Test");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_bar_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if (R.id.locate == item.getItemId()) {
final MenuItem menuItem = item.setActionView(R.layout.inderterminate_progress);
new Thread(new Runnable() {
#Override
public void run() {
SystemClock.sleep(3000);
runOnUiThread(new Runnable() {
#Override
public void run() {
menuItem.setActionView(null);
}
});
}
}).start();
}
return true;
}
}
It seems that explicitly defining the style and adding a 4dip padding in the root of the layout to inflate as shown below solves this issue
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="4dip"
android:paddingRight="4dip"
style="?android:attr/actionButtonStyle" />
This seems to be used in the android stylesheet here
A little late but the right solution is not an absolute value such as 4dp. The right approach is to set the minWidth to ABS values:
android:minWidth="#dimen/abs__action_button_min_width"
Example progress bar:
<?xml version="1.0" encoding="utf-8"?>
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="#dimen/abs__action_button_min_width"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:layout_gravity="center"
style="#android:style/Widget.ProgressBar.Small"/>